Class: DeployGate::Android::GradlePluginInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/deploygate/android/gradle_plugin_installer.rb

Constant Summary collapse

MAVEN_METADATA_URL =
'https://jcenter.bintray.com/com/deploygate/gradle/maven-metadata.xml'

Instance Method Summary collapse

Constructor Details

#initializeGradlePluginInstaller

Returns a new instance of GradlePluginInstaller.



6
7
8
# File 'lib/deploygate/android/gradle_plugin_installer.rb', line 6

def initialize
  @cli = HighLine.new
end

Instance Method Details

#apply(patch) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_patchObject



18
19
20
# File 'lib/deploygate/android/gradle_plugin_installer.rb', line 18

def create_patch
  create_root_patch + create_projects_patch
end

#create_projects_patchObject



38
39
40
41
42
43
44
45
# 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_patchObject



22
23
24
25
26
# 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_versionObject



32
33
34
35
36
# 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_scriptsObject



47
48
49
50
# 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

#installObject



10
11
12
13
14
15
16
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# 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


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/deploygate/android/gradle_plugin_installer.rb', line 64

def print_diff(patch)
  puts
  patch.each_line do |line|
    line.chomp!
    if line.match(/^(\+\+\+|---) /)
      puts @cli.color(line, HighLine::BOLD)
    elsif line.match(/^@@.+@@$/)
      puts @cli.color(line, HighLine::CYAN)
    elsif line.match(/^\+/)
      puts @cli.color(line, HighLine::GREEN)
    elsif line.match(/^\-/)
      puts @cli.color(line, HighLine::RED)
    else
      puts line
    end
  end
  puts
end

#recent_plugin_versionObject



28
29
30
# File 'lib/deploygate/android/gradle_plugin_installer.rb', line 28

def recent_plugin_version
  @plugin_version ||= fetch_recent_plugin_version
end