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(name) ⇒ 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(name)
  @cwd = Dir.pwd
  @name = name.downcase
  @module_name = name.split('_').collect(&:capitalize).join

  puts "Creating new plugin #{name}".bold
  engine_site_scaffod

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

  fix_gemspec_files

  @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

#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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
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



277
278
279
# File 'lib/cyborg/command/scaffold.rb', line 277

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

#bootstrap_gemObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cyborg/command/scaffold.rb', line 61

def bootstrap_gem

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

  # Simplify gempsec and set up to add assets properly
  File.open(gemspec_path, 'w') do |io|
    io.write gemspec
  end

  action_log "update", gemspec_path

  File.open "#{name}/lib/#{name}.rb", 'w' do |io|
    io.write %Q{require 'megatron'
require '#{name}/version'

module #{@module_name}
  class Plugin < Cyborg::Plugin
  end
end

Cyborg.register(#{@module_name}::Plugin, {
  name: '#{name}'
})}
  end
  action_log "update", "#{name}/lib/#{name}.rb"
end

#create_gemObject

Create a new gem with Bundle’s gem command



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cyborg/command/scaffold.rb', line 32

def create_gem
  begin
    require 'bundler'
    require 'bundler/cli'
    Bundler::CLI.start(['gem', name])

    Dir.glob(File.join(name, "/*.gemspec")).first

  rescue LoadError
    raise "To use this feature you'll need to install the bundler gem with `gem install bundler`."
  end
end

#engine_app_scaffoldObject

Add engine’s app assets and utilities



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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cyborg/command/scaffold.rb', line 90

def engine_app_scaffold

  # Add asset dirs
  %w(images javascripts stylesheets svgs).each do |path|
    path = "#{name}/app/assets/#{path}/#{name}"
    FileUtils.mkdir_p path
    FileUtils.touch File.join(path, '.keep')
    action_log "create", path
  end

  # Add helper and layout dirs
  %w(helpers views/layouts).each do |path|
    path = "#{name}/app/#{path}/#{name}"
    FileUtils.mkdir_p path
    action_log "create", path
  end

  # Add an application helper
  File.open("#{name}/app/helpers/#{name}/application_helper.rb", 'w') do |io|
    io.write %Q{module #{@module_name}
  module ApplicationHelper
  end
end}
    action_log "create", "#{name}/app/helpers/#{name}/application_helper.rb"
  end

  # Add an a base layout
  File.open("#{name}/app/views/layouts/#{name}/application.html.erb", 'w') do |io|
    io.write %Q{<!DOCTYPE html>
<html>
<head>
  <title>#{@module_name}</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>}
  end
  action_log "create", "#{name}/app/views/layouts/#{name}/application.html.erb"

  File.open("#{name}/.gitignore", 'a') do |io|
    io.write %Q{.DS_Store
log/*.log
pkg/
node_modules
site/log/*.log
site/tmp/
.sass-cache}
  end
  action_log "update", "#{name}/.gitignore"
end

#engine_copyObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cyborg/command/scaffold.rb', line 161

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

  Dir.chdir ".#{name}-tmp/#{name}" 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(".#{name}-tmp")
  %w(app/mailers app/models config/database.yml).each do |item|
    FileUtils.rm_rf(File.join(site_path, item))
  end
end

#engine_site_scaffodObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/cyborg/command/scaffold.rb', line 150

def engine_site_scaffod
  FileUtils.mkdir_p(".#{name}-tmp")
  Dir.chdir ".#{name}-tmp" do
    response = Open3.capture3("rails plugin new #{name} --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

#fix_gemspec_filesObject

First remove scaffold spec.files (which rely on git) to avoid errors when loading the spec



53
54
55
56
57
58
59
# File 'lib/cyborg/command/scaffold.rb', line 53

def fix_gemspec_files
  gs = File.read(gemspec_path)

  File.open(gemspec_path, 'w') do |io|
    io.write gs.gsub(/^.+spec\.files.+$/,'')
  end
end

#gemspecObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/cyborg/command/scaffold.rb', line 249

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     = #{@module_name}::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,config,lib/public}/**/*", "LICENSE.txt", "README.md"]
  spec.require_paths = ["lib"]

  spec.add_dependency "rails", "~> #{`rails -v`.strip.split(' ').last}"
  spec.add_runtime_dependency "megatron"

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

#install_npm_modulesObject



45
46
47
48
49
# File 'lib/cyborg/command/scaffold.rb', line 45

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

#prepare_engine_siteObject



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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cyborg/command/scaffold.rb', line 181

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

  File.open File.join(site_path, 'config/environments/development.rb'), 'w' do |io|
    io.write %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
    }
  end

  File.open File.join(site_path, 'config/application.rb'), 'w' do |io|
    io.write %Q{require File.expand_path('../boot', __FILE__)

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

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

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

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

  File.open File.join(site_path, 'app/controllers/docs_controller.rb'), 'w' do |io|
    io.write %Q{class DocsController < ApplicationController
  def show
render action: "#\{params[:page]\}"
  end
end}
  end

  File.open File.join(site_path, 'app/views/layouts/application.html.erb'), 'w' do |io|
    io.write %Q{<%= layout '#{name}' do %>\n<% end %>}
  end
  FileUtils.mkdir_p File.join(site_path, 'app/views/docs')
  File.open File.join(site_path, 'app/views/docs/index.html.erb'), 'w' do |io|
    io.write %Q{<h1>#{@module_name} Documentaiton</h1>}
  end
end

#update_gitObject



242
243
244
245
246
247
# File 'lib/cyborg/command/scaffold.rb', line 242

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