Skip to content

Instantly share code, notes, and snippets.

@keith
Created November 20, 2012 23:34
Show Gist options
  • Save keith/4122014 to your computer and use it in GitHub Desktop.
Save keith/4122014 to your computer and use it in GitHub Desktop.
A rakefile to build your xcodeproject and place the result in your applications folder.
#!/usr/bin/env rake
require 'rubygems'
require 'fileutils'
# This requires the 'colorize' gem. Install with `[sudo] gem install colorize`
require 'colorize'
task :default => [:build]
def choose_file(file_array)
# Print out each filename with a number beside it
file_array.each { |file| puts "#{ file_array.index(file) + 1}: #{ file }" }
# Have the user pick the appropriate file
print("Enter the number to open: ")
num = gets.to_i
# Make sure the pick is reasonable
if num > file_array.count || num < 1
puts "That doesn't seem reasonable"
elsif num <= file_array.count
num -= 1
puts "Chose: #{ file_array[num] }".green
return file_array[num]
end
end
desc "Build the current project and copy the product to /Applications"
task :build do
# The filename of the workspace or project file
project_name ||= ""
# An array just incase there are multiple files or projects
path_array = Array.new
# Check each file in the directory to find the files including 'xcworkspace'
Dir.foreach(".") { |file| path_array << file if file.include? "xcworkspace" }
# If there is only one workspace
if path_array.count == 1
# Put the name and assign it to the project_name
puts "Workspace: #{ path_array.first }".green
project_name = path_array.first
# If there is more than one
elsif path_array.count > 1
project_name = choose_file(path_array)
else # path_array.count != 1
puts "No xcworkspaces in #{ Dir.pwd }".red
puts "Looking for xcodeprojs..."
# Check each file in the cd this time for 'xcodeproj'
Dir.foreach(".") { |file| path_array << file if file.include? "xcodeproj" }
# If there is only one assign it to project_name and flip the workspace flag
if path_array.count == 1
puts "Project: #{ path_array.first }".green
project_name = path_array.first
# If there is more than one ask the user
elsif path_array.count > 1
project_name = choose_file(path_array)
else # path_array < 1
puts "No xcworkspace or xcodeproj files in #{ Dir.pwd }".red
exit
end # path_array count checking
end # original path_array count checking
# Should have the project filename at this point
# If the project is a workspace
if project_name.include? "xcworkspace"
# Send the terminal command to build the project with xcodebuild assume the
build_output = %x[xcodebuild -workspace "#{ project_name}" -scheme "#{ File.basename(project_name, ".*") }"]
# Put the output of the build into the array
build_output_array = build_output.split("\n")
directory_name = ""
# Load the containing path string
build_output_array.any? { |line| directory_name = line.strip if line.include? 'CODESIGNING_FOLDER_PATH' }
if directory_name.length > 0
# An ugly way to get the entire folder path of the product of the build
directory_name = File.dirname(directory_name.split('CODESIGNING_FOLDER_PATH').last.strip)
apps_array = Array.new
app_name = ""
Dir.chdir directory_name
Dir.foreach(".") { |file| apps_array << file if file.include? ".app" }
if apps_array.count == 1
app_name = apps_array.first
elsif apps_array.count <= 0
puts "No apps found in #{ Dir.pwd }".red
else
apps_array.each { |app| puts "#{ apps_array.index(app) + 1 }: #{ app }" }
print("Enter a number to open: ")
num = gets.to_i
if num > apps_array.count || num < 1
puts "That doesn't seem reasonable"
elsif num <= apps_array.count
num -= 1
puts "App: #{ apps_array[num] }"
app_name = apps_array[num]
end
end
%x[pkill "#{ File.basename(app_name, '.*') }"]
if File.directory? "/Applications/#{ app_name }"
FileUtils.rm_rf("/Applications/#{ app_name }")
if File.directory? "/Applications/#{ app_name }"
puts "Error deleting /Applications/#{ app_name } you may not have sufficient privledges. Quitting...".red
exit
end
end
FileUtils.mv app_name, "/Applications/#{ app_name }", :force => true
if File.directory? "/Applications/#{ app_name }"
puts "Successfully copied #{ app_name } to /Applications".green
else
puts "Failed to copy #{ app_name } to /Applications".red
end
else
puts "nope"
end
else
end
end # :build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment