class Launchy::Application

Public Class Methods

application_classes() click to toggle source
# File lib/launchy/application.rb, line 13
def application_classes
  @application_classes ||= []
end
find_application_class_for(*args) click to toggle source
# File lib/launchy/application.rb, line 17
def find_application_class_for(*args)
  Launchy.log "#{self.name} : finding application classes for [#{args.join(' ')}]"
  application_classes.find do |klass|
    Launchy.log "#{self.name} : Trying #{klass.name}"
    if klass.handle?(*args) then
      true
    else
      false
    end
  end
end
find_executable(bin,*paths) click to toggle source

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

# File lib/launchy/application.rb, line 31
def find_executable(bin,*paths)
  paths = ENV['PATH'].split(File::PATH_SEPARATOR) if paths.empty?
  paths.each do |path|
    file = File.join(path,bin)
    if File.executable?(file) then
      Launchy.log "#{self.name} : found executable #{file}"
      return file
    end
  end
  Launchy.log "#{self.name} : Unable to find `#{bin}' in #{paths.join(', ')}"
  return nil
end
inherited(sub_class) click to toggle source
# File lib/launchy/application.rb, line 10
def inherited(sub_class)
  application_classes << sub_class
end
known_os_families() click to toggle source
# File lib/launchy/application.rb, line 6
def known_os_families
  @known_os_families ||= [ :windows, :darwin, :nix, :cygwin, :testing ]
end
my_os() click to toggle source

return the current 'host_os' string from ruby's configuration

# File lib/launchy/application.rb, line 45
def my_os
  if ENV['LAUNCHY_HOST_OS'] then
    Launchy.log "#{self.name} : Using LAUNCHY_HOST_OS override of '#{ENV['LAUNCHY_HOST_OS']}'"
    return ENV['LAUNCHY_HOST_OS']
  else
    ::Config::CONFIG['host_os']
  end
end
my_os_family(test_os = my_os) click to toggle source

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

# File lib/launchy/application.rb, line 55
def my_os_family(test_os = my_os)
  case test_os
  when %rmingw/
    family = :windows
  when %rmswin/
    family = :windows
  when %rwindows/
    family = :windows
  when %rdarwin/
    family = :darwin
  when %rmac os/
    family = :darwin
  when %rsolaris/
    family = :nix
  when %rbsd/
    family = :nix
  when %rlinux/
    family = :nix
  when %raix/
    family = :nix
  when %rcygwin/
    family = :cygwin
  when %rtesting/
    family = :testing
  else
    $stderr.puts "Unknown OS familiy for '#{test_os}'.  Please report this bug to <jeremy at hinegardner dot org>"
    family = :unknown
  end
end

Public Instance Methods

app_list() click to toggle source

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.

# File lib/launchy/application.rb, line 125
def app_list
  @app_list ||= self.send("#{my_os_family}_app_list")
end
cygwin_app_list() click to toggle source

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

# File lib/launchy/application.rb, line 142
def cygwin_app_list
  Launchy.log "#{self.class.name} : Using 'cmd /C start' on windows."
  [ "cmd /C start" ]
end
darwin_app_list() click to toggle source

On darwin a good general default is the 'open' executable.

# File lib/launchy/application.rb, line 130
def darwin_app_list
  Launchy.log "#{self.class.name} : Using 'open' application on darwin."
  [ find_executable('open') ]
end
find_executable(bin,*paths) click to toggle source

find an executable in the available paths

# File lib/launchy/application.rb, line 108
def find_executable(bin,*paths)
  Application.find_executable(bin,*paths)
end
my_os() click to toggle source

return the current 'host_os' string from ruby's configuration

# File lib/launchy/application.rb, line 113
def my_os
  Application.my_os
end
my_os_family(test_os = my_os) click to toggle source

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

# File lib/launchy/application.rb, line 118
def my_os_family(test_os = my_os)
  Application.my_os_family(test_os)
end
nix_desktop_environment() click to toggle source

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

# File lib/launchy/application.rb, line 90
def nix_desktop_environment
  if not defined? @nix_desktop_environment then
    @nix_desktop_environment = :generic
    if ENV["KDE_FULL_SESSION"] || ENV["KDE_SESSION_UID"] then
      @nix_desktop_environment = :kde
    elsif ENV["GNOME_DESKTOP_SESSION_ID"] then
      @nix_desktop_environment = :gnome
    elsif find_executable("xprop") then
      if %x[ xprop -root _DT_SAVE_MODE | grep ' = \"xfce\"$' ].strip.size > 0 then
        @nix_desktop_environment = :xfce
      end
    end
    Launchy.log "#{self.class.name} : nix_desktop_environment => '#{@nix_desktop_environment}'"
  end
  return @nix_desktop_environment
end
run(cmd,*args) click to toggle source

run the command

# File lib/launchy/application.rb, line 153
def run(cmd,*args)
  Launchy.log "#{self.class.name} : Spawning on #{my_os_family} : #{cmd} #{args.inspect}"

  if my_os_family == :windows then
    # NOTE: the command is purposely omitted here because
    #       When "cmd /c start filename" is
    #       run, the shell interprets it as two commands:
    #       (1) "start" opens a new terminal, and (2)
    #       "filename" causes the file to be launched.
    system 'cmd', '/c', cmd, *args
  else
    # fork, and the child process should NOT run any exit handlers
    child_pid = fork do
      # NOTE: we pass a dummy argument *before*
      #       the actual command to prevent sh
      #       from silently consuming our actual
      #       command and assigning it to $0!
      dummy = ''
      system 'sh', '-c', '"$@" >/dev/null 2>&1', dummy, cmd, *args
      exit!
    end
    Process.detach(child_pid)
  end
end
testing_app_list() click to toggle source

used only for running tests

# File lib/launchy/application.rb, line 148
def testing_app_list
  []
end
windows_app_list() click to toggle source

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

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