Class: New::Cli

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

option :name, required: true


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/new/cli.rb', line 7

def method_missing method, *args
  if New.templates.include? method.to_sym
    # Split args that look like options (i.e start with - or --) into a separate array
    positional_args, opts = Thor::Options.split args

    # extract name from args
    name = positional_args[0]
    raise Thor::RequiredArgumentMissingError unless name

    # Add all options here
    # Make sure to include required options up above as well so they show in the help menu
    parser = Thor::Options.new(
    #   name: Thor::Option.new(:name, required: true, type: :string)
    )

    # The options hash is frozen in #initialize so you need to merge and re-assign
    self.options = options.merge(parser.parse(opts)).freeze

    # Dispatch the command
    project method, name
  else
    super
  end
end

Instance Method Details

#initObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/new/cli.rb', line 33

def init
  if Dir.exists? New::CUSTOM_DIR
    New.say 'Home folder already exists.', type: :warn
  else
    # create folders
    New.say 'Creating home folder.', type: :success
    FileUtils.mkdir_p File.join(New::CUSTOM_DIR, New::TASKS_DIR_NAME)
    FileUtils.mkdir_p File.join(New::CUSTOM_DIR, New::TEMPLATES_DIR_NAME)

    # create config file
    New.say 'Creating default configuration file.', type: :success
    File.open File.join(New::CUSTOM_DIR, New::CONFIG_FILE), 'w' do |f|
      f.write New::Template::CUSTOM_CONFIG_TEMPLATE.deep_stringify_keys.to_yaml
    end

    New.say "Edit `#{File.join(New::CUSTOM_DIR, New::CONFIG_FILE)}` with your custom configuration details.", type: :warn
  end
end

#releaseObject


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
# File 'lib/new/cli.rb', line 53

def release
  project_config_file = File.join(Dir.pwd,  New::CONFIG_FILE)
  raise unless File.exists? project_config_file

  # get project config file
  project_config = YAML.load(File.open(project_config_file)).deep_symbolize_keys!

  # extract tasks from configuration
  tasks = (project_config[:tasks] || {}).keys.map(&:to_sym)

  tasks.each do |task|
    # require custom task and initialize it
    begin
      if New.custom_tasks.include? task
        require "#{New::CUSTOM_DIR}/#{New::TASKS_DIR_NAME}/#{task}/#{task}"
      else
        require "#{New::DEFAULT_DIR}/#{New::TASKS_DIR_NAME}/#{task}/#{task}"
      end
    rescue LoadError
      New.say "No task '#{task}' found!", type: :fail
      next
    end
    New.say ">>> Initializing #{task.to_s.humanize} Task...", type: :warn
    "New::Task::#{task.to_s.classify}".constantize.new project_config
  end
end