#!/usr/bin/env ruby $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') require 'xcselect' require "fileutils" require "optparse" require "xcselect/string_colors.rb" include Xcselect class XcSelectInvalidSelectionException < Exception; end class Main attr_reader :exit_code attr_accessor :command attr_reader :optparse def initialize @options = default_options @exit_code = 0 @optparse = OptionParser.new do |opts| opts.on('-p', '--print', "print the path instead of opening it") { @options[:print] = true } opts.on('-t', '--touch-app' , "touch app so it becomes latest") { @options[:touch_app] = true } opts.on('--cd', "cd into the directory (spawns new shell, type `exit` to return) ") { @options[:print] = false; @options[:cd] = true } # Commands opts.on('-h', '--help', "Display this !!") { self.command = :show_help } opts.on('-v', '--version', "Print version info") { self.command = :show_version } opts.on('-d', '--latest-documents' , "Reveal last built app's documents folder") { self.command = :show_latest_docs} opts.on('-n', '--latest-newsstand' , "Show list of newsstand issues for last built app") { self.command = :show_latest_newsstand } opts.on('-c', '--latest-cache' , "Reveal last built app's cache folder") { self.command = :show_latest_cache } opts.on('-a', '--apps' , "Show list of apps") { self.command = :show_apps ;} end begin optparse.parse! run rescue OptionParser::InvalidOption => e puts "Invalid Option" puts optparse exit 2 rescue XcSelectInvalidSelectionException => e puts "xcsim: #{e}" end end def command=(c) return @command = c if @command.nil? @exit_code = 4 raise XcSelectInvalidSelectionException, "Please use only one command" end def run begin send(command || :show_apps) rescue Exception => e puts e exit 4 end end def default_options {:print => false, :touch_app => 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 doesn't work a lot of the time so removing it `open "#{p}"` end end def select_menu title, selections puts "#{title}\n\n" selections.each_with_index do |opt,i| puts " [#{i+1}] #{opt}" end print "\nSelection: " begin return unless STDIN.tty? input = STDIN.gets input = input.to_i if input.zero? or input > selections.size @exit_code = 1 raise XcSelectInvalidSelectionException, "Invalid Selection" 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_names = ns_issues.keys if issue_names.size == 1 puts "opening only issue #{issue_names.last}" unless @options[:print] open_path ns_issues.values.last.content_path return end select_menu("Select issue in #{latest_app} to open:", issue_names) {|i| open_path ns_issues.values[i].content_path } end def show_latest_cache app = XcApp.last_built_app raise "No sim folder" if app.nil? open_path app.cache_path end def show_latest_docs app = XcApp.last_built_app raise "No sim folder" if app.nil? open_path app.documents_path end def touch_app app FileUtils.touch app.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 apps.last.base_dir return end last_app = XcApp.sort_by_touch_time(apps).last last_newsstand_app = apps.select(&:newsstand?).last same_last_app = last_app.equal? last_newsstand_app options = apps.map do |a| s = a.to_s if a.equal? last_app s += " " + (same_last_app ? "[latest]".green : "[latest]".blue) elsif a.equal? last_newsstand_app s += "[latest-ns]".yellow end s end select_menu("Select an app to open:", options) do |selected_index| selected_app = apps[selected_index] if @options[:touch_app] touch_app selected_app else open_path(selected_app.base_dir) end end end def show_help puts @optparse end def show_version puts "xcselect-" + VERSION end end main = Main.new exit main.exit_code