001/*
002 * Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 */
009
010package org.eclipse.golo.cli.command;
011
012import com.beust.jcommander.IParameterValidator;
013import com.beust.jcommander.Parameter;
014import com.beust.jcommander.ParameterException;
015import com.beust.jcommander.Parameters;
016import org.eclipse.golo.cli.command.spi.CliCommand;
017import org.eclipse.golo.compiler.GoloCompiler;
018import org.eclipse.golo.compiler.GoloCompilationException;
019import org.eclipse.golo.doc.AbstractProcessor;
020import org.eclipse.golo.doc.CtagsProcessor;
021import org.eclipse.golo.doc.HtmlProcessor;
022import org.eclipse.golo.doc.MarkdownProcessor;
023import org.eclipse.golo.doc.ModuleDocumentation;
024
025import java.io.File;
026import java.io.IOException;
027import java.nio.file.Paths;
028import java.util.*;
029
030import static gololang.Messages.*;
031
032@Parameters(commandNames = {"doc"}, commandDescriptionKey = "doc", resourceBundle = "commands")
033public class DocCommand implements CliCommand {
034
035  @Parameter(names = "--format", descriptionKey = "doc.format", validateWith = DocFormatValidator.class)
036  String format = "html";
037
038  @Parameter(names = "--output", descriptionKey = "doc.output")
039  String output = ".";
040
041  @Parameter(descriptionKey = "source_files")
042  List<String> sources = new LinkedList<>();
043
044  private static final Map<String, AbstractProcessor> FORMATS = new HashMap<>();
045  static {
046    // TODO: use a service provider ?
047    FORMATS.put("markdown", new MarkdownProcessor());
048    FORMATS.put("html", new HtmlProcessor());
049    FORMATS.put("ctags", new CtagsProcessor());
050  }
051
052  private final GoloCompiler compiler = new GoloCompiler();
053
054  @Override
055  public void execute() throws Throwable {
056
057    AbstractProcessor processor = FORMATS.get(this.format);
058    HashMap<String, ModuleDocumentation> modules = new HashMap<>();
059    for (String source : this.sources) {
060      loadGoloFile(source, modules);
061    }
062    try {
063      processor.process(modules, Paths.get(this.output));
064    } catch (Throwable throwable) {
065      handleThrowable(throwable);
066    }
067  }
068
069  private void loadGoloFile(String goloFile, HashMap<String, ModuleDocumentation> modules) {
070    File file = new File(goloFile);
071    if (file.isDirectory()) {
072      File[] directoryFiles = file.listFiles();
073      if (directoryFiles != null) {
074        for (File directoryFile : directoryFiles) {
075          loadGoloFile(directoryFile.getAbsolutePath(), modules);
076        }
077      }
078    } else if (file.getName().endsWith(".golo")) {
079      try {
080        modules.put(goloFile, ModuleDocumentation.load(goloFile, compiler));
081      } catch (IOException e) {
082        error(message("file_not_found", goloFile));
083        return;
084      } catch (GoloCompilationException e) {
085        handleCompilationException(e);
086      }
087    }
088  }
089
090  public static class DocFormatValidator implements IParameterValidator {
091
092    @Override
093    public void validate(String name, String value) throws ParameterException {
094      if (!FORMATS.keySet().contains(value)) {
095        throw new ParameterException(message("format_error", FORMATS.keySet()));
096      }
097    }
098  }
099}