001/*
002 * Copyright (c) 2012-2018 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.Parameter;
014import com.beust.jcommander.Parameters;
015import com.beust.jcommander.ParametersDelegate;
016
017import java.io.File;
018import java.io.IOException;
019import java.util.LinkedList;
020import java.util.List;
021
022import org.eclipse.golo.cli.command.spi.CliCommand;
023import org.eclipse.golo.compiler.GoloCompilationException;
024import org.eclipse.golo.compiler.GoloCompiler;
025
026import static gololang.Messages.*;
027
028@Parameters(commandNames = {"check"}, resourceBundle = "commands", commandDescriptionKey = "check")
029public class CheckCommand implements CliCommand {
030
031  @Parameter(names = {"--exit"}, descriptionKey = "check.exit")
032  boolean exit = false;
033
034  @Parameter(names = {"--verbose"}, descriptionKey = "check.verbose")
035  boolean verbose = false;
036
037  @Parameter(descriptionKey = "source_files")
038  List<String> files = new LinkedList<>();
039
040  @ParametersDelegate
041  ClasspathOption classpath = new ClasspathOption();
042
043  @Override
044  public void execute() throws Throwable {
045    GoloCompiler compiler = classpath.initGoloClassLoader().getCompiler();
046    for (String file : files) {
047      check(new File(file), compiler);
048    }
049  }
050
051  private void check(File file, GoloCompiler compiler) {
052    if (file.isDirectory()) {
053      File[] directoryFiles = file.listFiles();
054      if (directoryFiles != null) {
055        for (File directoryFile : directoryFiles) {
056          check(directoryFile, compiler);
057        }
058      }
059    } else if (file.getName().endsWith(".golo")) {
060      try {
061        if (verbose) {
062          System.err.println(">>> " + message("check_info", file.getAbsolutePath()));
063        }
064        compiler.resetExceptionBuilder();
065        compiler.check(compiler.parse(file.getAbsolutePath()));
066      } catch (IOException e) {
067        error(message("file_not_found", file));
068      } catch (GoloCompilationException e) {
069        handleCompilationException(e, exit);
070      }
071    }
072  }
073}
074