Class: RStack::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name) ⇒ Generator

Returns a new instance of Generator.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rstack/generator.rb', line 7

def initialize(project_name)
  @project_name           = project_name.downcase
  @pathized_project_name  = @project_name.to_const_path
  @module_name            = @pathized_project_name.camel_case
  @path                   = Pathname.new(@project_name)
  
  parts = @pathized_project_name.split("/")
  @main = parts.last + ".rb"
  @main_path = Pathname.new((parts - [parts.last]).join("/"))
  @templates_path = Pathname.new(RStack.root) / 'templates'     
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



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

def main
  @main
end

#main_pathObject (readonly)

Returns the value of attribute main_path.



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

def main_path
  @main_path
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



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

def module_name
  @module_name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#pathized_project_nameObject (readonly)

Returns the value of attribute pathized_project_name.



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

def pathized_project_name
  @pathized_project_name
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#templates_pathObject (readonly)

Returns the value of attribute templates_path.



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

def templates_path
  @templates_path
end

Instance Method Details

#authorObject



54
55
56
# File 'lib/rstack/generator.rb', line 54

def author
  ENV['USERNAME'] || ENV['USER'] || '[ENTER YOUR USERNAME]'
end

#create_directoriesObject



39
40
41
42
43
44
# File 'lib/rstack/generator.rb', line 39

def create_directories
  paths.values.each do |path|
    puts "creating #{path}"
    path.mkpath
  end
end

#move_template(template_name, destination_path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rstack/generator.rb', line 46

def move_template(template_name, destination_path)
  puts "moving #{template_name} to #{destination_path}"
  contents = File.read(templates_path + template_name)
  File.open(destination_path, 'w') do |f| 
    f << eval('"' + contents + '"')
  end
end

#pathsObject



19
20
21
22
23
24
25
26
27
# File 'lib/rstack/generator.rb', line 19

def paths
  {
    :bin      => path / "bin",
    :lib      => path / 'lib',
    :main     => path / "lib" / @main_path,
    :project  => path / "lib" / @pathized_project_name,
    :spec     => path / "spec"
  }
end

#runObject



29
30
31
32
33
34
35
36
37
# File 'lib/rstack/generator.rb', line 29

def run
  create_directories
  move_template "Rakefile",         path / "Rakefile"
  move_template "README.txt",       path / "README.txt"
  move_template "cruise_config.rb", path / "cruise_config.rb"      
  move_template "main.rb",          paths[:main] / @main
  move_template "version.rb",       paths[:project] / "version.rb"
  move_template "spec_helper.rb",   paths[:spec] / "spec_helper.rb"
end