Class: Sprout::Generator::FileManifest

Inherits:
Manifest
  • Object
show all
Defined in:
sprout/lib/sprout/generator/file_manifest.rb

Direct Known Subclasses

TemplateManifest

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Manifest

#say

Instance Attribute Details

- (Object) template

Returns the value of attribute template



4
5
6
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 4

def template
  @template
end

- (Object) templates

Returns the value of attribute templates



5
6
7
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 5

def templates
  @templates
end

Instance Method Details

- (Object) create



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 7

def create
  content = resolve_template

  if File.exists?(path)
    if generator.force
      write_file path, content
      say "Replaced file:     #{path}"
      true
    else
      say "Skipped file:      #{path}"
      false
    end
  else
    write_file path, content
    say "Created file:      #{path}"
    true
  end
end

- (Object) destroy



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 26

def destroy
  if !File.exists?(path)
    say "Skipped remove missing file: #{path}"
    return true
  end
  expected_content = resolve_template
  actual_content = File.read path
  if generator.force || actual_content == expected_content
    FileUtils.rm path
    say "Removed file:                #{path}"
    true
  else
    say "Skipped remove file:         #{path}"
    false
  end
end

- (Object) read_source (protected)



55
56
57
58
59
60
61
62
63
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 55

def read_source
  templates.each do |template_path|
    path = File.join template_path, source_name
    if File.exists?(path)
      return File.read path
    end
  end
  raise Sprout::Errors::MissingTemplateError.new "Could not find template (#{source_name}) in any of the following paths:\n\n    (#{templates.inspect})\n\n"
end

- (Object) resolve_template (protected)



51
52
53
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 51

def resolve_template
  read_source
end

- (Object) source_name (protected)



65
66
67
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 65

def source_name
  template || File.basename(path)
end

- (Object) write_file(path, content) (protected)



45
46
47
48
49
# File 'sprout/lib/sprout/generator/file_manifest.rb', line 45

def write_file path, content
  File.open path, 'w+' do |file|
    file.write content
  end
end