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.Parameter; 014import com.beust.jcommander.Parameters; 015import com.beust.jcommander.ParametersDelegate; 016import com.beust.jcommander.converters.FileConverter; 017import org.eclipse.golo.cli.command.spi.CliCommand; 018import org.eclipse.golo.compiler.GoloCompiler; 019import org.eclipse.golo.compiler.GoloClassLoader; 020import org.eclipse.golo.cli.GoloFilesManager; 021 022import java.io.File; 023import java.util.Collection; 024import java.util.LinkedList; 025import java.util.Objects; 026 027 028@Parameters(commandNames = "compile", resourceBundle = "commands", commandDescriptionKey = "compile") 029public final class CompilerCommand implements CliCommand { 030 031 @Parameter(names = "--output", descriptionKey = "compile.output") 032 String output = "."; 033 034 @Parameter(descriptionKey = "source_files", converter = FileConverter.class) 035 LinkedList<File> sources = new LinkedList<>(); 036 037 @ParametersDelegate 038 ClasspathOption classpath = new ClasspathOption(); 039 040 @Parameter(names = "--verbose", descriptionKey = "verbose") 041 boolean verbose = false; 042 043 @Override 044 public boolean verbose() { 045 return this.verbose; 046 } 047 048 @Override 049 public void execute() throws Throwable { 050 GoloClassLoader loader = classpath.initGoloClassLoader(); 051 GoloCompiler compiler = loader.getCompiler(); 052 try (GoloFilesManager fm = GoloFilesManager.of(this.output)) { 053 GoloFilesManager.goloFiles(this.sources) 054 .filter(this::canRead) 055 .map(wrappedTreatment(compiler::parse)) 056 .map(wrappedTreatment(compiler::transform)) 057 .sorted(CliCommand.MODULE_COMPARATOR) 058 .map(displayInfo("Compiling %s")) 059 .map(wrappedTreatment(compiler::expand)) 060 .map(wrappedTreatment(compiler::refine)) 061 .map(wrappedTreatment(compiler::generate)) 062 .filter(Objects::nonNull) 063 .flatMap(Collection::stream) 064 .forEach(r -> { 065 loader.load(r); 066 fm.save(r); 067 }); 068 } 069 } 070}