From c318b49c541f974e2617525e45f9efc558d42b71 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 21 Aug 2020 15:31:53 +0300 Subject: [PATCH] Update a template again --- .rubocop.yml | 2 + flame-cli.gemspec | 2 +- spec/flame/cli/new/app_spec.rb | 69 ++++++----- template/.eslintrc.yaml | 4 +- template/.remarkrc.yaml | 2 + .../{.stylelintrc.yml => .stylelintrc.yaml} | 2 +- template/.toys/.toys.rb.erb | 23 ++-- template/application.rb.erb | 13 +-- template/assets/scripts/.eslintrc.yaml | 4 - template/babel.config.json | 8 -- template/browserslist | 2 + template/config/base.rb.erb | 110 ------------------ template/config/full.rb.erb | 16 --- template/config/main.rb.erb | 68 +++++++++++ template/config/processors/sentry.rb.erb | 2 +- template/config/processors/server.rb.erb | 2 + template/config/puma.rb.erb | 10 +- template/constants.rb.erb | 18 +++ template/exe/setup/node.sh | 8 +- template/filewatchers.yaml | 10 +- template/lib/flame/raven_context.rb | 3 +- template/rollup.config.js.erb | 4 - 22 files changed, 165 insertions(+), 217 deletions(-) create mode 100644 template/.remarkrc.yaml rename template/{.stylelintrc.yml => .stylelintrc.yaml} (99%) delete mode 100644 template/babel.config.json create mode 100644 template/browserslist delete mode 100644 template/config/base.rb.erb delete mode 100644 template/config/full.rb.erb create mode 100644 template/config/main.rb.erb create mode 100644 template/constants.rb.erb diff --git a/.rubocop.yml b/.rubocop.yml index 159dc1f..f4867c1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -42,6 +42,8 @@ Metrics/BlockLength: - 'spec/**/*' - '*.gemspec' +RSpec/MultipleMemoizedHelpers: + Enabled: false ## Because of we're testing CLI through native calls RSpec/DescribeClass: Enabled: false diff --git a/flame-cli.gemspec b/flame-cli.gemspec index e2898e5..3d6ee00 100644 --- a/flame-cli.gemspec +++ b/flame-cli.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 2.0' spec.add_development_dependency 'gem_toys', '~> 0.3.0' - spec.add_development_dependency 'toys', '~> 0.10.4' + spec.add_development_dependency 'toys', '~> 0.11.0' spec.add_development_dependency 'codecov', '~> 0.2.0' spec.add_development_dependency 'rspec', '~> 3.9' diff --git a/spec/flame/cli/new/app_spec.rb b/spec/flame/cli/new/app_spec.rb index 63708f2..9b31e29 100644 --- a/spec/flame/cli/new/app_spec.rb +++ b/spec/flame/cli/new/app_spec.rb @@ -14,9 +14,10 @@ let(:template_dir) { "#{root_dir}/template" } let(:template_dir_pathname) { Pathname.new(template_dir) } let(:template_ext) { '.erb' } + let(:temp_app_dir) { "#{root_dir}/#{app_name}" } after do - FileUtils.rm_r "#{root_dir}/#{app_name}" + FileUtils.rm_r temp_app_dir end describe 'output' do @@ -30,10 +31,9 @@ '- README.md', '- application.rb', '- config.ru', - '- config/base.rb', '- config/database.example.yaml', - '- config/full.rb', '- config/mail.example.yaml', + '- config/main.rb', '- config/processors/mail.rb', '- config/processors/r18n.rb', '- config/processors/sentry.rb', @@ -43,6 +43,7 @@ '- config/puma.rb', '- config/sentry.example.yaml', '- config/site.example.yaml', + '- constants.rb', '- controllers/_controller.rb', '- controllers/site/_controller.rb', '- controllers/site/index_controller.rb', @@ -52,6 +53,9 @@ '- mailers/mail/default.rb', '- rollup.config.js', '- routes.rb', + '- views/site/errors/400.html.erb', + '- views/site/errors/404.html.erb', + '- views/site/errors/500.html.erb', '- views/site/index.html.erb', '- views/site/layout.html.erb', # 'Grant permissions to files...', @@ -96,7 +100,7 @@ end describe 'cleans directories' do - subject { Dir.glob("#{app_name}/**/.keep", File::FNM_DOTMATCH) } + subject { Dir.glob("#{temp_app_dir}/**/.keep", File::FNM_DOTMATCH) } before { execute_command } @@ -114,8 +118,7 @@ let(:expected_words) do [ 'FB::Application', - 'expand FlameGenerateToys::Template, namespace: FooBar', - 'FB::Config::Base.new' + 'expand FlameGenerateToys::Template, namespace: FooBar' ] end @@ -125,10 +128,7 @@ describe 'application.rb' do let(:expected_words) do [ - 'config = FooBar::Config::Base.new', - 'FooBar.complete_config config', - 'module FooBar', - 'class Application < Flame::Application' + 'module FooBar' ] end @@ -150,23 +150,11 @@ it { is_expected.to match_words(*expected_words) } end - describe 'config/base.rb' do - let(:expected_words) do - [ - 'module FooBar', - '::FB = ::FooBar', - 'APP_DIRS =' - ] - end - - it { is_expected.to match_words(*expected_words) } - end - - describe 'config/full.rb' do + describe 'config/main.rb' do let(:expected_words) do [ - 'module FooBar', - 'FB::Config::Processors.const_get(processor_name).new config' + 'config = FB::Application.config', + 'FB::Config::Processors.const_get(processor_name).new self' ] end @@ -176,7 +164,7 @@ describe 'config/puma.rb' do let(:expected_words) do [ - 'config = FooBar::Config::Base.new' + 'config = FB::Application.config' ] end @@ -277,6 +265,17 @@ it { is_expected.to match_words(*expected_words) } end + describe 'constants.rb' do + let(:expected_words) do + [ + 'module FooBar', + '::FB = ::FooBar' + ] + end + + it { is_expected.to match_words(*expected_words) } + end + describe 'controllers/_controller.rb' do let(:expected_words) do [ @@ -506,20 +505,18 @@ end end - around do |example| - ## HACK: https://github.com/dazuma/toys/issues/57 - original_toys_file_name = "#{__dir__}/../../../../.toys.rb" - File.rename original_toys_file_name, "#{original_toys_file_name}.bak" - - example.run - - File.rename "#{original_toys_file_name}.bak", original_toys_file_name - end - before do Bundler.with_unbundled_env do execute_command + ## HACK: https://github.com/dazuma/toys/issues/57 + toys_command = 'truncate_load_path!' + temp_app_toys_file_path = "#{temp_app_dir}/.toys/.toys.rb" + File.write( + temp_app_toys_file_path, + File.read(temp_app_toys_file_path).sub("# #{toys_command}", toys_command) + ) + Dir.chdir app_name Dir['config/*.example.yaml'].each do |config_example_file_name| diff --git a/template/.eslintrc.yaml b/template/.eslintrc.yaml index 9cc83bb..0f75f9d 100644 --- a/template/.eslintrc.yaml +++ b/template/.eslintrc.yaml @@ -23,7 +23,7 @@ rules: - avoidEscape: true semi: - error - - always + - never no-multi-spaces: - error keyword-spacing: @@ -61,6 +61,8 @@ rules: - allow: - error - warn + no-var: + - error arrow-body-style: - warn arrow-parens: diff --git a/template/.remarkrc.yaml b/template/.remarkrc.yaml new file mode 100644 index 0000000..755a2b1 --- /dev/null +++ b/template/.remarkrc.yaml @@ -0,0 +1,2 @@ +plugins: + - remark-preset-lint-recommended diff --git a/template/.stylelintrc.yml b/template/.stylelintrc.yaml similarity index 99% rename from template/.stylelintrc.yml rename to template/.stylelintrc.yaml index 97ebd81..b433c4e 100644 --- a/template/.stylelintrc.yml +++ b/template/.stylelintrc.yaml @@ -9,7 +9,7 @@ rules: - tab max-line-length: - - 80 + - 100 - ignorePattern: /// https?:/// at-rule-no-unknown: diff --git a/template/.toys/.toys.rb.erb b/template/.toys/.toys.rb.erb index 524e03e..cdcef0c 100644 --- a/template/.toys/.toys.rb.erb +++ b/template/.toys/.toys.rb.erb @@ -1,20 +1,25 @@ # frozen_string_literal: true -include :bundler, static: true +## Uncomment if you want, read discussion here: https://github.com/dazuma/toys/issues/57 +# truncate_load_path! -config_dir = "#{__dir__}/../config" +include :bundler, static: true -require "#{config_dir}/base" +root_dir = "#{__dir__}/.." +config_dir = "#{root_dir}/config" +require "#{root_dir}/constants" require 'benchmark_toys' expand BenchmarkToys::Template alias_tool :b, :benchmark application_proc = proc do - require_relative '../application' + require "#{config_dir}/main" <%= @short_module_name %>::Application end +config_proc = proc { application_proc.call.config } + require 'config_toys' expand ConfigToys::Template, config_dir: config_dir @@ -27,10 +32,7 @@ expand SequelMigrationsToys::Template, db_connection_proc: db_connection_proc require 'psql_toys' expand PSQLToys::Template, - db_config_proc: (proc do - ## For full config, not base - application_proc.call.config[:database] - end), + db_config_proc: proc { config_proc.call[:database] }, db_connection_proc: db_connection_proc, db_extensions: %w[citext pgcrypto].freeze @@ -46,10 +48,7 @@ require 'flame_routes_toys' expand FlameRoutesToys::Template, application_proc: application_proc require 'flame_server_toys' -expand FlameServerToys::Template, - config_proc: (proc do - <%= @short_module_name %>::Config::Base.new - end) +expand FlameServerToys::Template, config_proc: config_proc require 'locales_toys' expand LocalesToys::Template diff --git a/template/application.rb.erb b/template/application.rb.erb index e5afc3c..e3f456f 100644 --- a/template/application.rb.erb +++ b/template/application.rb.erb @@ -1,14 +1,9 @@ # frozen_string_literal: true -require_relative 'config/base' -config = <%= @module_name %>::Config::Base.new - ## Require gems require 'bundler/setup' Bundler.require( - :system, :server, :database, - :translations, :forms, :views, :assets, :mails, :others, - config[:environment] + :system, :server, :database, :translations, :forms, :views, :assets, :mails, :others ) require 'erubi/capture_end' @@ -27,9 +22,7 @@ require 'pp' # require 'money/bank/google_currency' -## Load full application config, with dependencies from Bundler -require_relative 'config/full' -<%= @module_name %>.complete_config config +require_relative 'constants' module <%= @module_name %> ## Class for application @@ -74,5 +67,3 @@ module <%= @module_name %> end end end - -<%= @short_module_name %>::Application.config = config diff --git a/template/assets/scripts/.eslintrc.yaml b/template/assets/scripts/.eslintrc.yaml index 4b47c3e..cf7ae01 100644 --- a/template/assets/scripts/.eslintrc.yaml +++ b/template/assets/scripts/.eslintrc.yaml @@ -7,7 +7,3 @@ parserOptions: ## Libs: # google: true # Cccombo: true - -rules: - no-var: - - error diff --git a/template/babel.config.json b/template/babel.config.json deleted file mode 100644 index d2020cd..0000000 --- a/template/babel.config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "presets": [ - ["@babel/preset-env", { "modules": false }] - ], - "plugins": [ - "@babel/plugin-transform-object-assign" - ] -} diff --git a/template/browserslist b/template/browserslist new file mode 100644 index 0000000..e639fb0 --- /dev/null +++ b/template/browserslist @@ -0,0 +1,2 @@ +> 1% +not IE 11 diff --git a/template/config/base.rb.erb b/template/config/base.rb.erb deleted file mode 100644 index 4972030..0000000 --- a/template/config/base.rb.erb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -require 'yaml' - -module <%= @module_name %> - ::<%= @short_module_name %> = ::<%= @module_name %> - - APP_DIRS = [ - 'lib', - 'config', - 'models', - # 'policies', - 'helpers', - 'mailers', - # 'actions', - 'forms', - # 'view_objects', - 'controllers' - ].freeze - - module Config - ## Class for config like a Hash with helper methods. - ## It was a part of the Flame, but some places, like Puma config, - ## need for this without Bundler (dependecies): - ## https://github.com/puma/puma/issues/2319 - class Base < Hash - ## Create an instance of application config - def initialize - populate_dirs - - self[:stdout_file] = "#{self[:logs_dir]}/out" - self[:stderr_file] = "#{self[:logs_dir]}/err" - - require_relative 'processors/server' - Processors::Server.new self - - %i[session site].each do |config_name| - load_yaml config_name, required: true - end - end - - ## Method for loading YAML-files from config directory - ## @param name [Symbol] - ## file base name (extension is `.yml` or '.yaml') - ## @example Load SMTP file without extension, by Symbol - ## config.load_yaml(:smtp) - def load_yaml(name, required: false) - file_name = "#{name}.y{a,}ml" - - file_path = find_config_file file_name, required: required - - return unless file_path - - self[name] = YAML.load_file(file_path) - end - - private - - def populate_dirs - self[:root_dir] = File.realpath "#{__dir__}/.." - - %i[config logs public tmp views].each do |dir_name| - self[:"#{dir_name}_dir"] = "#{self[:root_dir]}/#{dir_name}" - end - - self[:pids_dir] = "#{self[:tmp_dir]}/pids" - end - - def find_config_file(file_name, required:) - file_path = nil - - loop do - file_path = Dir[File.join(self[:config_dir], file_name)].first - break if file_path - - config_relative_dir = self[:config_dir].sub(self[:root_dir], '') - puts "Config file '#{file_name}' not found in '#{config_relative_dir}'" - - next if ask_to_check_config_files - - required ? abort : break - end - - file_path - end - - def ask_to_check_config_files - highline.choose do |menu| - menu.layout = :one_line - - menu.prompt = 'Do you want to check config files? ' - - menu.choice(:yes) do - system 'toys config check' - true - end - - menu.choice(:no) { false } - end - end - - def highline - @highline ||= begin - require 'highline' - HighLine.new - end - end - end - end -end diff --git a/template/config/full.rb.erb b/template/config/full.rb.erb deleted file mode 100644 index d8cace6..0000000 --- a/template/config/full.rb.erb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -## Config split into `base` and `full` because of `prune_bundler`: -## https://github.com/puma/puma/issues/2319 -module <%= @module_name %> - class << self - using GorillaPatch::Inflections - - def complete_config(config) - %w[Sentry R18n Mail Sequel Shrine].each do |processor_name| - require_relative "processors/#{processor_name.underscore}" - <%= @short_module_name %>::Config::Processors.const_get(processor_name).new config - end - end - end -end diff --git a/template/config/main.rb.erb b/template/config/main.rb.erb new file mode 100644 index 0000000..b6aaa26 --- /dev/null +++ b/template/config/main.rb.erb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require_relative '../application' + +config = <%= @short_module_name %>::Application.config + +class << config + private + + def find_config_file(filename, required:) + file_path = nil + + loop do + file_path = super + break + rescue Flame::Errors::ConfigFileNotFoundError => e + puts e.message + + next if ask_to_check_config_files + + required ? abort : break + end + + file_path + end + + def ask_to_check_config_files + highline.choose do |menu| + menu.layout = :one_line + + menu.prompt = 'Do you want to check config files? ' + + menu.choice(:yes) do + system 'toys config check' + true + end + + menu.choice(:no) { false } + end + end + + def highline + @highline ||= begin + require 'highline' + HighLine.new + end + end +end + +using GorillaPatch::Inflections + +config.instance_exec do + self[:root_dir] = File.realpath "#{__dir__}/.." + + self[:pids_dir] = "#{self[:tmp_dir]}/pids" + + self[:stdout_file] = "#{self[:log_dir]}/out" + self[:stderr_file] = "#{self[:log_dir]}/err" + + %i[session site].each do |config_name| + load_yaml config_name, required: true + end + + %w[Server Sentry R18n Mail Sequel Shrine].each do |processor_name| + require_relative "processors/#{processor_name.underscore}" + <%= @short_module_name %>::Config::Processors.const_get(processor_name).new self + end +end diff --git a/template/config/processors/sentry.rb.erb b/template/config/processors/sentry.rb.erb index 4948fe9..b140f4b 100644 --- a/template/config/processors/sentry.rb.erb +++ b/template/config/processors/sentry.rb.erb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'raven' +require_relative '../../lib/flame/raven_context' module <%= @module_name %> module Config diff --git a/template/config/processors/server.rb.erb b/template/config/processors/server.rb.erb index df2ed70..e7b42ba 100644 --- a/template/config/processors/server.rb.erb +++ b/template/config/processors/server.rb.erb @@ -11,6 +11,8 @@ module <%= @module_name %> environment = config[:environment] = ENV['RACK_ENV'] ||= server_config[:environment] || 'development' + Bundler.require environment + server_config = config[:server] = server_config[environment] server_config[:puma_pid_file] = "#{config[:pids_dir]}/#{server_config[:puma_pid_file]}" diff --git a/template/config/puma.rb.erb b/template/config/puma.rb.erb index 0039af7..b478d67 100644 --- a/template/config/puma.rb.erb +++ b/template/config/puma.rb.erb @@ -3,9 +3,9 @@ require 'fileutils' require 'etc' -require_relative 'base' +require_relative 'main' -config = <%= @module_name %>::Config::Base.new +config = <%= @short_module_name %>::Application.config server_config = config[:server] @@ -13,7 +13,9 @@ environment config[:environment] directory config[:root_dir] -prune_bundler +## Don't call phase-restart +## https://github.com/puma/puma/issues/2319 +# prune_bundler rackup 'config.ru' @@ -24,7 +26,7 @@ FileUtils.mkdir_p pids_dir pidfile File.join pids_dir, 'puma.pid' state_path File.join pids_dir, 'puma.state' -FileUtils.mkdir_p config[:logs_dir] +FileUtils.mkdir_p config[:log_dir] if server_config[:daemonize] stdout_redirect( diff --git a/template/constants.rb.erb b/template/constants.rb.erb new file mode 100644 index 0000000..f5fffce --- /dev/null +++ b/template/constants.rb.erb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module <%= @module_name %> + ::<%= @short_module_name %> = ::<%= @module_name %> + + APP_DIRS = [ + 'lib', + 'config', + 'models', + # 'policies', + 'helpers', + 'mailers', + # 'actions', + 'forms', + # 'view_objects', + 'controllers' + ].freeze +end diff --git a/template/exe/setup/node.sh b/template/exe/setup/node.sh index cce3cc9..d0398ea 100755 --- a/template/exe/setup/node.sh +++ b/template/exe/setup/node.sh @@ -17,6 +17,10 @@ then fi fi -exe npm install +exe npm install -g pnpm -exe npm run build +exe nodenv rehash + +exe pnpm install + +exe pnpm run build diff --git a/template/filewatchers.yaml b/template/filewatchers.yaml index 2176609..448bcc8 100644 --- a/template/filewatchers.yaml +++ b/template/filewatchers.yaml @@ -1,12 +1,12 @@ - :pattern: '**/{Gemfile,*.{rb,ru,y{a,}ml}}' :exclude: '**/{spec/**/*,config/**/*.example*}' - :command: bundle exec pumactl restart -F config/puma.rb + :command: bundle exec pumactl restart - :pattern: '{assets/styles/**/*,postcss.config.js}' - :command: npm run build:styles + :command: pnpm run build:styles -- :pattern: '{assets/scripts/**/*,babel.config.json,rollup.config.js}' - :command: npm run build:scripts +- :pattern: '{assets/scripts/**/*,rollup.config.js}' + :command: pnpm run build:scripts - :pattern: 'package.json' - :command: npm install && npm run build + :command: pnpm install && pnpm run build diff --git a/template/lib/flame/raven_context.rb b/template/lib/flame/raven_context.rb index 99a48b9..3fa2c34 100644 --- a/template/lib/flame/raven_context.rb +++ b/template/lib/flame/raven_context.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -require 'flame/raven_context' +## Required via `Bundler.require` in `application.rb` +# require 'flame/raven_context' module Flame ## Redefine `user` object diff --git a/template/rollup.config.js.erb b/template/rollup.config.js.erb index 7ab4d53..7a36240 100644 --- a/template/rollup.config.js.erb +++ b/template/rollup.config.js.erb @@ -1,7 +1,6 @@ import json from '@rollup/plugin-json'; import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; -import babel from '@rollup/plugin-babel'; export default { output: { @@ -16,8 +15,5 @@ export default { browser: true }), commonjs(), - babel({ - babelHelpers: 'bundled' - }) ] };