001/* 002 * Copyright (c) 2012-2021 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others 003 * 004 * This program and the accompanying materials are made available under the 005 * terms of the Eclipse Public License 2.0 which is available at 006 * http://www.eclipse.org/legal/epl-2.0. 007 * 008 * SPDX-License-Identifier: EPL-2.0 009 */ 010 011package org.eclipse.golo.cli.command; 012 013import com.beust.jcommander.IParameterValidator; 014import com.beust.jcommander.Parameter; 015import com.beust.jcommander.ParameterException; 016import com.beust.jcommander.Parameters; 017import com.beust.jcommander.ParametersDelegate; 018import com.beust.jcommander.converters.FileConverter; 019import org.eclipse.golo.cli.command.spi.CliCommand; 020import org.eclipse.golo.cli.GoloFilesManager; 021import org.eclipse.golo.compiler.GoloCompiler; 022import org.eclipse.golo.doc.AbstractProcessor; 023import org.eclipse.golo.doc.CtagsProcessor; 024import org.eclipse.golo.doc.HtmlProcessor; 025import org.eclipse.golo.doc.MarkdownProcessor; 026import org.eclipse.golo.doc.ModuleDocumentation; 027 028import java.io.File; 029import java.nio.file.Paths; 030import java.util.function.Supplier; 031import java.util.*; 032 033import static java.util.stream.Collectors.toSet; 034import static gololang.Messages.*; 035 036@Parameters(commandNames = "doc", commandDescriptionKey = "doc", resourceBundle = "commands") 037public final class DocCommand implements CliCommand { 038 039 @Parameter(names = "--format", descriptionKey = "doc.format", validateWith = DocFormatValidator.class) 040 String format = "html"; 041 042 @Parameter(names = "--output", descriptionKey = "doc.output") 043 String output = "."; 044 045 @Parameter(descriptionKey = "source_files", converter = FileConverter.class) 046 List<File> sources = new LinkedList<>(); 047 048 @ParametersDelegate 049 ClasspathOption classpath = new ClasspathOption(); 050 051 private static final Map<String, Supplier<? extends AbstractProcessor>> FORMATS = new HashMap<>(); 052 static { 053 FORMATS.put("markdown", MarkdownProcessor::new); 054 FORMATS.put("html", HtmlProcessor::new); 055 FORMATS.put("ctags", CtagsProcessor::new); 056 } 057 058 @Override 059 public void execute() throws Throwable { 060 GoloCompiler compiler = classpath.initGoloClassLoader().getCompiler(); 061 AbstractProcessor processor = FORMATS.get(this.format).get(); 062 Set<ModuleDocumentation> modules = GoloFilesManager.goloFiles(this.sources) 063 .map(wrappedTreatment(file -> ModuleDocumentation.load(file, compiler))) 064 .collect(toSet()); 065 066 try { 067 processor.process(modules, Paths.get(this.output)); 068 } catch (Throwable throwable) { 069 handleThrowable(throwable); 070 } 071 } 072 073 public static final class DocFormatValidator implements IParameterValidator { 074 075 @Override 076 public void validate(String name, String value) throws ParameterException { 077 if (!FORMATS.keySet().contains(value)) { 078 throw new ParameterException(message("format_error", FORMATS.keySet())); 079 } 080 } 081 } 082}