launchy 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,10 @@
1
1
  == Changes
2
2
 
3
+ === Version 0.3.1 - 2007-09-08
4
+
5
+ * finalize the command line wrapper around the launchy library.
6
+ * added more tests
7
+
3
8
  === Version 0.3.0 - 2007-08-30
4
9
 
5
10
  * reorganize the code structure, removing Spawnable namespace
data/bin/launchy CHANGED
@@ -9,4 +9,4 @@ rescue LoadError
9
9
  retry
10
10
  end
11
11
 
12
- Launchy.open(*ARGV)
12
+ Launchy.command_line.run(ARGV)
data/lib/launchy.rb CHANGED
@@ -22,11 +22,19 @@ module Launchy
22
22
 
23
23
  class << self
24
24
  def open(*params)
25
- klass = Launchy::Application.find_application_class_for(*params)
26
- if klass then
27
- klass.run(*params)
28
- else
29
- $stderr.puts "Unable to launch #{params.join(' ')}"
25
+ begin
26
+ klass = Launchy::Application.find_application_class_for(*params)
27
+ if klass then
28
+ klass.run(*params)
29
+ else
30
+ msg = "Unable to launch #{params.join(' ')}"
31
+ Launchy.log "#{self.name} : #{msg}"
32
+ $stderr.puts msg
33
+ end
34
+ rescue Exception => e
35
+ msg = "Failure in opening #{params.join(' ')} : #{e}"
36
+ Launchy.log "#{self.name} : #{msg}"
37
+ $stderr.puts msg
30
38
  end
31
39
  end
32
40
 
@@ -37,7 +45,14 @@ module Launchy
37
45
  $stderr.puts "LAUNCHY_DEBUG: #{msg}"
38
46
  end
39
47
  end
48
+
49
+ # Create an instance of the commandline application of launchy
50
+ def command_line
51
+ Launchy::CommandLine.new
52
+ end
40
53
  end
54
+
55
+
41
56
  end
42
57
 
43
58
  Launchy.require_all_libs_relative_to(__FILE__)
@@ -14,10 +14,10 @@ module Launchy
14
14
  end
15
15
 
16
16
  def find_application_class_for(*args)
17
- Launchy.log "finding application classes for [#{args.join(' ')}]"
17
+ Launchy.log "#{self.name} : finding application classes for [#{args.join(' ')}]"
18
18
  application_classes.find do |klass|
19
+ Launchy.log "#{self.name} : Trying #{klass.name}"
19
20
  if klass.handle?(*args) then
20
- Launchy.log " #{klass.name}"
21
21
  true
22
22
  else
23
23
  false
@@ -32,18 +32,18 @@ module Launchy
32
32
  paths.each do |path|
33
33
  file = File.join(path,bin)
34
34
  if File.executable?(file) then
35
- Launchy.log "found executable #{file}"
35
+ Launchy.log "#{self.name} : found executable #{file}"
36
36
  return file
37
37
  end
38
38
  end
39
- Launchy.log "Unable to find `#{bin}' in paths #{paths.join(', ')}"
39
+ Launchy.log "#{self.name} : Unable to find `#{bin}' in #{paths.join(', ')}"
40
40
  return nil
41
41
  end
42
42
 
43
43
  # return the current 'host_os' string from ruby's configuration
44
44
  def my_os
45
45
  if ENV['LAUNCHY_HOST_OS'] then
46
- Launchy.log "Using LAUNCHY_HOST_OS override of '#{ENV['LAUNCHY_HOST_OS']}'"
46
+ Launchy.log "#{self.name} : Using LAUNCHY_HOST_OS override of '#{ENV['LAUNCHY_HOST_OS']}'"
47
47
  return ENV['LAUNCHY_HOST_OS']
48
48
  else
49
49
  ::Config::CONFIG['host_os']
@@ -92,7 +92,7 @@ module Launchy
92
92
  @nix_desktop_environment = :xfce
93
93
  end
94
94
  end
95
- Launchy.log "nix_desktop_environment => #{@nix_dekstop_environment}"
95
+ Launchy.log "#{self.class.name} : nix_desktop_environment => '#{@nix_desktop_environment}'"
96
96
  end
