Class: Qwandry::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/qwandry/launcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#editorObject

The default editor to be used by Qwandry#launch.



6
7
8
# File 'lib/qwandry/launcher.rb', line 6

def editor
  @editor
end

Instance Method Details

#find(*pattern) ⇒ Object

Searches all of the loaded repositories for ‘name`



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/qwandry/launcher.rb', line 9

def find(*pattern)
  # Create a glob pattern from the user's input, for instance
  # ["rails","2.3"] => "rails*2.3*"
  pattern = pattern.join('*')
  pattern << '*' unless pattern =~ /\*$/
  
  packages = []
  repositories = Qwandry::Configuration.repositories
  repositories.each do |repo|
    packages.concat(repo.scan(pattern))
  end
  
  differentiate packages
  packages
end

#launch(package, editor = nil) ⇒ Object

Launches a Package or path represented by a String. Unless ‘editor` will check against the environment by default.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/qwandry/launcher.rb', line 27

def launch(package, editor=nil)
  editor ||= @editor || ENV['QWANDRY_EDITOR'] || ENV['VISUAL'] || ENV['EDITOR']
  
  if (!editor) || (editor =~ /^\s*$/) # if the editor is not set, or is blank, exit with a message:
    puts "Please set QWANDRY_EDITOR, VISUAL or EDITOR, or pass in an editor to use"
    exit 1
  end
  
  paths = package.is_a?(String) ? [package] : package.paths
  # Editors may have options, 'mate -w' for instance
  editor_and_options = editor.strip.split(/\s+/)
  
  Dir.chdir(File.dirname paths.first) do
    # Launch the editor with its options and any paths that we have been passed
    system(*(editor_and_options + paths))
  end
end