Class: Forge::Generator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, layout = 'default') ⇒ Generator

Returns a new instance of Generator.



11
12
13
14
15
# File 'lib/forge/generator.rb', line 11

def initialize(project, layout='default')
  @project = project
  @task    = project.task
  @layout  = layout
end

Class Method Details

.run(project, layout = 'default') ⇒ Object



5
6
7
8
# File 'lib/forge/generator.rb', line 5

def run(project, layout='default')
  generator = self.new(project, layout)
  generator.run
end

Instance Method Details

#copy_functionsObject



70
71
72
73
74
75
# File 'lib/forge/generator.rb', line 70

def copy_functions
  source = File.expand_path(File.join(self.layout_path, 'functions', 'functions.php.erb'))
  target = File.expand_path(File.join(@project.source_path, 'functions', 'functions.php'))

  write_template(source, target)
end

#copy_javascriptObject



52
53
54
55
56
57
58
59
# File 'lib/forge/generator.rb', line 52

def copy_javascript
  source = File.expand_path(File.join(self.layout_path, 'javascripts'))
  target = File.expand_path(File.join(@project.assets_path, 'javascripts'))

  render_directory(source, target)

  self
end

#copy_stylesheetsObject



43
44
45
46
47
48
49
50
# File 'lib/forge/generator.rb', line 43

def copy_stylesheets
  source = File.expand_path(File.join(self.layout_path, 'stylesheets'))
  target = File.expand_path(File.join(@project.assets_path, 'stylesheets'))

  render_directory(source, target)

  self
end

#copy_templatesObject



61
62
63
64
65
66
67
68
# File 'lib/forge/generator.rb', line 61

def copy_templates
  source = File.expand_path(File.join(self.layout_path, 'templates'))
  target = File.expand_path(File.join(@project.source_path, 'templates'))

  render_directory(source, target)

  self
end

#create_structureObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/forge/generator.rb', line 17

def create_structure
  # Create the build directory for Forge output
  @task.empty_directory @project.build_path

  source_paths = [
    ['assets', 'images'],
    ['assets', 'javascripts'],
    ['assets', 'stylesheets'],
    ['assets', 'lib'],

    ['functions'],

    ['includes'],

    ['templates', 'pages'],
    ['templates', 'partials'],
  ]

  # Build out Forge structure in the source directory
  source_paths.each do |path|
    @task.empty_directory File.join(@project.source_path, path)
  end

  self
end

#layout_pathObject



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

def layout_path
  @layout_path ||= File.join(Forge::ROOT, 'layouts', @layout)
end

#runObject



81
82
83
84
85
86
87
88
89
# File 'lib/forge/generator.rb', line 81

def run
  write_config
  create_structure
  copy_stylesheets
  copy_javascript
  copy_templates
  copy_functions
  return self
end

#write_configObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/forge/generator.rb', line 91

def write_config
  unless File.exists?(@project.global_config_file)
    @task.shell.mute do
      @task.create_file(@project.global_config_file) do
        "# Place your global configuration values here\n# config[:livereload] = true"
      end
    end
  end

  write_template(['config', 'config.tt'], @project.config_file)

  self
end

#write_template(source, target) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/forge/generator.rb', line 105

def write_template(source, target)
  source   = File.join(source)
  template = File.expand_path(@task.find_in_source_paths((source)))
  target   = File.expand_path(File.join(target))

  @task.create_file target do
    @project.parse_erb(template)
  end
end