launchy 0.2.1 → 0.3.0

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.
data/CHANGES CHANGED
@@ -1,5 +1,12 @@
1
1
  == Changes
2
2
 
3
+ === Version 0.3.0 - 2007-08-30
4
+
5
+ * reorganize the code structure, removing Spawnable namespace
6
+ * removed 'do_magic' method, changed it to 'open'
7
+ * added override environment variable LAUNCHY_HOST_OS for testing
8
+ * fix broken cygwin support [Bug #13472]
9
+
3
10
  === Version 0.2.1 - 2007-08-18
4
11
 
5
12
  * fix inability to find windows executables [Bug #13132]
data/README CHANGED
@@ -22,15 +22,15 @@ Currently only launching a browser is supported.
22
22
 
23
23
  From within your ruby code you can trust launchy to do the right thing:
24
24
 
25
- Launchy.do_magic("http://www.ruby-lang.org/")
25
+ Launchy.open("http://www.ruby-lang.org/")
26
26
 
27
27
  Or, if you want to launch the application yourself:
28
28
 
29
- Launchy::Spawnable::Browser.run("http://www.ruby-lang.org/")
29
+ Launchy::Browser.run("http://www.ruby-lang.org/")
30
30
 
31
31
  OR
32
32
 
33
- Launchy::Spawnable::Browser.new.visit("http://www.ruby-lang.org/")
33
+ Launchy::Browser.new.visit("http://www.ruby-lang.org/")
34
34
 
35
35
  == LICENSE
36
36
 
@@ -9,4 +9,4 @@ rescue LoadError
9
9
  retry
10
10
  end
11
11
 
12
- Launchy.do_magic(*ARGV)
12
+ Launchy.open(*ARGV)
@@ -21,8 +21,8 @@ module Launchy
21
21
  module_function :require_all_libs_relative_to
22
22
 
23
23
  class << self
24
- def do_magic(*params)
25
- klass = Launchy::Spawnable::Application.find_application_class_for(*params)
24
+ def open(*params)
25
+ klass = Launchy::Application.find_application_class_for(*params)
26
26
  if klass then
27
27
  klass.run(*params)
28
28
  else
@@ -0,0 +1,158 @@
1
+ require 'rbconfig'
2
+
3
+ module Launchy
4
+ class Application
5
+
6
+ KNOWN_OS_FAMILIES = [ :windows, :darwin, :nix, :cygwin ]
7
+
8
+ class << self
9
+ def inherited(sub_class)
10
+ application_classes << sub_class
11
+ end
12
+ def application_classes
13
+ @application_classes ||= []
14
+ end
15
+
16
+ def find_application_class_for(*args)
17
+ Launchy.log "finding application classes for [#{args.join(' ')}]"
18
+ application_classes.find do |klass|
19
+ if klass.handle?(*args) then
20
+ Launchy.log " #{klass.name}"
21
+ true
22
+ else
23
+ false
24
+ end
25
+ end
26
+ end
27
+
28
+ # find an executable in the available paths
29
+ # mkrf did such a good job on this I had to borrow it.
30
+ def find_executable(bin,*paths)
31
+ paths = ENV['PATH'].split(File::PATH_SEPARATOR) if paths.empty?
32
+ paths.each do |path|
33
+ file = File.join(path,bin)
34
+ if File.executable?(file) then
35
+ Launchy.log "found executable #{file}"
36
+ return file
37
+ end
38
+ end
39
+ Launchy.log "Unable to find `#{bin}' in paths #{paths.join(', ')}"
40
+ return nil
41
+ end
42
+
43
+ # return the current 'host_os' string from ruby's configuration
44
+ def my_os
45
+ if ENV['LAUNCHY_HOST_OS'] then
46
+ Launchy.log "Using LAUNCHY_HOST_OS override of '#{ENV['LAUNCHY_HOST_OS']}'"
47
+ return ENV['LAUNCHY_HOST_OS']
48
+ else
49
+ ::Config::CONFIG['host_os']
50
+ end
51
+ end
52
+
53
+ # detect what the current os is and return :windows, :darwin or :nix
54
+ def my_os_family(test_os = my_os)
55
+ case test_os
56
+ when /mswin/i
57
+ family = :windows
58
+ when /windows/i
59
+ family = :windows
60
+ when /darwin/i
61
+ family = :darwin
62
+ when /mac os/i
63
+ family = :darwin
64
+ when /solaris/i
65
+ family = :nix
66
+ when /bsd/i
67
+ family = :nix
68
+ when /linux/i
69
+ family = :nix
70
+ when /cygwin/i
71
+ family = :cygwin
72
+ else
73
+ $stderr.puts "Unknown OS familiy for '#{test_os}'. Please report this bug to #{Launchy::SPEC.email}"
74
+ family = :unknown
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ # Determine the appropriate desktop environment for *nix machine. Currently this is
81
+ # linux centric. The detection is based upon the detection used by xdg-open from
82
+ # http://portland.freedesktop.org/wiki/XdgUtils
83
+ def nix_desktop_environment
84
+ if not @nix_desktop_environment then
85
+ @nix_desktop_environment = :generic
86
+ if ENV["KDE_FULL_SESSION"] || ENV["KDE_SESSION_UID"] then
87
+ @nix_desktop_environment = :kde
88
+ elsif ENV["GNOME_DESKTOP_SESSION_ID"] then
89
+ @nix_desktop_environment = :gnome
90
+ elsif find_executable("xprop") then
91
+ if %x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0 then
92
+ @nix_desktop_environment = :xfce
93
+ end
94
+ end
95
+ Launchy.log "nix_desktop_environment => #{@nix_dekstop_environment}"
96
+ end
97
+ return @nix_desktop_environment
98
+ end
99
+
100
+ # find an executable in the available paths
101
+ def find_executable(bin,*paths)
102
+ Application.find_executable(bin,*paths)
103
+ end
104
+
105
+ # return the current 'host_os' string from ruby's configuration
106
+ def my_os
107
+ Application.my_os
108
+ end
109
+
110
+ # detect what the current os is and return :windows, :darwin, :nix, or :cygwin
111
+ def my_os_family(test_os = my_os)
112
+ Application.my_os_family(test_os)
113
+ end
114
+
115
+ # returns the list of command line application names for the current os. The list
116
+ # returned should only contain appliations or commands that actually exist on the
117
+ # system. The list members should have their full path to the executable.
118
+ def app_list
119
+ @app_list ||= self.send("#{my_os_family}_app_list")
120
+ end
121
+
122
+ # On darwin a good general default is the 'open' executable.
123
+ def darwin_app_list
124
+ Launchy.log "Using 'open' application on darwin."
125
+ [ find_executable('open') ]
126
+ end
127
+
128
+ # On windows a good general default is the 'start' Command Shell command
129
+ def windows_app_list
130
+ Launchy.log "Using 'start' command on windows."
131
+ %w[ start ]
132
+ end
133
+
134
+ # Cygwin uses the windows start but through an explicit execution of the cmd shell
135
+ def cygwin_app_list
136
+ Launchy.log "Using 'cmd /C start' on windows."
137
+ [ "cmd /C start" ]
138
+ end
139
+
140
+ # run the command
141
+ def run(cmd,*args)
142
+ args.unshift(cmd)
143
+ cmd_line = args.join(' ')
144
+ Launchy.log "Spawning on #{my_os_family} : #{cmd_line}"
145
+ if my_os_family == :windows then
146
+ system cmd_line
147
+ else
148
+ # fork and the child process should NOT run any exit handlers
149
+ child_pid = fork do
150
+ cmd_line += " > /dev/null 2>&1"
151
+ system cmd_line
152
+ exit!
153
+ end
154
+ Process.detach(child_pid)
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,80 @@
1
+ require 'launchy/application'
2
+ require 'uri'
3
+
4
+ module Launchy
5
+ class Browser < Application
6
+
7
+ DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS = {
8
+ :kde => "kfmclient",
9
+ :gnome => "gnome-open",
10
+ :xfce => "exo-open",
11
+ :generic => "htmlview"
12
+ }
13
+
14
+ FALLBACK_BROWSERS = %w[ firefox seamonkey opera mozilla netscape galeon ]
15
+
16
+ class << self
17
+ def run(*args)
18
+ Browser.new.visit(args[0])
19
+ end
20
+
21
+ # return true if this class can handle the given parameter(s)
22
+ def handle?(*args)
23
+ begin
24
+ uri = URI.parse(args[0])
25
+ return [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
26
+ rescue Exception
27
+ return false
28
+ end
29
+ end
30
+ end
31
+
32
+ def initialize
33
+ raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
34
+ end
35
+
36
+ # Find a list of potential browser applications to run on *nix machines.
37
+ # The order is:
38
+ # 1) What is in ENV['LAUNCHY_BROWSER'] or ENV['BROWSER']
39
+ # 2) xdg-open
40
+ # 3) desktop environment launcher program
41
+ # 4) a list of fallback browsers
42
+ def nix_app_list
43
+ if not @nix_app_list then
44
+ browser_cmds = ['xdg-open']
45
+ browser_cmds << DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS[nix_desktop_environment]
46
+ browser_cmds << FALLBACK_BROWSERS
47
+ browser_cmds.flatten!
48
+ browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
49
+ Launchy.log "Initial *Nix Browser List: #{browser_cmds.join(', ')}"
50
+ @nix_app_list = browser_cmds.collect { |bin| find_executable(bin) }.find_all { |x| not x.nil? }
51
+ Launchy.log "Filtered *Nix Browser List: #{@nix_app_list.join(', ')}"
52
+ end
53
+ @nix_app_list
54
+ end
55
+
56
+ # return the full command line path to the browser or nil
57
+ def browser
58
+ if not @browser then
59
+ if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
60
+ Launchy.log "Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
61
+ @browser = ENV['LAUNCHY_BROWSER']
62
+ elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
63
+ Launchy.log "Using BROWSER environment variable : #{ENV['BROWSER']}"
64
+ @browser = ENV['BROWSER']
65
+ elsif app_list.size > 0 then
66
+ @browser = app_list.first
67
+ Launchy.log "Using application list : #{@browser}"
68
+ else
69
+ $stderr.puts "Unable to launch. No Browser application found."
70
+ end
71
+ end
72
+ return @browser
73
+ end
74
+
75
+ # launch the browser at the appointed url
76
+ def visit(url)
77
+ run(browser,url)
78
+ end
79
+ end
80
+ end
@@ -1,8 +1,8 @@
1
1
  module Launchy
2
2
  class Version
3
3
  MAJOR = 0
4
- MINOR = 2
5
- BUILD = 1
4
+ MINOR = 3
5
+ BUILD = 0
6
6
 
7
7
  class << self
8
8
  def to_a
@@ -1,17 +1,17 @@
1
1
  require File.join(File.dirname(__FILE__),"spec_helper.rb")
2
2
  require 'yaml'
3
3
 
4
- describe Launchy::Spawnable::Application do
4
+ describe Launchy::Application do
5
5
  before(:each) do
6
6
  yml = YAML::load(IO.read(File.join(File.dirname(__FILE__),"tattle-host-os.yml")))
7
7
  @host_os = yml['host_os']
8
- @app = Launchy::Spawnable::Application.new
8
+ @app = Launchy::Application.new
9
9
 
10
10
  end
11
11
 
12
12
  it "should find all tattled os" do
13
13
  @host_os.keys.each do |os|
14
- Launchy::Spawnable::Application::KNOWN_OS_FAMILIES.should include(@app.my_os_family(os))
14
+ Launchy::Application::KNOWN_OS_FAMILIES.should include(@app.my_os_family(os))
15
15
  end
16
16
  end
17
17
 
@@ -33,6 +33,6 @@ describe Launchy::Spawnable::Application do
33
33
  end
34
34
 
35
35
  it "should find the correct class to launch an ftp url" do
36
- Launchy::Spawnable::Application.find_application_class_for("ftp://ftp.ruby-lang.org/pub/ruby/").should == Launchy::Spawnable::Browser
36
+ Launchy::Application.find_application_class_for("ftp://ftp.ruby-lang.org/pub/ruby/").should == Launchy::Browser
37
37
  end
38
38
  end
@@ -1,23 +1,23 @@
1
1
  require File.join(File.dirname(__FILE__),"spec_helper.rb")
2
2
 
3
- describe Launchy::Spawnable::Browser do
3
+ describe Launchy::Browser do
4
4
  it "should find a path to a executable" do
5
- File.executable?(Launchy::Spawnable::Browser.new.browser).should == true
5
+ File.executable?(Launchy::Browser.new.browser).should == true
6
6
  end
7
7
 
8
8
  it "should handle an http url" do
9
- Launchy::Spawnable::Browser.handle?("http://www.example.com") == true
9
+ Launchy::Browser.handle?("http://www.example.com") == true
10
10
  end
11
11
 
12
12
  it "should handle an https url" do
13
- Launchy::Spawnable::Browser.handle?("https://www.example.com") == true
13
+ Launchy::Browser.handle?("https://www.example.com") == true
14
14
  end
15
15
 
16
16
  it "should handle an ftp url" do
17
- Launchy::Spawnable::Browser.handle?("ftp://download.example.com") == true
17
+ Launchy::Browser.handle?("ftp://download.example.com") == true
18
18
  end
19
19
 
20
20
  it "should not handle a mailto url" do
21
- Launchy::Spawnable::Browser.handle?("mailto:jeremy@example.com") == false
21
+ Launchy::Browser.handle?("mailto:jeremy@example.com") == false
22
22
  end
23
23
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: launchy
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2007-08-18 00:00:00 -06:00
6
+ version: 0.3.0
7
+ date: 2007-08-30 00:00:00 -06:00
8
8
  summary: A helper to launch apps from within ruby programs.
9
9
  require_paths:
10
10
  - lib
@@ -29,24 +29,23 @@ post_install_message:
29
29
  authors:
30
30
  - Jeremy Hinegardner
31
31
  files:
32
+ - spec/application_spec.rb
32
33
  - spec/browser_spec.rb
33
- - spec/spawnable_application_spec.rb
34
34
  - spec/spec_helper.rb
35
35
  - spec/version_spec.rb
36
36
  - CHANGES
37
37
  - LICENSE
38
38
  - README
39
+ - lib/launchy/application.rb
40
+ - lib/launchy/browser.rb
39
41
  - lib/launchy/gemspec.rb
40
- - lib/launchy/spawnable/application.rb
41
- - lib/launchy/spawnable/browser.rb
42
- - lib/launchy/spawnable.rb
43
42
  - lib/launchy/specification.rb
44
43
  - lib/launchy/version.rb
45
44
  - lib/launchy.rb
46
45
  - bin/launchy
47
46
  test_files:
47
+ - spec/application_spec.rb
48
48
  - spec/browser_spec.rb
49
- - spec/spawnable_application_spec.rb
50
49
  - spec/spec_helper.rb
51
50
  - spec/version_spec.rb
52
51
  rdoc_options:
@@ -1,2 +0,0 @@
1
- require 'launchy/spawnable/application'
2
- require 'launchy/spawnable/browser'
@@ -1,150 +0,0 @@
1
- require 'launchy/spawnable'
2
- require 'rbconfig'
3
-
4
- module Launchy
5
- module Spawnable
6
- class Application
7
-
8
- KNOWN_OS_FAMILIES = [ :windows, :darwin, :nix ]
9
-
10
- class << self
11
- def inherited(sub_class)
12
- application_classes << sub_class
13
- end
14
- def application_classes
15
- @application_classes ||= []
16
- end
17
-
18
- def find_application_class_for(*args)
19
- Launchy.log "finding application classes for [#{args.join(' ')}]"
20
- application_classes.find do |klass|
21
- if klass.handle?(*args) then
22
- Launchy.log " #{klass.name}"
23
- true
24
- else
25
- false
26
- end
27
- end
28
- end
29
-
30
- # find an executable in the available paths
31
- # mkrf did such a good job on this I had to borrow it.
32
- def find_executable(bin,*paths)
33
- paths = ENV['PATH'].split(File::PATH_SEPARATOR) if paths.empty?
34
- paths.each do |path|
35
- file = File.join(path,bin)
36
- if File.executable?(file) then
37
- Launchy.log "found executable #{file}"
38
- return file
39
- end
40
- end
41
- Launchy.log "Unable to find `#{bin}' in paths #{paths.join(', ')}"
42
- return nil
43
- end
44
-
45
- # return the current 'host_os' string from ruby's configuration
46
- def my_os
47
- ::Config::CONFIG['host_os']
48
- end
49
-
50
- # detect what the current os is and return :windows, :darwin or :nix
51
- def my_os_family(test_os = my_os)
52
- case test_os
53
- when /mswin/i
54
- family = :windows
55
- when /windows/i
56
- family = :windows
57
- when /darwin/i
58
- family = :darwin
59
- when /mac os/i
60
- family = :darwin
61
- when /solaris/i
62
- family = :nix
63
- when /bsd/i
64
- family = :nix
65
- when /linux/i
66
- family = :nix
67
- when /cygwin/i
68
- family = :nix
69
- else
70
- $stderr.puts "Unknown OS familiy for '#{test_os}'. Please report this bug to #{Launchy::SPEC.email}"
71
- family = :unknown
72
- end
73
- end
74
- end
75
-
76
-
77
- # Determine the appropriate desktop environment for *nix machine. Currently this is
78
- # linux centric. The detection is based upon the detection used by xdg-open from
79
- # http://portland.freedesktop.org/wiki/XdgUtils
80
- def nix_desktop_environment
81
- if not @nix_desktop_environment then
82
- @nix_desktop_environment = :generic
83
- if ENV["KDE_FULL_SESSION"] || ENV["KDE_SESSION_UID"] then
84
- @nix_desktop_environment = :kde
85
- elsif ENV["GNOME_DESKTOP_SESSION_ID"] then
86
- @nix_desktop_environment = :gnome
87
- elsif find_executable("xprop") then
88
- if %x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0 then
89
- @nix_desktop_environment = :xfce
90
- end
91
- end
92
- Launchy.log "nix_desktop_environment => #{@nix_dekstop_environment}"
93
- end
94
- return @nix_desktop_environment
95
- end
96
-
97
- # find an executable in the available paths
98
- def find_executable(bin,*paths)
99
- Application.find_executable(bin,*paths)
100
- end
101
-
102
- # return the current 'host_os' string from ruby's configuration
103
- def my_os
104
- Application.my_os
105
- end
106
-
107
- # detect what the current os is and return :windows, :darwin or :nix
108
- def my_os_family(test_os = my_os)
109
- Application.my_os_family(test_os)
110
- end
111
-
112
- # returns the list of command line application names for the current os. The list
113
- # returned should only contain appliations or commands that actually exist on the
114
- # system. The list members should have their full path to the executable.
115
- def app_list
116
- @app_list ||= self.send("#{my_os_family}_app_list")
117
- end
118
-
119
- # On darwin a good general default is the 'open' executable.
120
- def darwin_app_list
121
- Launchy.log "Using 'open' application on darwin."
122
- [ find_executable('open') ]
123
- end
124
-
125
- # On windows a good general default is the 'start' Command Shell command
126
- def windows_app_list
127
- Launchy.log "Using 'start' command on windows."
128
- %w[ start ]
129
- end
130
-
131
- # run the command
132
- def run(cmd,*args)
133
- args.unshift(cmd)
134
- cmd_line = args.join(' ')
135
- Launchy.log "Spawning on #{my_os_family} : #{cmd_line}"
136
- if my_os_family == :windows then
137
- system cmd_line
138
- else
139
- # fork and the child process should NOT run any exit handlers
140
- child_pid = fork do
141
- cmd_line += " > /dev/null 2>&1"
142
- system cmd_line
143
- exit!
144
- end
145
- Process.detach(child_pid)
146
- end
147
- end
148
- end
149
- end
150
- end
@@ -1,83 +0,0 @@
1
- require 'launchy/spawnable/application'
2
- require 'uri'
3
-
4
- module Launchy
5
- module Spawnable
6
- class Browser < Application
7
-
8
- DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS = {
9
- :kde => "kfmclient",
10
- :gnome => "gnome-open",
11
- :xfce => "exo-open",
12
- :generic => "htmlview"
13
- }
14
-
15
- FALLBACK_BROWSERS = %w[ firefox seamonkey opera mozilla netscape galeon ]
16
-
17
- class << self
18
- def run(*args)
19
- Browser.new.visit(args[0])
20
- end
21
-
22
- # return true if this class can handle the given parameter(s)
23
- def handle?(*args)
24
- begin
25
- uri = URI.parse(args[0])
26
- return [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
27
- rescue Exception
28
- return false
29
- end
30
- end
31
- end
32
-
33
- def initialize
34
- raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
35
- end
36
-
37
- # Find a list of potential browser applications to run on *nix machines.
38
- # The order is:
39
- # 1) What is in ENV['LAUNCHY_BROWSER'] or ENV['BROWSER']
40
- # 2) xdg-open
41
- # 3) desktop environment launcher program
42
- # 4) a list of fallback browsers
43
- def nix_app_list
44
- if not @nix_app_list then
45
- browser_cmds = ['xdg-open']
46
- browser_cmds << DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS[nix_desktop_environment]
47
- browser_cmds << FALLBACK_BROWSERS
48
- browser_cmds.flatten!
49
- browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
50
- Launchy.log "Initial *Nix Browser List: #{browser_cmds.join(', ')}"
51
- @nix_app_list = browser_cmds.collect { |bin| find_executable(bin) }.find_all { |x| not x.nil? }
52
- Launchy.log "Filtered *Nix Browser List: #{@nix_app_list.join(', ')}"
53
- end
54
- @nix_app_list
55
- end
56
-
57
- # return the full command line path to the browser or nil
58
- def browser
59
- if not @browser then
60
- if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
61
- Launchy.log "Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
62
- @browser = ENV['LAUNCHY_BROWSER']
63
- elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
64
- Launchy.log "Using BROWSER environment variable : #{ENV['BROWSER']}"
65
- @browser = ENV['BROWSER']
66
- elsif app_list.size > 0 then
67
- @browser = app_list.first
68
- Launchy.log "Using application list : #{@browser}"
69
- else
70
- $stderr.puts "Unable to launch. No Browser application found."
71
- end
72
- end
73
- return @browser
74
- end
75
-
76
- # launch the browser at the appointed url
77
- def visit(url)
78
- run(browser,url)
79
- end
80
-
81
- end
82
- end
83
- end