Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xcode post archive Rakefile to prepare files for upload
require 'json'
class Ipa
attr_reader :info
def initialize(path)
raise "File does not exist at path #{path}" unless File.exists? path
@info = JSON.parse(`unzip -cq #{path} '*/Info.plist' | plutil -convert json -o - -`)
end
def short_version
info["CFBundleShortVersionString"]
end
def version
info["CFBundleVersion"]
end
end
# Rakefile needs ruby > 1.8, so add brew location to path
PATH=/usr/local/bin:$PATH
# setup logging
LOG="$PROJECT_DIR/post_archive_action.log"
echo '' > $LOG
# run post_archive rake task
cd "$PROJECT_DIR"
rake post_archive >> $LOG 2>&1
require 'fileutils'
require_relative 'tools/ipa'
require_relative 'tools/nexus_uploader'
def working_directory
File.join(File.dirname(__FILE__), 'nexus_payload')
end
def environment_variable(name)
raise "Environment variable #{name} not set" if ENV[name].nil?
ENV[name].dup
end
def short_git_revision
`git rev-parse --short HEAD`.strip
end
def number_of_branch_commits
`git rev-list remotes/origin/develop..HEAD --count`.strip
end
def number_of_commits
`git rev-list HEAD --count`.strip
end
desc 'Removes any files from the working directory'
task :clean do
path = File.join working_directory, '*'
puts "Cleaning #{path}..."
FileUtils.rm_rf Dir.glob(path), secure: true
end
desc "Copies the xcarchive created by the archive action to the working directory.
Requires the following environment variables to be set:
ARCHIVE_PATH"
task :copy do
puts "Copying to #{working_directory}..."
xcarchive_path = environment_variable('ARCHIVE_PATH')
puts "...#{File.basename(xcarchive_path)}"
FileUtils.cp_r xcarchive_path, working_directory
end
desc "Creates a signed IPA in the working directory from an xcarchive.
Requires the following environment variables to be set:
ARCHIVE_PRODUCTS_PATH
PRODUCT_NAME
CODE_SIGN_IDENTITY
PROVISIONING_PROFILE"
task :ipa do
puts "Creating signed IPA file..."
app_path = File.join environment_variable('ARCHIVE_PRODUCTS_PATH'), 'Applications', "#{environment_variable('PRODUCT_NAME')}.app"
ipa_path = File.join working_directory, "#{environment_variable('PRODUCT_NAME')}.ipa"
puts `xcrun -sdk iphoneos PackageApplication -v "#{app_path}" -o "#{ipa_path}" --sign "#{environment_variable('CODE_SIGN_IDENTITY')}" --embed "#{environment_variable('PROVISIONING_PROFILE')}"`
end
desc 'ZIPs all folders in the working directory'
task :zip_folders do
puts "Zipping folders..."
Dir.chdir working_directory do |path|
Dir.glob '*' do |entry_name|
next if entry_name == '.' or entry_name == '..'
next unless File.directory? entry_name
puts "...#{entry_name} -> #{entry_name}.zip"
puts `zip -r "#{entry_name}.zip" "#{entry_name}"`
end
end
end
desc "Renames any files in the working directory according to the defined filename pattern.
Supports the following variables in the pattern:
@Version@ (CFBundleVersion)
@ShortVersion@ (CFBundleShortVersionString)
@ShortGitRevision@ (short SHA1)
@Commits@ (number of commits)
@BranchCommits@ (number of commits on current branch after branching from develop)
Requires exactly one IPA in the working directory.
Requires the following environment variables to be set:
NEXUS_FILENAME_PATTERN"
task :rename do
puts "Renaming files in #{working_directory} with pattern #{environment_variable('NEXUS_FILENAME_PATTERN')}..."
Dir.chdir working_directory do |path|
ipas = Dir.glob '*.ipa'
raise "Found more than one IPA file in #{working_directory}" if ipas.size > 1
raise "No IPA file found in #{working_directory}" if ipas.empty?
ipa = Ipa.new File.join path, ipas.first
Dir.glob '*' do |filename|
next if File.directory? filename
new_filename = environment_variable 'NEXUS_FILENAME_PATTERN'
new_filename.sub! "@Version@", ipa.version
new_filename.sub! "@ShortVersion@", ipa.short_version
new_filename.sub! "@ShortGitRevision@", short_git_revision
new_filename.sub! "@Commits@", number_of_commits
new_filename.sub! "@BranchCommits@", number_of_branch_commits
new_filename = new_filename + File.extname(filename)
if filename == new_filename
puts "...skipping #{filename} (same name)"
else
puts "...renaming #{filename} -> #{new_filename}"
raise "File with name #{new_filename} already exists" if File.exists? new_filename
File.rename filename, new_filename
end
end
end
end
desc "Upload all files in the working directory to the Nexus.
Requires the following environment variables to be set:
NEXUS_UPLOAD_TYPE [snapshot, release_candidate, production]
NEXUS_FAKE_UPLOAD (optional)"
task :upload do
fake_upload = ENV['NEXUS_FAKE_UPLOAD'].to_i != 0
if (fake_upload)
puts "Uploading files to Nexus - not really, only faking..."
else
puts "Uploading files to Nexus..."
end
uploader = NexusUploader.new environment_variable('NEXUS_UPLOAD_TYPE').to_sym, fake_upload
Dir.chdir working_directory do |path|
Dir.glob '*' do |filename|
next if File.directory? filename
puts "...#{filename}"
uploader.upload File.join(path, filename)
end
end
end
task :post_archive => [:clean, :copy, :zip_folders, :ipa, :rename, :upload]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.