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