#!/usr/bin/env ruby require 'thor' require 'xcodeproj' require 'erb' class FWT < Thor include Thor::Actions def self.source_root File.join(File.dirname(__FILE__), '..') end desc "new APP_PATH PREFIX", "Creates a new Future Workshops iOS project" def new(app_path, prefix) setup_cocoapods @app_name = File.basename(app_path) @prefix = prefix.upcase @path = app_path supporting_files = 'Supporting Files' @project = Xcodeproj::Project.new @project.root_object.build_configuration_list info_plist = "#{@app_name}-Info.plist" process_template("Info.plist.erb", info_plist, supporting_files) target = @project.new_target(:application, @app_name, :ios) add_system_framework('UIKit') add_system_framework('CoreGraphics') add_build_settings_to_target({ 'INFOPLIST_FILE' => "$(SRCROOT)/#{@app_name}/#{@app_name}-Info.plist", 'GCC_PREFIX_HEADER' => "#{@app_name}/#{@app_name}-Prefix.pch", 'SKIP_INSTALL' => 'NO', 'IPHONEOS_DEPLOYMENT_TARGET' => '6.0', }, target) app_d_h = process_template("AppDelegate.h.erb", "#{@prefix}AppDelegate.h") app_d_m = process_template("AppDelegate.m.erb", "#{@prefix}AppDelegate.m") main_m = process_template("main.m.erb", "main.m", supporting_files) prefix_pch = process_template("Prefix.pch.erb", "#{@app_name}-Prefix.pch", supporting_files) target.add_file_references([app_d_m, main_m]) # final save @project.save_as File.join(@path, "#{@app_name}.xcodeproj") @workspace = Xcodeproj::Workspace.new("#{@app_name}.xcodeproj") @workspace.save_as File.join(@path, "#{@app_name}.xcworkspace") # Ruby template 'templates/fwt/rvmrc.erb', File.join(@path, '.rvmrc') template 'templates/fwt/Gemfile.erb', File.join(@path, 'Gemfile') template 'templates/fwt/Rakefile.erb', File.join(@path, 'Rakefile') template 'templates/fwt/Podfile.erb', File.join(@path, 'Podfile') template 'templates/fwt/gitignore.erb', File.join(@path, '.gitignore') sleep 2 # flushing issues? inside(@path) do run "rvm gemset create #{@app_name}", {:capture => true} run "rvm gemset use #{@app_name}", {:capture => true} run 'bundle', {:capture => true} #run 'rake pod:clean_install', {:capture => true} run "rake frank:setup['#{@app_name}','#{@prefix}']", {:capture => true} end end private def add_build_settings_to_target(map, target) target.build_settings('Debug').merge! map target.build_settings('Release').merge! map end def add_system_framework(name) framework = @project.add_system_framework(name) target = @project.targets.first build_phase = target.frameworks_build_phase build_phase.add_file_reference framework end def setup_cocoapods unless Dir.exists?(File.join(Dir.home, '.cocoapods', 'fw')) run 'gem install cocoapods', {:capture => true} run 'pod repo add fw git@github.com:FutureWorkshops/FWTPodspecs.git', {:capture => true} end end def process_template(template_filename, destination_filename, destination_group = "", template_project = 'default_project') template "templates/fwt/#{template_project}/#{template_filename}", File.join(@path, @app_name, destination_filename) @project.new_file(File.join(@app_name, destination_filename), File.join(@app_name, destination_group)) end end module Xcodeproj class Project def add_system_framework(name) path = "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/#{name}.framework" if file = frameworks_group.files.select { |f| f.path == path }.first file else framework_ref = frameworks_group.new_file(path) framework_ref.name = "#{name}.framework" framework_ref.source_tree = 'SDKROOT' framework_ref end end end end FWT.start