Class: SparkEngine::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/spark_engine/scaffold.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Scaffold

Returns a new instance of Scaffold.



8
9
10
11
12
13
14
15
16
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
42
43
# File 'lib/spark_engine/scaffold.rb', line 8

def initialize(options)
  require 'erb'
  @options = options

  return new_component(@options) if @options[:component]

  @cwd = File.expand_path(File.dirname(@options[:name]))
  @gem = underscorize(File.basename(@options[:name]))
  @engine = underscorize(@options[:engine] || @gem)
  @namespace = @engine
  @plugin_module = modulize @engine
  @gem_module = modulize @gem
  @gem_temp = ".#{@gem}-temp"

  if Dir.exist?(@options[:name]) && !@options[:force]
    return puts "Path #{@options[:name]} exists. Use --force to override"
  end

  FileUtils.mkdir_p @cwd

  Dir.chdir @cwd do
    puts "Creating new plugin #{@namespace}".bold

    @gemspec_path = new_gem
    @spec = Gem::Specification.load(@gemspec_path)
    @path = File.expand_path(File.dirname(@gemspec_path))

    engine_scaffold

    bootstrap_gem
    setup_package_json
    update_git
  end

  post_install
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def engine
  @engine
end

#gemObject (readonly)

Returns the value of attribute gem.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def gem
  @gem
end

#gemspec_pathObject (readonly)

Returns the value of attribute gemspec_path.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def gemspec_path
  @gemspec_path
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def namespace
  @namespace
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def path
  @path
end

#plugin_moduleObject (readonly)

Returns the value of attribute plugin_module.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def plugin_module
  @plugin_module
end

Instance Method Details

#action_log(action, path) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/spark_engine/scaffold.rb', line 223

def action_log(action, path)
  color = case action
          when 'create', 'update'
            :green
          when 'skip'
            :white
          when 'delete'
            :red
          end
  puts action.rjust(12).colorize(color).bold + "  #{path.sub(Dir.pwd+'/','')}"
end

#bootstrap_gemObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/spark_engine/scaffold.rb', line 58

