Class: Perennial::Generator

Inherits:
Object show all
Defined in:
lib/perennial/generator.rb

Defined Under Namespace

Classes: CommandEnv

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination, opts = {}) ⇒ Generator

Returns a new instance of Generator.



33
34
35
36
37
38
# File 'lib/perennial/generator.rb', line 33

def initialize(destination, opts = {})
  @destination_path = destination
  @template_path    = opts[:template_path] || File.join(Settings.library_root, "templates")
  @silent = !!opts[:silent]
  describe "Initializing generator in #{destination}"
end

Instance Attribute Details

#destination_pathObject

Returns the value of attribute destination_path.



31
32
33
# File 'lib/perennial/generator.rb', line 31

def destination_path
  @destination_path
end

#template_pathObject

Returns the value of attribute template_path.



31
32
33
# File 'lib/perennial/generator.rb', line 31

def template_path
  @template_path
end

Instance Method Details

#chmod(permissions, path) ⇒ Object



46
47
48
49
# File 'lib/perennial/generator.rb', line 46

def chmod(permissions, path)
  describe "Changing permissions for #{path} to #{permissions}"
  FileUtils.chmod(permissions, expand_destination_path(path))
end

#directory?(path) ⇒ Boolean Also known as: folder?

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/perennial/generator.rb', line 61

def directory?(path)
  describe "Checking if #{path} is a directory"
  File.directory?(expand_destination_path(path))
end

#download(from, to, append = false) ⇒ Object



73
74
75
76
# File 'lib/perennial/generator.rb', line 73

def download(from, to, append = false)
  describe "Downloading #{from}"
  file to, open(from).read, append
end

#executable?(path) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/perennial/generator.rb', line 56

def executable?(path)
  describe "Checking if #{path} is an executable"
  File.executable?(expand_destination_path(path))
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/perennial/generator.rb', line 68

def exists?(path)
  describe "Checking if #{path} exists"
  File.exits?(expand_destination_path(path))
end

#file(name, contents, append = false) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/perennial/generator.rb', line 85

def file(name, contents, append = false)
  dest_folder = File.dirname(name)
  folders(dest_folder) unless File.directory?(expand_destination_path(dest_folder))
  describe "Creating file #{name}"
  File.open(expand_destination_path(name), "#{append ? "a" : "w"}+") do |f|
    f.write(contents)
  end
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/perennial/generator.rb', line 51

def file?(path)
  describe "Checking if #{path} is a file"
  File.file?(expand_destination_path(path))
end

#folders(*args) ⇒ Object



78
79
80
81
82
83
# File 'lib/perennial/generator.rb', line 78

def folders(*args)
  args.each do |f|
    describe "Creating folder #{f}"
    FileUtils.mkdir_p(expand_destination_path(f))
  end
end

#fuObject

Helpers for testing file state



42
43
44
# File 'lib/perennial/generator.rb', line 42

def fu
  FileUtils
end

#template(source, destination, environment = {}, append = false) ⇒ Object



94
95
96
97
98
99
# File 'lib/perennial/generator.rb', line 94

def template(source, destination, environment = {}, append = false)
  describe "Processing template #{source}"
  raw_template = File.read(expand_template_path(source))
  processed_template = ERB.new(raw_template).result(binding_for(environment))
  file destination, processed_template, append
end