Class: Gem::Commands::NewCommand::Skeleton

Inherits:
Object
  • Object
show all
Defined in:
lib/gem/commands/new_command/skeleton.rb

Defined Under Namespace

Classes: Processor

Constant Summary collapse

DefaultOptions =
{
  :verbose  => false,
  :silent   => false,
  :out      => $stdout,
}
Processors =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = nil) ⇒ Skeleton

Returns a new instance of Skeleton.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gem/commands/new_command/skeleton.rb', line 36

def initialize(path, options=nil)
  @name                 = File.basename(path)
  @options              = options ? DefaultOptions.merge(options) : 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_pathObject (readonly)

Returns the value of attribute base_path.



31
32
33
# File 'lib/gem/commands/new_command/skeleton.rb', line 31

def base_path
  @base_path
end

#directoriesObject (readonly)

Returns the value of attribute directories.



33
34
35
# File 'lib/gem/commands/new_command/skeleton.rb', line 33

def directories
  @directories
end

#filesObject (readonly)

Returns the value of attribute files.



34
35
36
# File 'lib/gem/commands/new_command/skeleton.rb', line 34

def files
  @files
end

#metaObject (readonly)

Returns the value of attribute meta.



30
31
32
# File 'lib/gem/commands/new_command/skeleton.rb', line 30

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/gem/commands/new_command/skeleton.rb', line 29

def name
  @name
end

#source_sliceObject (readonly)

Returns the value of attribute source_slice.



32
33
34
# File 'lib/gem/commands/new_command/skeleton.rb', line 32

def source_slice
  @source_slice
end

Class Method Details

.register_processor(suffix, name, &execute) ⇒ Object



18
19
20
# File 'lib/gem/commands/new_command/skeleton.rb', line 18

def self.register_processor(suffix, name, &execute)
  Processors[suffix] = Processor.new(suffix, name, execute)
end

Instance Method Details

#includesObject



53
54
55
# File 'lib/gem/commands/new_command/skeleton.rb', line 53

def includes
  @meta['includes'] || []
end

#materialize(in_path, env = {}, &on_collision) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gem/commands/new_command/skeleton.rb', line 57

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



97
98
99
# File 'lib/gem/commands/new_command/skeleton.rb', line 97

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gem/commands/new_command/skeleton.rb', line 101

def source_to_target_path_and_processors(source_path, target_dir, replacer)
  replaced_source   = replacer.replace(source_path[@source_slice])
  extname           = File.extname(replaced_source)
  processors        = []
  processed_source  = replaced_source

  if processor = Processors[extname] then
    processors      << processor
    processed_source = processed_source.chomp(extname)
  end
  target_path = File.join(target_dir, processed_source)
#       while processor = Processors[extname]
#         processors       << processor
#         processed_source  = File.basename(processed_source, extname)
#         extname           = File.extname(processed_source)
#       end

  [target_path, processors]
end