Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Created October 7, 2016 20:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushikgopal/c5d6be0b976b8dea1325b47477f214cf to your computer and use it in GitHub Desktop.
Save kaushikgopal/c5d6be0b976b8dea1325b47477f214cf to your computer and use it in GitHub Desktop.
a ruby release script for android apps
#!/usr/bin/env ruby
require 'highline/import'
require 'nokogiri'
# i suggest looking at this folder and seeing your app prefix ¯\_(ツ)_/¯
$gradle_properties = File.join(File.dirname(__FILE__), "..", "..", "gradle.properties")
$app_directory = "./<App Parent Directory>"
$app_name_prefix = "<Prefix for final apk e.g. uber-release->"
$splash_screen_location = "co.kaush.instashop/co.kaush.instashop.SplashActivity"
$apk_filename = File.join(File.dirname(__FILE__), "..", "build", "outputs", "apk", "uber-full-release")
$build_config_file = "#{$app_directory}/build/generated/source/buildConfig/full/release/co/kaush/instashop/BuildConfig.java"
$config_file = "#{$app_directory}/src/main/res/values/config.xml"
$mapping_file = "#{$app_directory}/build/outputs/mapping/full/release/mapping.txt"
def install_and_run_git_hook
# installing git hook for future pre-emptive checking
%x[rsync #{$app_directory}/util/pre-commit .git/hooks]
# run the script now to make sure
result = %x[ruby #{$app_directory}/util/pre-commit]
if !(result.nil? || result.empty?)
puts result
exit
end
end
def possibly_show_help_options
if "help".eql? ARGV[0]
puts "
******************** Permitted upgrade types ***************************
none - don't upgrade or change any version numbers, just build a release apk
major - major version update (do this for massively new features) (very rare)
minor - all new features
patch - any changes including bug fixes to prev. minor bump \n\n"
exit
end
end
def calculate_upgrade_type
case ARGV[0]
when "major"
return "major"
when "minor"
return "minor"
when "patch"
return "patch"
when "none"
return "none"
when nil
puts "\n\n------- You didn't specify an upgrade type, so i'm going to assume you meant \"patch\"\n\n"
return "patch"
else
puts "Grr... I don't understand that upgrade type. Run again with \"help\" to see permitted options"
exit
end
end
def calc_versions
_check_gradle_properties_exist
File.open($gradle_properties, 'r').map(&:strip).each do |line|
key, value = line.split '=', 2
$vMajor = value.to_i if key == "versionMajor"
$vMinor = value.to_i if key == "versionMinor"
$vPatch = value.to_i if key == "versionPatch"
end
_calc_new_version $upgrade
end
def check_working_directory_is_clean
num_of_lines_containing_wd_clean_output = %x[git status | grep "working directory clean" | wc -l].strip.to_i
if num_of_lines_containing_wd_clean_output == 0
STDERR.puts "your working directory is not clean"
exit
end
end
def cut_my_apk
check_working_directory_is_clean
# ./gradlew testDebugUnitTest
# ./gradlew connectedDebugAndroidTest
puts "------- cutting your apk now"
system "./gradlew clean assembleFullRelease"
check_working_directory_is_clean
end
def update_app_to_new_version
puts "\n\n------- committing version bump in gradle.properties"
puts "----------------------------------------------------"
puts "\ncurrent version is #{_get_old_version}"
puts " new version is #{_get_new_version}\n\n"
puts "----------------------------------------------------\n\n"
confirmation = ask "you cool with that (y/n)?"
if confirmation != "y"
puts "Grr... exiting"
exit
end
puts "------- updating app version now"
File.open('new_gradle.properties', 'w') do |out_file|
File.open($gradle_properties, 'r').each do |line|
key, value = line.split '=', 2
case key
when "versionMajor"
out_file.print line.sub("#{$vMajor}", "#{$vMajorNew}")
when "versionMinor"
out_file.print line.sub("#{$vMinor}", "#{$vMinorNew}")
when "versionPatch"
out_file.print line.sub("#{$vPatch}", "#{$vPatchNew}")
else
out_file.print line
end
end
end
File.rename('new_gradle.properties', 'gradle.properties')
puts "------- committing version bump in gradle.properties"
system "git add #{$gradle_properties}"
system "git commit -vm \"bot: update version #{_get_old_version} -> #{_get_new_version}\""
end
def git_tag_new_release
puts "------- tagging new release"
system "git tag -a v#{_get_new_version} -m \"app release v#{_get_new_version}\""
# confirmation = ask "should i push ur tag to github (y/n)?"
# if confirmation != "y"
# system "git push --tags"
# else
# puts "Ok, not pushing to github. But remember to do later!"
# end
end
def copy_new_build_apk_to_desktop
system "cp -a #{$apk_filename}.apk ~/Desktop/#{$app_name_prefix}#{_get_new_version}.apk"
puts "Ok, your new apk is in the desktop folder! [#{$app_name_prefix}#{_get_new_version}.apk]"
end
def install_and_launch_apk
system "adb install -r ~/Desktop/#{$app_name_prefix}#{_get_new_version}.apk"
system "adb shell am start -n #{$splash_screen_location}"
end
def _calc_new_version(v_type = nil)
case v_type
when "major"
$vMajorNew = $vMajor + 1
$vMinorNew = 0
$vPatchNew = 0
when "minor"
$vMinorNew = $vMinor + 1
$vPatchNew = 0
when "patch"
$vPatchNew = $vPatch + 1
end
$vPatchNew = $vPatch if $vPatchNew.nil?
$vMinorNew = $vMinor if $vMinorNew.nil?
$vMajorNew = $vMajor if $vMajorNew.nil?
end
def _check_gradle_properties_exist
if !File.exist? $gradle_properties
STDERR.puts "can't find your gradle properties yo!"
exit 1
end
end
def _get_old_version
"#{$vMajor}.#{$vMinor}.#{$vPatch}"
end
def _get_new_version
return _get_old_version if !$upgrade
"#{$vMajorNew}.#{$vMinorNew}.#{$vPatchNew}"
end
def _upload_mapping_to_crash_reporters
puts "\n\n------- uploading mapping.txt to Crash Reporting tools for proper de-obfuscation\n"
if !File.exist? $mapping_file
STDERR.puts "can't find mapping.txt for obfuscation: Crash reporters won't show proper traces"
return
end
if !File.exist? $config_file
STDERR.puts "can't find config.xml in project: Crash reporters won't show proper traces"
return
end
doc = Nokogiri::XML(File.open($config_file).read)
rollbar_server_token = doc.xpath('//*[@name="rollbar_server_token"]').text
if !File.exist? $build_config_file
STDERR.puts "can't find generated BuildConfig.java in project: Crash reporters won't show proper traces"
return
end
app_version_name = ""
app_version_code = ""
app_id = ""
File.open($build_config_file, 'r').each do |line|
key, value = line.split '=', 2
if key.include? "VERSION_NAME"
app_version_name = value.strip.scan(/"(.*)"/)[0][0]
end
if key.include? "APPLICATION_ID"
app_id = value.strip.chomp(";")
end
if key.include? "VERSION_CODE"
app_version_code = value.strip.chomp(";")
end
if !(app_version_name.nil? || app_version_name.empty?) &&
!(app_version_code.nil? || app_version_code.empty?) &&
!(app_id.nil? || app_id.empty?)
break
end
end
puts "\n------ uploading to rollbar \n"
# puts "rollbar = #{rollbar_server_token}\napp_version_name = #{app_version_name}"
system ("curl 'https://api.rollbar.com/api/1/proguard' \
-F access_token=#{rollbar_server_token} \
-F version=#{app_version_name} \
-F mapping=@#{$mapping_file} \
> /dev/null")
puts "\n------ upload to rollbar complete\n"
end
def main
install_and_run_git_hook
possibly_show_help_options
$upgrade = calculate_upgrade_type
puts "------- Ok proceeding with upgrade type = #{$upgrade}"
check_working_directory_is_clean
calc_versions
if $upgrade.eql? "none"
cut_my_apk
else
update_app_to_new_version
cut_my_apk
git_tag_new_release
end
_upload_mapping_to_crash_reporters
copy_new_build_apk_to_desktop
puts "\n\n------- all done! remember to push your changes to github. I won't do that\n"
install_and_launch_apk
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment