Class Launchy::Application
In: lib/launchy/application.rb
lib/launchy/application.rb
Parent: Object

Methods

Public Class methods

[Source]

    # File lib/launchy/application.rb, line 13
13:       def application_classes
14:         @application_classes ||= []
15:       end

[Source]

    # File lib/launchy/application.rb, line 13
13:       def application_classes
14:         @application_classes ||= []
15:       end

[Source]

    # File lib/launchy/application.rb, line 17
17:       def find_application_class_for(*args)
18:         Launchy.log "#{self.name} : finding application classes for [#{args.join(' ')}]"
19:         application_classes.find do |klass|
20:           Launchy.log "#{self.name} : Trying #{klass.name}"
21:           if klass.handle?(*args) then
22:             true
23:           else
24:             false
25:           end
26:         end
27:       end

[Source]

    # File lib/launchy/application.rb, line 17
17:       def find_application_class_for(*args)
18:         Launchy.log "#{self.name} : finding application classes for [#{args.join(' ')}]"
19:         application_classes.find do |klass|
20:           Launchy.log "#{self.name} : Trying #{klass.name}"
21:           if klass.handle?(*args) then
22:             true
23:           else
24:             false
25:           end
26:         end
27:       end

find an executable in the available paths mkrf did such a good job on this I had to borrow it.

[Source]

    # File lib/launchy/application.rb, line 31
31:       def find_executable(bin,*paths)
32:         paths = ENV['PATH'].split(File::PATH_SEPARATOR) if paths.empty?
33:         paths.each do |path|
34:           file = File.join(path,bin)
35:           if File.executable?(file) then
36:             Launchy.log "#{self.name} : found executable #{file}"
37:             return file
38:           end
39:         end
40:         Launchy.log "#{self.name} : Unable to find `#{bin}' in #{paths.join(', ')}"
41:         return nil
42:       end

find an executable in the available paths mkrf did such a good job on this I had to borrow it.

[Source]

    # File lib/launchy/application.rb, line 31
31:       def find_executable(bin,*paths)
32:         paths = ENV['PATH'].split(File::PATH_SEPARATOR) if paths.empty?
33:         paths.each do |path|
34:           file = File.join(path,bin)
35:           if File.executable?(file) then
36:             Launchy.log "#{self.name} : found executable #{file}"
37:             return file
38:           end
39:         end
40:         Launchy.log "#{self.name} : Unable to find `#{bin}' in #{paths.join(', ')}"
41:         return nil
42:       end

[Source]

    # File lib/launchy/application.rb, line 10
10:       def inherited(sub_class)
11:         application_classes << sub_class
12:       end

[Source]

    # File lib/launchy/application.rb, line 10
10:       def inherited(sub_class)
11:         application_classes << sub_class
12:       end

[Source]

   # File lib/launchy/application.rb, line 6
6:       def known_os_families
7:         @known_os_families ||= [ :windows, :darwin, :nix, :cygwin ]
8:       end

[Source]

   # File lib/launchy/application.rb, line 6
6:       def known_os_families
7:         @known_os_families ||= [ :windows, :darwin, :nix, :cygwin ]
8:       end

return the current ‘host_os’ string from ruby‘s configuration

[Source]

    # File lib/launchy/application.rb, line 45
45:       def my_os
46:         if ENV['LAUNCHY_HOST_OS'] then
47:           Launchy.log "#{self.name} : Using LAUNCHY_HOST_OS override of '#{ENV['LAUNCHY_HOST_OS']}'"
48:           return ENV['LAUNCHY_HOST_OS']
49:         else
50:           ::Config::CONFIG['host_os']
51:         end
52:       end

return the current ‘host_os’ string from ruby‘s configuration

[Source]

    # File lib/launchy/application.rb, line 45
45:       def my_os
46:         if ENV['LAUNCHY_HOST_OS'] then
47:           Launchy.log "#{self.name} : Using LAUNCHY_HOST_OS override of '#{ENV['LAUNCHY_HOST_OS']}'"
48:           return ENV['LAUNCHY_HOST_OS']
49:         else
50:           ::Config::CONFIG['host_os']
51:         end
52:       end

detect what the current os is and return :windows, :darwin or :nix

[Source]

    # File lib/launchy/application.rb, line 55
