Skip to content

Instantly share code, notes, and snippets.

@paulocheque
Last active December 14, 2015 16:09
Show Gist options
  • Save paulocheque/5113161 to your computer and use it in GitHub Desktop.
Save paulocheque/5113161 to your computer and use it in GitHub Desktop.
Rake scripts
# You may put this on the rakelib directory or renamed it to .rb and add a 'require' command
MAIN_PATH = File.dirname(__FILE__)
module OS
def OS.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def OS.unix?
!OS.windows?
end
def OS.linux?
OS.unix? and not OS.mac?
end
end
def colorize(text, color)
color_codes = {
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37
}
code = color_codes[color]
if code == nil
text
else
"\033[#{code}m#{text}\033[0m"
end
end
def md5(source, target)
require "digest/md5"
md5 = Digest::MD5.hexdigest(File.read(source))
File.open(target, 'w') { |file| file.write(md5) }
end
def check_env_var(var)
if not ENV[var]
puts colorize("#{var} enviroment variable not found", :red)
exit(0)
end
end
def run_in_dir(command, dir='.')
sh "cd #{dir} && #{command}"
end
# Git
def git_rebase(remote="origin", branch="master")
sh "git fetch #{remote} && git rebase #{remote}/#{branch}"
end
def git_push(remote="origin", branch="master")
sh "git pull #{remote} #{branch} && git push #{remote} #{branch}"
end
def git_tag(tag, remote="origin")
sh "git tag #{tag} && git push #{remote} #{tag}"
end
def git_reset_tag(tag, remote="origin")
sh "git tag -d #{tag} && git push origin :refs/tags/#{tag}"
sh "git tag #{tag} && git push #{remote} #{tag}"
end
# Python
def create_virtual_env(dir, python="python2.7")
sh "virtualenv #{dir} -p #{python}"
end
def virtual_env(command, env="env27")
sh "source #{env}/bin/activate && #{command}"
end
def python_install_dependencies(envs)
envs.each { |env|
puts colorize("Environment #{env}", :blue)
virtual_env("pip install -r requirements.txt", "#{env}")
virtual_env("pip install -r requirements-test.txt", "#{env}")
}
end
def python_tests(envs)
envs.each { |env|
puts colorize("Environment #{env}", :blue)
virtual_env("nosetests --process-timeout=3 --verbosity=2", env)
}
end
def python_package()
virtual_env("python setup.py sdist")
end
# iOS
def ios_xcode_build(project, scheme, configuration="Release", arch="armv7 armv7s", sdk="iphoneos6.1")
sh "xcodebuild -project #{project}.xcodeproj -scheme '#{scheme}' -configuration #{configuration} -arch '#{arch}' -sdk #{sdk} clean build"
end
def ios_install_provision_ipa(ipa)
puts colorize("Installing provisioning", :blue)
end
def ios_install_ipa(ipa)
check_env_var("LIB_IMOBILE_DEVICE")
# sh "git clone https://github.com/benvium/libimobiledevice-macosx.git"
sh "export PATH=$PATH:$LIB_IMOBILE_DEVICE"
sh "export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$LIB_IMOBILE_DEVICE"
puts colorize("Installing on iOS device", :blue)
sh "PATH=$PATH:$LIB_IMOBILE_DEVICE PATH=$PATH:$LIB_IMOBILE_DEVICE ideviceinstaller -i #{ipa}"
end
# Android
def android_install_apk(apk)
sh "adb install -r #{apk}"
end
def android_logcat(apk, expression=nil)
if expression
sh "adb logcat | grep #{expression}"
else
sh "adb logcat"
end
end
# Package
def pack(package_file, files, exclude_list)
puts colorize("Packing to #{package_file}", :blue)
exclude_command = "--exclude=\*.DS_Store\* --exclude=\*~ "
exclude_list.each { | term |
exclude_command += "--exclude=#{term} "
}
sh "zip -y -qdgds 1m -b #{MAIN_PATH} #{package_file} -r #{files} #{exclude_command}"
puts colorize("Package created: ", :green) + "#{package_file}"
end
# Upload
def upload_to_s3(files, bucket_name)
# rvm install 1.9.3
# gem install aws-sdk
require 'aws-sdk'
puts colorize("Uploading to S3", :blue)
AWS.config(access_key_id: ENV["AWS_ACCESS_KEY_ID"], secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], region: ENV["AWS_REGION"])
bucket = AWS.s3.buckets[bucket_name]
if not bucket.exists? then
bucket = AWS.s3.buckets.create(bucket_name, :acl => :public_read)
bucket.configure_website
end
files.each { |filename|
key = File.basename(filename)
obj = bucket.objects[key]
obj.write(:file => filename, :acl => :public_read)
puts obj.public_url
}
puts colorize("Upload finished", :green)
end
# rvm install 1.9.3
# gem install aws-sdk
task :upload => [] do
filename = ""
budget = ""
upload_to_s3([filename], bucket)
end
def colorize(text, color)
color_codes = {
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37
}
code = color_codes[color]
if code == nil
text
else
"\033[#{code}m#{text}\033[0m"
end
end
def clone(repo)
puts colorize("Cloning #{repo}", :yellow)
sh "git clone #{repo}"
end
def backup(repo, backup_repo, directory=nil)
if not directory
re = /[^\/]+[.]git$/
directory = repo.scan(re)[0].gsub(".git", "")
end
puts colorize("Backing up #{repo} to #{directory}", :blue)
if not File.directory?(directory) then
clone(repo)
puts colorize("Adding backup remote branch", :blue)
if backup_repo != "" then
sh "cd #{directory} && git remote add backup #{backup_repo} && cd -"
end
end
sh "cd #{directory} && git pull && cd -"
if backup_repo != "" then
begin
sh "cd #{directory} && git push backup master && cd -"
rescue
end
end
puts colorize("Done", :green)
end
task :backup_all => [] do
sh "clear"
backup("", "", "")
end
task :default => [:backup_all]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment