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/hooks_generator.rb,
lib/charyf/utils/generators/skill/skill_generator.rb,
lib/charyf/utils/generators/installers/installers_generator.rb
Defined Under Namespace
Modules: ActionMethods, Actions, Defaults Classes: ARGVScrubber, AppBase, AppBuilder, AppGenerator, Base, Error, InstallersGenerator, NamedBase, SkillGenerator, SkillHooksGenerator
Constant Summary collapse
- REMOVED_GENERATORS =
%w(app cli_app skill_hooks installers)
- DEFAULT_ALIASES =
{}
- DEFAULT_OPTIONS =
{ charyf: { # Skill hooks: true => allows automatic call for all hooks when generating new skill skill_hooks: true } }
- CHARYF_DEV_PATH =
File.('../../../../..', __dir__)
- RESERVED_NAMES =
%w[application skill plugin runner test generator install new]
Class Method Summary collapse
-
.aliases ⇒ Object
:nodoc:.
-
.configure!(config) ⇒ Object
:nodoc:.
-
.fallbacks ⇒ Object
Hold configured generators fallbacks.
-
.find_by_namespace(name, base = nil, context = nil) ⇒ Object
Charyf finds namespaces similar to Thor, it only adds one rule:.
-
.help(command = "generate") ⇒ Object
Show help message with available generators.
-
.hidden_namespaces ⇒ Object
Returns an array of generator namespaces that are hidden.
- .hide_namespaces(*namespaces) ⇒ Object (also: hide_namespace)
-
.invoke(namespace, args = ARGV, config = {}) ⇒ Object
Receives a namespace, arguments and the behavior to invoke the generator.
-
.options ⇒ Object
:nodoc:.
- .print_generators ⇒ Object
- .public_namespaces ⇒ Object
- .sorted_groups ⇒ Object
Methods included from Command::Behavior
Class Method Details
.aliases ⇒ Object
:nodoc:
26 27 28 |
# File 'lib/charyf/utils/generators.rb', line 26 def aliases #:nodoc: @aliases ||= DEFAULT_ALIASES.dup end |
.configure!(config) ⇒ Object
:nodoc:
86 87 88 89 |
# File 'lib/charyf/utils/generators.rb', line 86 def configure!(config) #:nodoc: .deep_merge! config., union_arrays: true hide_namespaces(*config.hidden_namespaces) end |
.fallbacks ⇒ Object
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
163 164 165 |
# File 'lib/charyf/utils/generators.rb', line 163 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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/charyf/utils/generators.rb', line 128 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/charyf/utils/generators.rb', line 47 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_namespaces ⇒ Object
Returns an array of generator namespaces that are hidden. Generator namespaces may be hidden for a variety of reasons.
36 37 38 |
# File 'lib/charyf/utils/generators.rb', line 36 def hidden_namespaces @hidden_namespaces ||= [] end |
.hide_namespaces(*namespaces) ⇒ Object Also known as: hide_namespace
40 41 42 |
# File 'lib/charyf/utils/generators.rb', line 40 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/charyf/utils/generators.rb', line 66 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 = sorted_groups.flat_map(&:last) suggestions = .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 |
.options ⇒ Object
:nodoc:
30 31 32 |
# File 'lib/charyf/utils/generators.rb', line 30 def #:nodoc: @options ||= DEFAULT_OPTIONS.dup end |
.print_generators ⇒ Object
91 92 93 |
# File 'lib/charyf/utils/generators.rb', line 91 def print_generators sorted_groups.each { |b, n| print_list(b, n) } end |
.public_namespaces ⇒ Object
114 115 116 |
# File 'lib/charyf/utils/generators.rb', line 114 def public_namespaces subclasses.map(&:namespace) end |
.sorted_groups ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/charyf/utils/generators.rb', line 95 def sorted_groups namespaces = public_namespaces namespaces.sort! hidden_namespaces.each { |n| namespaces.delete(n.to_s) } 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) } [[ "charyf", charyf ]] + groups.sort.to_a end |