def bootstrap_gem
  # Remove bin
  FileUtils.rm_rf(File.join(path, 'bin'))

  scaffold_path = File.expand_path("scaffold/**/*", File.dirname(__FILE__))

  Dir.glob(scaffold_path, File::FNM_DOTMATCH).select{|f| File.file? f}.each do |f|
    write_template f.split(/spark_engine\/scaffold\//)[1], force: true
  end

  system 'bundle'
end

#engine_copyObject

Copy parts of the engine scaffold into site directory



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/spark_engine/scaffold.rb', line 94

def engine_copy
  site_path = File.join path, 'site'
  FileUtils.mkdir_p site_path

  ## Copy Rails plugin files
  Dir.chdir "#{@gem_temp}/#{gem}/site" do
    %w(app config bin config.ru Rakefile public log).each do |item|
      target = File.join site_path, item

      FileUtils.cp_r item, target

      action_log "create", target.sub(@cwd+'/','')
    end

  end

  # Remove temp dir
  FileUtils.rm_rf @gem_temp
end

#engine_scaffoldObject

Create an Rails plugin engine for documentation site



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spark_engine/scaffold.rb', line 72

def engine_scaffold
  FileUtils.mkdir_p(@gem_temp)
  Dir.chdir(@gem_temp) do
    response = Open3.capture3("rails plugin new #{gem} --mountable --dummy-path=site --skip-test-unit")
    if !response[1].empty?
      puts response[1]
      abort "FAILED: Please be sure you have the rails gem installed with `gem install rails`"
    end

    # Remove files and directories that are unnecessary for the
    # light-weight Rails documentation site
    remove = %w(mailers models assets channels jobs views).map{ |f| File.join('app', f) }
    remove.concat %w(cable.yml storage.yml database.yml).map{ |f| File.join('config', f) }

    remove.each { |f| FileUtils.rm_rf File.join(@gem, 'site', f), secure: true }
  end
  

  engine_copy
end

#modulize(input) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/spark_engine/scaffold.rb', line 235

def modulize(input)
  classify = lambda { |name| 
    name = (name =~ /_/) ? name.split('_').map(&classify).join : name
    (name =~ /[A-Z]/) ? name : name.capitalize 
  }
  input.split('/').map(&classify).join('::')
end

#new_component(options = {}) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/spark_engine/scaffold.rb', line 191

def new_component(options={})
  path = File.join(SparkEngine.plugin.paths[:components], options[:component])
  name = options[:component].split('/').last

  paths = {
    base: path,
    component: path+'_component.rb',
    template: File.join(path, "_#{name}.html.erb"),
    css: File.join(SparkEngine.plugin.paths[:stylesheets], "components", "#{options[:component].sub(name, '_'+name)}.scss"),
    js: File.join(SparkEngine.plugin.paths[:javascripts], "components", "#{options[:component].sub(name, '_'+name)}.js")
  }

  if options[:delete]
    return paths.values.each do |p| 
      action_log('delete', FileUtils.rm_rf(p).first) if File.exist?(p)
    end
  end

  # Convert "foo/bar" || "Foo::Bar" -> "Foo::BarComponent"
  options[:class] = options[:class] ? "#{modulize(options[:class])}" : "SparkComponents::Component"
  options[:class] << "Component" unless options[:class].end_with? "Component"

  # Write component class
  component_content = %Q{class #{modulize(options[:component])}Component < #{options[:class] }\nend} 
  write_file(paths[:component], component_content, options)

  write_file(paths[:template], '', options) if options[:template]
  write_file(paths[:css], '', options)      if options[:css]
  write_file(paths[:js], '', options)       if options[:js] 
end

#new_gemObject

Create a new gem with Bundle’s gem command



47
48
49
50
# File 'lib/spark_engine/scaffold.rb', line 47

def new_gem
  system "bundler gem #{gem}"
  Dir.glob(File.join(gem, "/*.gemspec")).first
end

#post_installObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/spark_engine/scaffold.rb', line 169

def post_install
  require 'pathname'

  target = Pathname.new File.join(@cwd, @gem) 
  dir = target.relative_path_from Pathname.new(Dir.pwd)
  victory = "#{@plugin_module} Design System created at #{dir}. Huzzah!"
  dashes = ''
  victory.size.times{ dashes += '-' }

  puts "\n#{victory}\n#{dashes}".bold

  puts "Install dependencies:"
  puts "  - cd #{dir}"
  puts "  - bundle"
  puts "  - yarn install (or npm install)\n\n"
  puts "Then give it a spin.\n\n"
  puts "  spark build".bold + "  - builds assets"
  puts "  spark server".bold + " - view documentation site in a server"
  puts "  spark help".bold + "   - learn more…"
  puts dashes + "\n\n"
end

#read_template(file_path) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/spark_engine/scaffold.rb', line 137

def read_template(file_path)
  contents = ''
  File.open file_path do |f|
    contents = ERB.new(f.read).result(binding)
  end
  contents
end

#setup_package_jsonObject



52
53
54
55
56
# File 'lib/spark_engine/scaffold.rb', line 52

def setup_package_json
  Dir.chdir path do
    NPM.setup
  end
end

#underscorize(input) ⇒ Object



243
244
245
246
247
# File 'lib/spark_engine/scaffold.rb', line 243

def underscorize(input)
  input.gsub(/[A-Z]/) do |char|
    '_'+char
  end.sub(/^_/,'').downcase
end

#update_gitObject



114
115
116
117
118
119
# File 'lib/spark_engine/scaffold.rb', line 114

def update_git
  Dir.chdir gem do
    system "git reset"
    system "git add -A"
  end
end

#write_file(path, content = '', options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/spark_engine/scaffold.rb', line 145

def write_file(path, content='', options={})
  options[:mode] ||= 'w'

  if File.exist?(path) 
    if options[:force]
      type = 'update'
    else
      return action_log('skipped', path)
    end
  else
    type = 'create'
  end

  FileUtils.mkdir_p(File.dirname(path))

  if content.empty?
    FileUtils.touch(path)
  else
    File.open(path, options[:mode]) { |io| io.write(content) }
  end

  action_log(type, path)
end

#write_template(template, options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/spark_engine/scaffold.rb', line 121

def write_template(template, options)
  template_path = File.expand_path("scaffold/#{template}", File.dirname(__FILE__))

  # Extract file extension
  ext = File.extname(template)

  # Replace keywords with correct names (excluding file extensions)
  target_path = template.sub(/#{ext}$/, '').gsub(/(gem|engine|namespace)/, { 
    'gem' => @gem, 
    'engine' => @engine,
    'namespace' => @namespace
  }) + ext

  write_file target_path, read_template(template_path), options
end