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;
014import com.beust.jcommander.ParametersDelegate;
015import org.eclipse.golo.cli.command.spi.CliCommand;
016
017import java.util.LinkedList;
018import java.util.List;
019
020import static gololang.Messages.*;
021
022@Parameters(commandNames = {"run"}, resourceBundle = "commands", commandDescriptionKey = "run")
023public class RunCommand implements CliCommand {
024
025  @Parameter(names = "--module", descriptionKey = "main_module", required = true)
026  String module;
027
028  @Parameter(descriptionKey = "arguments")
029  List<String> arguments = new LinkedList<>();
030
031  @ParametersDelegate
032  ClasspathOption classpath = new ClasspathOption();
033
034  @Override
035  public void execute() throws Throwable {
036
037    try {
038      Class<?> module = Class.forName(this.module, true, classpath.initGoloClassLoader());
039      callRun(module, this.arguments.toArray(new String[this.arguments.size()]));
040    } catch (ClassNotFoundException e) {
041      error(message("module_not_found", this.module));
042    } catch (CliCommand.NoMainMethodException e) {
043      error(message("module_no_main", this.module));
044    }
045  }
046}