Module: Charyf::Generators

Includes:
Command::Behavior
Defined in:
lib/charyf/utils/generators.rb,
lib/charyf/utils/generator/base.rb,
lib/charyf/utils/generator/actions.rb,
lib/charyf/utils/generators/app_base.rb,
lib/charyf/utils/generators/defaults.rb,
lib/charyf/utils/generators/named_base.rb,
lib/charyf/utils/generators/app/app_generator.rb,
lib/charyf/utils/generators/skill/skill_generator.rb,
lib/charyf/utils/generators/intents/intents_generator.rb

Defined Under Namespace

Modules: ActionMethods, Actions, Defaults Classes: ARGVScrubber, AppBase, AppBuilder, AppGenerator, Base, Error, IntentsGenerator, NamedBase, SkillGenerator

Constant Summary collapse

REMOVED_GENERATORS =
%w(app cli_app intents)
DEFAULT_ALIASES =
{}
DEFAULT_OPTIONS =
{
    charyf: {
        intents: true
    }
}
CHARYF_DEV_PATH =
File.expand_path('../../../../..', __dir__)
RESERVED_NAMES =
%w[application skill plugin runner test generator install new]

Class Method Summary collapse

Methods included from Command::Behavior

included

Class Method Details

.aliasesObject

:nodoc:



25
26
27
# File 'lib/charyf/utils/generators.rb', line 25

def aliases #:nodoc:
  @aliases ||= DEFAULT_ALIASES.dup
end

.configure!(config) ⇒ Object

:nodoc:



85
86
87
88
# File 'lib/charyf/utils/generators.rb', line 85

def configure!(config) #:nodoc:
  options.deep_merge! config.options, union_arrays: true
  hide_namespaces(*config.hidden_namespaces)
end

.fallbacksObject

Hold configured generators fallbacks. If a plugin developer wants a generator group to fallback to another group in case of missing generators, they can add a fallback.

For example, shoulda is considered a test_framework and is an extension of test_unit. However, most part of shoulda generators are similar to test_unit ones.

Shoulda then can tell generators to search for test_unit generators when some of them are not available by adding a fallback:

Charyf::Generators.fallbacks[:shoulda] = :test_unit


162
163
164
# File 'lib/charyf/utils/generators.rb', line 162

def fallbacks
  @fallbacks ||= {}
end

.find_by_namespace(name, base = nil, context = nil) ⇒ Object

Charyf finds namespaces similar to Thor, it only adds one rule:

find_by_namespace :foo, :charyf, :bar

Will search for the following generators:

"charyf:foo", "foo:bar", "foo"

Notice that “charyf:generators:bar” could be loaded as well, what Charyf looks for is the first and last parts of the namespace.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/charyf/utils/generators.rb', line 127

def find_by_namespace(name, base = nil, context = nil) #:nodoc:
  lookups = []
  lookups << "#{base}:#{name}"    if base
  lookups << "#{name}:#{context}" if context

  unless base || context
    unless name.to_s.include?(?:)
      lookups << "#{name}:#{name}"
      lookups << "charyf:#{name}"
    end
    lookups << "#{name}"
  end

  namespaces = Hash[subclasses.map { |klass| [klass.namespace, klass] }]
  lookups.each do |namespace|

    klass = namespaces[namespace]
    return klass if klass
  end

  invoke_fallbacks_for(name, base) || invoke_fallbacks_for(context, name)
end

.help(command = "generate") ⇒ Object

Show help message with available generators.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/charyf/utils/generators.rb', line 46

def help(command = "generate")
  puts "Usage: charyf #{command} GENERATOR [args] [options]"
  puts
  puts "General options:"
  puts "  -h, [--help]     # Print generator's options and usage"
  puts "  -p, [--pretend]  # Run but do not make any changes"
  puts "  -f, [--force]    # Overwrite files that already exist"
  puts "  -s, [--skip]     # Skip files that already exist"
  puts "  -q, [--quiet]    # Suppress status output"
  puts
  puts "Please choose a generator below."
  puts

  print_generators
end

.hidden_namespacesObject

Returns an array of generator namespaces that are hidden. Generator namespaces may be hidden for a variety of reasons.



35
36
37
# File 'lib/charyf/utils/generators.rb', line 35

def hidden_namespaces
  @hidden_namespaces ||= []
end

.hide_namespaces(*namespaces) ⇒ Object Also known as: hide_namespace



39
40
41
# File 'lib/charyf/utils/generators.rb', line 39

def hide_namespaces(*namespaces)
  hidden_namespaces.concat(namespaces)
end

.invoke(namespace, args = ARGV, config = {}) ⇒ Object

Receives a namespace, arguments and the behavior to invoke the generator. It’s used as the default entry point for generate, destroy and update commands.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/charyf/utils/generators.rb', line 65

def invoke(namespace, args = ARGV, config = {})
  names = namespace.to_s.split(":")
  if klass = find_by_namespace(names.pop, names.any? && names.join(":"))
    args << "--help" if args.empty? && klass.arguments.any?(&:required?)
    klass.start(args, config)
  else
    options     = sorted_groups.flat_map(&:last)
    suggestions = options.sort_by { |suggested| levenshtein_distance(namespace.to_s, suggested) }.first(3)
    suggestions.map! { |s| "'#{s}'" }
    msg =  "Could not find generator '#{namespace}'. ".dup

    if suggestions.any?
      msg << "Maybe you meant #{ suggestions[0...-1].join(', ')} or #{suggestions[-1]}\n"
      msg << "Run `charyf generate --help` for more options."
    end

    puts msg
  end
end

.optionsObject

:nodoc:



29
30
31
# File 'lib/charyf/utils/generators.rb', line 29

def options #:nodoc:
  @options ||= DEFAULT_OPTIONS.dup
end


90
91
92
# File 'lib/charyf/utils/generators.rb', line 90

def print_generators
  sorted_groups.each { |b, n| print_list(b, n) }
end

.public_namespacesObject



113
114
115
# File 'lib/charyf/utils/generators.rb', line 113

def public_namespaces
  subclasses.map(&:namespace)
end

.sorted_groupsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/charyf/utils/generators.rb', line 94

def sorted_groups
  namespaces = public_namespaces
  namespaces.sort!

  groups = Hash.new { |h, k| h[k] = [] }
  namespaces.each do |namespace|
    base = namespace.split(":").first
    groups[base] << namespace
  end

  charyf = groups.delete("charyf") || []
  charyf.map! { |n| n.sub(/^charyf:/, "") }
  REMOVED_GENERATORS.map { |name| charyf.delete(name) }

  hidden_namespaces.each { |n| groups.delete(n.to_s) }

  [[ "charyf", charyf ]] + groups.sort.to_a
end