|
| 1 | +package com.lookout.jruby |
| 2 | + |
| 3 | + |
| 4 | +import org.gradle.api.Project |
| 5 | +import org.gradle.api.artifacts.Configuration |
| 6 | +import org.gradle.api.file.FileCollection |
| 7 | +import org.gradle.api.tasks.Input |
| 8 | +import org.gradle.api.tasks.InputFile |
| 9 | +import org.gradle.api.tasks.InputFiles |
| 10 | +import org.gradle.api.tasks.JavaExec |
| 11 | +import org.gradle.api.tasks.Optional |
| 12 | +import org.gradle.api.tasks.TaskInstantiationException |
| 13 | +import org.gradle.internal.FileUtils |
| 14 | +import org.gradle.process.JavaExecSpec |
| 15 | +import org.gradle.util.CollectionUtils |
| 16 | + |
| 17 | +/** Runs a ruby script using JRuby |
| 18 | + * |
| 19 | + * @author Schalk W. Cronjé |
| 20 | + */ |
| 21 | +class JRubyExec extends JavaExec { |
| 22 | + |
| 23 | + static final String JRUBYEXEC_CONFIG = 'jrubyExec' |
| 24 | + |
| 25 | + static void updateJRubyDependencies(Project proj) { |
| 26 | + proj.dependencies { |
| 27 | + jrubyExec "org.jruby:jruby-complete:${proj.jruby.execVersion}" |
| 28 | + } |
| 29 | + |
| 30 | + proj.tasks.withType(JRubyExec) { t -> |
| 31 | + if(t.jrubyConfigurationName != proj.configurations.jrubyExec) { |
| 32 | + proj.dependencies.add(t.jrubyConfigurationName,"org.jruby:jruby-complete:${t.jrubyVersion}") |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** Script to execute. |
| 38 | + * |
| 39 | + */ |
| 40 | + @InputFile |
| 41 | + File script |
| 42 | + |
| 43 | + /** Configuration to copy gems from. If {@code jRubyVersion} has not been set, {@code jRubyExec} will used as |
| 44 | + * configuration. However, if {@code jRubyVersion} has been set, not gems will be used unless an explicit |
| 45 | + * configuration has been provided |
| 46 | + * |
| 47 | + */ |
| 48 | + @Optional |
| 49 | + @Input |
| 50 | + String configuration |
| 51 | + |
| 52 | + @Input |
| 53 | + String jrubyVersion |
| 54 | + |
| 55 | + JRubyExec() { |
| 56 | + super() |
| 57 | + super.setMain 'org.jruby.Main' |
| 58 | + |
| 59 | + try { |
| 60 | + project.configurations.getByName(JRUBYEXEC_CONFIG) |
| 61 | + } catch(UnknownConfigurationException ) { |
| 62 | + throw new TaskInstantiationException('Cannot instantiate a JRubyExec instance before jruby plugin has been loaded') |
| 63 | + } |
| 64 | + |
| 65 | + jrubyVersion = project.jruby.execVersion |
| 66 | + jrubyConfigurationName = JRUBYEXEC_CONFIG |
| 67 | + } |
| 68 | + |
| 69 | + /** Sets the scriptname |
| 70 | + * |
| 71 | + * @param fName Path to script |
| 72 | + */ |
| 73 | + void setScript(Object fName) { |
| 74 | + switch (fName) { |
| 75 | + case File: |
| 76 | + script=fName |
| 77 | + break |
| 78 | + case String: |
| 79 | + script=new File(fName) |
| 80 | + break |
| 81 | + default: |
| 82 | + script=new File(fName.toString()) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** Returns a list of script arguments |
| 87 | + */ |
| 88 | + List<String> scriptArgs() {CollectionUtils.stringize(this.scriptArgs)} |
| 89 | + |
| 90 | + /** Set arguments for script |
| 91 | + * |
| 92 | + * @param args |
| 93 | + */ |
| 94 | + void scriptArgs(Object... args) { |
| 95 | + this.scriptArgs.addAll(args as List) |
| 96 | + } |
| 97 | + |
| 98 | + /** Returns a list of jruby arguments |
| 99 | + */ |
| 100 | + List<String> jrubyArgs() {CollectionUtils.stringize(this.jrubyArgs)} |
| 101 | + |
| 102 | + /** Set arguments for jruby |
| 103 | + * |
| 104 | + * @param args |
| 105 | + */ |
| 106 | + void jrubyArgs(Object... args) { |
| 107 | + this.jrubyArgs.addAll(args as List) |
| 108 | + } |
| 109 | + |
| 110 | + /** Setting the {@code jruby-complete} version allows for tasks to be run using different versions of JRuby. |
| 111 | + * This is useful for comparing the results of different version or running with a gem that is only |
| 112 | + * compatible with a specific version or when running a script with a different version that what will |
| 113 | + * be packaged. |
| 114 | + * |
| 115 | + * @param version String in the form '1.7.13' |
| 116 | + */ |
| 117 | + void setJrubyVersion(final String version) { |
| 118 | + if(version == project.jruby.execVersion) { |
| 119 | + jrubyConfigurationName = JRUBYEXEC_CONFIG |
| 120 | + } else { |
| 121 | + final String cfgName= 'jrubyExec$$' + name |
| 122 | + project.configurations.maybeCreate(cfgName) |
| 123 | + jrubyConfigurationName = cfgName |
| 124 | + } |
| 125 | + jrubyVersion = version |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + void exec() { |
| 130 | + if(configuration == null && jrubyConfigurationName == JRUBYEXEC_CONFIG) { |
| 131 | + configuration = JRUBYEXEC_CONFIG |
| 132 | + } |
| 133 | + |
| 134 | + def jrubyCompletePath = project.configurations.getByName(jrubyConfigurationName) |
| 135 | + File gemDir = tmpGemDir() |
| 136 | + environment 'GEM_HOME' : gemDir |
| 137 | + |
| 138 | + if(configuration != null) { |
| 139 | + project.configurations.getByName(jrubyConfigurationName) |
| 140 | + project.with { |
| 141 | + mkdir gemDir |
| 142 | + configurations.getByName(configuration).files.findAll { File f -> |
| 143 | + f.name.endsWith('.gem') |
| 144 | + }.each { File f -> |
| 145 | + GemUtils.extractGem(project,jrubyCompletePath,f,gemDir,true) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + super.classpath project.configurations.getByName(jrubyConfigurationName).files |
| 151 | + super.setArgs(getArgs()) |
| 152 | + super.exec() |
| 153 | + } |
| 154 | + |
| 155 | + /** getArgs gets overridden in order to add JRuby options, script name and script argumens in the correct order |
| 156 | + */ |
| 157 | + @Override |
| 158 | + List<String> getArgs() { |
| 159 | + def cmdArgs = [] |
| 160 | + cmdArgs.addAll(jrubyArgs) |
| 161 | + cmdArgs.add(script.absolutePath) |
| 162 | + cmdArgs.addAll(scriptArgs) |
| 163 | + cmdArgs as List<String> |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + JavaExec setMain(final String mainClassName) { |
| 168 | + if(mainClassName == 'org.jruby.Main') { |
| 169 | + super.setMain(mainClassName) |
| 170 | + } else { |
| 171 | + throw notAllowed("Setting main class for jruby to ${mainClassName} is not a valid operation") |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + JavaExec setArgs(Iterable<?> applicationArgs) { |
| 177 | + throw notAllowed('Use jvmArgs / scriptArgs instead') |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + JavaExec args(Object... args) { |
| 182 | + throw notAllowed('Use jvmArgs / scriptArgs instead') |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + JavaExecSpec args(Iterable<?> args) { |
| 187 | + throw notAllowed('Use jvmArgs / scriptArgs instead') |
| 188 | + } |
| 189 | + |
| 190 | + /** Returns the {@code Configuration} object this task is tied to |
| 191 | + */ |
| 192 | + String getJrubyConfigurationName() { |
| 193 | + return this.jrubyConfigurationName |
| 194 | + } |
| 195 | + |
| 196 | + private static UnsupportedOperationException notAllowed(final String msg) { |
| 197 | + return new UnsupportedOperationException (msg) |
| 198 | + } |
| 199 | + |
| 200 | + private File tmpGemDir() { |
| 201 | + String ext = FileUtils.toSafeFileName(jrubyConfigurationName) |
| 202 | + if(configuration) { |
| 203 | + ext= "${ext}-${FileUtils.toSafeFileName(configuration)}" |
| 204 | + } |
| 205 | + new File( project.buildDir, "tmp/${ext}").absoluteFile |
| 206 | + } |
| 207 | + |
| 208 | + private String jrubyConfigurationName |
| 209 | + private List<Object> jrubyArgs = [] |
| 210 | + private List<Object> scriptArgs = [] |
| 211 | + |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | + |
0 commit comments