class DeployGate::Android::GradlePluginInstaller

Constants

MAVEN_METADATA_URL

Public Class Methods

new() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 6
def initialize
  @cli = HighLine.new
end

Public Instance Methods

apply(patch) click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 83
def apply(patch)
  Open3.popen3('patch -p0') do |stdin, stdout, stderr, wait_thr|
    stdin.puts patch
    stdin.close_write
    out = stdout.read
    error = stderr.read
    if !error.empty?
      print out
      print @cli.color(error, HighLine::RED)
      false
    else
      print @cli.color(out, HighLine::GREEN)
      true
    end
  end
end
create_patch() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 18
def create_patch
  create_root_patch + create_projects_patch
end
create_projects_patch() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 38
def create_projects_patch
  files = find_project_scripts
  files.map do |file|
    make_diff file,
              /apply plugin:\s*["'](android|com\.android\.application)["']/,
              "apply plugin: 'deploygate'"
  end.join "\n"
end
create_root_patch() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 22
def create_root_patch
  make_diff 'build.gradle',
            /classpath\s*['"]com\.android\.tools\.build:gradle:/,
            "classpath 'com.deploygate:gradle:#{recent_plugin_version}'"
end
fetch_recent_plugin_version() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 32
def fetch_recent_plugin_version
  open(MAVEN_METADATA_URL) do |io|
    REXML::Document.new(io).elements['metadata/versioning/release'].text
  end
end
find_project_scripts() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 47
def find_project_scripts
  %x(grep -REl --include build.gradle "apply plugin:[ ]*[\\"'](android|com.android.application)[\\"']" .).
      split(/\n/)
end
install() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 10
def install
  patch = create_patch
  print_diff patch
  if @cli.agree("<%= color('These lines will be added to activate the plugin.', BOLD) %> Apply changes? ") {|q| q.default = 'y'}
    apply patch
  end
end
make_diff(file, match, insert) click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 52
def make_diff(file, match, insert)
  tmpfile = Tempfile.open(%w(build .gradle))
  File.readlines(file).each do |line|
    tmpfile.print line
    if (md = line.match(match))
      tmpfile.puts "#{' '*md.begin(0)}#{insert}"
    end
  end
  tmpfile.close
  %x(diff -u "#{file}" "#{tmpfile.path}")
end
print_diff(patch) click to toggle source
recent_plugin_version() click to toggle source
# File lib/deploygate/android/gradle_plugin_installer.rb, line 28
def recent_plugin_version
  @plugin_version ||= fetch_recent_plugin_version
end