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