Class: Thor::Runner

Inherits:
Thor
  • Object
show all
Defined in:
lib/thor/runner.rb

Constant Summary

Constants inherited from Thor

HELP_MAPPINGS, THOR_RESERVED_WORDS

Instance Attribute Summary

Attributes included from Base

#options

Instance Method Summary collapse

Methods inherited from Thor

default_task, desc, help, install_task, map, method_option, method_options, package_task, spec_task, start

Methods included from Base

included, #initialize, register_klass_file, shell, shell=, subclass_files, subclasses

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

If a task is not found on Thor::Runner, method missing is invoked and Thor::Runner is then responsable for finding the task in all classes.



25
26
27
28
29
30
31
# File 'lib/thor/runner.rb', line 25

def method_missing(meth, *args)
  meth = meth.to_s
  initialize_thorfiles(meth)
  klass, task = Thor::Util.namespace_to_thor_class(meth)
  args.unshift(task) if task
  klass.start(args, :shell => shell)
end

Instance Method Details

#help(meth = nil) ⇒ Object

Override Thor#help so it can give information about any class and any method.



12
13
14
15
16
17
18
19
20
# File 'lib/thor/runner.rb', line 12

def help(meth=nil)
  if meth && !self.respond_to?(meth)
    initialize_thorfiles(meth)
    klass, task = Thor::Util.namespace_to_thor_class(meth)
    klass.start(["-h", task].compact, :shell => self.shell) # send mapping -h because it works with Thor::Group too
  else
    super
  end
end

#install(name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/thor/runner.rb', line 35

def install(name)
  initialize_thorfiles

  # If a directory name is provided as the argument, look for a 'main.thor' 
  # task in said directory.
  begin
    if File.directory?(File.expand_path(name))
      base, package = File.join(name, "main.thor"), :directory
      contents      = open(base).read
    else
      base, package = name, :file
      contents      = open(name).read
    end
  rescue OpenURI::HTTPError
    raise Error, "Error opening URI '#{name}'"
  rescue Errno::ENOENT
    raise Error, "Error opening file '#{name}'"
  end

  say "Your Thorfile contains:"
  say contents

  return false if no?("Do you wish to continue [y/N]?")

  as = options["as"] || begin
    first_line = contents.split("\n")[0]
    (match = first_line.match(/\s*#\s*module:\s*([^\n]*)/)) ? match[1].strip : nil
  end

  unless as
    basename = File.basename(name)
    as = ask("Please specify a name for #{name} in the system repository [#{basename}]:")
    as = basename if as.empty?
  end

  location = if options[:relative] || name =~ /^http:\/\//
    name
  else
    File.expand_path(name)
  end

  thor_yaml[as] = {
    :filename   => Digest::MD5.hexdigest(name + as),
    :location   => location,
    :namespaces => Thor::Util.namespaces_in_contents(contents, base)
  }

  save_yaml(thor_yaml)
  say "Storing thor file in your system repository"
  destination = File.join(thor_root, thor_yaml[as][:filename])

  if package == :file
    File.open(destination, "w") { |f| f.puts contents }
  else
    FileUtils.cp_r(name, destination)
  end

  thor_yaml[as][:filename] # Indicate success
end

#installedObject



124
125
126
127
128
129
130
131
# File 'lib/thor/runner.rb', line 124

def installed
  initialize_thorfiles(nil, true)

  klasses = Thor::Base.subclasses
  klasses -= [Thor, Thor::Runner] unless options["internal"]

  display_klasses(true, klasses)
end

#list(search = "") ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/thor/runner.rb', line 136

def list(search="")
  initialize_thorfiles

  search = ".*#{search}" if options["substring"]
  search = /^#{search}.*/i
  group  = options[:group] || "standard"

  klasses = Thor::Base.subclasses.select do |k|
    (options[:all] || k.group == group) && k.namespace =~ search
  end

  display_klasses(false, klasses)
end

#uninstall(name) ⇒ Object

Raises:



96
97
98
99
100
101
102
103
104
105
# File 'lib/thor/runner.rb', line 96

def uninstall(name)
  raise Error, "Can't find module '#{name}'" unless thor_yaml[name]
  say "Uninstalling #{name}."
  FileUtils.rm_rf(File.join(thor_root, "#{thor_yaml[name][:filename]}"))

  thor_yaml.delete(name)
  save_yaml(thor_yaml)

  puts "Done."
end

#update(name) ⇒ Object

Raises:



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/thor/runner.rb', line 108

def update(name)
  raise Error, "Can't find module '#{name}'" if !thor_yaml[name] || !thor_yaml[name][:location]

  say "Updating '#{name}' from #{thor_yaml[name][:location]}"

  old_filename = thor_yaml[name][:filename]
  self.options = self.options.merge("as" => name)
  filename     = install(thor_yaml[name][:location])

  unless filename == old_filename
    File.delete(File.join(thor_root, old_filename))
  end
end