forked from workarea-commerce/workarea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_template.rb
451 lines (360 loc) · 14.7 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
=begin
To generate a new plugin, run:
rails plugin new path/to/my_plugin \
--template=https://raw.githubusercontent.com/workarea-commerce/workarea/master/plugin_template.rb \
--full \
--skip-spring \
--skip-active-record \
--skip-action-cable \
--skip-puma \
--skip-coffee \
--skip-turbolinks \
--skip-bootsnap \
--skip-yarn \
--skip-webpack-install
=end
#
# Namespace plugin under Workarea
#
remove_file "lib/#{name}.rb"
create_file "lib/workarea/#{name}.rb", <<~CODE
require 'workarea'
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 = "1.0.0.pre".freeze
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
create_file "app/assets/images/workarea/admin/#{name}/.keep"
create_file "app/assets/images/workarea/storefront/#{name}/.keep"
remove_dir "app/assets/images/#{name}"
create_file "app/assets/javascripts/workarea/admin/#{name}/.keep"
create_file "app/assets/javascripts/workarea/storefront/#{name}/.keep"
remove_dir "app/assets/javascripts/#{name}"
create_file "app/assets/stylesheets/workarea/admin/#{name}/.keep"
create_file "app/assets/stylesheets/workarea/storefront/#{name}/.keep"
remove_dir "app/assets/stylesheets/#{name}"
#
# 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'
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'
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[\s=]+).*/, '\\1`git ls-files`.split("\\n")'
#
# Modify licence, rails & sqlite dependencies
#
gsub_file "#{name}.gemspec", /(spec.license[^'"]+['"])MIT/, '\\1Business Software License'
gsub_file "#{name}.gemspec", /spec.add_dependency.+rails.+/, ''
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$/
#
# Cleanup whitespace
#
gsub_file "#{name}.gemspec", /^\s*\n/, "\n\n"
gsub_file "#{name}.gemspec", /^\n\n/, "\n"
#
# 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"
#
# 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 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 tag -a v\#{Workarea::#{camelized}::VERSION} -m 'Tagging \#{Workarea::#{camelized}::VERSION}'"
system 'git push origin HEAD --follow-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
yarn.lock
yarn-error.log
package-lock.json
CODE
#
# Replace License
#
remove_file 'MIT-LICENSE'
create_file 'LICENSE', <<~CODE
WebLinc
Business Source License
Licensor: WebLinc Corporation, 22 S. 3rd Street, 2nd Floor, Philadelphia PA 19106
Licensed Work: Workarea Commerce Platform
The Licensed Work is (c) 2019 WebLinc Corporation
Additional Use Grant:
You may make production use of the Licensed Work without an additional license agreement with WebLinc so long as you do not use the Licensed Work for a Commerce Service.
A "Commerce Service" is a commercial offering that allows third parties (other than your employees and contractors) to access the functionality of the Licensed Work by creating or managing commerce functionality, the products, taxonomy, assets and/or content of which are controlled by such third parties.
For information about obtaining an additional license agreement with WebLinc, contact licensing@workarea.com.
Change Date: 2019-08-20
Change License: Version 2.0 or later of the GNU General Public License as published by the Free Software Foundation
Terms
The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use.
Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate.
If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work.
All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor.
You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work.
Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work.
This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. MariaDB hereby grants you permission to use this License’s text to license your works and to refer to it using the trademark "Business Source License" as long as you comply with the Covenants of Licensor below.
Covenants of Licensor
In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor:
To specify as the Change License the GPL Version 2.0 or any later version, or a license that is compatible with GPL Version 2.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 2.0 or a later version. Licensor may specify additional Change Licenses without limitation.
To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None."
To specify a Change Date.
Not to modify this License in any other way.
Notice
The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License.
For more information on the use of the Business Source License generally, please visit the Adopting and Developing Business Source License FAQ.
License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab.
CODE
#
# Update README
#
remove_file 'README.md'
create_file 'README.md', <<~README
Workarea #{name.titleize}
================================================================================
#{name.titleize} plugin for the Workarea platform.
Overview
--------------------------------------------------------------------------------
1. TODO
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
Features
--------------------------------------------------------------------------------
### TODO
Workarea Platform Documentation
--------------------------------------------------------------------------------
See [https://developer.workarea.com](https://developer.workarea.com) for Workarea platform documentation.
License
--------------------------------------------------------------------------------
Workarea #{name.titleize} is released under the [Business Software License](LICENSE)
README