import groovy.util.AntBuilder
buildscript {
dependencies {
classpath 'org.jruby:jruby-complete:1.7.13'
}
}
jar {
from ("${project.projectDir}/src/main/ruby")
}
dependencies {
runtime project(':shared')
}
sourceSets {
main {
java {
srcDir file("${project.projectDir}/src/main/ruby")
}
}
}
/*
* Installs Bundler to the project's build directory. Bundler is used to
* install any Gems required by this plugin.
*/
task installBundler(type:JavaExec, description:'Installs Bundler') {
args = "--2.0 -S gem install -i ${project.buildDir}/bundler --no-rdoc --no-ri bundler".tokenize()
classpath = project.buildscript.configurations.classpath
jvmArgs("-Xmx800M")
main = 'org.jruby.Main'
environment = [HOME:System.getProperty('user.home'),
PATH:['/usr/local/bin', '/usr/bin','/bin','/usr/sbin','/sbin'].join(File.pathSeparator)]
workingDir = project.projectDir
}
/*
* Installs the Gems required by this plugin to the project's build directory.
* This task uses Bundler to perform the Gem installations.
*/
task installGems(type:JavaExec, description:'Installs all required Gems via Bundle.', dependsOn:'installBundler') {
args = "--2.0 -S bundle install --path ${project.buildDir}".tokenize()
classpath = project.buildscript.configurations.classpath
main = 'org.jruby.Main'
environment = [GEM_PATH: "${project.buildDir}/bundler",
HOME:System.getProperty('user.home'),
PATH:["${project.buildDir}/bundler/bin", '/usr/local/bin', '/usr/bin','/bin','/usr/sbin','/sbin'].join(File.pathSeparator)]
workingDir = project.projectDir
}
/*
* Moves the installed Gem files from the project's build directory to src/main/resources
* so that they will be included in the packaged JAR.
*/
task packageGems(dependsOn:'installGems') {
doLast {
Properties props = new Properties()
props.load(new File("${project.projectDir}/src/main/resources/META-INF/notification/${project.name}.plugin").newDataInputStream())
File parent = new File("${project.projectDir}/src/main/resources/${props.getProperty('plugin-name')}/gems")
parent.deleteDir()
parent.mkdirs()
// Normalize each installed gem directory name and move it to src/main/resources
new File("${project.buildDir}/jruby/1.9/gems").listFiles().each { file ->
processSourceFiles(parent, new File(file, 'lib'))
File vendorGems = new File(file, 'vendor/gems')
if(vendorGems.exists()) {
vendorGems.listFiles().each { vendorFile ->
processSourceFiles(parent, new File(vendorFile, 'lib'))
}
}
}
}
}
project.tasks.installGems.inputs.file("${project.projectDir}/Gemfile")
project.tasks.installGems.outputs.dir("${project.buildDir}/jruby")
project.tasks.installBundler.outputs.upToDateWhen { new File("${project.buildDir}/bundler").exists() }
project.tasks.jar.dependsOn(['packageGems'])
def processSourceFiles(File newParent, File rootDir) {
File newRootDir = new File(newParent, rootDir.getParentFile().getName())
new AntBuilder().copy(todir : newRootDir.getAbsolutePath(), quiet:true) {
fileset(dir: rootDir.getAbsolutePath())
}
newRootDir.eachFileRecurse { rubyFile ->
if(rubyFile.isFile() && rubyFile.text.contains('require_relative')) {
def builder = new StringBuilder()
rubyFile.eachLine { line ->
line = line.replaceAll('require_relative\\s+\'\\.\\/(.+)\'', 'require_relative \'$1\'')
def matcher = line =~ /require_relative\s+'((\.\.\/)+).+'/
if(matcher.find()) {
def numberOfParentDirs = matcher[0][1].split('/').length
def actualParent = rubyFile.getParentFile()
numberOfParentDirs.times { actualParent = actualParent.getParentFile() }
actualParent = actualParent.getAbsolutePath().minus("${newRootDir.getAbsolutePath()}/")
line = line.replaceAll('require_relative\\s+\'(?:\\.\\.\\/)+(.+)\'', "require '${actualParent ? "${actualParent}/" : actualParent}\$1'")
}
builder.append(line.trim())
builder.append('\n')
}
rubyFile.write(builder.toString())
}
}
}