Created
June 3, 2015 06:41
-
-
Save nathankleyn/24d468058d489acf918e to your computer and use it in GitHub Desktop.
Gradle Shanty Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'english' | |
require 'shanty/plugin' | |
module Shanty | |
# Public: Gradle plugin for building and testing Gradle projects. | |
module GradleMultiProjectPlugin | |
extend Plugin | |
INCLUDE_REGEX = /include\s+((?:(?:,[\s\n\\]+)?['"][^'"]+["'])+)/ | |
QUOTED_ITEM_REGEX = /['"]([^'"]+)["']/ | |
PROJECT_COMPILE_REGEX = /compile\sproject\(['"]([^'"]+)['"]\)/ | |
adds_tags :gradle, :multiproject | |
subscribe :build, :on_build | |
subscribe :test, :on_test | |
wants_projects_matching { gradle_projects } | |
def self.gradle_projects | |
Dir["#{root}/**/settings.gradle"].flat_map do |path| | |
gradle_project_root = File.absolute_path(File.dirname(path)) | |
project_paths(path, gradle_project_root).map do |project_path| | |
parents = dependencies(project_path, gradle_project_root) | |
Project.new(File.absolute_path(project_path)).tap do |project| | |
parents.each { |parent| project.parent(parent) } | |
end | |
end | |
end | |
end | |
def self.project_paths(settings_path, root) | |
File.read(settings_path) | |
.scan(INCLUDE_REGEX) | |
.flatten | |
.map { |l| l.scan(QUOTED_ITEM_REGEX) } # Get the things being included, dropping the surrounding quotes. | |
.flatten | |
.map { |l| name_to_path(l, root) } | |
end | |
def self.dependencies(project_path, root) | |
build_gradle_file_path = File.join(project_path, 'build.gradle') | |
return [] unless File.exist?(build_gradle_file_path) | |
File.read(build_gradle_file_path) | |
.scan(PROJECT_COMPILE_REGEX) | |
.flatten | |
.map { |l| name_to_path(l, root) } | |
end | |
# Takes a Gradle name of the format :foo:bar:lux, and returns an absolute path to that project. | |
def self.name_to_path(name, root) | |
name = name[1..-1] if name[0] == ':' | |
File.join(root, name.gsub(':', '/')) | |
end | |
def on_build | |
within_project_dir do | |
puts "Running `gradle assemble` for #{name}..." | |
execute('gradle --info assemble 2>&1') | |
end | |
end | |
def on_test | |
within_project_dir do | |
puts "Running `gradle assemble` for #{name}..." | |
execute('gradle --info test 2>&1') | |
end | |
end | |
private | |
def execute(cmd) | |
output = `#{cmd}` | |
success = $CHILD_STATUS.exitstatus.zero? | |
$stderr.puts output unless success | |
success | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment