cyborg 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07f10e98822a1c85fef58e64172d84c27f5c6411
4
- data.tar.gz: 3a672afc95f926981c9960c653878856a5954efc
3
+ metadata.gz: de1ffb656fd08f1bc6ce7993f13df4f0d40a8055
4
+ data.tar.gz: 14313e29b7ae7968042e0027a8498d9953cd2d39
5
5
  SHA512:
6
- metadata.gz: 4e28ba651014ba3e67018cda38c4bca465e92f88ecddbb4de00546b1e6975a5d950ad18de5d545dcb48c4cc13a297cf1e0e3d16716222f51ca695a697f4f637b
7
- data.tar.gz: c601e198c70fc951f4e74e82fb51dfcf2e17fb3c9e3fda2f220a933e628401171e09a8fc97793f5d193307a65f2dc8e6c83519d0c1d9f2b99105d0cd9f2113d6
6
+ metadata.gz: 8d55c4eacd4a0e75d6462776143c8e221a9e570b76d2fb05af9a54658fd17019b47e805f6020f4f681835f38767ead191606a66afc3c6d513576cc04f8de6db1
7
+ data.tar.gz: 844bce677e103e3e487ba0c6adeadb5815ac675f066fc4ac71f6ae55908d408a8b9ce52cac666ff6005afc68d17c7b32540bdcd38edee38358d9ba593e75bf3a
data/bin/cyborg CHANGED
@@ -28,7 +28,11 @@ OptionParser.new do |opts|
28
28
  if %w(n new).include? options[:command]
29
29
  options[:name] = next_arg
30
30
 
31
- opts.on("-f", "--force", "Overwrite existing files") do |val|
31
+ opts.on("-e", "--engine ENGINE_NAME", String, "Name the engine (defaults to gem name)") do |engine|
32
+ options[:engine] = engine
33
+ end
34
+
35
+ opts.on("-f", "--force", "overwrite existing files") do |val|
32
36
  options[:force] = true
33
37
  end
34
38
  end
data/lib/cyborg.rb CHANGED
@@ -22,6 +22,7 @@ module Cyborg
22
22
 
23
23
  def register(plugin_module, options={})
24
24
  @plugin = plugin_module.new(options)
25
+ @plugin.create_engine
25
26
  patch_rails
26
27
  end
27
28
 
@@ -11,7 +11,7 @@ module Cyborg
11
11
 
12
12
  case options[:command]
13
13
  when 'new', 'n'
14
- Scaffold.new(options[:name])
14
+ Scaffold.new(options)
15
15
  when 'build', 'b'
16
16
  from_root { dispatch(:build, options) }
17
17
  when 'watch', 'w'
@@ -21,17 +21,11 @@ module Cyborg
21
21
 
22
22
  if File.exist?(package_path)
23
23
  update_package_json
24
- install
25
24
  else
26
25
  write_package_json(DEPENDENCIES)
27
- install
28
26
  end
29
27
  end
30
28
 
31
- def install
32
- system "npm install"
33
- end
34
-
35
29
  def package_path
36
30
  File.join(Dir.pwd, 'package.json')
37
31
  end
@@ -2,14 +2,16 @@ require 'fileutils'
2
2
 
3
3
  module Cyborg
4
4
  class Scaffold
5
- attr_reader :name, :spec, :path, :gemspec_path
5
+ attr_reader :gem, :engine, :namespace, :plugin_module, :spec, :path, :gemspec_path
6
6
 
7
- def initialize(name)
7
+ def initialize(options)
8
8
  @cwd = Dir.pwd
9
- @name = name.downcase
10
- @module_name = name.split('_').collect(&:capitalize).join
9
+ @gem = underscorize(options[:name])
10
+ @engine = underscorize(options[:engine] || options[:name])
11
+ @namespace = @engine
12
+ @plugin_module = modulize @engine
11
13
 
12
- puts "Creating new plugin #{name}".bold
14
+ puts "Creating new plugin #{@namespace}".bold
13
15
  engine_site_scaffold
14
16
 
15
17
  @gemspec_path = create_gem
@@ -33,9 +35,9 @@ module Cyborg
33
35
  begin
34
36
  require 'bundler'
35
37
  require 'bundler/cli'
36
- Bundler::CLI.start(['gem', name])
38
+ Bundler::CLI.start(['gem', gem])
37
39
 
