DocAsCodeRepository.java

  1. package org.docascode.api.core;

  2. import org.docascode.api.core.chrono.ChronoRepository;
  3. import org.docascode.api.core.errors.DocAsCodeException;
  4. import org.docascode.api.core.git.GitRepository;
  5. import org.docascode.api.core.mvn.MvnRepository;
  6. import org.docascode.api.listener.APIEventListener;
  7. import org.eclipse.jgit.errors.ConfigInvalidException;
  8. import org.eclipse.jgit.lib.*;

  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;

  13. public class DocAsCodeRepository extends APIEventListener {
  14.     private File docascodeDir;

  15.     private GitRepository gitRepository;

  16.     /**
  17.      * A DocAsCodeRepository constructor
  18.      * @param repo The base Git Repository.
  19.      */

  20.     public DocAsCodeRepository(Repository repo) throws IOException {
  21.         gitRepository = new GitRepository(repo);
  22.         docascodeDir = new File(getWorkTree(),".docascode");
  23.     }

  24.     /**
  25.      * The workTree of the current DocAsCode/Git Repository.
  26.      */

  27.     public File getWorkTree(){
  28.         return gitRepository.getWorkTree();
  29.     }

  30.     /**
  31.      * The Maven project settings file for this DocAsCode repository.
  32.      * @return the Maven project file.
  33.      */

  34.     private File getProjectSettingsFile() {
  35.         return new File(getDocAsCodeDir(),"settings.xml");
  36.     }


  37.     private MvnRepository mvnRepository;

  38.     /**
  39.      * A Maven Repository Controller for this DocAsCode repository.
  40.      * @return the MvnRepository
  41.      */

  42.     public MvnRepository mvn(){
  43.         if (mvnRepository==null){
  44.             mvnRepository = new MvnRepository(getProjectSettingsFile(),getWorkTree());
  45.             mvnRepository.addListener(this);
  46.         }
  47.         return mvnRepository;
  48.     }

  49.     private ChronoRepository chronoRepository;

  50.     /**
  51.      * A Chrono Repository Controller for this DocAsCode repository.
  52.      * @return the ChronoRepository
  53.      */

  54.     public ChronoRepository chrono(){
  55.         if (chronoRepository==null){
  56.             chronoRepository = new ChronoRepository(getChronoXML(),getWorkTree());
  57.             chronoRepository.addListener(this);
  58.         }
  59.         return chronoRepository;
  60.     }

  61.     /**
  62.      * The delivery.properties file for this repository.
  63.      * @return the delivery.properties file.
  64.      */

  65.     public File getDeliveryProperties() {
  66.         return new File(getWorkTree(),"delivery.properties");
  67.     }

  68.     /**
  69.      * The .docascode directory.
  70.      */

  71.     public File getDocAsCodeDir(){
  72.         return this.docascodeDir;
  73.     }

  74.     /**
  75.      * The chrono.xml file.
  76.      */

  77.     public File getChronoXML(){
  78.         return new File(getDocAsCodeDir(),"chrono.xml");
  79.     }

  80.     public File  getDeliveryXML(){
  81.         return new File(getWorkTree(),"delivery.xml");
  82.     }

  83.     /**
  84.      * Resolve file path relatively to the worktree of this DocAsCode repository.
  85.      * Paths are returned in Unix styles.
  86.      * @param file the file to resolve.
  87.      * @return the resolved path.
  88.      */

  89.     public String relativize (File file){
  90.         Path workTreePath = Paths.get(getWorkTree().getAbsolutePath());
  91.         Path filePath = Paths.get(file.getAbsolutePath());
  92.         return workTreePath.relativize(filePath).normalize().toString().replace("\\","/").replace("../","");
  93.     }

  94.     /**
  95.      * The temporary working directory for this DocAsCode repository.
  96.      * @return the temporary working directory.
  97.      */

  98.     public File getTmpDir(){
  99.         return new File(getDocAsCodeDir(),"tmp");
  100.     }

  101.     /**
  102.      * The version of this DocAsCode repository.
  103.      * Usually this version is set through an init or upgrade
  104.      * @return this DocAsCode repository version
  105.      */

  106.     public String getVersion() throws DocAsCodeException {
  107.         StoredConfig c = git().getProjectConfig();
  108.         try {
  109.             c.load();
  110.             return c.getString("docascode",null,"version");
  111.         } catch (IOException | ConfigInvalidException e) {
  112.             throw new DocAsCodeException("Unable to get current repository version",e);
  113.         }
  114.     }

  115.     /**
  116.      * A Git Repository Controller for this DocAsCode repository.
  117.      * @return the GitRepository.
  118.      */

  119.     public GitRepository git() {
  120.         return gitRepository;
  121.     }
  122. }