Class: MiddlemanEmberScaffold::Cli::Base

Inherits:
Thor
  • Object
show all
Defined in:
lib/middleman_ember_scaffold/cli.rb

Overview

The base task from which everything else etends

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Intercept missing methods and search subtasks for them

Parameters:

  • meth (Symbol)


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
94
95
96
97
# File 'lib/middleman_ember_scaffold/cli.rb', line 66

def method_missing(meth, *args)
  unless meth.to_s.include?(":")
    meth = meth.to_s
    myclass = meth.to_s
  else
    parts = meth.to_s.split(":")
    meth = parts[1]
    myclass = parts[0]
  end

  if self.class.map.has_key?(meth)
    meth = self.class.map[meth]
  end

  klass, task = Thor::Util.find_class_and_task_by_namespace("#{myclass}:#{meth}")

  if klass.nil?
    tasks_dir = File.join(Dir.pwd, "tasks")

    if File.exists?(tasks_dir)
      Dir[File.join(tasks_dir, "**/*_task.rb")].each { |f| require f }
      klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
    end
  end

  if klass.nil?
    raise Thor::Error.new "There's no '#{meth}' command for mes. Try 'mes help' for a list of commands."
  else
    args.unshift(task) if task
    klass.start(args, :shell => self.shell)
  end
end

Class Method Details

.start(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/middleman_ember_scaffold/cli.rb', line 13

def start(*args)
  # Change flag to a module
  ARGV.unshift("help") if ARGV.delete("--help")

  unless ARGV[1].nil? || ARGV[1].start_with?("-")
  	ARGV.push ARGV[1]
  	ARGV[1] = '-p'
  end

  unless ARGV[3].nil? || ARGV[3].start_with?("-")
    ARGV.push ARGV[3]
    ARGV[3] = '-f'
  end

  # Default command is server
  if ARGV[0] != "help" && (ARGV.length < 1 || ARGV.first.include?("-"))
    ARGV.unshift("help")
  end
  super
end

Instance Method Details

#help(meth = nil, subcommand = false) ⇒ void

This method returns an undefined value.

Override the Thor help method to find help for subtasks

Parameters:

  • meth (Symbol, String, nil) (defaults to: nil)
  • subcommand (Boolean) (defaults to: false)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/middleman_ember_scaffold/cli.rb', line 47

def help(meth = nil, subcommand = false)
  if meth && !self.respond_to?(meth)
    klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}")
    klass.start(["-h", task].compact, :shell => self.shell)
  else
    list = []
    Thor::Util.thor_classes_in(MiddlemanEmberScaffold::Cli).each do |thor_class|
      list += thor_class.printable_tasks(false)
    end
    list.sort!{ |a,b| a[0] <=> b[0] }

    shell.say "Tasks:"
    shell.print_table(list, :ident => 2, :truncate => true)
    shell.say
  end
end

#versionObject



36
37
38
39
# File 'lib/middleman_ember_scaffold/cli.rb', line 36

def version
  require 'middleman_ember_scaffold/version'
  say "mes #{MiddlemanEmberScaffold::VERSION}"
end