InitCtrl.java

package org.docascode.init;

import org.apache.commons.io.FileUtils;
import org.docascode.git.ConfigCtrl;
import org.docascode.utils.DocAsCodeException;
import org.docascode.utils.DocAsCodeRepository;
import org.docascode.utils.Utils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class InitCtrl {
    public void initGitRepository(File directory) throws DocAsCodeException {
        Git git;
        try {
            git = Git.init().setDirectory( directory ).call();
        } catch (GitAPIException e) {
            throw new DocAsCodeException("Unable to initialize a DocAsCode repository in " + directory.getAbsolutePath(), e);
        }
        try {
            if(!git.status().call().isClean()){
                throw new DocAsCodeException("Working tree contains unstaged changes. Aborting.");
            }
        } catch (GitAPIException e) {
            throw new DocAsCodeException("Unable to get status of git repository " + directory.getAbsolutePath(), e);
        }
    }

    public void copyDocAsCodeFiles(File directory) throws DocAsCodeException {
        Utils.setDirectory(directory);
        InputStream srcConfig = getClass().getClassLoader().getResourceAsStream("org/docascode/init/config");
        InputStream srcDocAsCodeXML = getClass().getClassLoader().getResourceAsStream("org/docascode/init/delivery/docascode.xml");
        InputStream srcChronoXML = getClass().getClassLoader().getResourceAsStream("org/docascode/init/delivery/chrono.xml");
        InputStream srcPreCommit = getClass().getClassLoader().getResourceAsStream("org/docascode/init/hooks/pre-commit");
        InputStream srcPostCommit = getClass().getClassLoader().getResourceAsStream("org/docascode/init/hooks/post-commit");
        InputStream srcDeliveryXML = getClass().getClassLoader().getResourceAsStream("org/docascode/init/delivery/delivery.xml");
        InputStream srcDeliveryProperties = getClass().getClassLoader().getResourceAsStream("org/docascode/init/delivery/delivery.properties");
        try {
            Utils.copyInputStreamToFile(srcConfig, Utils.getRepository().getProjectConfig(),false);
            Utils.copyInputStreamToFile(srcDocAsCodeXML, Utils.getRepository().getDocAsCodeXML(),false);
            Utils.copyInputStreamToFile(srcChronoXML, Utils.getRepository().getChronoXML(),false);
            Utils.copyInputStreamToFile(srcPreCommit, Utils.getRepository().getPreCommit(),false);
            Utils.copyInputStreamToFile(srcPostCommit, Utils.getRepository().getPostCommit(),false);
            Utils.copyInputStreamToFile(srcDeliveryXML, Utils.getRepository().getDeliveryXML(),false);
            Utils.copyInputStreamToFile(srcDeliveryProperties, Utils.getRepository().getDeliveryProperties(),false);
        } catch (IOException e) {
            throw new DocAsCodeException("Unable to copy DocAsCode mandatory files.",e);
        }
    }

    public void initGitConfig(File directory){
        Utils.setDirectory(directory);
        ConfigCtrl config = new ConfigCtrl(Utils.getRepository());
        try {
            config.add(DocAsCodeRepository.ConfigLevel.LOCAL,"include",null,"path","../.docascode/config");
            config.add(DocAsCodeRepository.ConfigLevel.LOCAL,"docascode",null,"version", Utils.getDocAsCodeVersion());
        } catch (DocAsCodeException e) {
            throw new DocAsCodeException("Unable to initialize Git configuration.",e);
        }

    }


}