97
97
  return @nix_desktop_environment
98
98
  end
@@ -118,22 +118,22 @@ module Launchy
118
118
  def app_list
119
119
  @app_list ||= self.send("#{my_os_family}_app_list")
120
120
  end
121
-
121
+
122
122
  # On darwin a good general default is the 'open' executable.
123
123
  def darwin_app_list
124
- Launchy.log "Using 'open' application on darwin."
124
+ Launchy.log "#{self.class.name} : Using 'open' application on darwin."
125
125
  [ find_executable('open') ]
126
126
  end
127
127
 
128
128
  # On windows a good general default is the 'start' Command Shell command
129
129
  def windows_app_list
130
- Launchy.log "Using 'start' command on windows."
130
+ Launchy.log "#{self.class.name} : Using 'start' command on windows."
131
131
  %w[ start ]
132
132
  end
133
133
 
134
134
  # Cygwin uses the windows start but through an explicit execution of the cmd shell
135
135
  def cygwin_app_list
136
- Launchy.log "Using 'cmd /C start' on windows."
136
+ Launchy.log "#{self.class.name} : Using 'cmd /C start' on windows."
137
137
  [ "cmd /C start" ]
138
138
  end
139
139
 
@@ -141,11 +141,12 @@ module Launchy
141
141
  def run(cmd,*args)
142
142
  args.unshift(cmd)
143
143
  cmd_line = args.join(' ')
144
- Launchy.log "Spawning on #{my_os_family} : #{cmd_line}"
144
+ Launchy.log "#{self.class.name} : Spawning on #{my_os_family} : #{cmd_line}"
145
+
145
146
  if my_os_family == :windows then
146
147
  system cmd_line
147
148
  else
148
- # fork and the child process should NOT run any exit handlers
149
+ # fork, and the child process should NOT run any exit handlers
149
150
  child_pid = fork do
150
151
  cmd_line += " > /dev/null 2>&1"
151
152
  system cmd_line
@@ -21,9 +21,12 @@ module Launchy
21
21
  # return true if this class can handle the given parameter(s)
22
22
  def handle?(*args)
23
23
  begin
24
+ Launchy.log "#{self.name} : testing if [#{args[0]}] (#{args[0].class}) is a url."
24
25
  uri = URI.parse(args[0])