55:       def my_os_family(test_os = my_os)
56:         case test_os
57:         when /mingw/i
58:           family = :windows
59:         when /mswin/i
60:           family = :windows
61:         when /windows/i
62:           family = :windows
63:         when /darwin/i
64:           family = :darwin
65:         when /mac os/i
66:           family = :darwin
67:         when /solaris/i
68:           family = :nix
69:         when /bsd/i
70:           family = :nix
71:         when /linux/i
72:           family = :nix
73:         when /aix/i
74:           family = :nix
75:         when /cygwin/i
76:           family = :cygwin
77:         else
78:           $stderr.puts "Unknown OS familiy for '#{test_os}'.  Please report this bug to <jeremy at hinegardner dot org>"
79:           family = :unknown
80:         end
81:       end

detect what the current os is and return :windows, :darwin or :nix

[Source]

    # File lib/launchy/application.rb, line 55
55:       def my_os_family(test_os = my_os)
56:         case test_os
57:         when /mingw/i
58:           family = :windows
59:         when /mswin/i
60:           family = :windows
61:         when /windows/i
62:           family = :windows
63:         when /darwin/i
64:           family = :darwin
65:         when /mac os/i
66:           family = :darwin
67:         when /solaris/i
68:           family = :nix
69:         when /bsd/i
70:           family = :nix
71:         when /linux/i
72:           family = :nix
73:         when /aix/i
74:           family = :nix
75:         when /cygwin/i
76:           family = :cygwin
77:         else
78:           $stderr.puts "Unknown OS familiy for '#{test_os}'.  Please report this bug to <jeremy at hinegardner dot org>"
79:           family = :unknown
80:         end
81:       end

Public Instance methods

returns the list of command line application names for the current os. The list returned should only contain appliations or commands that actually exist on the system. The list members should have their full path to the executable.

[Source]

     # File lib/launchy/application.rb, line 123
123:     def app_list
124:       @app_list ||= self.send("#{my_os_family}_app_list")
125:     end

returns the list of command line application names for the current os. The list returned should only contain appliations or commands that actually exist on the system. The list members should have their full path to the executable.

[Source]

     # File lib/launchy/application.rb, line 123
123:     def app_list
124:       @app_list ||= self.send("#{my_os_family}_app_list")
125:     end

Cygwin uses the windows start but through an explicit execution of the cmd shell

[Source]

     # File lib/launchy/application.rb, line 140
140:     def cygwin_app_list
141:       Launchy.log "#{self.class.name} : Using 'cmd /C start' on windows."
142:       [ "cmd /C start" ]
143:     end

Cygwin uses the windows start but through an explicit execution of the cmd shell

[Source]

     # File lib/launchy/application.rb, line 140
140:     def cygwin_app_list
141:       Launchy.log "#{self.class.name} : Using 'cmd /C start' on windows."
142:       [ "cmd /C start" ]
143:     end

On darwin a good general default is the ‘open’ executable.

[Source]

     # File lib/launchy/application.rb, line 128
128:     def darwin_app_list
129:       Launchy.log "#{self.class.name} : Using 'open' application on darwin."
130:       [ find_executable('open') ]
131:     end

On darwin a good general default is the ‘open’ executable.

[Source]

     # File lib/launchy/application.rb, line 128
128:     def darwin_app_list
129:       Launchy.log "#{self.class.name} : Using 'open' application on darwin."
130:       [ find_executable('open') ]
131:     end

find an executable in the available paths

[Source]

     # File lib/launchy/application.rb, line 106
106:     def find_executable(bin,*paths)
107:       Application.find_executable(bin,*paths)
108:     end

find an executable in the available paths

[Source]

     # File lib/launchy/application.rb, line 106
106:     def find_executable(bin,*paths)
107:       Application.find_executable(bin,*paths)
108:     end

return the current ‘host_os’ string from ruby‘s configuration

[Source]

     # File lib/launchy/application.rb, line 111
111:     def my_os
112:       Application.my_os
113:     end

return the current ‘host_os’ string from ruby‘s configuration

[Source]

     # File lib/launchy/application.rb, line 111
111:     def my_os
112:       Application.my_os
113:     end

detect what the current os is and return :windows, :darwin, :nix, or :cygwin

[Source]

     # File lib/launchy/application.rb, line 116
116:     def my_os_family(test_os = my_os)
117:       Application.my_os_family(test_os)
118:     end

detect what the current os is and return :windows, :darwin, :nix, or :cygwin

[Source]

     # File lib/launchy/application.rb, line 116
116:     def my_os_family(test_os = my_os)
117:       Application.my_os_family(test_os)
118:     end

Determine the appropriate desktop environment for *nix machine. Currently this is linux centric. The detection is based upon the detection used by xdg-open from portland.freedesktop.org/wiki/XdgUtils

