-
Notifications
You must be signed in to change notification settings - Fork 9
/
template.rb
executable file
·977 lines (807 loc) · 26.7 KB
/
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
rails_spec = (Gem.loaded_specs["railties"] || Gem.loaded_specs["rails"])
version = rails_spec.version.to_s
mongoid = options[:skip_active_record]
yarn = !options[:skip_yarn]
is_dev = !options[:template].start_with?("http")
spring = !options[:skip_spring]
if is_dev
rocket_cms_path = File.realpath(options[:template] + "/..")
end
if Gem::Version.new(version) < Gem::Version.new('6.0.0')
puts "You are using an old version of Rails (#{version})"
puts "Please update"
puts "Stopping"
exit 1
end
git :init
remove_file 'Gemfile'
create_file 'Gemfile' do <<-TEXT
source 'https://rubygems.org'
#{'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
'}
gem 'rails', '6.0.3.4'
gem 'rails-i18n'
#{if mongoid then "gem 'mongoid', '~> 6.1.0'" else "gem 'pg', '>= 0.18', '< 2.0'" end}
gem 'turbolinks' #required for redirects even if using via webpack
#{
"#{if mongoid then "gem 'rocket_cms_mongoid', path: '#{rocket_cms_path}'" else "gem 'rocket_cms_activerecord', path: '#{rocket_cms_path}'" end}
gem 'rocket_cms', path: '#{rocket_cms_path}'" if is_dev}
#{"#{if mongoid then "gem 'rocket_cms_mongoid'" else "gem 'rocket_cms_activerecord'" end}" if !is_dev}
gem 'glebtv-ckeditor'
# wait for https://github.com/sferik/rails_admin/pull/3207
gem 'rails_admin', github: "sferik/rails_admin"
gem 'slim'
#gem 'sass'
#gem 'sass-rails'
gem 'rs-webpack-rails'
gem 'devise'
gem 'devise-i18n'
gem 'cancancan'
#{if mongoid then "gem 'cancancan'" end}
gem 'cloner'
gem 'puma'
gem 'sentry-raven'
gem 'shrine'
#{"gem 'shrine-mongoid'" if mongoid}
gem 'image_processing'
#gem 'uglifier'
#gem 'rs_russian'
#gem 'enumerize'
#gem 'active_model_serializers'
# windows
gem 'tzinfo-data' if Gem.win_platform?
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
gem 'bootsnap', require: false
gem 'irb'
group :development do
#gem 'binding_of_caller'
#gem 'better_errors', github: 'charliesome/better_errors'
#gem 'pry-rails'
gem 'listen'
gem 'annotate'
#{" gem 'spring'" if spring}
gem 'capistrano', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
gem 'capistrano-rails', require: false
end
group :development, :test do
gem "factory_bot_rails"
gem 'rspec-rails'
#{if mongoid then " gem 'mongoid-rspec'" else "" end}
gem 'capybara'
# https://github.com/mattheworiordan/capybara-screenshot/issues/243
gem 'capybara-screenshot'
gem 'selenium-webdriver'
gem 'webdrivers'
gem 'database_cleaner'
#{if mongoid then " gem 'database_cleaner-mongoid'" else "" end}
#gem 'database_cleaner-redis'
gem 'ffaker'
gem 'timecop'
gem "pry-rails"
gem 'childprocess'
end
TEXT
end
remove_file '.gitignore'
create_file '.gitignore' do <<-TEXT
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
/.bundle
/log/*.log
*.swp
/tmp
/public/system
/public/ckeditor_assets
/public/assets
/node_modules
/public/webpack
#{if mongoid then '/config/mongoid.yml' else '/config/database.yml' end}
/config/secrets.yml
yarn-error.log
spec/examples.txt
TEXT
end
create_file 'extra/.gitkeep', ''
remove_file 'app/controllers/application_controller.rb'
create_file 'app/controllers/application_controller.rb' do <<-TEXT
class ApplicationController < ActionController::Base
include RocketCMS::Controller
def page_title #default page title
"#{app_name}"
end
end
TEXT
end
create_file 'config/navigation.rb' do <<-TEXT
# empty file to please simple_navigation, we are not using it
# See https://github.com/rs-pro/rocket_cms/blob/master/app/controllers/concerns/rs_menu.rb
TEXT
end
port = rand(100..999) * 10
remove_file 'README.md'
create_file 'README.md', "## #{app_name}
Project generated by RocketCMS
ORM: #{if mongoid then 'Mongoid' else 'ActiveRecord' end}
To run (windows):
```
yarn start
bundle exec rails s webrick
```
To run (nix/mac):
```
yarn start
puma
```
"
#create_file '.ruby-version', "2.6.5\n"
#create_file '.ruby-gemset', "#{app_name}\n"
run 'bundle install --without production'
if mongoid
create_file 'config/mongoid.yml' do <<-TEXT
development:
clients:
default:
database: #{app_name.downcase}_development
hosts:
- localhost:27017
test:
clients:
default:
database: #{app_name.downcase}_test
hosts:
- localhost:27017
TEXT
end
else
remove_file 'config/database.yml'
create_file 'config/database.yml' do <<-TEXT
development:
adapter: postgresql
encoding: unicode
database: #{app_name.downcase}_development
pool: 5
host: 'localhost'
username: #{app_name.downcase}
password: #{app_name.downcase}
template: template0
test:
adapter: postgresql
encoding: unicode
database: #{app_name.downcase}_test
pool: 5
host: 'localhost'
username: #{app_name.downcase}
password: #{app_name.downcase}
template: template0
TEXT
end
say "Please create a PostgreSQL user #{app_name.downcase} with password #{app_name.downcase} and a database #{app_name.downcase}_development owned by him for development NOW.", :red
ask("Press <enter> when done.")
end
unless mongoid
generate 'simple_captcha'
end
generate "simple_form:install"
generate "devise:install"
generate "devise", "User"
remove_file "config/locales/devise.en.yml"
remove_file "config/locales/en.yml"
gsub_file 'app/models/user.rb', '# :confirmable, :lockable, :timeoutable and :omniauthable', '# :confirmable, :registerable, :timeoutable and :omniauthable'
gsub_file 'app/models/user.rb', ':registerable,', ' :lockable,'
gsub_file Dir.glob("db/migrate/*_devise_create_users.rb").first, '# t.datetime :locked_at', 't.datetime :locked_at'
if mongoid
gsub_file 'app/models/user.rb', '# field :failed_attempts', 'field :failed_attempts'
gsub_file 'app/models/user.rb', '# field :unlock_token', 'field :unlock_token'
gsub_file 'app/models/user.rb', '# field :locked_at', 'field :locked_at'
end
if mongoid
generate "ckeditor:install", "--orm=mongoid", "--backend=shrine"
else
generate "ckeditor:install", "--orm-active_record", "--backend=shrine"
end
remove_file 'config/initializers/ckeditor_shrine.rb'
unless mongoid
generate "rocket_cms:migration"
generate "rails_admin_settings:migration"
end
generate "rocket_cms:admin"
generate "rocket_cms:ability"
generate "rocket_cms:layout"
generate "rocket_cms:webpack", port+1
unless mongoid
rake "db:migrate"
end
generate "rspec:install"
remove_file 'config/routes.rb'
create_file 'config/routes.rb' do <<-TEXT
Rails.application.routes.draw do
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
mount Ckeditor::Engine => '/ckeditor'
#get 'contacts' => 'contacts#new', as: :contacts
#post 'contacts' => 'contacts#create', as: :create_contacts
#get 'contacts/sent' => 'contacts#sent', as: :contacts_sent
#get 'search' => 'search#index', as: :search
#resources :news, only: [:index, :show]
root to: 'home#index'
get '*slug' => 'pages#show'
resources :pages, only: [:show]
end
TEXT
end
create_file 'config/locales/ru.yml' do <<-TEXT
ru:
TEXT
end
remove_file 'db/seeds.rb'
require 'securerandom'
admin_pw = SecureRandom.urlsafe_base64(6)
create_file 'db/seeds.rb' do <<-TEXT
admin_pw = "#{admin_pw}"
User.destroy_all
User.create!(email: 'admin@#{app_name.dasherize.downcase}.ru', password: admin_pw, password_confirmation: admin_pw)
Page.destroy_all
Menu.destroy_all
News.destroy_all
h = Menu.create(name: 'Главное', text_slug: 'main').id
Page.create!(name: 'О компании', fullpath: '/company', menu_ids: [h], content: 'О Компании')
Page.create!(name: 'Новости', fullpath: '/news', menu_ids: [h])
Page.create!(name: 'Контакты', fullpath: '/contacts', menu_ids: [h], content: 'Текст стр контакты')
3.times do |i|
News.create!(name: "test " + i.to_s, content: "test", time: i.days.ago)
end
TEXT
end
create_file 'app/uploaders/news_uploader.rb' do <<-TEXT
class NewsUploader < Shrine
plugin :determine_mime_type
plugin :validation_helpers
plugin :derivatives
Attacher.validate do
validate_mime_type_inclusion %w[image/jpeg image/gif image/png]
validate_max_size 2.megabytes
end
Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
main: magick.resize_to_limit!(800, 800),
thumb: magick.resize_to_limit!(300, 300)
}
end
end
TEXT
end
create_file 'app/uploaders/og_image_uploader.rb' do <<-TEXT
class OgImageUploader < Shrine
plugin :determine_mime_type
plugin :validation_helpers
Attacher.validate do
validate_mime_type_inclusion %w[image/jpeg image/gif image/png]
validate_max_size 2.megabytes
end
end
TEXT
end
create_file 'extra/shrine/plugins/custom_pretty_location.rb' do <<-TEXT
require 'shrine/plugins/pretty_location'
class Shrine
module Plugins
module CustomPrettyLocation
def self.configure(uploader, **opts)
uploader.opts[:custom_pretty_location] ||= { identifier: :id }
uploader.opts[:custom_pretty_location].merge!(opts)
end
module InstanceMethods
def generate_location(io, **options)
custom_pretty_location(io, **options)
end
def custom_pretty_location(io, name: nil, record: nil, version: nil, derivative: nil, identifier: nil, metadata: {}, **)
if record
namespace = record_namespace(record)
identifier ||= record_identifier(record)
end
basename = basic_location(io, metadata: metadata)
basename = [*(version || derivative), basename].join("-")
[*namespace, *identifier, *name, basename].join("/")
end
private
def record_identifier(record)
id = record.public_send(opts[:custom_pretty_location][:identifier])
case id
when Integer
str_id = "%09d".freeze % id
str_id.scan(/\\d{3}/).join("/".freeze)
when String
id.scan(/.{3}/).first(3).join("/".freeze)
else
# NOTE: 'raise' cannot be used. It fails on save.
nil
end
end
def transform_class_name(class_name)
if opts[:custom_pretty_location][:class_underscore]
class_name.gsub(/([A-Z]+)([A-Z][a-z])/, '\\1_\\2').gsub(/([a-z])([A-Z])/, '\\1_\\2').downcase
else
class_name.downcase
end
end
def record_namespace(record)
class_name = record.class.name or return
parts = transform_class_name(class_name).split("::")
if separator = opts[:custom_pretty_location][:namespace]
parts.join(separator)
else
parts.last
end
end
end
end
register_plugin(:custom_pretty_location, CustomPrettyLocation)
end
end
TEXT
end
remove_file 'app/assets/config/manifest.js'
create_file 'app/assets/config/manifest.js' do <<-TEXT
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link ckcontent.css
//= link ckeditor/application.css
//= link ckeditor/application.js
TEXT
end
remove_file 'config/initializers/assets.rb'
create_file 'config/initializers/assets.rb' do <<-TEXT
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path.
# Rails.application.config.assets.paths << Emoji.images_path
# Add Yarn node_modules folder to the asset load path.
#Rails.application.config.assets.paths << Rails.root.join('node_modules')
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
TEXT
end
create_file 'config/initializers/shrine.rb' do <<-TEXT
require "shrine"
require "shrine/storage/file_system"
Shrine.logger = Rails.logger
# Choose your favorite image processor
require 'image_processing/mini_magick'
SHRINE_PICTURE_PROCESSOR = ImageProcessing::MiniMagick
Shrine.storages = {
# temporary
cache: Shrine::Storage::FileSystem.new(
"public",
prefix: "uploads/cache"
),
# permanent
store: Shrine::Storage::FileSystem.new(
"public",
prefix: "uploads"
),
}
Shrine.plugin :upload_options, cache: { move: true }, store: { move: true }
Shrine.plugin :custom_pretty_location, class_underscore: :true
Shrine.plugin :determine_mime_type
#{"Shrine.plugin :mongoid" if mongoid}
Shrine.plugin :instrumentation
Shrine.plugin :activerecord # loads Active Record integration
Shrine.plugin :cached_attachment_data # enables retaining cached file across form redisplays
Shrine.plugin :restore_cached_data # extracts metadata for assigned cached files
Shrine.plugin :validation_helpers
Shrine.plugin :derivatives
require 'ckeditor/backend/shrine'
TEXT
end
create_file 'config/initializers/rack.rb' do <<-TEXT
Rack::Utils.multipart_part_limit = 0
TEXT
end
create_file 'app/assets/stylesheets/rails_admin/custom/theming.sass' do <<-TEXT
TEXT
end
remove_file 'public/robots.txt'
create_file 'public/robots.txt' do <<-TEXT
User-Agent: *
Disallow: /
TEXT
end
remove_file 'app/helpers/application_helper.rb'
create_file 'app/helpers/application_helper.rb' do <<-TEXT
module ApplicationHelper
def body_class
r = []
#{'r.push "b-#{params[:controller].gsub("/", "_")}"'}
#{'r.push "b-#{params[:controller].gsub("/", "_")}-#{params[:action]}"'}
r.join(" ")
end
end
TEXT
end
remove_file 'config/puma.rb'
create_file 'config/puma.rb' do <<-TEXT
# Min and Max threads per worker
threads 1, 3
current_dir = File.expand_path("../..", __FILE__)
base_dir = File.expand_path("../../..", __FILE__)
# rackup DefaultRackup
# Default to production
rails_env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development"
environment rails_env
if rails_env == 'development'
Puma.windows? { workers 1 }
bind 'tcp://0.0.0.0:#{port}'
else
# https://github.com/seuros/capistrano-puma/blob/642d141ee502546bd5a43a76cd9f6766dc0fcc7a/lib/capistrano/templates/puma.rb.erb#L25
prune_bundler
preload_app!
# Change to match your CPU core count
workers 1
#{'shared_dir = "#{base_dir}/shared"'}
# Set up socket location
#bind 'tcp://0.0.0.0:4000'
#{'socket_dir = "#{shared_dir}/tmp/puma/"'}
#{'Dir.mkdir socket_dir unless File.directory? socket_dir'}
#{'bind "unix://#{shared_dir}/tmp/puma/socket"'}
# Logging
#{'stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true'}
# Set master PID and state locations
#{'pidfile "#{shared_dir}/tmp/puma/pid"'}
#{'state_path "#{shared_dir}/tmp/puma/state"'}
activate_control_app
on_restart do
puts 'Refreshing Gemfile'
#{'ENV["BUNDLE_GEMFILE"] = "#{current_dir}/Gemfile" unless rails_env == \'development\''}
end
#{"#mongoid reconnects by itself" if mongoid}
#{'on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{base_dir}/current/config/database.yml")[rails_env])
end' if !mongoid}
end
TEXT
end
remove_file 'app/views/layouts/application.html.erb'
remove_file 'config/application.rb'
create_file 'config/application.rb' do <<-TEXT
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_model/railtie"
#{'#' if mongoid}require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "active_storage/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module #{app_name.camelize}
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
config.generators do |g|
g.test_framework :rspec
g.view_specs false
g.helper_specs false
g.feature_specs false
g.template_engine :slim
g.stylesheets false
g.javascripts false
g.helper false
g.fixture_replacement :factory_bot, :dir => 'spec/factories'
end
config.i18n.locale = :ru
config.i18n.default_locale = :ru
config.i18n.available_locales = [:ru, :en]
config.i18n.enforce_available_locales = true
# #{'config.active_record.schema_format = :sql' unless mongoid}
#{'config.autoload_paths += %W(#{config.root}/extra)'}
#{'config.eager_load_paths += %W(#{config.root}/extra)'}
config.time_zone = 'Europe/Moscow'
config.webpack.dev_server.manifest_port = #{port+1}
#config.webpack.dev_server.manifest_host = "192.168.1.1"
config.webpack.dev_server.host = proc { request.host }
config.webpack.dev_server.port = #{port+1}
config.webpack.manifest_type = "manifest"
config.webpack.dev_server.enabled = Rails.env.development?
end
end
TEXT
end
remove_file 'app/assets/javascripts/application.js'
create_file 'app/assets/javascripts/application.js' do <<-TEXT
TEXT
end
remove_file 'app/assets/stylesheets/application.css'
create_file 'app/assets/stylesheets/application.css' do <<-TEXT
TEXT
end
create_file 'app/assets/stylesheets/ckcontent.css' do <<-TEXT
div.red {
color: red;
}
TEXT
end
if mongoid
FileUtils.cp(Pathname.new(destination_root).join('config', 'mongoid.yml').to_s, Pathname.new(destination_root).join('config', 'mongoid.yml.example').to_s)
else
FileUtils.cp(Pathname.new(destination_root).join('config', 'database.yml').to_s, Pathname.new(destination_root).join('config', 'database.yml.example').to_s)
end
remove_file 'config/secrets.yml'
key = SecureRandom.hex(64)
create_file 'config/secrets.yml' do <<-TEXT
development:
secret_key_base: #{key}
production:
secret_key_base: #{key}
test:
secret_key_base: #{key}
TEXT
end
remove_file 'config/credentials.yml.enc'
remove_file 'config/master.key'
create_file 'Procfile' do <<-TEXT
web: bundle exec puma
webpack: yarn start
TEXT
end
unless mongoid
create_file 'Dockerfile' do <<-TEXT
# docker build -t #{app_name.downcase} /root/#{app_name.downcase}/
FROM ruby:2.5.1-stretch
RUN apt-get update -qq >/dev/null
RUN apt-get install -y -qq postgresql postgresql-contrib libpq-dev cmake libpq5 >/dev/null
RUN apt-get install -y -qq locales apt-utils apt-transport-https >/dev/null
RUN apt-get install -y -qq qt5-default libqt5webkit5-dev gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x >/dev/null
RUN apt-get install -y -qq xvfb > /dev/null
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq >/dev/null
RUN apt-get install -y -qq yarn >/dev/null
RUN echo "en_US UTF-8" > /etc/locale.gen
RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
RUN /bin/bash -c "source /root/.bashrc && nvm install node && nvm use node"
ENV TZ=Europe/Moscow
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
TEXT
end
create_file '.gitlab-ci.yml' do <<-TEXT
image: #{app_name.downcase}:latest
services:
- postgres:latest
variables:
POSTGRES_DB: #{app_name.downcase}_test
POSTGRES_USER: #{app_name.downcase}
POSTGRES_PASSWORD: "#{app_name.downcase}"
cache:
untracked: true
key: "$CI_PROJECT_ID"
paths:
- node_modules/
- .bundled/
- .yarn
- tmp/cache
rspec:
before_script:
- source /root/.bashrc
- nvm use node
- cp -f config/database.yml.ci config/database.yml
- cp -f config/secrets.yml.sample config/secrets.yml
- cat config/database.yml
- cat config/secrets.yml
- ruby -v
- which ruby
- export RAILS_ENV=test
- export CI=YES
- gem install bundler --no-ri --no-rdoc
- bundle install --jobs $(nproc) --path=/cache/bundler
- yarn --version
- yarn config set cache-folder .yarn
- yarn install
- bundle exec rake webpack:compile
- date
- RAILS_ENV=test bundle exec rake db:create db:schema:load
script:
- RAILS_ENV=test xvfb-run -a bundle exec rspec
- bundle exec pronto run -c=origin/master --exit-code
dependency_scanning:
image: docker:stable
variables:
DOCKER_DRIVER: overlay2
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\\([0-9]*\\)\\.\\([0-9]*\\).*/\\1-\\2-stable/')
- docker run
--env DEP_SCAN_DISABLE_REMOTE_CHECKS="${DEP_SCAN_DISABLE_REMOTE_CHECKS:-false}"
--volume "$PWD:/code"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/dependency-scanning:$SP_VERSION" /code
artifacts:
paths: [gl-dependency-scanning-report.json]
TEXT
end
create_file "config/database.yml.ci" do <<-TEXT
test:
adapter: postgresql
encoding: unicode
database: #{app_name.downcase}_test
pool: 5
host: postgres
username: #{app_name.downcase}
password: #{app_name.downcase}
template: template0
TEXT
end
create_file "config/secrets.yml.sample" do <<-TEXT
test:
secret_key_base: #{key}
TEXT
end
create_file "spec/features/home_page_spec.rb" do <<-TEXT
require 'rails_helper'
RSpec.feature 'main page', :js do
scenario "visit" do
visit root_path
expect(page.status_code).to eq(200).or eq(304)
end
end
TEXT
end
create_file "spec/support/capybara.rb" do <<-TEXT
if ENV['CI']
Selenium::WebDriver::Chrome.path = '/usr/bin/google-chrome-stable'
else
begin
Selenium::WebDriver::Chrome.path = '/usr/bin/chromium'
rescue Selenium::WebDriver::Error::WebDriverError
end
end
Capybara.register_driver :chrome_root do |app|
service = ::Selenium::WebDriver::Service.chrome#(args: { verbose: true, log_path: 'chromedriver.log' })
options = ::Selenium::WebDriver::Chrome::Options.new
options.args << '--headless' unless ENV['NO_HEADLESS']
options.args << '--no-sandbox'
options.args << '--window-size=1280,1024'
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, service: service)
end
# не работает, установлено выше
# Capybara::Screenshot.webkit_options = { width: 1280, height: 1024 }
if ENV['CHROME_VISIBLE']
Capybara.javascript_driver = :selenium_chrome
else
Capybara.javascript_driver = :chrome_root
end
Capybara.default_driver = :rack_test
Capybara.default_max_wait_time = 15
Capybara.register_server :puma do |app, port, host|
require 'rack/handler/puma'
Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: "1:1")
end
Capybara.configure do |config|
config.app_host = "http://\#{Rails.application.secrets.host}"
config.server = :puma
config.server_port = 9332
config.run_server = true
config.always_include_port = true
end
Capybara::Screenshot.register_driver(:selenium_chrome) do |driver, path|
driver.browser.save_screenshot(path)
end
Capybara::Screenshot.register_driver(:chrome_root) do |driver, path|
driver.browser.save_screenshot(path)
end
Capybara::Screenshot.autosave_on_failure = true
TEXT
end
create_file "spec/support/database_cleaner.rb" do <<-TEXT
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
TEXT
end
remove_file "spec/rails_helper.rb"
create_file "spec/rails_helper.rb" do <<-TEXT
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
require 'capybara/rails'
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#\{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
TEXT
end
end # end if unless mongoid
FileUtils.cp(Pathname.new(destination_root).join('config', 'secrets.yml').to_s, Pathname.new(destination_root).join('config', 'secrets.yml.example').to_s)
unless mongoid
#generate "paper_trail:install", "--with-associations"
generate "paper_trail:install"
generate "friendly_id"
rake "db:migrate"
end
if yarn
run 'yarn install'
else
run 'npm install'
end
git add: "."
git commit: %Q{ -m 'Initial commit' }
unless mongoid
rake "db:migrate"
end
rake 'db:seed'