forked from workarea-commerce/workarea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_template.rb
410 lines (335 loc) · 9.79 KB
/
plugin_template.rb
1
2
3
4
5
6
7
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Start with...
# rails plugin new path/to/my_plugin --full -m path/to/plugin_template.rb --skip-spring --skip-active-record --skip-action-cable
#
#
# Namespace plugin under Workarea
#
remove_file "lib/#{name}.rb"
create_file "lib/workarea/#{name}.rb", <<~CODE
require 'workarea'
require 'workarea/storefront'
require 'workarea/admin'
require 'workarea/#{name}/engine'
require 'workarea/#{name}/version'
module Workarea
module #{camelized}
end
end
CODE
remove_file "lib/#{name}/version.rb"
create_file "lib/workarea/#{name}/version.rb", <<~CODE
module Workarea
module #{camelized}
VERSION = "0.1.0"
end
end
CODE
remove_file "lib/#{name}/engine.rb"
create_file "lib/workarea/#{name}/engine.rb", <<~CODE
require 'workarea/#{name}'
module Workarea
module #{camelized}
class Engine < ::Rails::Engine
include Workarea::Plugin
isolate_namespace Workarea::#{camelized}
end
end
end
CODE
create_file 'config/initializers/workarea.rb', <<~CODE
Workarea.configure do |config|
# Add custom configuration here
end
CODE
#
# Selectively require rails gems, removing ActiveRecord
#
rails_requires = <<~CODE
require "action_controller/railtie"
require "action_view/railtie"
require "action_mailer/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
require 'teaspoon-mocha'
CODE
gsub_file 'bin/rails', /.+rails\/all.+/, rails_requires
gsub_file 'test/dummy/config/application.rb', /.+rails\/all.+/, rails_requires
#
# Require Workarea in dummy app configuration, before plugin
#
require_workarea = <<~CODE
# Workarea must be required before other gems to ensure control over Rails.env
# for running tests
require 'workarea/core'
require 'workarea/admin'
require 'workarea/storefront'
CODE
inject_into_file 'test/dummy/config/application.rb', require_workarea, before: 'Bundler.require(*Rails.groups)'
#
# Modify bin/rails, correct path to engine file
#
gsub_file 'bin/rails', %r(lib/#{name}/engine), %(lib/workarea/#{name}/engine)
#
# Require Plugin in dummy app configuration
#
gsub_file "test/dummy/config/application.rb", /require.+"#{name}"/, %(require "workarea/#{name}")
#
# Comment out any active record config in dummy app
#
comment_lines 'test/dummy/config/environments/development.rb', /active_record/
comment_lines 'test/dummy/config/environments/production.rb', /active_record/
#
# Remove database config from dummy app
#
remove_file 'test/dummy/config/database.yml'
#
# Allow seeds to be run from dummy app
#
create_file 'test/dummy/db/seeds.rb', <<~CODE
require 'workarea/seeds'
Workarea::Seeds.run
CODE
#
# Remove ActionCable files from dummy app
#
remove_file 'test/dummy/app/assets/javascripts/cable.js'
remove_dir 'test/dummy/app/assets/javascripts/channels'
remove_dir 'test/dummy/app/channels'
#
# Remove assets config
# This is for Sprockets 4, remove this when we upgrade Sprockets
#
remove_dir 'app/assets/config'
#
# Remove application_record from dummy app
#
remove_file 'test/dummy/app/models/application_record.rb'
#
# Configure dummy app
#
create_file 'test/dummy/config/initializers/workarea.rb', <<~CODE
Workarea.configure do |config|
# Basic site info
config.site_name = 'Workarea #{name.titleize}'
config.host = 'www.example.com'
end
CODE
#
# Update routes in dummy app
#
remove_file 'test/dummy/config/routes.rb'
create_file 'test/dummy/config/routes.rb', <<~CODE
Rails.application.routes.draw do
mount Workarea::Core::Engine => '/'
mount Workarea::Admin::Engine => '/admin', as: 'admin'
mount Workarea::Storefront::Engine => '/', as: 'storefront'
end
CODE
#
# Create teaspoon env in dummy app
#
create_file 'test/teaspoon_env.rb', <<~CODE
require 'workarea/testing/teaspoon'
Teaspoon.configure do |config|
config.root = Workarea::#{camelized}::Engine.root
Workarea::Teaspoon.apply(config)
end
CODE
#
# Remove default tests
#
remove_file "test/#{name}_test.rb"
remove_file 'test/integration/navigation_test.rb'
#
# Namespace plugin under Workarea in gemspec
#
gsub_file "#{name}.gemspec", /#{name}\/version/, "workarea/#{name}/version"
gsub_file "#{name}.gemspec", /(#{camelized}::VERSION)/, 'Workarea::\1'
gsub_file "#{name}.gemspec", /(spec\.name\s+).+/, "\\1= \"workarea-#{name}\""
#
# Use the git file list for plugin's manifest
#
gsub_file "#{name}.gemspec", /spec.files.+\n\n/, "spec.files = `git ls-files`.split(\"\\n\")"
#
# Remove licence, rails & sqlite dependencies
#
gsub_file "#{name}.gemspec", /spec.license.+\n/, ''
gsub_file "#{name}.gemspec", /spec.add_dependency.+rails.+\n/, ''
gsub_file "#{name}.gemspec", /spec.add_development_dependency.+sqlite.+/, ''
#
# Add Workarea dependency
#
workarea_dependency = " spec.add_dependency 'workarea', '~> 3.x'\n"
inject_into_file "#{name}.gemspec", workarea_dependency, before: /^end$/
#
# Rename gemspec
#
def source_paths
[Dir.pwd] # this hack allows the copy_file method to work
end
inside do # root of plugin
copy_file "#{name}.gemspec", "workarea-#{name}.gemspec"
remove_file "#{name}.gemspec"
end
#
# Add null test cache store to test environment config
#
inject_into_file 'test/dummy/config/environments/test.rb', "\n\n config.cache_store = :null_store", before: "\nend"
#
# Update Github path in Gemfile
#
gsub_file 'Gemfile', /https:\/\/github.com\//, '[email protected]:'
#
# Add workarea to Gemfile
#
gem 'workarea', github: 'workarea-commerce/workarea'
#
# Setup test helper
#
remove_file 'test/test_helper.rb'
create_file 'test/test_helper.rb', <<~CODE
# Configure Rails Environment
ENV['RAILS_ENV'] = 'test'
require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
require 'rails/test_help'
require 'workarea/test_help'
# Filter out Minitest backtrace while allowing backtrace from other libraries
# to be shown.
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
CODE
#
# Setup CI
#
create_file 'script/admin_ci', <<~CODE
#!/usr/bin/env bash
gem install bundler
bundle update
bin/rails app:workarea:test:admin
CODE
create_file 'script/core_ci', <<~CODE
#!/usr/bin/env bash
gem install bundler
bundle update
bin/rails app:workarea:test:core
CODE
create_file 'script/plugins_ci', <<~CODE
#!/usr/bin/env bash
gem install bundler
bundle update
bin/rails app:workarea:test:plugins
CODE
create_file 'script/storefront_ci', <<~CODE
#!/usr/bin/env bash
gem install bundler
bundle update
bin/rails app:workarea:test:storefront
CODE
`chmod a+x ../../script/*`
#
# Setup Rakefile
#
remove_file 'Rakefile'
create_file 'Rakefile', <<~CODE
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
require 'rdoc/task'
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = '#{name.titleize}'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
end
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
load 'rails/tasks/statistics.rake'
load 'workarea/changelog.rake'
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
task default: :test
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'workarea/#{name}/version'
desc "Release version \#{Workarea::#{camelized}::VERSION} of the gem"
task :release do
Rake::Task['workarea:changelog'].execute
system 'git add CHANGELOG.md'
system 'git commit -m "Update CHANGELOG"'
system 'git push origin HEAD'
system "git tag -a v\#{Workarea::#{camelized}::VERSION} -m 'Tagging \#{Workarea::#{camelized}::VERSION}'"
system 'git push --tags'
system "gem build workarea-#{name}.gemspec"
system "gem push workarea-#{name}-\#{Workarea::#{camelized}::VERSION}.gem"
system "rm workarea-#{name}-\#{Workarea::#{camelized}::VERSION}.gem"
end
desc 'Run the JavaScript tests'
ENV['TEASPOON_RAILS_ENV'] = File.expand_path('../test/dummy/config/environment', __FILE__)
task teaspoon: 'app:teaspoon'
desc 'Start a server at http://localhost:3000/teaspoon for JavaScript tests'
task :teaspoon_server do
Dir.chdir("test/dummy")
teaspoon_env = File.expand_path('../test/teaspoon_env.rb', __FILE__)
system "RAILS_ENV=test TEASPOON_ENV=\#{teaspoon_env} rails s"
end
CODE
#
# Remove default Rake task
#
remove_file "lib/tasks/#{name}_tasks.rake"
#
# Update .gitignore
#
append_to_file '.gitignore', <<~CODE
.DS_Store
.byebug_history
.bundle/
.sass-cache/
Gemfile.lock
pkg/
test/dummy/tmp/
test/dummy/public/
log/*.log
test/dummy/log/*.log
test/dummy/db/*.sqlite3
test/dummy/db/*.sqlite3-journal
node_modules
package.json
yarn.lock
CODE
#
# Remove MIT License
#
remove_file 'MIT-LICENSE'
#
# Update README
#
remove_file 'README.md'
create_file 'README.md', <<~README
Workarea #{name.titleize}
================================================================================
#{name.titleize} plugin for the Workarea platform.
Getting Started
--------------------------------------------------------------------------------
This gem contains a Rails engine that must be mounted onto a host Rails application.
Then add the gem to your application's Gemfile specifying the source:
# ...
gem 'workarea-#{name}'
# ...
Update your application's bundle.
cd path/to/application
bundle
Workarea Platform Documentation
--------------------------------------------------------------------------------
See [http://developer.workarea.com](http://developer.workarea.com) for Workarea platform documentation.
Copyright & Licensing
--------------------------------------------------------------------------------
Copyright Workarea #{Time.now.year}. All rights reserved.
For licensing, contact [email protected].
README