38
- Dir.glob(File.join(name, "/*.gemspec")).first
40
+ Dir.glob(File.join(gem, "/*.gemspec")).first
39
41
 
40
42
  rescue LoadError
41
43
  raise "To use this feature you'll need to install the bundler gem with `gem install bundler`."
@@ -51,11 +53,7 @@ module Cyborg
51
53
  # First remove scaffold spec.files (which rely on git) to avoid errors
52
54
  # when loading the spec
53
55
  def fix_gemspec_files
54
- gs = File.read(gemspec_path)
55
-
56
- File.open(gemspec_path, 'w') do |io|
57
- io.write gs.gsub(/^.+spec\.files.+$/,'')
58
- end
56
+ write_file(gemspec_path, File.read(gemspec_path).gsub(/^.+spec\.files.+$/,''))
59
57
  end
60
58
 
61
59
  def bootstrap_gem
@@ -64,61 +62,55 @@ module Cyborg
64
62
  FileUtils.rm_rf(File.join(path, 'bin'))
65
63
 
66
64
  # Simplify gempsec and set up to add assets properly
67
- File.open(gemspec_path, 'w') do |io|
68
- io.write gemspec
69
- end
65
+ write_file(gemspec_path, gemspec)
70
66
 
71
- action_log "update", gemspec_path
67
+ write_file("#{gem}/lib/#{gem}.rb", %Q{require 'cyborg'
68
+ require '#{gem}/version'
72
69
 
73
- File.open "#{name}/lib/#{name}.rb", 'w' do |io|
74
- io.write %Q{require 'megatron'
75
- require '#{name}/version'
76
-
77
- module #{@module_name}
70
+ module #{modulize(gem)}
78
71
  class Plugin < Cyborg::Plugin
79
72
  end
80
73
  end
81
74
 
82
- Cyborg.register(#{@module_name}::Plugin, {
83
- name: '#{name}'
84
- })}
85
- end
86
- action_log "update", "#{name}/lib/#{name}.rb"
75
+ Cyborg.register(#{modulize(gem)}::Plugin, {
76
+ #{cyborg_plugin_config}
77
+ })})
78
+ end
79
+
80
+ def cyborg_plugin_config
81
+ plugin_config = "gem: '#{gem}'"
82
+ plugin_config += ",\n engine: '#{engine}'" if engine
83
+ plugin_config
87
84
  end
88
85
 
89
86
  # Add engine's app assets and utilities
90
87
  def engine_app_scaffold
91
88
 
92
89
  # Add asset dirs
93
- %w(images javascripts stylesheets svgs).each do |path|
94
- path = "#{name}/app/assets/#{path}/#{name}"
95
- FileUtils.mkdir_p path
96
- FileUtils.touch File.join(path, '.keep')
97
- action_log "create", path
98
- end
90
+ files = %w(images javascripts stylesheets svgs).map { |path|
91
+ "#{gem}/app/assets/#{path}/#{namespace}/.keep"
92
+ }
93
+
94
+ write_file(files, '')
99
95
 
100
96
  # Add helper and layout dirs
101
- %w(helpers views/layouts).each do |path|
102
- path = "#{name}/app/#{path}/#{name}"
103
- FileUtils.mkdir_p path
104
- action_log "create", path
105
- end
97
+ files = %w(helpers views/layouts).each { |path|
98
+ "#{gem}/app/#{path}/#{namespace}"
99
+ }
100
+
101
+ write_file(files, '')
106
102
 
107
103
  # Add an application helper
108
- File.open("#{name}/app/helpers/#{name}/application_helper.rb", 'w') do |io|
109
- io.write %Q{module #{@module_name}
104
+ write_file("#{gem}/app/helpers/#{namespace}/application_helper.rb", %Q{module #{plugin_module}
110
105
  module ApplicationHelper
111
106
  end
112
- end}
113
- action_log "create", "#{name}/app/helpers/#{name}/application_helper.rb"
114
- end
107
+ end})
115
108
 
116
109
  # Add an a base layout
