From 1be55c66f10c9c85b8ddb0ca3f728f18a0e56e79 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Fri, 7 Feb 2014 08:38:40 -0500 Subject: [PATCH] devscript: support -t opt in list_running_app_ids and refactor --- developer/bin/list_running_app_ids | 114 ++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 27 deletions(-) diff --git a/developer/bin/list_running_app_ids b/developer/bin/list_running_app_ids index 1b47979366..e6214aeb4c 100755 --- a/developer/bin/list_running_app_ids +++ b/developer/bin/list_running_app_ids @@ -3,45 +3,105 @@ # list_running_app_ids # -if ARGV.length > 0 - puts <<-EOS -list_running_app_ids +### +### dependencies +### -Print a list of currently running Applications and associated Bundle IDs, -which may be useful in a Cask uninstall stanza, eg +require 'open3' +require 'set' + +### +### globals +### + +$opt_test = nil + +### +### methods +### + +def usage + <<-EOS +list_running_app_ids [ -t ] + +Print a list of currently running Applications and associated +Bundle IDs, which may be useful in a Cask uninstall stanza, eg uninstall :quit => 'bundle.id.goes.here' Applications attributed to Apple are excluded from the output. +With optional "-t ", silently test if a given app +is running, exiting with an error code if not. + See CONTRIBUTING.md for more information. EOS - exit end -require 'open3' - -running = {} -out, err, status = Open3.capture3('/usr/bin/osascript', '-e', 'tell application "System Events" to get (name, bundle identifier, unix id) of every process') -if status.exitstatus > 0 - puts err - exit status.exitstatus +def process_args + while ! ARGV.empty? do + if ARGV.first =~ /^-+t(?:est)?$/ and ARGV.length > 1 + ARGV.shift + $opt_test = ARGV.shift + elsif ARGV.first =~ /^-+h(?:elp)?$/ + puts usage + exit 0 + else + puts usage + exit 1 + end + end end -out = out.split(', ') -one_third = out.length / 3 -app_names = out.shift(one_third) -bundle_ids = out.shift(one_third) -unix_ids = out.shift(one_third) - -app_names.zip(bundle_ids, unix_ids).each do |app_name, bundle_id, unix_id| - next if bundle_id =~ %r{^com\.apple\.} - next if app_name == 'osascript' # this script itself - bundle_id.gsub!(/^(missing value)$/, '<\1>') - running["#{bundle_id}\t#{app_name}"] = 1 +def load_apps + out, err, status = Open3.capture3('/usr/bin/osascript', '-e', 'tell application "System Events" to get (name, bundle identifier, unix id) of every process') + if status.exitstatus > 0 + puts err + exit status.exitstatus + end + out = out.split(', ') + one_third = out.length / 3 + @app_names = out.shift(one_third) + @bundle_ids = out.shift(one_third) + @unix_ids = out.shift(one_third) end -puts "bundle_id\tapp_name\n" -puts "--------------------------------------\n" -puts running.keys.sort +def test_app(bundle) + @bundle_ids.include?(bundle) ? 0 : 1 +end + +def excluded_bundle_id(bundle_id) + %r{^com\.apple\.}.match(bundle_id) +end + +def excluded_app_name(app_name) + %r{^osascript$}.match(app_name) # this script itself +end + +def report_apps + running = Set.new + @app_names.zip(@bundle_ids, @unix_ids).each do |app_name, bundle_id, unix_id| + next if excluded_bundle_id bundle_id + next if excluded_app_name app_name + bundle_id.gsub!(/^(missing value)$/, '<\1>') + running.add "#{bundle_id}\t#{app_name}" + end + + puts "bundle_id\tapp_name\n" + puts "--------------------------------------\n" + puts running.to_a.sort +end + +### +### main +### + +process_args +load_apps + +if $opt_test + exit test_app($opt_test) +else + report_apps +end