launchy 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,71 +1,112 @@
1
- module Launchy::Detect
2
- # Detect the current host os family
3
- #
4
- # If the current host familiy cannot be detected then return
5
- # HostOsFamily::Unknown
6
- class HostOsFamily
7
- class NotFoundError < Launchy::Error; end
8
- extend ::Launchy::DescendantTracker
9
-
10
- class << self
11
-
12
- def detect( host_os = HostOs.new )
13
- found = find_child( :matches?, host_os )
14
- return found.new( host_os ) if found
15
- raise NotFoundError, "Unknown OS family for host os '#{host_os}'. #{Launchy.bug_report_message}"
1
+ # frozen_string_literal: true
2
+
3
+ module Launchy
4
+ module Detect
5
+ # Detect the current host os family
6
+ #
7
+ # If the current host familiy cannot be detected then return
8
+ # HostOsFamily::Unknown
9
+ class HostOsFamily
10
+ class NotFoundError < Launchy::Error; end
11
+ extend ::Launchy::DescendantTracker
12
+
13
+ class << self
14
+ def detect(host_os = HostOs.new)
15
+ found = find_child(:matches?, host_os)
16
+ return found.new(host_os) if found
17
+
18
+ raise NotFoundError, "Unknown OS family for host os '#{host_os}'. #{Launchy.bug_report_message}"
19
+ end
20
+
21
+ def matches?(host_os)
22
+ matching_regex.match(host_os.to_s)
23
+ end
24
+
25
+ def windows?
26
+ self == Windows
27
+ end
28
+
29
+ def darwin?
30
+ self == Darwin
31
+ end
32
+
33
+ def nix?
34
+ self == Nix
35
+ end
36
+
37
+ def cygwin?
38
+ self == Cygwin
39
+ end
16
40
  end
17
41
 
18
- def matches?( host_os )
19
- matching_regex.match( host_os.to_s )
42
+ attr_reader :host_os
43
+
44
+ def initialize(host_os = HostOs.new)
45
+ @host_os = host_os
20
46
  end
21
47
 
22
- def windows?() self == Windows; end
23
- def darwin?() self == Darwin; end
24
- def nix?() self == Nix; end
25
- def cygwin?() self == Cygwin; end
26
- end
48
+ def windows?
49
+ self.class.windows?
50
+ end
27
51
 
52
+ def darwin?
53
+ self.class.darwin?
54
+ end
28
55
 
29
- attr_reader :host_os
30
- def initialize( host_os = HostOs.new )
31
- @host_os = host_os
32
- end
56
+ def nix?
57
+ self.class.nix?
58
+ end
33
59
 
34
- def windows?() self.class.windows?; end
35
- def darwin?() self.class.darwin?; end
36
- def nix?() self.class.nix?; end
37
- def cygwin?() self.class.cygwin?; end
60
+ def cygwin?
61
+ self.class.cygwin?
62
+ end
38
63
 
39
- #---------------------------
40
- # All known host os families
41
- #---------------------------
42
- #
43
- class Windows < HostOsFamily
44
- def self.matching_regex
45
- /(mingw|mswin|msys|windows)/i
64
+ #---------------------------
65
+ # All known host os families
66
+ #---------------------------
67
+ #
68
+ class Windows < HostOsFamily
69
+ def self.matching_regex
70
+ /(mingw|mswin|msys|windows)/i
71
+ end
72
+
73
+ def app_list(app)
74
+ app.windows_app_list
75
+ end
46
76
  end
47
- def app_list( app ) app.windows_app_list; end
48
- end
49
77
 
50
- class Darwin < HostOsFamily
51
- def self.matching_regex
52
- /(darwin|mac os)/i
78
+ # Mac OS X family
79
+ class Darwin < HostOsFamily
80
+ def self.matching_regex
81
+ /(darwin|mac os)/i
82
+ end
83
+
84
+ def app_list(app)
85
+ app.darwin_app_list
86
+ end
53
87
  end
54
- def app_list( app ) app.darwin_app_list; end
55
- end
56
88
 
57
- class Nix < HostOsFamily
58
- def self.matching_regex
59
- /(linux|bsd|aix|solaris|sunos|dragonfly)/i
89
+ # All the *nix family of operating systems, and BSDs
90
+ class Nix < HostOsFamily
91
+ def self.matching_regex
92
+ /(linux|bsd|aix|solaris|sunos|dragonfly)/i
93
+ end
94
+
95
+ def app_list(app)
96
+ app.nix_app_list
97
+ end
60
98
  end
61
- def app_list( app ) app.nix_app_list; end
62
- end
63
99
 
64
- class Cygwin < HostOsFamily
65
- def self.matching_regex
66
- /cygwin/i
100
+ # Cygwin - if anyone is still using that
101
+ class Cygwin < HostOsFamily
102
+ def self.matching_regex
103
+ /cygwin/i
104
+ end
105
+
106
+ def app_list(app)
107
+ app.cygwin_app_list
108
+ end
67
109
  end
68
- def app_list( app ) app.cygwin_app_list; end
69
110
  end
70
111
  end
71
112
  end
@@ -1,93 +1,98 @@
1
- module Launchy::Detect
2
- #
3
- # Detect the current desktop environment for *nix machines
4
- # Currently this is Linux centric. The detection is based upon the detection
5
- # used by xdg-open from http://portland.freedesktop.org/
6
- class NixDesktopEnvironment
7
- class NotFoundError < Launchy::Error; end
1
+ # frozen_string_literal: true
8
2
 
9
- extend ::Launchy::DescendantTracker
10
-
11
- # Detect the current *nix desktop environment
3
+ module Launchy
4
+ module Detect
12
5
  #
13
- # If the current dekstop environment be detected, the return
14
- # NixDekstopEnvironment::Unknown
15
- def self.detect
16
- found = find_child( :is_current_desktop_environment? )
17
- Launchy.log("Current Desktop environment not found. #{Launchy.bug_report_message}") unless found
18
- return found
19
- end
6
+ # Detect the current desktop environment for *nix machines
7
+ # Currently this is Linux centric. The detection is based upon the detection
8
+ # used by xdg-open from http://portland.freedesktop.org/
9
+ class NixDesktopEnvironment
10
+ class NotFoundError < Launchy::Error; end
11
+
12
+ extend ::Launchy::DescendantTracker
13
+
14
+ # Detect the current *nix desktop environment
15
+ #
16
+ # If the current dekstop environment be detected, the return
17
+ # NixDekstopEnvironment::Unknown
18
+ def self.detect
19
+ found = find_child(:is_current_desktop_environment?)
20
+ Launchy.log("Current Desktop environment not found. #{Launchy.bug_report_message}") unless found
21
+ found
22
+ end
20
23
 
21
- def self.fallback_browsers
22
- %w[ firefox iceweasel seamonkey opera mozilla netscape galeon links lynx ].map { |x| ::Launchy::Argv.new( x ) }
23
- end
24
+ def self.fallback_browsers
25
+ %w[firefox iceweasel seamonkey opera mozilla netscape galeon links lynx].map { |x| ::Launchy::Argv.new(x) }
26
+ end
24
27
 
25
- def self.browsers
26
- [ browser, fallback_browsers ].flatten
27
- end
28
+ def self.browsers
29
+ [browser, fallback_browsers].flatten
30
+ end
28
31
 
29
- #---------------------------------------
30
- # The list of known desktop environments
31
- #---------------------------------------
32
+ #---------------------------------------
33
+ # The list of known desktop environments
34
+ #---------------------------------------
32
35
 
33
- class Kde < NixDesktopEnvironment
34
- def self.is_current_desktop_environment?
35
- ENV['KDE_FULL_SESSION'] &&
36
- Launchy::Application.find_executable( 'kde-open' )
37
- end
36
+ # KDE desktop environment
37
+ class Kde < NixDesktopEnvironment
38
+ def self.is_current_desktop_environment?
39
+ ENV.fetch("KDE_FULL_SESSION", nil) &&
40
+ Launchy::Application.find_executable("kde-open")
41
+ end
38
42
 
39
- def self.browser
40
- ::Launchy::Argv.new( 'kde-open' )
43
+ def self.browser
44
+ ::Launchy::Argv.new("kde-open")
45
+ end
41
46
  end
42
- end
43
47
 
44
- class Gnome < NixDesktopEnvironment
45
- def self.is_current_desktop_environment?
46
- ENV['GNOME_DESKTOP_SESSION_ID'] &&
47
- Launchy::Application.find_executable( 'gnome-open' )
48
- end
48
+ # Gnome desktop environment
49
+ class Gnome < NixDesktopEnvironment
50
+ def self.is_current_desktop_environment?
51
+ ENV.fetch("GNOME_DESKTOP_SESSION_ID", nil) &&
52
+ Launchy::Application.find_executable("gnome-open")
53
+ end
49
54
 
50
- def self.browser
51
- ::Launchy::Argv.new( 'gnome-open' )
55
+ def self.browser
56
+ ::Launchy::Argv.new("gnome-open")
57
+ end
52
58
  end
53
- end
54
59
 
55
- class Xfce < NixDesktopEnvironment
56
- def self.is_current_desktop_environment?
57
- if Launchy::Application.find_executable( 'xprop' ) then
58
- %x[ xprop -root _DT_SAVE_MODE].include?("xfce")
59
- else
60
- false
60
+ # Xfce desktop environment
61
+ class Xfce < NixDesktopEnvironment
62
+ def self.is_current_desktop_environment?
63
+ if Launchy::Application.find_executable("xprop")
64
+ `xprop -root _DT_SAVE_MODE`.include?("xfce")
65
+ else
66
+ false
67
+ end
61
68
  end
62
- end
63
69
 
64
- def self.browser
65
- ::Launchy::Argv.new( %w[ exo-open --launch WebBrowser ] )
70
+ def self.browser
71
+ ::Launchy::Argv.new(%w[exo-open --launch WebBrowser])
72
+ end
66
73
  end
67
- end
68
74
 
69
- # Fall back environment as the last case
70
- class Xdg < NixDesktopEnvironment
71
- def self.is_current_desktop_environment?
72
- Launchy::Application.find_executable( browser )
73
- end
75
+ # Fall back environment as the last case
76
+ class Xdg < NixDesktopEnvironment
77
+ def self.is_current_desktop_environment?
78
+ Launchy::Application.find_executable(browser)
79
+ end
74
80
 
75
- def self.browser
76
- ::Launchy::Argv.new( 'xdg-open' )
81
+ def self.browser
82
+ ::Launchy::Argv.new("xdg-open")
83
+ end
77
84
  end
78
- end
79
85
 
80
- # The one that is found when all else fails. And this must be declared last
81
- class NotFound < NixDesktopEnvironment
82
- def self.is_current_desktop_environment?
83
- true
84
- end
86
+ # The one that is found when all else fails. And this must be declared last
87
+ class NotFound < NixDesktopEnvironment
88
+ def self.is_current_desktop_environment?
89
+ true
90
+ end
85
91
 
86
- def self.browser
87
- ::Launchy::Argv.new
92
+ def self.browser
93
+ ::Launchy::Argv.new
94
+ end
88
95
  end
89
96
  end
90
-
91
97
  end
92
98
  end
93
-
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Launchy
4
+ # Internal: Namespace for detecting the environment that Launchy is running in
5
+ #
2
6
  module Detect
3
7
  end
4
8
  end
5
9
 
6
- require 'launchy/detect/host_os'
7
- require 'launchy/detect/host_os_family'
8
- require 'launchy/detect/nix_desktop_environment'
10
+ require "launchy/detect/host_os"
11
+ require "launchy/detect/host_os_family"
12
+ require "launchy/detect/nix_desktop_environment"
data/lib/launchy/error.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Launchy
2
4
  class Error < ::StandardError; end
3
5
  class ApplicationNotFoundError < Error; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Launchy
2
4
  #
3
5
  # Model all the Operating system families that can exist.
@@ -1,19 +1,27 @@
1
- require 'childprocess'
1
+ # frozen_string_literal: true
2
+
3
+ require "childprocess"
2
4
  module Launchy
5
+ # Internal: Run a command in a child process
6
+ #
3
7
  class Runner
4
- def run( cmd, *args )
5
- raise Launchy::CommandNotFoundError, "No command found to run with args '#{args.join(' ')}'. If this is unexpected, #{Launchy.bug_report_message}" unless cmd
6
- if Launchy.dry_run? then
7
- $stdout.puts dry_run( cmd, *args )
8
+ def run(cmd, *args)
9
+ unless cmd
10
+ raise Launchy::CommandNotFoundError,
11
+ "No command found to run with args '#{args.join(' ')}'. If this is unexpected, #{Launchy.bug_report_message}"
12
+ end
13
+
14
+ if Launchy.dry_run?
15
+ $stdout.puts dry_run(cmd, *args)
8
16
  else
9
- wet_run( cmd, *args )
17
+ wet_run(cmd, *args)
10
18
  end
11
19
  end
12
20
 
13
- def wet_run( cmd, *args )
14
- argv = [ cmd, *args ].flatten
21
+ def wet_run(cmd, *args)
22
+ argv = [cmd, *args].flatten
15
23
  Launchy.log "ChildProcess: argv => #{argv.inspect}"
16
- process = ChildProcess.build( *argv )
24
+ process = ChildProcess.build(*argv)
17
25
 
18
26
  process.io.inherit!
19
27
  process.leader = true
@@ -21,7 +29,7 @@ module Launchy
21
29
  process.start
22
30
  end
23
31
 
24
- def dry_run( cmd, *args )
32
+ def dry_run(cmd, *args)
25
33
  shell_commands(cmd, args).join(" ")
26
34
  end
27
35
 
@@ -30,17 +38,17 @@ module Launchy
30
38
  # args are not escaped because the whole set is passed to system as *args
31
39
  # and in that case system shell escaping rules are not done.
32
40
  #
33
- def shell_commands( cmd, args )
34
- cmdline = [ cmd.to_s.shellsplit ]
35
- cmdline << args.flatten.collect{ |a| a.to_s }
36
- return commandline_normalize( cmdline )
41
+ def shell_commands(cmd, args)
42
+ cmdline = [cmd.to_s.shellsplit]
43
+ cmdline << args.flatten.collect(&:to_s)
44
+ commandline_normalize(cmdline)
37
45
  end
38
46
 
39
- def commandline_normalize( cmdline )
47
+ def commandline_normalize(cmdline)
40
48
  c = cmdline.flatten!
41
- c = c.find_all { |a| (not a.nil?) and ( a.size > 0 ) }
49
+ c = c.find_all { |a| !a.nil? and a.size.positive? }
42
50
  Launchy.log "commandline_normalized => #{c.join(' ')}"
43
- return c
51
+ c
44
52
  end
45
53
  end
46
54
  end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Launchy
2
- VERSION = "3.0.0"
4
+ VERSION = "3.0.1"
3
5
 
6
+ # Internal: Version number of Launchy
4
7
  module Version
5
-
6
- MAJOR = Integer(VERSION.split('.')[0])
7
- MINOR = Integer(VERSION.split('.')[1])
8
- PATCH = Integer(VERSION.split('.')[2])
8
+ MAJOR = Integer(VERSION.split(".")[0])
9
+ MINOR = Integer(VERSION.split(".")[1])
10
+ PATCH = Integer(VERSION.split(".")[2])
9
11
 
10
12
  def self.to_a
11
13
  [MAJOR, MINOR, PATCH]
data/lib/launchy.rb CHANGED
@@ -1,6 +1,9 @@
1
- require 'addressable/uri'
2
- require 'shellwords'
3
- require 'stringio'
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require "addressable/uri"
5
+ require "shellwords"
6
+ require "stringio"
4
7
 
5
8
  #
6
9
  # The entry point into Launchy. This is the sole supported public API.