25
- return [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
26
- rescue Exception
26
+ result = [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
27
+ rescue Exception => e
28
+ # hmm... why does rcov not see that this is executed ?
29
+ Launchy.log "#{self.name} : not a url, #{e}"
27
30
  return false
28
31
  end
29
32
  end
@@ -46,9 +49,9 @@ module Launchy
46
49
  browser_cmds << FALLBACK_BROWSERS
47
50
  browser_cmds.flatten!
48
51
  browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
49
- Launchy.log "Initial *Nix Browser List: #{browser_cmds.join(', ')}"
52
+ Launchy.log "#{self.class.name} : Initial *Nix Browser List: #{browser_cmds.join(', ')}"
50
53
  @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(', ')}"
54
+ Launchy.log "#{self.class.name} : Filtered *Nix Browser List: #{@nix_app_list.join(', ')}"
52
55
  end
53
56
  @nix_app_list
54
57
  end
@@ -57,16 +60,18 @@ module Launchy
57
60
  def browser
58
61
  if not @browser then
59
62
  if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
60
- Launchy.log "Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
63
+ Launchy.log "#{self.class.name} : Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
61
64
  @browser = ENV['LAUNCHY_BROWSER']
62
65
  elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
63
- Launchy.log "Using BROWSER environment variable : #{ENV['BROWSER']}"
66
+ Launchy.log "#{self.class.name} : Using BROWSER environment variable : #{ENV['BROWSER']}"
64
67
  @browser = ENV['BROWSER']
65
68
  elsif app_list.size > 0 then
66
69
  @browser = app_list.first
67
- Launchy.log "Using application list : #{@browser}"
70
+ Launchy.log "#{self.class.name} : Using application list : #{@browser}"
68
71
  else
69
- $stderr.puts "Unable to launch. No Browser application found."
72
+ msg = "Unable to launch. No Browser application found."
73
+ Launchy.log "#{self.class.name} : #{msg}"
74
+ $stderr.puts msg
70
75
  end
71
76
  end
72
77
  return @browser
@@ -0,0 +1,48 @@
1
+ require 'optparse'
2
+
3
+ module Launchy
4
+ class CommandLine
5
+
6
+ def parser
7
+ @parser ||= OptionParser.new do |op|
8
+ op.banner = "Usage: launchy [options] url"
9
+ op.separator ""
10
+ op.on("-d", "--debug", "Force debug, output lots of information.",
11
+ "This sets the LAUNCHY_DEBUG environment variable to 'true'.") do |d|
12
+ ENV["LAUNCHY_DEBUG"] = 'true'
13
+ end
14
+
15
+ op.on("-h", "--help", "Print this message") do |h|
16
+ puts op.to_s
17
+ exit 0
18
+ end
19
+
20
+ op.on("-v", "--version", "Output the version of Launchy") do |v|
21
+ puts "Launchy version #{Launchy::VERSION}"
22
+ exit 0
23
+ end
24
+
25
+ op.on("-o", "--host-os HOST_OS","Force the behavior of a particular host os.",
26
+ "This sets the LAUNCHY_HOST_OS environment variable.") do |os|
27
+ ENV["LAUNCHY_HOST_OS"] = os
28
+ end
29
+
30
+ op.on("-b", "--browser BROWSER", "Force launchy to use a particular browser.",
31
+ "This sets the LAUNCHY_BROWSER environment variable.") do |browser|
32
+ ENV["LAUNCHY_BROWSER"] = browser
33
+ end
34
+ end
35
+ end
36
+
37
+ def run(argv = ARGV)
38
+ begin
39
+ parser.parse!(argv)
40
+ Launchy.open(*argv)
41
+ rescue ::OptionParser::ParseError => pe
42
+ $stderr.puts "#{parser.programn_name}: #{pe}"
43
+ $stderr.puts "Try `#{parser.program_name} --help' for more information."
44
+ exit 1
45
+ end
46
+ end
47
+ end
48
+ end
@@ -2,7 +2,7 @@ module Launchy
2
2
  class Version
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- BUILD = 0
5
+ BUILD = 1
6
6
 
7
7
  class << self
8
8
  def to_a
@@ -6,7 +6,6 @@ describe Launchy::Application 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
8
  @app = Launchy::Application.new
9
-
10
9
  end
11
10
 
12
11
  it "should find all tattled os" do
@@ -35,4 +34,26 @@ describe Launchy::Application do
35
34
  it "should find the correct class to launch an ftp url" do
36
35
  Launchy::Application.find_application_class_for("ftp://ftp.ruby-lang.org/pub/ruby/").should == Launchy::Browser
37
36
  end
37
+
38
+ it "knows when it cannot find an application class" do
39
+ Launchy::Application.find_application_class_for("xyzzy:stuff,things").should == nil
40
+ end
41
+
42
+ it "allows for environmental override of host_os" do
43
+ ENV["LAUNCHY_HOST_OS"] = "hal-9000"
44
+ Launchy::Application.my_os.should == "hal-9000"
45
+ ENV["LAUNCHY_HOST_OS"] = nil
46
+ end
47
+
48
+ it "can detect the desktop environment of a *nix machien" do
49
+ @app.nix_desktop_environment.should == :generic
50
+
51
+ { "KDE_FULL_SESSION" => :kde,
52
+ "KDE_SESSION_UID" => :kde,
53
+ "GNOME_DESKTOP_SESSION_ID" => :gnome }.each_pair do |k,v|
54
+ ENV[k] = "launchy-test"
55
+ Launchy::Application.new.nix_desktop_environment.should == v
56
+ ENV[k] = nil
57
+ end
58
+ end
38
59
  end
data/spec/browser_spec.rb CHANGED
@@ -1,23 +1,50 @@
1
1
  require File.join(File.dirname(__FILE__),"spec_helper.rb")
2
-
2
+ require 'stringio'
3
3
  describe Launchy::Browser do
4
4
  it "should find a path to a executable" do
5
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::Browser.handle?("http://www.example.com") == true
9
+ Launchy::Browser.handle?("http://www.example.com").should == true
10
10
  end
11
11
 
12
12
  it "should handle an https url" do
13
- Launchy::Browser.handle?("https://www.example.com") == true
13
+ Launchy::Browser.handle?("https://www.example.com").should == true
14
14
  end
15
15
 
16
16
  it "should handle an ftp url" do
17
- Launchy::Browser.handle?("ftp://download.example.com") == true
17
+ Launchy::Browser.handle?("ftp://download.example.com").should == true
18
18
  end
19
19
 
20
20
  it "should not handle a mailto url" do
21
- Launchy::Browser.handle?("mailto:jeremy@example.com") == false
21
+ Launchy::Browser.handle?("mailto:jeremy@example.com").should == false
22
22
  end
23
- end
23
+
24
+ it "creates a default unix application list" do
25
+ Launchy::Browser.new.nix_app_list.class.should == Array
26
+ end
27
+
28
+ it "can use environmental variable overrides for the browser" do
29
+ { "BROWSER" => "/usr/bin/true",
30
+ "LAUNCHY_BROWSER" => "/usr/bin/true"}.each_pair do |e,v|
31
+ ENV[e] = v
32
+ Launchy::Browser.new.browser.should == v
33
+ ENV[e] = nil
34
+ end
35
+ end
36
+
37
+ it "reports when it cannot find an browser" do
38
+ old_error = $stderr
39
+ $stderr = StringIO.new
40
+ ENV["LAUNCHY_HOST_OS"] = "linux"
41
+ begin
42
+ browser = Launchy::Browser.new
43
+ rescue => e
44
+ e.message.should =~ /Unable to find browser to launch for os family/m
45
+ end
46
+ ENV["LAUNCHY_HOST_OS"] = nil
47
+ $stderr.string.should =~ /Unable to launch. No Browser application found./m
48
+ $stderr = old_error
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__),"spec_helper.rb")
2
+ require 'stringio'
3
+
4
+ describe Launchy do
5
+ it "instantiates an insteadn of Launchy::CommandLine for commandline" do
6
+ Launchy.command_line.class.should == Launchy::CommandLine
7
+ end
8
+
9
+ it "logs to stderr when LAUNCHY_DEBUG environment variable is set" do
10
+ ENV["LAUNCHY_DEBUG"] = 'true'
11
+ old_stderr = $stderr
12
+ $stderr = StringIO.new
13
+ Launchy.log "This is a test log message"
14
+ $stderr.string.strip.should == "LAUNCHY_DEBUG: This is a test log message"
15
+ $stderr = old_stderr
16
+ ENV["LAUNCHY_DEBUG"] = nil
17
+ end
18
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: launchy
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.0
7
- date: 2007-08-30 00:00:00 -06:00
6
+ version: 0.3.1
7
+ date: 2007-09-08 00:00:00 -06:00
8
8
  summary: A helper to launch apps from within ruby programs.
9
9
  require_paths:
10
10
  - lib
@@ -31,6 +31,7 @@ authors:
31
31
  files:
32
32
  - spec/application_spec.rb
33
33
  - spec/browser_spec.rb
34
+ - spec/launchy_spec.rb
34
35
  - spec/spec_helper.rb
35
36
  - spec/version_spec.rb
36
37
  - CHANGES
@@ -38,6 +39,7 @@ files:
38
39
  - README
39
40
  - lib/launchy/application.rb
40
41
  - lib/launchy/browser.rb
42
+ - lib/launchy/command_line.rb
41
43
  - lib/launchy/gemspec.rb
42
44
  - lib/launchy/specification.rb
43
45
  - lib/launchy/version.rb
@@ -46,6 +48,7 @@ files:
46
48
  test_files:
47
49
  - spec/application_spec.rb
48
50
  - spec/browser_spec.rb
51
+ - spec/launchy_spec.rb
49
52
  - spec/spec_helper.rb
50
53
  - spec/version_spec.rb
51
54
  rdoc_options: