Class: Cyborg::Scaffold
- Inherits:
-
Object
- Object
- Cyborg::Scaffold
- Defined in:
- lib/cyborg/command/scaffold.rb
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#gem ⇒ Object
readonly
Returns the value of attribute gem.
-
#gemspec_path ⇒ Object
readonly
Returns the value of attribute gemspec_path.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#plugin_module ⇒ Object
readonly
Returns the value of attribute plugin_module.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
- #action_log(action, path) ⇒ Object
- #bootstrap_gem ⇒ Object
- #cyborg_plugin_config ⇒ Object
-
#engine_app_scaffold ⇒ Object
Add engine’s app assets and utilities.
- #engine_copy ⇒ Object
- #engine_site_scaffold ⇒ Object
-
#fix_gemspec_files ⇒ Object
First remove scaffold spec.files (which rely on git) to avoid errors when loading the spec.
- #gemspec ⇒ Object
-
#initialize(options) ⇒ Scaffold
constructor
A new instance of Scaffold.
- #install_npm_modules ⇒ Object
- #modulize(input) ⇒ Object
-
#new_gem ⇒ Object
Create a new gem with Bundle’s gem command.
- #prepare_engine_site ⇒ Object
- #underscorize(input) ⇒ Object
- #update_git ⇒ Object
- #write_file(paths, content, mode = 'w') ⇒ Object
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 29 30 |
# File 'lib/cyborg/command/scaffold.rb', line 7 def initialize() @cwd = Dir.pwd @gem = underscorize([:name]) @engine = underscorize([:engine] || [:name]) @namespace = @engine @plugin_module = modulize @engine puts "Creating new plugin #{@namespace}".bold engine_site_scaffold @gemspec_path = new_gem @path = File.(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
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
5 6 7 |
# File 'lib/cyborg/command/scaffold.rb', line 5 def engine @engine end |
#gem ⇒ Object (readonly)
Returns the value of attribute gem.
5 6 7 |
# File 'lib/cyborg/command/scaffold.rb', line 5 def gem @gem end |
#gemspec_path ⇒ Object (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 |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/cyborg/command/scaffold.rb', line 5 def namespace @namespace end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/cyborg/command/scaffold.rb', line 5 def path @path end |
#plugin_module ⇒ Object (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 |
#spec ⇒ Object (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
270 271 272 |
# File 'lib/cyborg/command/scaffold.rb', line 270 def action_log(action, path) puts action.rjust(12).colorize(:green).bold + " #{path}" end |
#bootstrap_gem ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cyborg/command/scaffold.rb', line 51 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_config ⇒ Object
72 73 74 75 76 |
# File 'lib/cyborg/command/scaffold.rb', line 72 def cyborg_plugin_config plugin_config = "gem: '#{gem}'" plugin_config += ",\n engine: '#{engine}'" if engine plugin_config end |
#engine_app_scaffold ⇒ Object
Add engine’s app assets and utilities
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 125 126 127 128 129 130 131 |
# File 'lib/cyborg/command/scaffold.rb', line 79 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}/application.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 .sass-cache}, 'a') end |
#engine_copy ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/cyborg/command/scaffold.rb', line 144 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_scaffold ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cyborg/command/scaffold.rb', line 133 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 |
#fix_gemspec_files ⇒ Object
First remove scaffold spec.files (which rely on git) to avoid errors when loading the spec
47 48 49 |
# File 'lib/cyborg/command/scaffold.rb', line 47 def fix_gemspec_files write_file(gemspec_path, File.read(gemspec_path).gsub(/^.+spec\.files.+$/,'')) end |
#gemspec ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/cyborg/command/scaffold.rb', line 224 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}/**/*", "LICENSE.txt", "README.md"] spec.require_paths = ["lib"] spec.add_dependency "rails", "~> 4.2.6" spec.add_runtime_dependency "cyborg" spec.add_development_dependency "bundler", "~> 1.12" spec.add_development_dependency "rake", "~> 10.0" end } end |
#install_npm_modules ⇒ Object
39 40 41 42 43 |
# File 'lib/cyborg/command/scaffold.rb', line 39 def install_npm_modules Dir.chdir path do NPM.setup end end |
#modulize(input) ⇒ Object
274 275 276 |
# File 'lib/cyborg/command/scaffold.rb', line 274 def modulize(input) input.split('_').collect(&:capitalize).join end |
#new_gem ⇒ Object
Create a new gem with Bundle’s gem command
34 35 36 37 |
# File 'lib/cyborg/command/scaffold.rb', line 34 def new_gem system "bundler gem #{gem}" Dir.glob(File.join(gem, "/*.gemspec")).first end |
#prepare_engine_site ⇒ Object
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 |
# File 'lib/cyborg/command/scaffold.rb', line 164 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 'bundler' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) require '#{gem}' require 'sprockets/railtie' 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 render action: "#\{params[:page]\}" end end}) write_file(File.join(site_path, 'app/views/layouts/application.html.erb'), "<%= layout do %>\n<% end %>") write_file(File.join(site_path, 'app/views/docs/index.html.erb'), "<h1>#{plugin_module} Documentation</h1>") end |
#underscorize(input) ⇒ Object
278 279 280 281 282 |
# File 'lib/cyborg/command/scaffold.rb', line 278 def underscorize(input) input.gsub(/[A-Z]/) do |char| '_'+char end.sub(/^_/,'').downcase end |
#update_git ⇒ Object
217 218 219 220 221 222 |
# File 'lib/cyborg/command/scaffold.rb', line 217 def update_git Dir.chdir gem do system "git reset" system "git add -A" end end |
#write_file(paths, content, mode = 'w') ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/cyborg/command/scaffold.rb', line 252 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 |