|
| 1 | +/** |
| 2 | + * Copyright (c) 2013, Adam Retter <[email protected]> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * Redistributions in binary form must reproduce the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer in the documentation and/or |
| 13 | + * other materials provided with the distribution. |
| 14 | + * |
| 15 | + * Neither the name of the {organization} nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package hudson.plugins.scala.executer |
| 31 | + |
| 32 | +import hudson.model.{BuildListener, AbstractBuild} |
| 33 | +import hudson.{EnvVars, FilePath, Launcher} |
| 34 | + |
| 35 | +class ForkedScalaExecutor { |
| 36 | + |
| 37 | + def execute(build: AbstractBuild[_, _], launcher: Launcher, listener: BuildListener, scalaHome: String, scalaExecutable: String, script: FilePath, scalaParameters: String, classpath: String, scriptParameters: String, debug: Boolean, suspend: Boolean, jdwpPort: Integer) : Boolean = { |
| 38 | + |
| 39 | + /** |
| 40 | + * Strings that come in from java may be null or empty |
| 41 | + * but to us the effect is the same, we are only interested |
| 42 | + * in strings with content |
| 43 | + */ |
| 44 | + def nonEmptyString(javaString: String) : Option[String] = { |
| 45 | + if(javaString == null || javaString.isEmpty) { |
| 46 | + None |
| 47 | + } else { |
| 48 | + Some(javaString) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + //TODO add checkbox options for "-nocompdaemon" and "-savecompiled" make nocompdaemon on by default |
| 53 | + def scalaCmdParameters : Option[String] = Some(("-nocompdaemon" :: nonEmptyString(scalaParameters).toList).mkString(" ").trim()) |
| 54 | + |
| 55 | + def javaDebugParameters : Option[String] = { |
| 56 | + def booleanToChar(boolean: Boolean) = if(boolean) 'y' else 'n' |
| 57 | + debug match { |
| 58 | + case true => Some(s"-J-debug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=${booleanToChar(suspend)},address=$jdwpPort") |
| 59 | + case false => None |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + def scalaClassPathParameter : Option[String] = nonEmptyString(classpath).map(cl => "-cp " + cl) |
| 64 | + |
| 65 | + def execCommand(script: FilePath) : String = { |
| 66 | + val cmdParts: List[Option[String]] = List(nonEmptyString(scalaExecutable), scalaCmdParameters, javaDebugParameters, scalaClassPathParameter, nonEmptyString(script.getRemote), nonEmptyString(scriptParameters)) |
| 67 | + cmdParts.flatten.mkString(" ") |
| 68 | + } |
| 69 | + |
| 70 | + def executeScript(env: EnvVars, workspace: FilePath, script: FilePath) : Boolean = { |
| 71 | + val cmd = execCommand(script) |
| 72 | + //val shell = new Shell(scala_launch_cmd) |
| 73 | + listener.getLogger().println("Scala command is: " + cmd) |
| 74 | + |
| 75 | + val result = launcher.launch().cmdAsSingleString(cmd).envs(env).stdout(listener).pwd(workspace).join() |
| 76 | + //val result = launcher.launch().cmds(cmd).envs(env).stdout(listener).pwd(workspace).join() |
| 77 | + //shell.perform(build, launcher, listener); |
| 78 | + result == 0 |
| 79 | + } |
| 80 | + |
| 81 | + val env = build.getEnvironment(listener) |
| 82 | + nonEmptyString(scalaHome) match { |
| 83 | + case Some(scalaHome) => { |
| 84 | + env.put("SCALA_HOME", scalaHome) |
| 85 | + listener.getLogger.println(s"Set SCALA_HOME=$scalaHome") |
| 86 | + } |
| 87 | + case None => |
| 88 | + } |
| 89 | + |
| 90 | + val workspace = build.getWorkspace() |
| 91 | + Option(script) match { |
| 92 | + case Some(script) => { |
| 93 | + executeScript(env, workspace, script) |
| 94 | + } |
| 95 | + case None => { |
| 96 | + listener.fatalError("Could not process Scala Script") |
| 97 | + false |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments