Module: Launchy

Defined in:
lib/launchy.rb,
lib/launchy/cli.rb,
lib/launchy/argv.rb,
lib/launchy/error.rb,
lib/launchy/detect.rb,
lib/launchy/runner.rb,
lib/launchy/version.rb,
lib/launchy/os_family.rb,
lib/launchy/application.rb,
lib/launchy/descendant_tracker.rb

Overview

The entry point into Launchy. This is the sole supported public API.

Launchy.open( uri, options = {} )

The currently defined global options are:

:debug        Turn on debugging output
:application  Explicitly state what application class is going to be used.
              This must be a child class of Launchy::Application
:host_os      Explicitly state what host operating system to pretend to be
:dry_run      Do nothing and print the command that would be executed on $stdout

Other options may be used, and those will be passed directly to the application class

Defined Under Namespace

Modules: DescendantTracker, Detect, Version Classes: Application, ApplicationNotFoundError, ArgumentError, Argv, Cli, CommandNotFoundError, Error, OSFamily, Runner

Constant Summary collapse

VERSION =
"3.0.0"

Class Method Summary collapse

Class Method Details

.app_for_name(name) ⇒ Object



55
56
57
58
59
# File 'lib/launchy.rb', line 55

def app_for_name( name )
  Launchy::Application.for_name( name )
rescue Launchy::ApplicationNotFoundError
  nil
end

.app_for_uri(uri) ⇒ Object



51
52
53
# File 'lib/launchy.rb', line 51

def app_for_uri( uri )
  Launchy::Application.handling( uri )
end

.app_for_uri_string(s) ⇒ Object



61
62
63
# File 'lib/launchy.rb', line 61

def app_for_uri_string( s )
  app_for_uri( string_to_uri( s ) )
end

.applicationObject



107
108
109
# File 'lib/launchy.rb', line 107

def application
  @application || ENV['LAUNCHY_APPLICATION']
end

.application=(app) ⇒ Object



103
104
105
# File 'lib/launchy.rb', line 103

def application=( app )
  @application = app
end

.bug_report_messageObject



127
128
129
# File 'lib/launchy.rb', line 127

def bug_report_message
  "Please rerun with environment variable LAUNCHY_DEBUG=true or the '-d' commandline option and file a bug at https://github.com/copiousfreetime/launchy/issues/new"
end

.debug=(d) ⇒ Object



93
94
95
# File 'lib/launchy.rb', line 93

def debug=( d )
  @debug = to_bool( d )
end

.debug?Boolean

we may do logging before a call to ‘open’, hence the need to check LAUNCHY_DEBUG here

Returns:

  • (Boolean)


99
100
101
# File 'lib/launchy.rb', line 99

def debug?
  @debug || to_bool( ENV['LAUNCHY_DEBUG'] )
end

.dry_run=(dry_run) ⇒ Object



119
120
121
# File 'lib/launchy.rb', line 119

def dry_run=( dry_run )
  @dry_run = to_bool( dry_run )
end

.dry_run?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/launchy.rb', line 123

def dry_run?
  @dry_run || to_bool( ENV['LAUNCHY_DRY_RUN'] )
end

.extract_global_options(options) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/launchy.rb', line 85

def extract_global_options( options )
  leftover = options.dup
  Launchy.debug        = leftover.delete( :debug       ) || ENV['LAUNCHY_DEBUG']
  Launchy.application  = leftover.delete( :application ) || ENV['LAUNCHY_APPLICATION']
  Launchy.host_os      = leftover.delete( :host_os     ) || ENV['LAUNCHY_HOST_OS']
  Launchy.dry_run      = leftover.delete( :dry_run     ) || ENV['LAUNCHY_DRY_RUN']
end

.host_osObject



115
116
117
# File 'lib/launchy.rb', line 115

def host_os
  @host_os || ENV['LAUNCHY_HOST_OS']
end

.host_os=(host_os) ⇒ Object



111
112
113
# File 'lib/launchy.rb', line 111

def host_os=( host_os )
  @host_os = host_os
end

.log(msg) ⇒ Object



131
132
133
# File 'lib/launchy.rb', line 131

def log(msg)
  $stderr.puts "LAUNCHY_DEBUG: #{msg}" if Launchy.debug?
end

.open(uri_s, options = {}, &error_block) ⇒ Object

Launch an application for the given uri string



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/launchy.rb', line 27

def open(uri_s, options = {}, &error_block )
  leftover = extract_global_options( options )
  uri = string_to_uri( uri_s )
  if name = options[:application] then
    app = app_for_name( name )
  end

  if app.nil? then
    app = app_for_uri( uri )
  end

  app.new.open( uri, leftover )
rescue Launchy::Error => le
  raise le
rescue Exception => e
  msg = "Failure in opening uri #{uri_s.inspect} with options #{options.inspect}: #{e}"
  raise Launchy::Error, msg
ensure
  if $! && block_given? then
    yield $!
    return # explicitly swallow the errors
  end
end

.pathObject



135
136
137
# File 'lib/launchy.rb', line 135

def path
  @path
end

.path=(path) ⇒ Object



139
140
141
# File 'lib/launchy.rb', line 139

def path=(path)
  @path = path
end

.reset_global_optionsObject



77
78
79
80
81
82
83
# File 'lib/launchy.rb', line 77

def reset_global_options
  Launchy.debug       = false
  Launchy.application = nil
  Launchy.host_os     = nil
  Launchy.dry_run     = false
  Launchy.path        = ENV['PATH']
end

.string_to_uri(s) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/launchy.rb', line 65

def string_to_uri( s )
  s = s.to_s
  uri = Addressable::URI.parse( s )
  Launchy.log "URI parsing pass 1 : #{s} -> #{uri.to_hash}"
  if not uri.scheme then
    uri = Addressable::URI.heuristic_parse( s )
    Launchy.log "URI parsing pass 2 : #{s} -> #{uri.to_hash}"
  end
  raise Launchy::ArgumentError, "Invalid URI given: #{s.inspect}" unless uri
  return uri
end