Class: HighFive::Thor::Tasks::Android

Inherits:
HighFive::Thor::Task show all
Includes:
AndroidHelper, ImageHelper, Thor::Actions
Defined in:
lib/high_five/thor/tasks/android.rb

Instance Method Summary collapse

Methods included from ImageHelper

#generate_ios_splash_screen_image, #generate_splash_screen_image, #replace_image

Methods included from AndroidHelper

#android_manifest_path, #gradle_file_path, #parse_resolution, project_name_from_build_xml, #res_map, #valid_directories

Methods inherited from HighFive::Thor::Task

inherited

Instance Method Details

#buildObject



20
21
22
23
# File 'lib/high_five/thor/tasks/android.rb', line 20

def build
  run('ant -file build.xml "-Dkey.store=/Users/Shared/Jenkins/Home/jobs/modern resident/workspace/modern_resident-mobile/android/Keystore.ks" -Dkey.store.password=modernresident -Dkey.alias=android -Dkey.alias.password=modernresident clean release
    Buildfile: /Users/Shared/Jenkins/Home/jobs/modern resident/workspace/modern_resident-mobile/android/build.xml')
end

#debug(target) ⇒ Object



14
15
16
17
# File 'lib/high_five/thor/tasks/android.rb', line 14

def debug(target)
  @destination_root = base_config.root
  puts "Debugy #{target}"
end

#generate_splash_screen(path) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/high_five/thor/tasks/android.rb', line 126

def generate_splash_screen(path)
  image = ChunkyPNG::Image.from_file(path)

  splash_name = 'screen.png'

  drawable_dir = File.join File.dirname(android_manifest_path), 'res'
  Dir.glob(File.join drawable_dir, "drawable*").each do |dir|
    splash_path = File.join(dir, splash_name)
    if File.exists?(splash_path)
      generate_splash_screen_image splash_path, path, options[:color] || "#ffffff"
      puts "Writing #{splash_path}"
    end
  end
end

#set_icon(path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/high_five/thor/tasks/android.rb', line 82

def set_icon(path)
  image = ChunkyPNG::Image.from_file(path)

  manifest = File.read(android_manifest_path)
  puts "Using android manifest: #{android_manifest_path}"
  if match = manifest.match(/android:icon="@drawable\/(.*?)"/)
    prefix = "drawable"
  elsif match = manifest.match(/android:icon="@mipmap\/(.*?)"/)
    prefix = "mipmap"
  end
  icon_name = match[1] + '.png'

  drawable_dir = File.join File.dirname(android_manifest_path), 'res'
  valid_directories(drawable_dir, prefix).each do |dir|
    res = parse_resolution(dir)
    size = res_map[res]
    icon_path = File.join(dir, icon_name)
    replace_image icon_path, path
    puts "Writing #{size}x#{size} -> #{icon_path}"
  end
end

#set_splash_screen(path) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/high_five/thor/tasks/android.rb', line 106

def set_splash_screen(path)
  image = ChunkyPNG::Image.from_file(path)

  splash_name = 'screen.png'

  drawable_dir = File.join File.dirname(android_manifest_path), 'res'
  Dir.glob(File.join drawable_dir, "drawable*").each do |dir|
    splash_path = File.join(dir, splash_name)
    if File.exists?(splash_path)
      res = parse_resolution(splash_path)
      size = res_map[res]
      replace_image splash_path, path
      puts "Writing #{size}x#{size} -> #{splash_path}"
    end
  end
end

#set_versionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/high_five/thor/tasks/android.rb', line 30

def set_version
  # read and parse the old file
  file = File.read(android_manifest_path)
  xml = Nokogiri::XML(file)

  # replace \n and any additional whitespace with a space
  puts "========================================="
  puts "#{android_manifest_path}"
  puts "========================================="
  xml.xpath("//manifest").each do |node|
    if (options[:version])
      old = node["android:versionName"]
      node["android:versionName"] = options[:version]
      puts "Setting version #{old} => #{options[:version]}"
    end
    if (options[:build_number])
      old = node["android:versionCode"]
      node["android:versionCode"] = options[:build_number]
      puts "Setting versionCode #{old} => #{options[:build_number]}"
    end
  end

  if gradle_file_path
    gradle_file = File.read(gradle_file_path)
    match = gradle_file.match(/versionName\s"(.+)"$/)
    if match
      puts "========================================="
      puts "#{gradle_file_path}"
      puts "========================================="

      old = match[1]
      gradle_file.gsub!(/versionName\s"(.+)"$/, "versionName \"#{options[:version]}\"")
      puts "Setting version #{old} => #{options[:version]}"

      old = gradle_file.match(/versionCode\s(\d+)$/)[1]
      gradle_file.gsub!(/versionCode\s\d+$/, "versionCode #{options[:build_number]}")
      puts "Setting versionCode #{old} => #{options[:build_number]}"

      File.open(gradle_file_path, "w") do |f|
        f.write gradle_file
      end
    end
  end

  # save the output into a new file
  File.open(android_manifest_path, "w") do |f|
    f.write xml.to_xml
  end
end