Class: Perennial::Generator

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

Defined Under Namespace

Classes: CommandEnv, HashBinding

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Generator.



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

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.



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

def destination_path
  @destination_path
end

#template_pathObject

Returns the value of attribute template_path.



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

def template_path
  @template_path
end

Instance Method Details

#chmod(permissions, path) ⇒ Object



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

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)


77
78
79
80
# File 'lib/perennial/generator.rb', line 77

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

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



89
90
91
92
# File 'lib/perennial/generator.rb', line 89

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

#executable?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/perennial/generator.rb', line 84

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

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



101
102
103
104
105
106
107
108
# File 'lib/perennial/generator.rb', line 101

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)


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

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

#folders(*args) ⇒ Object



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

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



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

def fu
  FileUtils
end

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



110
111
112
113
114
115
# File 'lib/perennial/generator.rb', line 110

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