001// Generated by delombok at Sun Jun 30 16:31:41 CEST 2019
002package com.credibledoc.substitution.doc;
003
004import com.credibledoc.combiner.tactic.TacticService;
005import com.credibledoc.substitution.core.configuration.Configuration;
006import com.credibledoc.substitution.core.configuration.ConfigurationService;
007import com.credibledoc.substitution.core.resource.ResourceService;
008import com.credibledoc.substitution.core.template.TemplateService;
009import com.credibledoc.substitution.doc.module.substitution.SubstitutionTactic;
010import com.credibledoc.substitution.doc.module.substitution.report.UmlDiagramType;
011import com.credibledoc.substitution.reporting.markdown.MarkdownService;
012import com.credibledoc.substitution.reporting.reportdocument.ReportDocumentType;
013import com.credibledoc.substitution.reporting.reportdocument.creator.ReportDocumentCreator;
014import com.credibledoc.substitution.reporting.reportdocument.creator.ReportDocumentCreatorService;
015import com.credibledoc.substitution.reporting.visualizer.VisualizerService;
016import lombok.NonNull;
017import org.springframework.context.annotation.AnnotationConfigApplicationContext;
018import org.springframework.context.annotation.ComponentScan;
019import javax.inject.Inject;
020import java.io.File;
021import java.util.Collections;
022import java.util.List;
023
024/**
025 * The main class for generation of documentation of the credibledoc-substitution tool.
026
027 * <p>
028
029 * This main method should be launched in the credible-doc\substitution (parent) working directory.
030
031 *
032
033 * @author Kyrylo Semenko
034 */
035@ComponentScan(basePackages = "com.credibledoc.substitution.doc")
036public class SubstitutionDocMain {
037    @java.lang.SuppressWarnings("all")
038    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SubstitutionDocMain.class);
039    public static final String APPLICATION_SUBSTITUTION_DOC_LAUNCHED = "Application substitution-doc launched.";
040    public static final String SUBSTITUTION_DOC = "substitution-doc";
041    public static final String APPLICATION_SUBSTITUTION_DOC_FINISHED = "Application finished.";
042    @NonNull
043    private final List<ReportDocumentCreator> reportDocumentCreators;
044    @NonNull
045    private final SubstitutionTactic substitutionSpecificTactic;
046
047    /**
048     * The main method for generation of documentation of the credibledoc-substitution tool.
049
050     * <p>
051
052     * This method should be launched within its parent (../substitution) folder.
053
054     *
055
056     * @param args not used
057     */
058    public static void main(String[] args) {
059        log.info(APPLICATION_SUBSTITUTION_DOC_LAUNCHED);
060        try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SubstitutionDocMain.class)) {
061            applicationContext.start();
062            log.info("Spring ApplicationContext created and started");
063            SubstitutionDocMain substitutionDocMain = applicationContext.getBean(SubstitutionDocMain.class);
064            substitutionDocMain.substitute();
065        }
066        log.info(APPLICATION_SUBSTITUTION_DOC_FINISHED);
067    }
068
069    private void substitute() {
070        TacticService.getInstance().getTactics().add(substitutionSpecificTactic);
071        ReportDocumentCreatorService reportDocumentCreatorService = ReportDocumentCreatorService.getInstance();
072        reportDocumentCreatorService.addReportDocumentCreators(reportDocumentCreators);
073        reportDocumentCreatorService.createReportDocuments();
074        copyResourcesToTargetDirectory();
075        List<Class<? extends ReportDocumentType>> reportDocumentTypes = Collections.singletonList(UmlDiagramType.class);
076        VisualizerService.getInstance().createReports(reportDocumentTypes);
077        MarkdownService.getInstance().generateContentFromTemplates();
078    }
079
080    private void copyResourcesToTargetDirectory() {
081        Configuration configuration = ConfigurationService.getInstance().getConfiguration();
082        ResourceService resourceService = ResourceService.getInstance();
083        List<String> allResources = resourceService.getResources(null, configuration.getTemplatesResource());
084        TemplateService templateService = TemplateService.getInstance();
085        for (String resource : allResources) {
086            if (!resource.endsWith(MarkdownService.MARKDOWN_FILE_EXTENSION) && containsDotInName(resource)) {
087                String targetFilePath = resourceService.generatePlaceholderResourceRelativePath(resource);
088                String targetFileAbsolutePath = configuration.getTargetDirectory() + targetFilePath;
089                log.info("Resource will be copied to file. Resource: \'{}\'. TargetFileAbsolutePath: \'{}\'", resource, targetFileAbsolutePath);
090                File file = templateService.exportResource(resource, targetFileAbsolutePath);
091                log.info("Resource copied to file: \'{}\'", file.getAbsolutePath());
092            }
093        }
094    }
095
096    /**
097     * @param resource for example '/template/markdown/' is a directory, and '/template/markdown/README.md' is a file.
098
099     * @return 'False' if this resource is directory
100     */
101    private boolean containsDotInName(String resource) {
102        int index = resource.lastIndexOf('/');
103        if (index == -1) {
104            index = 0;
105        }
106        String fileName = resource.substring(index);
107        return fileName.contains(".");
108    }
109
110    @Inject
111    @java.lang.SuppressWarnings("all")
112    public SubstitutionDocMain(@NonNull final List<ReportDocumentCreator> reportDocumentCreators, @NonNull final SubstitutionTactic substitutionSpecificTactic) {
113        if (reportDocumentCreators == null) {
114            throw new java.lang.NullPointerException("reportDocumentCreators");
115        }
116        if (substitutionSpecificTactic == null) {
117            throw new java.lang.NullPointerException("substitutionSpecificTactic");
118        }
119        this.reportDocumentCreators = reportDocumentCreators;
120        this.substitutionSpecificTactic = substitutionSpecificTactic;
121    }
122}