117
- File.open("#{name}/app/views/layouts/#{name}/application.html.erb", 'w') do |io|
118
- io.write %Q{<!DOCTYPE html>
110
+ write_file("#{gem}/app/views/layouts/#{namespace}/application.html.erb", %Q{<!DOCTYPE html>
119
111
  <html>
120
112
  <head>
121
- <title>#{@module_name}</title>
113
+ <title>#{plugin_module}</title>
122
114
  <%= csrf_meta_tags %>
123
115
  <%= asset_tags %>
124
116
  <%= yield :stylesheets %>
@@ -131,12 +123,11 @@ end}
131
123
  <div class='page'><%=yield %></div>
132
124
  </div>
133
125
  </body>
134
- </html>}
135
- end
136
- action_log "create", "#{name}/app/views/layouts/#{name}/application.html.erb"
126
+ </html>})
137
127
 
138
- File.open("#{name}/.gitignore", 'a') do |io|
139
- io.write %Q{.DS_Store
128
+
129
+ # Update .gitignore
130
+ write_file("#{gem}/.gitignore", %Q{.DS_Store
140
131
  log/*.log
141
132
  pkg/
142
133
  node_modules
@@ -144,15 +135,13 @@ site/log/*.log
144
135
  site/tmp/
145
136
  /public/
146
137
  _svg.js
147
- .sass-cache}
148
- end
149
- action_log "update", "#{name}/.gitignore"
138
+ .sass-cache}, 'a')
150
139
  end
151
140
 
152
141
  def engine_site_scaffold
153
- FileUtils.mkdir_p(".#{name}-tmp")
154
- Dir.chdir ".#{name}-tmp" do
155
- response = Open3.capture3("rails plugin new #{name} --mountable --dummy-path=site --skip-test-unit")
142
+ FileUtils.mkdir_p(".#{gem}-tmp")
143
+ Dir.chdir ".#{gem}-tmp" do
144
+ response = Open3.capture3("rails plugin new #{gem} --mountable --dummy-path=site --skip-test-unit")
156
145
  if !response[1].empty?
157
146
  puts response[1]
158
147
  abort "FAILED: Please be sure you have the rails gem installed with `gem install rails`"
@@ -164,7 +153,7 @@ _svg.js
164
153
  site_path = File.join(path, 'site')
165
154
  FileUtils.mkdir_p(site_path)
166
155
 
167
- Dir.chdir ".#{name}-tmp/#{name}" do
156
+ Dir.chdir ".#{gem}-tmp/#{gem}" do
168
157
  %w(app config bin config.ru Rakefile public log).each do |item|
169
158
  target = File.join(site_path, item)
170
159
 
@@ -174,7 +163,7 @@ _svg.js
174
163
 
175
164
  end
176
165
 
177
- FileUtils.rm_rf(".#{name}-tmp")
166
+ FileUtils.rm_rf(".#{gem}-tmp")
178
167
  %w(app/mailers app/models config/database.yml).each do |item|
179
168
  FileUtils.rm_rf(File.join(site_path, item))
180
169
  end
@@ -183,8 +172,7 @@ _svg.js
183
172
  def prepare_engine_site
184
173
  site_path = File.join(path, 'site')
185
174
 
186
- File.open File.join(site_path, 'config/environments/development.rb'), 'w' do |io|
187
- io.write %Q{Rails.application.configure do
175
+ write_file(File.join(site_path, 'config/environments/development.rb'), %Q{Rails.application.configure do
188
176
  config.cache_classes = false
189
177
 
190
178
  # Do not eager load code on boot.
@@ -197,11 +185,9 @@ _svg.js
197
185
  # Print deprecation notices to the Rails logger.
198
186
  config.active_support.deprecation = :log
199
187
  end
200
- }
201
- end
188
+ })
202
189
 
203
- File.open File.join(site_path, 'config/application.rb'), 'w' do |io|
204
- io.write %Q{require File.expand_path('../boot', __FILE__)
190
+ write_file(File.join(site_path, 'config/application.rb'), %Q{require File.expand_path('../boot', __FILE__)
205
191
 
206
192
  require "rails"
207
193
  # Pick the frameworks you want:
@@ -213,40 +199,31 @@ require 'bundler'
213
199
  # you've limited to :test, :development, or :production.
214
200
  Bundler.require(*Rails.groups)
215
201
 
216
- require '#{name}'
202
+ require '#{gem}'
217
203
  require 'sprockets/railtie'
218
204
 
219
205
  module Site
220
206
  class Application < Cyborg::Application
221
207
  end
222
- end}
223
- end
208
+ end})
224
209
 
225
- File.open File.join(site_path, 'config/routes.rb'), 'w' do |io|
226
- io.write %Q{Rails.application.routes.draw do
210
+ write_file(File.join(site_path, 'config/routes.rb'), %Q{Rails.application.routes.draw do
227
211
  resources :docs, param: :page, path: ''
228
- end}
229
- end
212
+ end})
230
213
 
231
- File.open File.join(site_path, 'app/controllers/docs_controller.rb'), 'w' do |io|
232
- io.write %Q{class DocsController < ApplicationController
214
+ write_file(File.join(site_path, 'app/controllers/docs_controller.rb'), %Q{class DocsController < ApplicationController
233
215
  def show
234
216
  render action: "#\{params[:page]\}"
235
217
  end
236
- end}
237
- end
218
+ end})
238
219
 
239
- File.open File.join(site_path, 'app/views/layouts/application.html.erb'), 'w' do |io|
240
- io.write %Q{<%= layout '#{name}' do %>\n<% end %>}
241
- end
242
- FileUtils.mkdir_p File.join(site_path, 'app/views/docs')
243
- File.open File.join(site_path, 'app/views/docs/index.html.erb'), 'w' do |io|
244
- io.write %Q{<h1>#{@module_name} Documentaiton</h1>}
245
- end
220
+ write_file(File.join(site_path, 'app/views/layouts/application.html.erb'), "<%= layout do %>\n<% end %>")
221
+
222
+ write_file(File.join(site_path, 'app/views/docs/index.html.erb'), "<h1>#{plugin_module} Documentation</h1>")
246
223
  end
247
224
 
248
225
  def update_git
249
- Dir.chdir @name do
226
+ Dir.chdir gem do
250
227
  system "git reset"
251
228
  system "git add -A"
252
229
  end
@@ -261,7 +238,7 @@ require "#{spec.name}/version"
261
238
  # Describe your gem and declare its dependencies:
262
239
  Gem::Specification.new do |spec|
263
240
  spec.name = "#{spec.name}"
264
- spec.version = #{@module_name}::VERSION
241
+ spec.version = #{modulize(gem)}::VERSION
265
242
  spec.authors = #{spec.authors}
266
243
  spec.email = #{spec.email}
267
244
  spec.summary = "Summary of your gem."
@@ -272,7 +249,7 @@ Gem::Specification.new do |spec|
272
249
  spec.require_paths = ["lib"]
273
250
 
274
251
  spec.add_dependency "rails", "~> 4.2.6"
275
- spec.add_runtime_dependency "megatron"
252
+ spec.add_runtime_dependency "cyborg"
276
253
 
277
254
  spec.add_development_dependency "bundler", "~> 1.12"
278
255
  spec.add_development_dependency "rake", "~> 10.0"
@@ -280,8 +257,36 @@ end
280
257
  }
281
258
  end
282
259
 
260
+ def write_file(paths, content, mode='w')
261
+ paths = [paths].flatten
262
+ paths.each do |path|
263
+ if File.exist?(path)
264
+ type = 'update'
265
+ else
266
+ FileUtils.mkdir_p(File.dirname(path))
267
+ type ='create'
268
+ end
269
+
270
+ File.open path, mode do |io|
271
+ io.write(content)
272
+ end
273
+
274
+ action_log(type, path)
275
+ end
276
+ end
277
+
283
278
  def action_log(action, path)
284
279
  puts action.rjust(12).colorize(:green).bold + " #{path}"
285
280
  end
281
+
282
+ def modulize(input)
283
+ input.split('_').collect(&:capitalize).join
284
+ end
285
+
286
+ def underscorize(input)
287
+ input.gsub(/[A-Z]/) do |char|
288
+ '_'+char
289
+ end.sub(/^_/,'').downcase
290
+ end
286
291
  end
287
292
  end
data/lib/cyborg/plugin.rb CHANGED
@@ -1,45 +1,50 @@
1
1
  module Cyborg
2
2
  class Plugin
3
- attr_reader :module_name, :gem, :engine, :name,
3
+ attr_reader :name, :gem, :engine,
4
4
  :stylesheets, :javascripts, :svgs, :destination
5
5
 
6
6
  def initialize(options)
7
- @name = options.delete(:name)
8
- @module_name = parent_module.name
9
- @gem = Gem.loaded_specs[@name]
7
+ @name = options.delete(:engine).downcase
8
+ @gem = Gem.loaded_specs[options.delete(:gem)]
10
9
  @maps = false
11
10
  config(options)
12
11
  expand_asset_paths
13
12
 
14
13
  # Store the gem path for access later when overriding root
15
14
  parent_module.instance_variable_set(:@gem_path, root)
15
+ parent_module.instance_variable_set(:@cyborg_plugin_name, name)
16
16
  add_assets
17
+ end
17
18
 
18
- @engine = create_engine if defined?(Rails)
19
+ def engine_name
20
+ @engine.name.sub(/::Engine/,'')
19
21
  end
20
22
 
21
23
  def create_engine
22
24
  # Create a new Rails::Engine
23
- return parent_module.const_set('Engine', Class.new(Rails::Engine) do
25
+ @engine = parent_module.const_set('Engine', Class.new(Rails::Engine) do
24
26
 
25
- def get_plugin_path
26
- parent = Object.const_get(self.class.to_s.split('::').first)
27
- path = parent.instance_variable_get("@gem_path")
28
- Pathname.new path
27
+ def cyborg_plugin_path
28
+ parent = Object.const_get(self.class.name.sub(/::Engine/,''))
29
+ Pathname.new parent.instance_variable_get("@gem_path")
29
30
  end
30
31
 
31
32
  def config
32
- @config ||= Rails::Engine::Configuration.new(get_plugin_path)
33
+ @config ||= Rails::Engine::Configuration.new(cyborg_plugin_path)
33
34
  end
34
35
 
35
- initializer "#{name.to_s.downcase}.static_assets" do |app|
36
+ engine_name Cyborg.plugin.name
37
+
38
+ initializer "#{name}.static_assets" do |app|
36
39
  app.middleware.insert_before(::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public")
37
40
  end
38
41
  end)
39
42
  end
40
43
 
41
44
  def parent_module
42
- Object.const_get(self.class.to_s.split('::').first)
45
+ mods = self.class.to_s.split('::')
46
+ mods.pop
47
+ Object.const_get(mods.join('::'))
43
48
  end
44
49
 
45
50
  def add_assets
@@ -89,15 +94,15 @@ module Cyborg
89
94
 
90
95
  def config(options)
91
96
  options = {
92
- production_asset_root: "/assets/#{@name}",
93
- asset_root: "/assets/#{@name}",
97
+ production_asset_root: "/assets/#{name}",
98
+ asset_root: "/assets/#{name}",
94
99
  destination: "public/",
95
100
  root: @gem.full_gem_path,
96
101
  version: @gem.version.to_s,
97
102
  paths: {
98
- stylesheets: "app/assets/stylesheets/#{@name}",
99
- javascripts: "app/assets/javascripts/#{@name}",
100
- svgs: "app/assets/svgs/#{@name}",
103
+ stylesheets: "app/assets/stylesheets/#{name}",
104
+ javascripts: "app/assets/javascripts/#{name}",
105
+ svgs: "app/assets/svgs/#{name}",
101
106
  }
102
107
  }.merge(options)
103
108
 
@@ -13,7 +13,7 @@ module Cyborg
13
13
  if Open3.capture3("npm ls browserify-incremental")[1].empty?
14
14
  find_files.each do |file|
15
15
  dest = destination(file).sub(/\.js$/,'')
16
- cmd = "browserifyinc --cachefile #{Cyborg.rails_path("tmp/cache/assets/.browserify-cache.json")} #{file} -t babelify --standalone #{plugin.module_name} -o #{dest}.js -d"
16
+ cmd = "browserifyinc --cachefile #{Cyborg.rails_path("tmp/cache/assets/.browserify-cache.json")} #{file} -t babelify --standalone #{plugin.name} -o #{dest}.js -d"
17
17
  cmd += " -p [ minifyify --map #{url(file).sub(/\.js$/,'')}.map.json --output #{dest}.map.json ]" if plugin.maps? || Cyborg.production?
18
18
  system cmd
19
19
  compress(destination(file))
@@ -1,3 +1,3 @@
1
1
  module Cyborg
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyborg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-20 00:00:00.000000000 Z
11
+ date: 2016-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sass