Class: Gem::Commands::NewCommand::Skeleton
- Defined in:
- lib/gem/commands/new_command/skeleton.rb
Overview
Preregistered processors
-
.erb: Interprets the file as ERB template, see rubys stdlib docs on ERB.
-
.stop: Stops the preprocessing chain, it’s advised to add that to all files.
-
.rb: Same as .stop
-
.yaml: Same as .stop
-
.html: Same as .stop
-
.js: Same as .stop
-
.png: Same as .stop
-
.jpg: Same as .stop
-
.gif: Same as .stop
Defined Under Namespace
Classes: Processor
Constant Summary collapse
- DefaultOptions =
{ :verbose => false, :silent => false, :out => $stdout, }
- Processors =
{ '.stop' => nil, '.rb' => nil, '.yaml' => nil, '.html' => nil, '.js' => nil, '.png' => nil, '.jpg' => nil, '.gif' => nil, }
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#directories ⇒ Object
readonly
Returns the value of attribute directories.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source_slice ⇒ Object
readonly
Returns the value of attribute source_slice.
Class Method Summary collapse
Instance Method Summary collapse
- #includes ⇒ Object
-
#initialize(path, options = nil) ⇒ Skeleton
constructor
A new instance of Skeleton.
- #materialize(in_path, env = {}, &on_collision) ⇒ Object
- #source_to_target_path(source_path, target_dir, replacer) ⇒ Object
- #source_to_target_path_and_processors(source_path, target_dir, replacer) ⇒ Object
Constructor Details
#initialize(path, options = nil) ⇒ Skeleton
Returns a new instance of Skeleton.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 56 def initialize(path, =nil) @name = File.basename(path) @options = ? DefaultOptions.merge() : DefaultOptions.dup @meta_path = File.join(path, 'meta.yaml') @meta = File.exist?(@meta_path) ? YAML.load_file(@meta_path) : {} @base_path = path @source_slice = (path.length+10)..-1 contents = Dir[File.join(path, "skeleton", "**", "{*,.gitkeep}")].sort @directories, @files = contents.partition { |path| File.directory?(path) } @verbose = @options.delete(:verbose) @silent = @options.delete(:silent) @out = @options.delete(:out) raise ArgumentError, "Unknown options: #{@options.keys.join(', ')}" unless @options.empty? end |
Instance Attribute Details
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
51 52 53 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 51 def base_path @base_path end |
#directories ⇒ Object (readonly)
Returns the value of attribute directories.
53 54 55 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 53 def directories @directories end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
54 55 56 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 54 def files @files end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
50 51 52 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 50 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
49 50 51 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 49 def name @name end |
#source_slice ⇒ Object (readonly)
Returns the value of attribute source_slice.
52 53 54 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 52 def source_slice @source_slice end |
Class Method Details
.register_processor(suffix, name, &execute) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 38 def self.register_processor(suffix, name, &execute) raise ArgumentError, "A processor named #{suffix.inspect} is already registered" if Processors[suffix] raise TypeError, "Processor name must be a String, but is #{suffix.class}:#{suffix.inspect}" unless suffix.is_a?(String) Processors[suffix] = Processor.new(suffix, name, execute) end |
Instance Method Details
#includes ⇒ Object
73 74 75 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 73 def includes @meta['includes'] || [] end |
#materialize(in_path, env = {}, &on_collision) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 77 def materialize(in_path, env={}, &on_collision) target_slice = (in_path.length+1)..-1 path_replacer = StringReplacer.new(env[:path_vars] || {}) content_vars = env[:content_vars] || {} unless File.exist?(in_path) then info "Creating root '#{in_path}'" FileUtils.mkdir_p(in_path) end if @directories.empty? then info "No directories to create" else info "Creating directories" @directories.each do |source_dir_path| target_dir_path = source_to_target_path(source_dir_path, in_path, path_replacer) info " #{target_dir_path[target_slice]}" FileUtils.mkdir_p(target_dir_path) end end if @files.empty? then info "No files to create" else info "Creating files" @files.each do |source_file_path| target_file_path, processors = source_to_target_path_and_processors(source_file_path, in_path, path_replacer) content = processors.inject(File.read(source_file_path)) { |data, processor| processor.execute.call(data, content_vars) } info " #{target_file_path[target_slice]}" if !File.exist?(target_file_path) || (block_given? && yield(target_file_path, content)) then File.open(target_file_path, 'wb') do |fh| fh.write(content) end end end end end |
#source_to_target_path(source_path, target_dir, replacer) ⇒ Object
117 118 119 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 117 def source_to_target_path(source_path, target_dir, replacer) File.join(target_dir, replacer.replace(source_path[@source_slice])) end |
#source_to_target_path_and_processors(source_path, target_dir, replacer) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gem/commands/new_command/skeleton.rb', line 121 def source_to_target_path_and_processors(source_path, target_dir, replacer) replaced_source = replacer.replace(source_path[@source_slice]) processed_source = replaced_source.dup processors = [] while processor = extract_processor_from_path(processed_source) processors << processor end target_path = File.join(target_dir, processed_source) [target_path, processors] end |