Class: Nugem::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/nugem/cli/cli_jekyll.rb,
lib/nugem/cli.rb,
lib/nugem/git.rb,
lib/nugem/cli/cli_gem.rb,
lib/nugem/cli/cli_rails.rb,
lib/nugem/cli/jekyll_demo.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#block_nameObject (readonly)

These declarations make the class instance variable values available as an accessor, which is necessary to name template files that are named ‘%variable_name%.extension’. See www.rubydoc.info/gems/thor/Thor/Actions#directory-instance_method



16
17
18
# File 'lib/nugem/cli.rb', line 16

def block_name
  @block_name
end

#class_nameObject

Returns the value of attribute class_name.



8
9
10
# File 'lib/nugem/cli/cli_jekyll.rb', line 8

def class_name
  @class_name
end

#filter_nameObject (readonly)

These declarations make the class instance variable values available as an accessor, which is necessary to name template files that are named ‘%variable_name%.extension’. See www.rubydoc.info/gems/thor/Thor/Actions#directory-instance_method



16
17
18
# File 'lib/nugem/cli.rb', line 16

def filter_name
  @filter_name
end

#filter_paramsObject

Returns the value of attribute filter_params.



8
9
10
# File 'lib/nugem/cli/cli_jekyll.rb', line 8

def filter_params
  @filter_params
end

#gem_nameObject

Surround gem_name with percent symbols when using the property to name files within the template directory For example: “generated/%gem_name%”



46
47
48
# File 'lib/nugem/cli.rb', line 46

def gem_name
  @gem_name
end

#generator_nameObject (readonly)

These declarations make the class instance variable values available as an accessor, which is necessary to name template files that are named ‘%variable_name%.extension’. See www.rubydoc.info/gems/thor/Thor/Actions#directory-instance_method



16
17
18
# File 'lib/nugem/cli.rb', line 16

def generator_name
  @generator_name
end

#tag_nameObject (readonly)

These declarations make the class instance variable values available as an accessor, which is necessary to name template files that are named ‘%variable_name%.extension’. See www.rubydoc.info/gems/thor/Thor/Actions#directory-instance_method



16
17
18
# File 'lib/nugem/cli.rb', line 16

def tag_name
  @tag_name
end

#test_frameworkObject (readonly)

These declarations make the class instance variable values available as an accessor, which is necessary to name template files that are named ‘%variable_name%.extension’. See www.rubydoc.info/gems/thor/Thor/Actions#directory-instance_method



16
17
18
# File 'lib/nugem/cli.rb', line 16

def test_framework
  @test_framework
end

#trailing_argsObject

Returns the value of attribute trailing_args.



8
9
10
# File 'lib/nugem/cli/cli_jekyll.rb', line 8

def trailing_args
  @trailing_args
end

#trailing_dumpObject

Returns the value of attribute trailing_dump.



8
9
10
# File 'lib/nugem/cli/cli_jekyll.rb', line 8

def trailing_dump
  @trailing_dump
end

#trailing_paramsObject

Returns the value of attribute trailing_params.



8
9
10
# File 'lib/nugem/cli/cli_jekyll.rb', line 8

def trailing_params
  @trailing_params
end

Class Method Details

.add_demo_example(tag, params, tag_type = :tag) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nugem/cli/jekyll_demo.rb', line 24

def self.add_demo_example(tag, params, tag_type = :tag)
  last_tag = ''
  examples = combinations(params).map do |option|
    options = option.join ' '
    label = options.empty? ? ' (invoked without parameters)' : options
    close_tag = case tag_type
                when :tag then ''
                when :block then "                  \\nThis is line 1 of the block content.<br>\n                  This is line 2.\n                  {% end\#{tag} %}\n                END_BLOCK\n                end\n    example = <<~END_EX\n      <!-- #region \#{tag} \#{label} -->\n      <h3 id=\"\#{tag}\" class=\"code\">\#{tag} \#{label}</h3>\n      {% \#{tag} \#{options} %}\#{close_tag}\n      <!-- endregion -->\n    END_EX\n    if tag == last_tag\n      example\n    else\n      last_tag = tag\n      \"<h2 id=\\\"tag_\#{tag}\\\" class='code'>\#{tag}</h2>\\n\" + example\n    end\n  end\n  examples.join(\"\\n\\n\")\nend\n"

.add_filter_example(filter_name, trailing_params) ⇒ Object



53
54
55
56
57
58
# File 'lib/nugem/cli/jekyll_demo.rb', line 53

def self.add_filter_example(filter_name, trailing_params)
  "    <h2 id=\"filter_\#{filter_name}\" class='code'>\#{filter_name}</h2>\n    {{ \"TODO: Provide filter input here\" | \#{filter_name}\#{trailing_params} }}\n  END_EX\nend\n"

.combinations(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nugem/cli/jekyll_demo.rb', line 5

def self.combinations(params)
  (0..params.length).flat_map do |n|
    params.combination(n).map do |param|
      next [] if param.empty?

      param.flat_map do |p|
        name = p.first
        type = p[1]
        case type
        when 'boolean' then name
        when 'string' then "#{name}='somevalue'"
        when 'numeric' then "#{name}=1234"
        else "#{name} has unknown type: #{type}"
        end
      end
    end
  end
end

.exit_on_failure?Boolean

Return a non-zero status code on error. See github.com/rails/thor/issues/244

Returns:

  • (Boolean)


49
50
51
# File 'lib/nugem/cli.rb', line 49

def self.exit_on_failure?
  true
end

.source_rootObject

Returns Path to the Thor generator templates.

Returns:

  • Path to the Thor generator templates



54
55
56
# File 'lib/nugem/cli.rb', line 54

def self.source_root
  File.expand_path '../../templates', __dir__
end

.test_option(default_value) ⇒ Object



58
59
60
61
62
# File 'lib/nugem/cli.rb', line 58

def self.test_option(default_value)
  method_option :test_framework, type: :string, default: default_value,
    enum: %w[minitest rspec],
    desc: "Use rspec or minitest for the test framework (default is #{default_value})."
end

.todoObject



64
65
66
# File 'lib/nugem/cli.rb', line 64

def self.todo
  'TODO: ' if @todos
end

Instance Method Details

#gem(gem_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nugem/cli/cli_gem.rb', line 20

def gem(gem_name)
  # puts set_color("gem_name=#{gem_name}", :yellow)
  super if gem_name.empty?

  @executable = options[:executable]
  @host           = options[:host] # FIXME: conflicts with @host in create_gem_scaffold()
  @out_dir        = options[:out_dir]
  @private        = options[:private]
  @test_framework = options[:test_framework]
  @yes            = options[:yes]

  @dir = Nugem.dest_root @out_dir, gem_name

  create_plain_scaffold gem_name
  initialize_repository gem_name
end

#jekyll(gem_name) ⇒ Object



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
# File 'lib/nugem/cli/cli_jekyll.rb', line 39

def jekyll(gem_name)
  # options is frozen, so it cannot be modified.
  # extracted values are ignored in the case statement below instead.
  @host           = options['host']
  @out_dir        = options['out_dir']
  @private        = options['private']
  @test_framework = options['test_framework']
  @todos          = options['todos']
  @yes            = options['yes']

  @gem_name   = gem_name
  @dir        = Nugem.dest_root @out_dir, @gem_name
  @class_name = Nugem.camel_case @gem_name
  @jekyll     = true
  @rspec      = true

  create_plain_scaffold @gem_name
  create_jekyll_scaffold
  options.each do |option|
    case option.first
    when 'block'     then option[1].each { |name| create_jekyll_block_scaffold        name }
    when 'blockn'    then option[1].each { |name| create_jekyll_block_no_arg_scaffold name }
    when 'filter'    then option[1].each { |name| create_jekyll_filter_scaffold       name }
    when 'generator' then option[1].each { |name| create_jekyll_generator_scaffold    name }
    when 'tag'       then option[1].each { |name| create_jekyll_tag_scaffold          name }
    when 'tagn'      then option[1].each { |name| create_jekyll_tag_no_arg_scaffold   name }
    when 'hooks'     then create_jekyll_hooks_scaffold option[1]
    when 'host', 'executable', 'out_dir', 'private', 'test_framework', 'todos', 'quiet' then next
    else puts "Warning: Unrecognized option: #{option}"
    end
  end

  initialize_repository @gem_name
end

#rails(gem_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nugem/cli/cli_rails.rb', line 22

def rails(gem_name)
  @host           = options['host']
  @mountable      = options['mountable']
  @private        = options['private']
  @test_framework = options['test_framework']
  @yes            = options['yes']

  @dir = Nugem.dest_root @out_dir, gem_name
  @engine = @host || @mountable
  @rails = true

  create_plain_scaffold gem_name
  create_rails_scaffold gem_name
  create_engine_scaffold gem_name if @engine
  create_mountable_scaffold gem_name if @mountable
  initialize_repository gem_name
end