[Source]

     # File lib/launchy/application.rb, line 88
 88:     def nix_desktop_environment
 89:       if not defined? @nix_desktop_environment then
 90:         @nix_desktop_environment = :generic
 91:         if ENV["KDE_FULL_SESSION"] || ENV["KDE_SESSION_UID"] then
 92:           @nix_desktop_environment = :kde
 93:         elsif ENV["GNOME_DESKTOP_SESSION_ID"] then
 94:           @nix_desktop_environment = :gnome
 95:         elsif find_executable("xprop") then
 96:           if %x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0 then
 97:             @nix_desktop_environment = :xfce
 98:           end
 99:         end
100:         Launchy.log "#{self.class.name} : nix_desktop_environment => '#{@nix_desktop_environment}'"
101:       end
102:       return @nix_desktop_environment
103:     end

Determine the appropriate desktop environment for *nix machine. Currently this is linux centric. The detection is based upon the detection used by xdg-open from portland.freedesktop.org/wiki/XdgUtils

[Source]

     # File lib/launchy/application.rb, line 88
 88:     def nix_desktop_environment
 89:       if not defined? @nix_desktop_environment then
 90:         @nix_desktop_environment = :generic
 91:         if ENV["KDE_FULL_SESSION"] || ENV["KDE_SESSION_UID"] then
 92:           @nix_desktop_environment = :kde
 93:         elsif ENV["GNOME_DESKTOP_SESSION_ID"] then
 94:           @nix_desktop_environment = :gnome
 95:         elsif find_executable("xprop") then
 96:           if %x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0 then
 97:             @nix_desktop_environment = :xfce
 98:           end
 99:         end
100:         Launchy.log "#{self.class.name} : nix_desktop_environment => '#{@nix_desktop_environment}'"
101:       end
102:       return @nix_desktop_environment
103:     end

run the command

[Source]

     # File lib/launchy/application.rb, line 146
146:     def run(cmd,*args)
147:       Launchy.log "#{self.class.name} : Spawning on #{my_os_family} : #{cmd} #{args.inspect}"
148: 
149:       if my_os_family == :windows then
150:         # NOTE: the command is purposely omitted here because
151:         #       running the filename via "cmd /c" is the same as
152:         #       running "start filename" at the command-prompt
153:         #
154:         #       furthermore, when "cmd /c start filename" is
155:         #       run, the shell interprets it as two commands:
156:         #       (1) "start" opens a new terminal, and (2)
157:         #       "filename" causes the file to be launched.
158:         system 'cmd', '/c', *args
159:       else
160:         # fork, and the child process should NOT run any exit handlers
161:         child_pid = fork do
162:           # NOTE: we pass a dummy argument *before*
163:           #       the actual command to prevent sh
164:           #       from silently consuming our actual
165:           #       command and assigning it to $0!
166:           dummy = ''
167:           system 'sh', '-c', '"$@" >/dev/null 2>&1', dummy, cmd, *args
168:           exit!
169:         end
170:         Process.detach(child_pid)
171:       end
172:     end

run the command

[Source]

     # File lib/launchy/application.rb, line 146
146:     def run(cmd,*args)
147:       Launchy.log "#{self.class.name} : Spawning on #{my_os_family} : #{cmd} #{args.inspect}"
148: 
149:       if my_os_family == :windows then
150:         # NOTE: the command is purposely omitted here because
151:         #       running the filename via "cmd /c" is the same as
152:         #       running "start filename" at the command-prompt
153:         #
154:         #       furthermore, when "cmd /c start filename" is
155:         #       run, the shell interprets it as two commands:
156:         #       (1) "start" opens a new terminal, and (2)
157:         #       "filename" causes the file to be launched.
158:         system 'cmd', '/c', *args
159:       else
160:         # fork, and the child process should NOT run any exit handlers
161:         child_pid = fork do
162:           # NOTE: we pass a dummy argument *before*
163:           #       the actual command to prevent sh
164:           #       from silently consuming our actual
165:           #       command and assigning it to $0!
166:           dummy = ''
167:           system 'sh', '-c', '"$@" >/dev/null 2>&1', dummy, cmd, *args
168:           exit!
169:         end
170:         Process.detach(child_pid)
171:       end
172:     end

On windows a good general default is the ‘start’ Command Shell command

[Source]

     # File lib/launchy/application.rb, line 134
134:     def windows_app_list
135:       Launchy.log "#{self.class.name} : Using 'start' command on windows."
136:             %w[ start ]
137:     end

On windows a good general default is the ‘start’ Command Shell command

[Source]

     # File lib/launchy/application.rb, line 134
134:     def windows_app_list
135:       Launchy.log "#{self.class.name} : Using 'start' command on windows."
136:             %w[ start ]
137:     end

[Validate]