From cbd613b29133cf10dbf5b067f401826f531aad03 Mon Sep 17 00:00:00 2001 From: Jeff Peterson Date: Mon, 21 Oct 2024 10:34:35 -0700 Subject: [PATCH] get it working --- Gemfile.lock | 240 ++++++++++++++++++ lib/generators/patch_me_in/USAGE | 8 + lib/generators/patch_me_in/patch_generator.rb | 7 + lib/generators/patch_me_in/templates/patch.tt | 6 + lib/patch.rb | 6 - lib/patch/base.rb | 21 -- lib/patch/engine.rb | 13 - lib/patch_me_in.rb | 6 + lib/patch_me_in/base.rb | 30 +++ lib/patch_me_in/engine.rb | 14 + lib/{patch => patch_me_in}/version.rb | 2 +- patch.gemspec | 26 -- patch_me_in.gemspec | 22 ++ .../generators/patch/patch_generator_test.rb | 16 ++ test/patch_test.rb | 2 +- 15 files changed, 351 insertions(+), 68 deletions(-) create mode 100644 Gemfile.lock create mode 100644 lib/generators/patch_me_in/USAGE create mode 100644 lib/generators/patch_me_in/patch_generator.rb create mode 100644 lib/generators/patch_me_in/templates/patch.tt delete mode 100644 lib/patch.rb delete mode 100644 lib/patch/base.rb delete mode 100644 lib/patch/engine.rb create mode 100644 lib/patch_me_in.rb create mode 100644 lib/patch_me_in/base.rb create mode 100644 lib/patch_me_in/engine.rb rename lib/{patch => patch_me_in}/version.rb (58%) delete mode 100644 patch.gemspec create mode 100644 patch_me_in.gemspec create mode 100644 test/lib/generators/patch/patch_generator_test.rb diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..79e1b50 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,240 @@ +PATH + remote: . + specs: + patch_me_in (0.1.0) + rails (>= 8.0.0.beta1) + +GEM + remote: https://rubygems.org/ + specs: + actioncable (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + zeitwerk (~> 2.6) + actionmailbox (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activestorage (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + mail (>= 2.8.0) + actionmailer (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + actionview (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + mail (>= 2.8.0) + rails-dom-testing (~> 2.2) + actionpack (8.0.0.beta1) + actionview (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + nokogiri (>= 1.8.5) + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + useragent (~> 0.16) + actiontext (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activestorage (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (8.0.0.beta1) + activesupport (= 8.0.0.beta1) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activejob (8.0.0.beta1) + activesupport (= 8.0.0.beta1) + globalid (>= 0.3.6) + activemodel (8.0.0.beta1) + activesupport (= 8.0.0.beta1) + activerecord (8.0.0.beta1) + activemodel (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + timeout (>= 0.4.0) + activestorage (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + marcel (~> 1.0) + activesupport (8.0.0.beta1) + base64 + benchmark (>= 0.3) + bigdecimal + concurrent-ruby (~> 1.0, >= 1.3.1) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + logger (>= 1.4.2) + minitest (>= 5.1) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + uri (>= 0.13.1) + ast (2.4.2) + base64 (0.2.0) + benchmark (0.3.0) + bigdecimal (3.1.8) + builder (3.3.0) + concurrent-ruby (1.3.4) + connection_pool (2.4.1) + crass (1.0.6) + date (3.3.4) + drb (2.2.1) + erubi (1.13.0) + globalid (1.2.1) + activesupport (>= 6.1) + i18n (1.14.6) + concurrent-ruby (~> 1.0) + io-console (0.7.2) + irb (1.14.1) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + json (2.7.2) + language_server-protocol (3.17.0.3) + logger (1.6.1) + loofah (2.22.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.4) + mini_mime (1.1.5) + minitest (5.25.1) + net-imap (0.5.0) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.5.0) + net-protocol + nio4r (2.7.3) + nokogiri (1.16.7-arm64-darwin) + racc (~> 1.4) + parallel (1.26.3) + parser (3.3.5.0) + ast (~> 2.4.1) + racc + propshaft (1.1.0) + actionpack (>= 7.0.0) + activesupport (>= 7.0.0) + rack + railties (>= 7.0.0) + psych (5.1.2) + stringio + puma (6.4.3) + nio4r (~> 2.0) + racc (1.8.1) + rack (3.1.7) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) + rails (8.0.0.beta1) + actioncable (= 8.0.0.beta1) + actionmailbox (= 8.0.0.beta1) + actionmailer (= 8.0.0.beta1) + actionpack (= 8.0.0.beta1) + actiontext (= 8.0.0.beta1) + actionview (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activemodel (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activestorage (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + bundler (>= 1.15.0) + railties (= 8.0.0.beta1) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + railties (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) + irb (~> 1.13) + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rainbow (3.1.1) + rake (13.2.1) + rdoc (6.7.0) + psych (>= 4.0.0) + regexp_parser (2.9.2) + reline (0.5.10) + io-console (~> 0.5) + rubocop (1.66.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.4, < 3.0) + rubocop-ast (>= 1.32.2, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.32.3) + parser (>= 3.3.1.0) + rubocop-minitest (0.36.0) + rubocop (>= 1.61, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + rubocop-performance (1.22.1) + rubocop (>= 1.48.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + rubocop-rails (2.26.2) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.52.0, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + rubocop-rails-omakase (1.0.0) + rubocop + rubocop-minitest + rubocop-performance + rubocop-rails + ruby-progressbar (1.13.0) + securerandom (0.3.1) + sqlite3 (2.1.0-arm64-darwin) + stringio (3.1.1) + thor (1.3.2) + timeout (0.4.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.6.0) + uri (0.13.1) + useragent (0.16.10) + webrick (1.8.2) + websocket-driver (0.7.6) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.6.18) + +PLATFORMS + arm64-darwin + +DEPENDENCIES + patch_me_in! + propshaft + puma + rubocop-rails-omakase + sqlite3 + +BUNDLED WITH + 2.5.21 diff --git a/lib/generators/patch_me_in/USAGE b/lib/generators/patch_me_in/USAGE new file mode 100644 index 0000000..ee90377 --- /dev/null +++ b/lib/generators/patch_me_in/USAGE @@ -0,0 +1,8 @@ +Description: + Explain the generator + +Example: + bin/rails generate patch Thing + + This will create: + what/will/it/create diff --git a/lib/generators/patch_me_in/patch_generator.rb b/lib/generators/patch_me_in/patch_generator.rb new file mode 100644 index 0000000..01dbafb --- /dev/null +++ b/lib/generators/patch_me_in/patch_generator.rb @@ -0,0 +1,7 @@ +class PatchGenerator < Rails::Generators::NamedBase + source_root File.expand_path("templates", __dir__) + + def create_patch_file + template "patch.tt", "app/patches/#{file_name}_patch.rb" + end +end diff --git a/lib/generators/patch_me_in/templates/patch.tt b/lib/generators/patch_me_in/templates/patch.tt new file mode 100644 index 0000000..e4284fe --- /dev/null +++ b/lib/generators/patch_me_in/templates/patch.tt @@ -0,0 +1,6 @@ +module <%= class_name %>Patch + include PatchMeIn::Base + + def some_new_method + end +end diff --git a/lib/patch.rb b/lib/patch.rb deleted file mode 100644 index d72a2ae..0000000 --- a/lib/patch.rb +++ /dev/null @@ -1,6 +0,0 @@ -require "patch/base" -require "patch/version" -require "patch/engine" - -module Patch -end diff --git a/lib/patch/base.rb b/lib/patch/base.rb deleted file mode 100644 index dc53448..0000000 --- a/lib/patch/base.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Patch - module Base - extend ActiveSupport::Concern - - @@patches = [] - - included do - extend ActiveSupport::Concern - end - - class_methods do - def patches(klass) - @@patches << klass - end - - def apply! - @@patches.each { _1.include self } - end - end - end -end diff --git a/lib/patch/engine.rb b/lib/patch/engine.rb deleted file mode 100644 index 7c326b9..0000000 --- a/lib/patch/engine.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Patch - class Engine < ::Rails::Engine - isolate_namespace Patch - - initializer "patch.reloader" do |app| - app.reloader.to_prepare do - app.root.glob("app/patches/**/*.rb") do |name| - name.sub(/\.rb$/, "").camelize.constantize.apply! - end - end - end - end -end diff --git a/lib/patch_me_in.rb b/lib/patch_me_in.rb new file mode 100644 index 0000000..dc5f5b5 --- /dev/null +++ b/lib/patch_me_in.rb @@ -0,0 +1,6 @@ +require "patch_me_in/base" +require "patch_me_in/version" +require "patch_me_in/engine" + +module PatchMeIn +end diff --git a/lib/patch_me_in/base.rb b/lib/patch_me_in/base.rb new file mode 100644 index 0000000..541a2ae --- /dev/null +++ b/lib/patch_me_in/base.rb @@ -0,0 +1,30 @@ +module PatchMeIn + module Base + extend ActiveSupport::Concern + + included do + extend ActiveSupport::Concern + @@patches = nil + end + + class_methods do + def patches(name) + name = name.name if name.is_a?(Module) + @@patches = name + end + + def patched_name + @@patches || name.sub(/Patch$/, "") + end + + def patched_module + patched_name.constantize + end + + def apply! + puts "Applying #{patched_name} to #{self}" + patched_module.tap { _1.include self } + end + end + end +end diff --git a/lib/patch_me_in/engine.rb b/lib/patch_me_in/engine.rb new file mode 100644 index 0000000..4326a3f --- /dev/null +++ b/lib/patch_me_in/engine.rb @@ -0,0 +1,14 @@ +module PatchMeIn + class Engine < ::Rails::Engine + isolate_namespace PatchMeIn + + initializer "patch_me_in.reloader" do |app| + app.reloader.to_prepare do + Dir.glob("**/*.rb", base: app.root / "app/patches") do |name| + puts "Patching #{name}" + name.sub(/\.rb$/, "").camelize.constantize.apply! + end + end + end + end +end diff --git a/lib/patch/version.rb b/lib/patch_me_in/version.rb similarity index 58% rename from lib/patch/version.rb rename to lib/patch_me_in/version.rb index 8f6dea2..0510d2e 100644 --- a/lib/patch/version.rb +++ b/lib/patch_me_in/version.rb @@ -1,3 +1,3 @@ -module Patch +module PatchMeIn VERSION = "0.1.0" end diff --git a/patch.gemspec b/patch.gemspec deleted file mode 100644 index 4d08a51..0000000 --- a/patch.gemspec +++ /dev/null @@ -1,26 +0,0 @@ -require_relative "lib/patch/version" - -Gem::Specification.new do |spec| - spec.name = "patch" - spec.version = Patch::VERSION - spec.authors = [ "Jeff Peterson" ] - spec.email = [ "jeff@yak.sh" ] - spec.homepage = "TODO" - spec.summary = "TODO: Summary of Patch." - spec.description = "TODO: Description of Patch." - spec.license = "MIT" - - # Prevent pushing this gem to RubyGems.org. To allow pushes either set the "allowed_push_host" - # to allow pushing to a single host or delete this section to allow pushing to any host. - spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'" - - spec.metadata["homepage_uri"] = spec.homepage - spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here." - spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." - - spec.files = Dir.chdir(File.expand_path(__dir__)) do - Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] - end - - spec.add_dependency "rails", ">= 8.0.0.beta1" -end diff --git a/patch_me_in.gemspec b/patch_me_in.gemspec new file mode 100644 index 0000000..3b1e222 --- /dev/null +++ b/patch_me_in.gemspec @@ -0,0 +1,22 @@ +require_relative "lib/patch_me_in/version" + +Gem::Specification.new do |spec| + spec.name = "patch_me_in" + spec.version = PatchMeIn::VERSION + spec.authors = [ "Jeff Peterson" ] + spec.email = [ "jeff@yak.sh" ] + spec.homepage = "https://github.com/craft-concept/patch_me_in" + spec.summary = "Easily patch objects in a Rails app with live-reload support." + spec.description = "Easily patch objects in a Rails app with live-reload support." + spec.license = "MIT" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = "https://github.com/craft-concept/patch_me_in" + spec.metadata["changelog_uri"] = "https://github.com/craft-concept/patch_me_in/blob/main/CHANGELOG.md" + + spec.files = Dir.chdir(File.expand_path(__dir__)) do + Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] + end + + spec.add_dependency "rails", ">= 8.0.0.beta1" +end diff --git a/test/lib/generators/patch/patch_generator_test.rb b/test/lib/generators/patch/patch_generator_test.rb new file mode 100644 index 0000000..87ea188 --- /dev/null +++ b/test/lib/generators/patch/patch_generator_test.rb @@ -0,0 +1,16 @@ +require "test_helper" +require "generators/patch/patch_generator" + +module PatchMeIn + class PatchGeneratorTest < Rails::Generators::TestCase + tests PatchMeIn::Generator + destination Rails.root.join("tmp/generators") + setup :prepare_destination + + # test "generator runs without errors" do + # assert_nothing_raised do + # run_generator ["arguments"] + # end + # end + end +end diff --git a/test/patch_test.rb b/test/patch_test.rb index 2e8c313..b05bc37 100644 --- a/test/patch_test.rb +++ b/test/patch_test.rb @@ -2,6 +2,6 @@ class PatchTest < ActiveSupport::TestCase test "it has a version number" do - assert Patch::VERSION + assert PatchMeIn::VERSION end end