/* Originally based on the javac task from apache-ant-1.7.1. * The license in that file is as follows: * * Licensed to the Apache Software Foundation (ASF) under one or * more contributor license agreements. See the NOTICE file * distributed with this work for additional information regarding * copyright ownership. The ASF licenses this file to You under * the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may * obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language * governing permissions and limitations under the License. * */ /* * Copyright Red Hat Inc. and/or its affiliates and other contributors * as indicated by the authors tag. All rights reserved. */ package com.redhat.ceylon.ant; import java.util.ArrayList; import java.util.List; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Commandline; public class CeylonRunAntTask extends RepoUsingCeylonAntTask { static final String FAIL_MSG = "Run failed; see the compiler error output for details."; public static class Arg { String value; public void setValue(String value) { this.value = value; } public void addText(String value) { this.value = value; } } private String run; private String module; private String compileFlags; private List<Arg> args = new ArrayList<Arg>(0); public CeylonRunAntTask() { super("run"); } /** * Calling the run tool ATM needs a new JVM: https://github.com/ceylon/ceylon-compiler/issues/1366 */ protected boolean shouldSpawnJvm() { return true; } /** * Set the fully qualified name of a toplevel method or class with no parameters. */ public void setRun(String run) { this.run = run; } /** * Set the name of a runnable module with an optional version */ public void setModule(String module) { this.module = module; } /** * Sets compile flags */ public void setCompile(String compileFlags) { this.compileFlags = compileFlags; } /** Adds an argument to be passed to the tool */ public void addConfiguredArg(Arg arg) { this.args.add(arg); } /** * Check that all required attributes have been set and nothing silly has * been entered. * * @exception BuildException if an error occurs */ protected void checkParameters() throws BuildException { if(module == null || module.isEmpty()){ throw new BuildException("Missing module parameter is required"); } } /** * Perform the compilation. */ protected void completeCommandline(Commandline cmd) { super.completeCommandline(cmd); if(run != null){ appendOptionArgument(cmd, "--run", run); } if(compileFlags != null){ appendOptionArgument(cmd, "--compile", compileFlags); } cmd.createArgument().setValue(module); if (!args.isEmpty()) { for (Arg arg : args) { appendOption(cmd, arg.value); } } } @Override protected String getFailMessage() { return "Run failed; see the compiler error output for details."; } }