#!/usr/bin/env ruby $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') require 'xcselect' require "fileutils" require "optparse" include Xcselect class Main attr_reader :exit_code def initialize @options = default_options @exit_code = 0 optparse = OptionParser.new do |opts| opts.on('-h', '--help', "Display this !!") { puts opts; exit } opts.on('-v', '--version', "Print version info") { puts "xcselect-" + VERSION; exit; } opts.on('-p', '--print', "print the path instead of opening it") { @options[:print] = true } opts.on('-c', '--cd', "cd into the directory (spawns new shell, type `exit` to return) ") { @options[:print] = false; @options[:cd] = true } opts.on('-a', '--apps' , "Show list of apps") { show_apps(); exit } opts.on('-d', '--latest-documents' , "Reveal last built app's documents folder") { show_latest_docs() ;exit} opts.on('-n', '--latest-newsstand' , "Show list of newsstand issues for last built app") { show_latest_newsstand(); exit } end begin optparse.parse! rescue OptionParser::InvalidOption => e puts "Invalid Option" puts optparse exit 2 end end def default_options {:print => false} end def open_path p if @options[:cd] # this will allow you to go into the folder # then type exit to return to your parent shell Dir.chdir p exec ENV['SHELL'] elsif @options[:print] puts p else `open -R "#{p}"` end end def select_menu title, selections i = 0 puts title puts "" selections.each do |opt| i += 1 puts " [#{i}] #{opt}" end print "\nSelection: " begin return unless STDIN.tty? input = STDIN.gets input = input.to_i if input.zero? or input > selections.size puts "Invalid Selection" @exit_code = 1 else yield input - 1 end rescue SystemExit, Interrupt puts "" end return -1 end # ============ # = Commands = # ============ def show_latest_newsstand latest_app = XcApp.last_built_newsstand_app ns_issues = latest_app.newsstand_objects issue_paths = latest_app.newsstand_issue_paths issue_names = ns_issues.keys if issue_names.size == 1 puts "opening only issue #{issue_names.last}" unless @options[:print] open_path ns_issues.last[issue_names.last] return end select_menu("Select issue in #{latest_app} to open:", issue_names) {|i| open_path ns_issues.values[i] } end def show_latest_docs app = XcApp.last_built_app open_path app.documents_path end def show_apps apps = XcApp.all_apps.sort return @exit_code = 1 if apps.size == 0 if apps.size == 1 puts "opening only app #{apps}" unless @options[:print] open_path issue_paths.last end select_menu("Select an app to open:", apps.map(&:to_s)) do |selected_index| open_path(apps[selected_index].base_dir) end end end main = Main.new exit main.exit_code