Class: Cyborg::Scaffold

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Scaffold

Returns a new instance of Scaffold.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cyborg/command/scaffold.rb', line 7

def initialize(options)
  @cwd = Dir.pwd
  @gem = underscorize(options[:name])
  @engine = underscorize(options[:engine] || options[:name])
  @namespace = @engine
  @plugin_module = modulize @engine

  puts "Creating new plugin #{@namespace}".bold
  engine_site_scaffold

  @gemspec_path = new_gem
  @path = File.expand_path(File.dirname(@gemspec_path))

  @spec = Gem::Specification.load(@gemspec_path)

  bootstrap_gem
  engine_app_scaffold
  engine_copy
  prepare_engine_site
  install_npm_modules
  update_git
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def engine
  @engine
end

#gemObject (readonly)

Returns the value of attribute gem.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def gem
  @gem
end

#gemspec_pathObject (readonly)

Returns the value of attribute gemspec_path.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def gemspec_path
  @gemspec_path
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def namespace
  @namespace
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def path
  @path
end

#plugin_moduleObject (readonly)

Returns the value of attribute plugin_module.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def plugin_module
  @plugin_module
end

#specObject (readonly)

Returns the value of attribute spec.



5
6
7
# File 'lib/cyborg/command/scaffold.rb', line 5

def spec
  @spec
end

Instance Method Details

#action_log(action, path) ⇒ Object



280
281
282
# File 'lib/cyborg/command/scaffold.rb', line 280

def action_log(action, path)
  puts action.rjust(12).colorize(:green).bold + "  #{path}"
end

#bootstrap_gemObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cyborg/command/scaffold.rb', line 43

def bootstrap_gem

  # Remove unnecessary bin dir
  FileUtils.rm_rf(File.join(path, 'bin'))

  # Simplify gempsec and set up to add assets properly
  write_file(gemspec_path, gemspec)

  write_file("#{gem}/lib/#{gem}.rb", %Q{require 'cyborg'
require '#{gem}/version'

module #{modulize(gem)}
  class Plugin < Cyborg::Plugin
  end
end

Cyborg.register(#{modulize(gem)}::Plugin, {
  #{cyborg_plugin_config}
})})
end

#cyborg_plugin_configObject



64
65
66
67
68
# File 'lib/cyborg/command/scaffold.rb', line 64

def cyborg_plugin_config
  plugin_config = "gem: '#{gem}'"
  plugin_config += ",\n  engine: '#{engine}'" if engine
  plugin_config
end

#engine_app_scaffoldObject

Add engine’s app assets and utilities



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cyborg/command/scaffold.rb', line 71