@@ -19,59 +22,62 @@ require 'stringio'
19
22
  # application class
20
23
  #
21
24
  module Launchy
22
-
23
25
  class << self
24
26
  #
25
27
  # Launch an application for the given uri string
26
28
  #
27
- def open(uri_s, options = {}, &error_block )
28
- leftover = extract_global_options( options )
29
- uri = string_to_uri( uri_s )
30
- if name = options[:application] then
31
- app = app_for_name( name )
29
+ def open(uri_s, options = {})
30
+ leftover = extract_global_options(options)
31
+ uri = string_to_uri(uri_s)
32
+ if (name = options[:application])
33
+ app = app_for_name(name)
32
34
  end
33
35
 
34
- if app.nil? then
35
- app = app_for_uri( uri )
36
- end
36
+ app = app_for_uri(uri) if app.nil?
37
37
 
38
- app.new.open( uri, leftover )
39
- rescue Launchy::Error => le
40
- raise le
41
- rescue Exception => e
38
+ app.new.open(uri, leftover)
39
+ rescue Launchy::Error => e
40
+ raise e
41
+ rescue StandardError => e
42
42
  msg = "Failure in opening uri #{uri_s.inspect} with options #{options.inspect}: #{e}"
43
43
  raise Launchy::Error, msg
44
44
  ensure
45
- if $! && block_given? then
46
- yield $!
47
- return # explicitly swallow the errors
45
+ if $ERROR_INFO && block_given?
46
+ yield $ERROR_INFO
47
+
48
+ # explicitly return here to swallow the errors if there was an error
49
+ # and we yielded to the block
50
+ # rubocop:disable Lint/EnsureReturn
51
+ return
52
+ # rubocop:enable Lint/EnsureReturn
48
53
  end
49
54
  end
50
55
 
51
- def app_for_uri( uri )
52
- Launchy::Application.handling( uri )
56
+ def app_for_uri(uri)
57
+ Launchy::Application.handling(uri)
53
58
  end
54
59
 
55
- def app_for_name( name )
56
- Launchy::Application.for_name( name )
60
+ def app_for_name(name)
61
+ Launchy::Application.for_name(name)
57
62
  rescue Launchy::ApplicationNotFoundError
58
63
  nil
59
64
  end
60
65
 
61
- def app_for_uri_string( s )
62
- app_for_uri( string_to_uri( s ) )
66
+ def app_for_uri_string(str)
67
+ app_for_uri(string_to_uri(str))
63
68
  end
64
69
 
65
- def string_to_uri( s )
66
- s = s.to_s
67
- uri = Addressable::URI.parse( s )
68
- Launchy.log "URI parsing pass 1 : #{s} -> #{uri.to_hash}"
69
- if not uri.scheme then
70
- uri = Addressable::URI.heuristic_parse( s )
71
- Launchy.log "URI parsing pass 2 : #{s} -> #{uri.to_hash}"
70
+ def string_to_uri(str)
71
+ str = str.to_s
72
+ uri = Addressable::URI.parse(str)
73
+ Launchy.log "URI parsing pass 1 : #{str} -> #{uri.to_hash}"
74
+ unless uri.scheme
75
+ uri = Addressable::URI.heuristic_parse(str)
76
+ Launchy.log "URI parsing pass 2 : #{str} -> #{uri.to_hash}"
72
77
  end
73
- raise Launchy::ArgumentError, "Invalid URI given: #{s.inspect}" unless uri
74
- return uri
78
+ raise Launchy::ArgumentError, "Invalid URI given: #{str.inspect}" unless uri
79
+
80
+ uri
75
81
  end
76
82
 
77
83
  def reset_global_options
@@ -79,49 +85,49 @@ module Launchy
79
85
  Launchy.application = nil
80
86
  Launchy.host_os = nil
81
87
  Launchy.dry_run = false
82
- Launchy.path = ENV['PATH']
88
+ Launchy.path = ENV.fetch("PATH", nil)
83
89
  end
84
90
 
