Browse Source

fix hot contants

main
pvincent 2 months ago
parent
commit
d7e28381c3
  1. 25
      config/initializers/content_security_policy.rb
  2. 16
      config/initializers/inflections.rb
  3. 14
      config/initializers/monkey_patcher.rb
  4. 13
      config/initializers/permissions_policy.rb
  5. 3
      config/initializers/rails_live_reload.rb
  6. 23
      lib/hot/constants.rb
  7. 20
      lib/monkey_patches/rails_live_reload/watcher.rb

25
config/initializers/content_security_policy.rb

@ -1,25 +0,0 @@
# Be sure to restart your server when you modify this file.
# Define an application-wide content security policy.
# See the Securing Rails Applications Guide for more information:
# https://guides.rubyonrails.org/security.html#content-security-policy-header
# Rails.application.configure do
# config.content_security_policy do |policy|
# policy.default_src :self, :https
# policy.font_src :self, :https, :data
# policy.img_src :self, :https, :data
# policy.object_src :none
# policy.script_src :self, :https
# policy.style_src :self, :https
# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"
# end
#
# # Generate session nonces for permitted importmap, inline scripts, and inline styles.
# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
# config.content_security_policy_nonce_directives = %w(script-src style-src)
#
# # Report violations without enforcing the policy.
# # config.content_security_policy_report_only = true
# end

16
config/initializers/inflections.rb

@ -1,16 +0,0 @@
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, "\\1en"
# inflect.singular /^(ox)en/i, "\\1"
# inflect.irregular "person", "people"
# inflect.uncountable %w( fish sheep )
# end
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym "RESTful"
# end

14
config/initializers/monkey_patcher.rb

@ -1,14 +1,14 @@
return unless Rails.application.server? return unless Rails.application.server?
puts 'MonkeyPatcher runs:'
# puts 'MonkeyPatcher runs:'
patches = Dir.glob(Rails.root.join('lib', 'monkey_patches', '**', '*.rb')) patches = Dir.glob(Rails.root.join('lib', 'monkey_patches', '**', '*.rb'))
patches.each do |file| patches.each do |file|
puts "🐵 patching... #{Pathname.new(file).relative_path_from Rails.root}"
# puts "🐵 patching... #{Pathname.new(file).relative_path_from Rails.root}"
require file require file
end end
puts case patches.count
when 0 then 'No patch found'
when 1 then '1 successful patch applied'
else "#{patches.count} successful patches applied"
end
# puts case patches.count
# when 0 then 'No patch found'
# when 1 then '1 successful patch applied'
# else "#{patches.count} successful patches applied"
# end

13
config/initializers/permissions_policy.rb

@ -1,13 +0,0 @@
# Be sure to restart your server when you modify this file.
# Define an application-wide HTTP permissions policy. For further
# information see: https://developers.google.com/web/updates/2018/06/feature-policy
# Rails.application.config.permissions_policy do |policy|
# policy.camera :none
# policy.gyroscope :none
# policy.microphone :none
# policy.usb :none
# policy.fullscreen :self
# policy.payment :self, "https://secure.example.com"
# end

3
config/initializers/rails_live_reload.rb

@ -1,6 +1,5 @@
# frozen_string_literal: true
if defined?(RailsLiveReload) && Rails.env.development? if defined?(RailsLiveReload) && Rails.env.development?
RailsLiveReload.configure do |config| RailsLiveReload.configure do |config|
# HOT Constants # HOT Constants
config.watch(%r{/\.env$}, reload: :always) config.watch(%r{/\.env$}, reload: :always)

23
lib/hot/constants.rb

@ -1,23 +1,25 @@
require 'dotenv' require 'dotenv'
# Hot Live Constants
module Hot::Constants
module Hot
# Hot Live Constants
module Constants
include SemanticLogger::Loggable
HOTENV = {} # rubocop:disable Style/MutableConstant HOTENV = {} # rubocop:disable Style/MutableConstant
LISTENERS = {} # rubocop:disable Style/MutableConstant LISTENERS = {} # rubocop:disable Style/MutableConstant
LOGGER = SemanticLogger[self]
TOTO = 'titi'
class << self class << self
def initialize def initialize
@old_definitions = {} @old_definitions = {}
@hot_definitions = {} @hot_definitions = {}
logger.level = :fatal
load_definitions load_definitions
load_values load_values
logger.level = :info
end end
def load_definitions def load_definitions
LOGGER.debug('--load_definitions3')
logger.debug('--load_definitions3')
new_definitions = Dotenv.parse('.env.sample') new_definitions = Dotenv.parse('.env.sample')
@ -31,7 +33,7 @@ module Hot::Constants
definitions_to_remove = @hot_definitions.except(*new_definitions.keys) definitions_to_remove = @hot_definitions.except(*new_definitions.keys)
definitions_to_remove.each_pair do |key, dkey| definitions_to_remove.each_pair do |key, dkey|
LOGGER.info("remove method <#{dkey}>")
logger.info("remove method <#{dkey}>")
singleton_class.undef_method(dkey) singleton_class.undef_method(dkey)
@hot_definitions.delete(key) @hot_definitions.delete(key)
end end
@ -41,19 +43,19 @@ module Hot::Constants
end end
def load_values def load_values
LOGGER.debug('--load_values')
logger.debug('--load_values')
new_env = Dotenv.parse new_env = Dotenv.parse
constants_to_delete = HOTENV.except(*new_env.keys) constants_to_delete = HOTENV.except(*new_env.keys)
constants_to_delete.each do |name, _| constants_to_delete.each do |name, _|
# FIXME: default should read default and type from @old_definitions # FIXME: default should read default and type from @old_definitions
type, default = @old_definitions[name] type, default = @old_definitions[name]
LOGGER.info("constant <#{name}> reverts to default value <#{default}> of type <#{type}>")
logger.info("constant <#{name}> reverts to default value <#{default}> of type <#{type}>")
end end
constants_to_add = new_env.except(*HOTENV.keys) constants_to_add = new_env.except(*HOTENV.keys)
constants_to_add.each do |constant| constants_to_add.each do |constant|
LOGGER.info("constant to add <#{constant}>")
logger.info("constant to add <#{constant}>")
end end
HOTENV.replace new_env HOTENV.replace new_env
@ -72,7 +74,7 @@ module Hot::Constants
method = infer_method_from_value(value) method = infer_method_from_value(value)
inferred_type = method.name.to_s.split('_')[1] inferred_type = method.name.to_s.split('_')[1]
LOGGER.info("#{mode} method <#{dkey}> of type <#{inferred_type}> with default value <#{value}> ")
logger.info("#{mode} method <#{dkey}> of type <#{inferred_type}> with default value <#{value}> ")
singleton_class.define_method(dkey) { method.call(key, value) } singleton_class.define_method(dkey) { method.call(key, value) }
@hot_definitions.store(key, dkey) @hot_definitions.store(key, dkey)
@ -98,4 +100,5 @@ module Hot::Constants
end end
initialize # done once on first require, cause this is just a module (not a class!) initialize # done once on first require, cause this is just a module (not a class!)
end
end end

20
lib/monkey_patches/rails_live_reload/watcher.rb

@ -2,10 +2,24 @@ module RailsLiveReload
ENV_FILE = Rails.root.join('.env').to_s ENV_FILE = Rails.root.join('.env').to_s
ENV_SAMPLE_FILE = Rails.root.join('.env.sample').to_s ENV_SAMPLE_FILE = Rails.root.join('.env.sample').to_s
INITIALIZER = Rails.root.join('config/initializers/hot_changes.rb').to_s INITIALIZER = Rails.root.join('config/initializers/hot_changes.rb').to_s
CHECKSUMS = {}
CHECKSUMS = {} # rubocop:disable Style/MutableConstant
# MonkeyPath Watcher # MonkeyPath Watcher
class Watcher class Watcher
def initialize
@files = {}
@sockets = []
# puts "Watching: #{root}"
# RailsLiveReload.patterns.each do |pattern, rule|
# puts " #{pattern} => #{rule}"
# end
build_tree
start_socket
start_listener
end
def reload_all def reload_all
before_reload(files) before_reload(files)
data = { event: RailsLiveReload::INTERNAL[:socket_events][:reload], files: }.to_json data = { event: RailsLiveReload::INTERNAL[:socket_events][:reload], files: }.to_json
@ -15,8 +29,8 @@ module RailsLiveReload
private private
def before_reload(files) def before_reload(files)
perform_when_change(files, ENV_SAMPLE_FILE) { HotConstants.load_definitions }
perform_when_change(files, ENV_FILE) { HotConstants.load_values }
perform_when_change(files, ENV_SAMPLE_FILE) { Hot::Constants.load_definitions }
perform_when_change(files, ENV_FILE) { Hot::Constants.load_values }
perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') } perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') }
end end

Loading…
Cancel
Save