def engine_app_scaffold

  # Add asset dirs
  files = %w(images javascripts stylesheets svgs).map { |path|
    "#{gem}/app/assets/#{path}/#{namespace}/.keep"
  }

  write_file(files, '')

  # Add helper and layout dirs
  files = %w(helpers views/layouts).each { |path|
    "#{gem}/app/#{path}/#{namespace}"
  }

  write_file(files, '')

  # Add an application helper
  write_file("#{gem}/app/helpers/#{namespace}/application_helper.rb", %Q{module #{plugin_module}
  module ApplicationHelper
  end
end})

  # Add an a base layout
  write_file("#{gem}/app/views/layouts/#{namespace}/default.html.erb", %Q{<!DOCTYPE html>
<html>
<head>
  <title>#{plugin_module}</title>
  <%= csrf_meta_tags %>
  <%= asset_tags %>
  <%= yield :stylesheets %>
  <%= yield :javascripts %>
  <%= yield :head %>
</head>
<body>

<div class='site'>
  <div class='page'><%=yield %></div>
</div>
</body>
</html>})


  # Update .gitignore
  write_file("#{gem}/.gitignore", %Q{.DS_Store
log/*.log
pkg/
node_modules
site/log/*.log
site/tmp/
/public/
_svg.js
.esvg-cache
.sass-cache}, 'a')
end

#engine_copyObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cyborg/command/scaffold.rb', line 137

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

  Dir.chdir ".#{gem}-tmp/#{gem}" do
    %w(app config bin config.ru Rakefile public log).each do |item|
      target = File.join(site_path, item)

      FileUtils.cp_r(File.join('site', item), target)
      action_log "create", target.sub(@cwd+'/','')
    end

  end

  FileUtils.rm_rf(".#{gem}-tmp")
  %w(app/mailers app/models config/database.yml).each do |item|
    FileUtils.rm_rf(File.join(site_path, item))
  end
end

#engine_site_scaffoldObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/cyborg/command/scaffold.rb', line 126

def engine_site_scaffold
  FileUtils.mkdir_p(".#{gem}-tmp")
  Dir.chdir ".#{gem}-tmp" 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
  end
end

#gemspecObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cyborg/command/scaffold.rb', line 234

def gemspec
%Q{# coding: utf-8
$:.push File.expand_path("../lib", __FILE__)

require "#{spec.name}/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |spec|
  spec.name        = "#{spec.name}"
  spec.version     = #{modulize(gem)}::VERSION
  spec.authors     = #{spec.authors}
  spec.email       = #{spec.email}
  spec.summary     = "Summary of your gem."
  spec.description = "Description of your gem (usually longer)."
  spec.license     = "#{spec.license}"

  spec.files = Dir["{app,lib,public,config}/**/*", "LICENSE.txt", "README.md"]
  spec.require_paths = ["lib"]

  spec.add_dependency "rails", ">= 4"
  spec.add_runtime_dependency "cyborg"

  spec.add_development_dependency "bundler", "~> 1.12"
  spec.add_development_dependency "rake", "~> 10.0"
end
}
end

#install_npm_modulesObject



37
38
39
40
41
# File 'lib/cyborg/command/scaffold.rb', line 37

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

#modulize(input) ⇒ Object



284
285
286
287
288
# File 'lib/cyborg/command/scaffold.rb', line 284

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

#new_gemObject

Create a new gem with Bundle’s gem command



32
33
34
35
# File 'lib/cyborg/command/scaffold.rb', line 32

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

#prepare_engine_siteObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
221
222
223
224
225
# File 'lib/cyborg/command/scaffold.rb', line 157

def prepare_engine_site
  site_path = File.join(path, 'site')

  write_file(File.join(site_path, 'config/environments/development.rb'), %Q{Rails.application.configure do
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log
end
})

  write_file(File.join(site_path, 'config/application.rb'), %Q{require File.expand_path('../boot', __FILE__)

require "rails"
# Pick the frameworks you want:
require "action_controller/railtie"
require "action_view/railtie"
require "sprockets/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

require '#{gem}'

module Site
  class Application < Cyborg::Application
  end
end})

  write_file(File.join(site_path, 'config/routes.rb'), %Q{Rails.application.routes.draw do
  resources :docs, param: :page, path: ''
end})

  write_file(File.join(site_path, 'app/controllers/docs_controller.rb'), %Q{class DocsController < ApplicationController
  def show
page = params[:page]

%w(docs).each do | root_page |
  if page.match(\/\#{root_page}\\/?$/)
    page = File.join(root_page, 'index')
  end
end

if template_exists? page
  render template: page
elsif template_exists? "docs/\#{page}"
  render template: "docs/\#{page}"
else
  render file: "404.html", status: :not_found
end
  end
end})

  layout = %Q{<%= render_layout '#{namespace}/default' do %>
  <% stylesheets { stylesheet_link_tag('application') } %>
  <% javascripts { javascript_include_tag('application') } %>
<% end %>}

  write_file(File.join(site_path, 'app/views/layouts/default.html.erb'), layout)
  write_file(File.join(site_path, 'app/views/docs/index.html.erb'), "<h1>#{plugin_module} Documentation</h1>")
end

#underscorize(input) ⇒ Object



290
291
292
293
294
# File 'lib/cyborg/command/scaffold.rb', line 290

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

#update_gitObject



227
228
229
230
231
232
# File 'lib/cyborg/command/scaffold.rb', line 227

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

#write_file(paths, content, mode = 'w') ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/cyborg/command/scaffold.rb', line 262

def write_file(paths, content, mode='w')
  paths = [paths].flatten
  paths.each do |path|
    if File.exist?(path)
      type = 'update'
    else
      FileUtils.mkdir_p(File.dirname(path))
      type = 'create'
    end

    File.open path, mode do |io|
      io.write(content)
    end

    action_log(type, path)
  end
end