85
- def extract_global_options( options )
91
+ def extract_global_options(options)
86
92
  leftover = options.dup
87
- Launchy.debug = leftover.delete( :debug ) || ENV['LAUNCHY_DEBUG']
88
- Launchy.application = leftover.delete( :application ) || ENV['LAUNCHY_APPLICATION']
89
- Launchy.host_os = leftover.delete( :host_os ) || ENV['LAUNCHY_HOST_OS']
90
- Launchy.dry_run = leftover.delete( :dry_run ) || ENV['LAUNCHY_DRY_RUN']
93
+ Launchy.debug = leftover.delete(:debug) || ENV.fetch("LAUNCHY_DEBUG", nil)
94
+ Launchy.application = leftover.delete(:application) || ENV.fetch("LAUNCHY_APPLICATION", nil)
95
+ Launchy.host_os = leftover.delete(:host_os) || ENV.fetch("LAUNCHY_HOST_OS", nil)
96
+ Launchy.dry_run = leftover.delete(:dry_run) || ENV.fetch("LAUNCHY_DRY_RUN", nil)
91
97
  end
92
98
 
93
- def debug=( d )
94
- @debug = to_bool( d )
99
+ def debug=(enabled)
100
+ @debug = to_bool(enabled)
95
101
  end
96
102
 
97
103
  # we may do logging before a call to 'open', hence the need to check
98
104
  # LAUNCHY_DEBUG here
99
105
  def debug?
100
- @debug || to_bool( ENV['LAUNCHY_DEBUG'] )
106
+ @debug || to_bool(ENV.fetch("LAUNCHY_DEBUG", nil))
101
107
  end
102
108
 
103
- def application=( app )
109
+ def application=(app)
104
110
  @application = app
105
111
  end
106
112
 
107
113
  def application
108
- @application || ENV['LAUNCHY_APPLICATION']
114
+ @application || ENV.fetch("LAUNCHY_APPLICATION", nil)
109
115
  end
110
116
 
111
- def host_os=( host_os )
117
+ def host_os=(host_os)
112
118
  @host_os = host_os
113
119
  end
114
120
 
115
121
  def host_os
116
- @host_os || ENV['LAUNCHY_HOST_OS']
122
+ @host_os || ENV.fetch("LAUNCHY_HOST_OS", nil)
117
123
  end
118
124
 
119
- def dry_run=( dry_run )
120
- @dry_run = to_bool( dry_run )
125
+ def dry_run=(dry_run)
126
+ @dry_run = to_bool(dry_run)
121
127
  end
122
128
 
123
129
  def dry_run?
124
- @dry_run || to_bool( ENV['LAUNCHY_DRY_RUN'] )
130
+ @dry_run || to_bool(ENV.fetch("LAUNCHY_DRY_RUN", nil))
125
131
  end
126
132
 
127
133
  def bug_report_message
@@ -140,15 +146,13 @@ module Launchy
140
146
  @path = path
141
147
  end
142
148
 
143
- private
144
- def to_bool( arg )
149
+ private
150
+
151
+ def to_bool(arg)
145
152
  if arg.is_a? String
146
- arg == 'true'
147
- elsif arg.is_a? TrueClass
148
- true
153
+ arg == "true"
149
154
  else
150
- # All other values mapped to false.
151
- false
155
+ arg.is_a? TrueClass
152
156
  end
153
157
  end
154
158
  end
@@ -157,11 +161,11 @@ module Launchy
157
161
  Launchy.reset_global_options
158
162
  end
159
163
 
160
- require 'launchy/version'
161
- require 'launchy/argv'
162
- require 'launchy/cli'
163
- require 'launchy/descendant_tracker'
164
- require 'launchy/error'
165
- require 'launchy/application'
166
- require 'launchy/detect'
167
- require 'launchy/runner'
164
+ require "launchy/version"
165
+ require "launchy/argv"
166
+ require "launchy/cli"
167
+ require "launchy/descendant_tracker"
168
+ require "launchy/error"
169
+ require "launchy/application"
170
+ require "launchy/detect"
171
+ require "launchy/runner"