jets-upgrade 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4641f2c18a99d751e16e8fb6af45960f4257c15c641ca70275b73c9fe310c231
4
+ data.tar.gz: df2a439fddaedd3abc139ee7b420bf8e56617edf51244b986e57f8de8c14d4b2
5
+ SHA512:
6
+ metadata.gz: 01db354c57e6c03d540119193604a3eb4df81cc63b78412d429c8a96665f012cf5e922c5f07b1bab416013f5b15effec7b589c0c12668fba7194878039615f49
7
+ data.tar.gz: 630c8728fece3e7909664081e95b1ad38ec7119456ecbcd0b1c3b2d7c2ac49a536170b7ac39f93de1f0a932241a723e10c78319d479ce60b774c7135a8e909d1
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ /.bundle
4
+ /.config
5
+ /.yardoc
6
+ /_yardoc
7
+ /coverage
8
+ /doc/
9
+ /Gemfile.lock
10
+ /InstalledFiles
11
+ /lib/bundler/man
12
+ /pkg
13
+ /rdoc
14
+ /spec/reports
15
+ /test/tmp
16
+ /test/version_tmp
17
+ /tmp
18
+ /Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
+
6
+ ## [0.1.0]
7
+ - Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem dependencies in jets-upgrade.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) Tung Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Jets Upgrade
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/jets-upgrade.svg)](http://badge.fury.io/rb/jets-upgrade)
4
+
5
+ [![BoltOps Badge](https://img.boltops.com/boltops/badges/boltops-badge.png)](https://www.boltops.com)
6
+
7
+ [![BoltOps Learn Badge](https://img.boltops.com/boltops-learn/boltops-learn.png)](https://learn.boltops.com)
8
+
9
+ This is a standalone CLI tool to help with jets upgrades.
10
+
11
+ * It can handle going from Jets v3 or v4 to Jets v5.
12
+
13
+ ## Usage
14
+
15
+ cd old-jets-project
16
+ jets-upgrade go
17
+
18
+ Before proceeding, the tool will prompt you with a "Are you sure" message.
19
+
20
+ ## What It Does
21
+
22
+ Here are some of the things the tool does. It updates:
23
+
24
+ * config/application.rb: to the new inherited based style
25
+ * Rakefile: use `Jets.application.load_tasks`
26
+ * Gemfile: update to add jets v5 related gems and comment out the current ones in your Gemfile
27
+ * config/environment: update environment files to new config settings
28
+ * controllers: updates controller to use `def destroy` instead of `def delete`
29
+
30
+ ## Important
31
+
32
+ It's unfeasible to account for all cases and Jets apps. This script cannot perform miracle upgrades. It's a best-effort script, and the hope is that this script gets you pretty far and is helpful. 😄
33
+
34
+ ## Install
35
+
36
+ gem install jets-upgrade
37
+
38
+ ## Javascript Option
39
+
40
+ By default, the `jets-upgrade go` command also tries to upgrade JavaScript. Jets v5 goes from webpacker to importmap. So, the node-related files are no longer needed. The upgrade command will remove node-related files. If you do not want this behavior, use the `--no-javascript` option.
41
+
42
+ ## Docs
43
+
44
+ [Jets Upgrade Docs](https://rubyonjets.com/docs/extras/upgrading/)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new
data/exe/jets-upgrade ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Trap ^C
4
+ Signal.trap("INT") {
5
+ puts "\nCtrl-C detected. Exiting..."
6
+ sleep 0.1
7
+ exit
8
+ }
9
+
10
+ $:.unshift(File.expand_path("../../lib", __FILE__))
11
+ require "jets-upgrade"
12
+ require "jets_upgrade/cli"
13
+
14
+ JetsUpgrade::CLI.start(ARGV)
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jets_upgrade/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jets-upgrade"
8
+ spec.version = JetsUpgrade::VERSION
9
+ spec.authors = ["Tung Nguyen"]
10
+ spec.email = ["tongueroo@gmail.com"]
11
+ spec.summary = "Jets Upgrade Tool"
12
+ spec.homepage = "https://github.com/rubyonjets/jets-upgrade"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = File.directory?('.git') ? `git ls-files`.split($/) : Dir.glob("**/*")
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "memoist"
23
+ spec.add_dependency "rainbow"
24
+ spec.add_dependency "thor"
25
+ spec.add_dependency "zeitwerk"
26
+
27
+ spec.add_development_dependency "bundler"
28
+ spec.add_development_dependency "byebug"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec"
31
+ end
@@ -0,0 +1 @@
1
+ require_relative "jets_upgrade"
@@ -0,0 +1,22 @@
1
+ require "zeitwerk"
2
+
3
+ module JetsUpgrade
4
+ class Autoloader
5
+ class Inflector < Zeitwerk::Inflector
6
+ def camelize(basename, _abspath)
7
+ map = { cli: "CLI", version: "VERSION" }
8
+ map[basename.to_sym] || super
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def setup
14
+ loader = Zeitwerk::Loader.new
15
+ loader.inflector = Inflector.new
16
+ loader.push_dir(File.dirname(__dir__)) # lib
17
+ loader.ignore("#{File.dirname(__dir__)}/jets-upgrade.rb")
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ class JetsUpgrade::CLI
2
+ class Go
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def run
8
+ sure?
9
+ JetsUpgrade::Rewrite.run(@options)
10
+ JetsUpgrade::PublicIndex.run
11
+ JetsUpgrade::Javascript.run if @options[:javascript]
12
+ JetsUpgrade::Iam.run
13
+ finish_message
14
+ end
15
+
16
+ def finish_message
17
+ puts <<~EOL
18
+ Upgrade complete! Please double check the files to make sure they look correct.
19
+ Remember, this is a best-effort upgrade tool. It does not cover all cases.
20
+
21
+ Try running:
22
+
23
+ EOL
24
+ puts " jets server".color(:green)
25
+ puts
26
+ end
27
+
28
+ def sure?
29
+ return if @options[:yes]
30
+ puts <<~EOL
31
+ This script will make changes to your project source code.
32
+
33
+ Note: It's unfeasible to account for all cases and Jets apps.
34
+ This script cannot perform miracle upgrades. It's a best-effort script,
35
+ and the hope is that this script gets you pretty far and is helpful. 😄
36
+
37
+ Please make sure you have backed up and committed your changes first.
38
+ Are you sure you want to continue?
39
+
40
+ EOL
41
+ unless agree("Continue? (y/n)")
42
+ puts "Exiting without making changes."
43
+ exit
44
+ end
45
+ end
46
+
47
+ def agree(prompt)
48
+ print(prompt + " ")
49
+ $stdin.gets.chomp.downcase =~ /^y/
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ ## Examples
2
+
3
+ jets-upgrade completion
4
+
5
+ Prints words for TAB auto-completion.
6
+
7
+ jets-upgrade completion
8
+ jets-upgrade completion hello
9
+ jets-upgrade completion hello name
10
+
11
+ To enable, TAB auto-completion add the following to your profile:
12
+
13
+ eval $(jets-upgrade completion_script)
14
+
15
+ Auto-completion example usage:
16
+
17
+ jets-upgrade [TAB]
18
+ jets-upgrade hello [TAB]
19
+ jets-upgrade hello name [TAB]
20
+ jets-upgrade hello name --[TAB]
@@ -0,0 +1,3 @@
1
+ To use, add the following to your `~/.bashrc` or `~/.profile`
2
+
3
+ eval $(jets-upgrade completion_script)
@@ -0,0 +1,11 @@
1
+ class JetsUpgrade::CLI
2
+ module Help
3
+ class << self
4
+ def text(namespaced_command)
5
+ path = namespaced_command.to_s.gsub(':','/')
6
+ path = File.expand_path("../help/#{path}.md", __FILE__)
7
+ IO.read(path) if File.exist?(path)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module JetsUpgrade
2
+ class CLI < Command
3
+ class_option :verbose, type: :boolean
4
+ class_option :noop, type: :boolean
5
+
6
+ desc "go", "Run jets upgrade script"
7
+ long_desc Help.text(:go)
8
+ option :javascript, type: :boolean, default: true, desc: "Upgrade javascript files"
9
+ option :yes, type: :boolean, aliases: :y, default: false, desc: "By pass are you sure prompt"
10
+ def go
11
+ Go.new(options).run
12
+ end
13
+
14
+ desc "version", "prints version"
15
+ def version
16
+ puts VERSION
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,89 @@
1
+ require "thor"
2
+
3
+ # Override thor's long_desc identation behavior
4
+ # https://github.com/erikhuda/thor/issues/398
5
+ class Thor
6
+ module Shell
7
+ class Basic
8
+ def print_wrapped(message, options = {})
9
+ message = "\n#{message}" unless message[0] == "\n"
10
+ stdout.puts message
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ module JetsUpgrade
17
+ class Command < Thor
18
+ class << self
19
+ def dispatch(m, args, options, config)
20
+ # Allow calling for help via:
21
+ # jets-upgrade command help
22
+ # jets-upgrade command -h
23
+ # jets-upgrade command --help
24
+ # jets-upgrade command -D
25
+ #
26
+ # as well thor's normal way:
27
+ #
28
+ # jets-upgrade help command
29
+ help_flags = Thor::HELP_MAPPINGS + ["help"]
30
+ if args.length > 1 && !(args & help_flags).empty?
31
+ args -= help_flags
32
+ args.insert(-2, "help")
33
+ end
34
+
35
+ # jets-upgrade version
36
+ # jets-upgrade --version
37
+ # jets-upgrade -v
38
+ version_flags = ["--version", "-v"]
39
+ if args.length == 1 && !(args & version_flags).empty?
40
+ args = ["version"]
41
+ end
42
+
43
+ super
44
+ end
45
+
46
+ # Override command_help to include the description at the top of the
47
+ # long_description.
48
+ def command_help(shell, command_name)
49
+ meth = normalize_command_name(command_name)
50
+ command = all_commands[meth]
51
+ alter_command_description(command)
52
+ super
53
+ end
54
+
55
+ def alter_command_description(command)
56
+ return unless command
57
+
58
+ # Add description to beginning of long_description
59
+ long_desc = if command.long_description
60
+ "#{command.description}\n\n#{command.long_description}"
61
+ else
62
+ command.description
63
+ end
64
+
65
+ # add reference url to end of the long_description
66
+ unless website.empty?
67
+ full_command = [command.ancestor_name, command.name].compact.join('-')
68
+ url = "#{website}/reference/jets-upgrade-#{full_command}"
69
+ long_desc += "\n\nHelp also available at: #{url}"
70
+ end
71
+
72
+ command.long_description = long_desc
73
+ end
74
+ private :alter_command_description
75
+
76
+ # meant to be overriden
77
+ def website
78
+ ""
79
+ end
80
+
81
+ # https://github.com/erikhuda/thor/issues/244
82
+ # Deprecation warning: Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?` in `Lono::CLI`
83
+ # You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
84
+ def exit_on_failure?
85
+ true
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,33 @@
1
+ module JetsUpgrade
2
+ class Iam
3
+ def run
4
+ # warn_about_iam_policies
5
+ Dir.glob("app/controllers/**/*").each do |path|
6
+ next if File.directory?(path)
7
+ next if path =~ /application_controller\.rb$/
8
+ warn(path)
9
+ end
10
+ end
11
+
12
+ def warn(path)
13
+ lines = IO.readlines(path)
14
+ lines.each_with_index do |line, i|
15
+ n = i + 1
16
+ if line =~ /iam_policy/
17
+ puts "WARNING: #{path}:#{n} contains iam_policy".color(:yellow)
18
+ puts <<~EOL
19
+ Please move the iam_policy the ApplicationController
20
+ or config/application.rb
21
+
22
+ Jets v5 deploys a single Lambda function for controllers so
23
+ the iam_policy at each controller does not work anymore.
24
+ EOL
25
+ end
26
+ end
27
+ end
28
+
29
+ def self.run
30
+ new.run
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ class JetsUpgrade::Javascript
2
+ class Base
3
+ def remove_empty_dir(path)
4
+ return unless Dir.exist?(path)
5
+ if Dir.empty?(path)
6
+ FileUtils.rmdir(path)
7
+ puts "Removed empty dir #{path}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ class JetsUpgrade::Javascript
2
+ class Gitignore < Base
3
+ def run
4
+ return unless File.exist?(".gitignore")
5
+ lines = IO.readlines(".gitignore")
6
+ return if lines.detect { |l| l.include?("public/assets") }
7
+ lines << "public/assets\n"
8
+ content = lines.join('')
9
+ IO.write(".gitignore", content)
10
+ end
11
+
12
+ def self.run
13
+ new.run
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ class JetsUpgrade::Javascript
2
+ class Move < Base
3
+ def run
4
+ Dir.glob("app/javascript/packs/*").each do |src|
5
+ if src.ends_with?(".js")
6
+ dest = src.sub("app/javascript/packs/", "app/javascript/")
7
+ else # css or scss
8
+ dest = src.sub("app/javascript/packs/", "app/assets/stylesheets/")
9
+ end
10
+ FileUtils.mkdir_p(File.dirname(dest))
11
+ FileUtils.mv(src, dest)
12
+ puts "Moved #{src} => #{dest}"
13
+ end
14
+ remove_empty_dir("app/javascript/packs")
15
+ end
16
+
17
+ def self.run
18
+ new.run
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ class JetsUpgrade::Javascript
2
+ class Remove < Base
3
+ def run
4
+ return unless any_remove_files_exist?
5
+ puts "Jets v5 defaults to importmap. Do not need node-related code anymore."
6
+ remove_files.each do |path|
7
+ remove(path)
8
+ end
9
+ remove_empty_dir("bin")
10
+ end
11
+
12
+ def remove(path)
13
+ if File.exist?(path)
14
+ FileUtils.rm_rf(path)
15
+ puts "Removed #{path}"
16
+ end
17
+ end
18
+
19
+ def any_remove_files_exist?
20
+ remove_files.any? { |path| File.exist?(path) }
21
+ end
22
+
23
+ def remove_files
24
+ %w[
25
+ node_modules
26
+ babel.config.js
27
+ bin/webpack
28
+ bin/webpack-dev-server
29
+ package.json
30
+ postcss.config.js
31
+ yarn.lock
32
+ ]
33
+ end
34
+
35
+ def self.run
36
+ new.run
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ module JetsUpgrade
2
+ class Javascript
3
+ def run
4
+ Remove.run
5
+ Move.run
6
+ Gitignore.run
7
+ rewrite_view_js
8
+ end
9
+
10
+ def rewrite_view_js
11
+ changed = false
12
+ Dir.glob("app/views/**/*").each do |path|
13
+ next if File.directory?(path)
14
+ changed ||= JetsUpgrade::Rewrite::ViewJs.new(path).run
15
+ end
16
+ puts note if changed
17
+ end
18
+
19
+ def note
20
+ puts <<~EOL
21
+ Rewrote files use javascript_include_tag and stylesheet_link_tag instead of javascript_pack_tag and stylesheet_pack_tag.
22
+ Please double check the file to make sure it looks correct.
23
+
24
+ This does not take you all the way to importmaps. It just gets rid of the webpacker logic.
25
+ More info: https://rubyonjets.com/docs/assets/importmap/
26
+ To set up importmaps, please run:
27
+
28
+ EOL
29
+ puts " jets importmap:install".color(:green)
30
+ puts
31
+ puts "Note: If they are conflicts, you can probably overwrite them."
32
+ end
33
+
34
+ def self.run
35
+ new.run
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module JetsUpgrade
2
+ class PublicIndex
3
+ def run
4
+ return unless File.exist?(filename)
5
+
6
+ lines = IO.readlines(filename)
7
+ if lines.detect { |l| l.include?("Welcome and congrats") }
8
+ FileUtils.rm(filename)
9
+ puts "Removed #{filename}. Jets handles with jets/welcome#index controller"
10
+ end
11
+ end
12
+
13
+ def filename
14
+ "public/index.html"
15
+ end
16
+
17
+ def self.run
18
+ new.run
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ module JetsUpgrade::Rewrite
2
+ class Application < Base
3
+ def filename
4
+ "config/application.rb"
5
+ end
6
+
7
+ def marker
8
+ " < Jets::Application"
9
+ end
10
+
11
+ def content
12
+ inside_do_block = false
13
+ modified_lines = lines.map do |line|
14
+ if line.match(/Jets\.application\.configure\s+do/)
15
+ inside_do_block = true
16
+ "module Main\n class Application < Jets::Application\n"
17
+ elsif inside_do_block && line.match(/^\s*end\s*$/)
18
+ inside_do_block = false
19
+ " end\nend\n"
20
+ elsif inside_do_block
21
+ if line.match(/^\s+$/)
22
+ "\n"
23
+ elsif line.match(/config.controllers.filtered_parameters(.*)/)
24
+ " # Recommend moving filter_parameters to config/initializers/filter_parameter_logging.rb instead\n" \
25
+ " # Jets.application.config.filter_parameters#{$1}\n" \
26
+ " config.filter_parameters#{$1}\n"
27
+ else
28
+ " #{line}" # add 2 spaces
29
+ end
30
+ else
31
+ line
32
+ end
33
+ end.compact
34
+ modified_lines.join('')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,69 @@
1
+ module JetsUpgrade::Rewrite
2
+ # interface methods:
3
+ # marker
4
+ # filename
5
+ # new_content
6
+ class Base
7
+ extend Memoist
8
+
9
+ def initialize(options={})
10
+ @options = options
11
+ end
12
+
13
+ def lines
14
+ IO.readlines(filename)
15
+ end
16
+ memoize :lines
17
+
18
+ def run
19
+ unless File.exist?(filename)
20
+ puts "File does not exist: #{filename}".color(:red)
21
+ return
22
+ end
23
+
24
+ marker_found = lines.detect { |l| marker && l.include?(marker) }
25
+ not_marker_found = lines.detect { |l| not_marker && l.include?(not_marker) }
26
+ if marker_found && !not_marker_found
27
+ puts "#{filename} looks good".color(:green)
28
+ return
29
+ end
30
+
31
+ if ENV['DEBUG']
32
+ puts "filename: #{filename}".color(:purple)
33
+ puts "content:".color(:purple)
34
+ puts content
35
+ end
36
+ rewrite
37
+ end
38
+
39
+ def marker
40
+ nil
41
+ end
42
+
43
+ # optional interface method
44
+ def not_marker
45
+ nil
46
+ end
47
+
48
+ # optional interface method
49
+ def message
50
+ nil
51
+ end
52
+
53
+ def rewrite
54
+ return unless changed?
55
+ IO.write(filename, content)
56
+ puts "Updated #{filename}"
57
+ puts message if message
58
+ true
59
+ end
60
+
61
+ def changed?
62
+ content != lines.join('')
63
+ end
64
+
65
+ def self.run(options={})
66
+ new(options).run
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,28 @@
1
+ module JetsUpgrade::Rewrite
2
+ class Controller < Base
3
+ def initialize(path)
4
+ @path = path
5
+ end
6
+
7
+ def filename
8
+ @path # /full/path/to/app/controllers/posts_controller.rb
9
+ end
10
+
11
+ def marker
12
+ "def destroy"
13
+ end
14
+
15
+ def content
16
+ modified_lines = lines.map do |line|
17
+ if line =~ /\s{2,}def delete$/
18
+ line = line.sub('delete', 'destroy')
19
+ end
20
+ if line =~ /before_action :/ && line =~ /delete/
21
+ line = line.sub('delete', 'destroy')
22
+ end
23
+ line
24
+ end
25
+ modified_lines.join('')
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,98 @@
1
+ module JetsUpgrade::Rewrite
2
+ class Environment < Base
3
+ def initialize(name)
4
+ @name = name
5
+ end
6
+
7
+ def filename
8
+ "config/environments/#{@name}.rb"
9
+ end
10
+
11
+ def marker
12
+ "config.eager_load"
13
+ end
14
+
15
+ def for_env(env)
16
+ send("for_#{env}")
17
+ end
18
+
19
+ def for_test
20
+ <<EOL
21
+ config.cache_classes = false
22
+ config.eager_load = ENV["CI"].present?
23
+ config.cache_store = :null_store
24
+ config.consider_all_requests_local = true
25
+ config.server_timing = true
26
+ config.jets_controller.perform_caching = false
27
+ config.action_dispatch.show_exceptions = false
28
+ config.action_mailer.delivery_method = :test
29
+ config.action_mailer.perform_caching = false
30
+ # config.action_mailer.default_url_options = { host: 'localhost', port: 8888 }
31
+ EOL
32
+ end
33
+
34
+ def for_production
35
+ <<EOL
36
+ config.cache_classes = true
37
+ config.eager_load = true
38
+ config.log_level = :info
39
+ config.logging.event = false # true can be useful for CloudWatch logs but pretty noisy locally
40
+ config.consider_all_requests_local = false
41
+ config.assets.compile = false
42
+ config.action_mailer.raise_delivery_errors = true
43
+ config.action_mailer.perform_caching = false
44
+ # config.action_mailer.default_url_options = { host: 'localhost', port: 8888 }
45
+ EOL
46
+ end
47
+
48
+ def for_development
49
+ <<EOL
50
+ config.enable_reloading = true
51
+ config.eager_load = false
52
+ config.consider_all_requests_local = true
53
+ config.server_timing = true
54
+
55
+ config.action_dispatch.show_exceptions = true
56
+
57
+ # Enable/disable caching. By default caching is disabled.
58
+ if Jets.root.join("tmp/caching-dev.txt").exist?
59
+ config.jets_controller.perform_caching = true
60
+ config.cache_store = :memory_store
61
+ else
62
+ config.jets_controller.perform_caching = false
63
+ config.cache_store = :null_store
64
+ end
65
+
66
+ config.action_mailer.raise_delivery_errors = false
67
+ config.action_mailer.perform_caching = false
68
+ # config.action_mailer.default_url_options = { host: 'localhost', port: 8888 }
69
+
70
+ config.logging.event = false # true can be useful for CloudWatch logs but pretty noisy locally
71
+ EOL
72
+ end
73
+
74
+ def content
75
+ new_configs = for_env(@name)
76
+
77
+ inside_do_block = false
78
+ modified_lines = lines.map do |line|
79
+ if line.match(/Jets\.application\.configure\s+do/)
80
+ inside_do_block = true
81
+ line + new_configs
82
+ elsif inside_do_block && line.match(/^\s*end\s*$/)
83
+ inside_do_block = false
84
+ line
85
+ elsif inside_do_block
86
+ if line.match(/^\s+$/)
87
+ "\n"
88
+ else
89
+ line
90
+ end
91
+ else
92
+ line
93
+ end
94
+ end.compact
95
+ modified_lines.join('')
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,55 @@
1
+ module JetsUpgrade::Rewrite
2
+ class Gemfile < Base
3
+ def filename
4
+ "Gemfile"
5
+ end
6
+
7
+ def marker
8
+ "sprockets-jets"
9
+ end
10
+
11
+ def content
12
+ new_gems = <<~EOL
13
+ gem "jets", "~> 5.0.0"
14
+ gem "importmap-jets"
15
+ gem "sprockets-jets"
16
+ gem "sassc" # only required if using sass in stylesheets
17
+ EOL
18
+ new_dynomite = <<~EOL
19
+ gem "dynomite", "~> 2.0.0" # recommend upgrading
20
+ EOL
21
+
22
+ modified_lines = lines.map do |line|
23
+ if line =~ /gem "jets"/
24
+ line = "# #{line}" + new_gems
25
+ end
26
+ if line =~ /gem "jetpacker"/ && @options[:javascript]
27
+ line = "# #{line}"
28
+ end
29
+ if line =~ /gem "dynomite"/
30
+ if using_dynomite?
31
+ line = "# #{line}" + new_dynomite
32
+ else
33
+ line = "# #{line}"
34
+ end
35
+ end
36
+ line
37
+ end
38
+
39
+ modified_lines.join('')
40
+ end
41
+
42
+ def message
43
+ puts <<~EOL
44
+ Your Gemfile has been updated. Please run:
45
+
46
+ EOL
47
+ puts " bundle install".color(:green)
48
+ puts
49
+ end
50
+
51
+ def using_dynomite?
52
+ File.exist?("app/models/application_item.rb")
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ module JetsUpgrade::Rewrite
2
+ class Rakefile < Base
3
+ def filename
4
+ "Rakefile"
5
+ end
6
+
7
+ def marker
8
+ "Jets.application.load_tasks"
9
+ end
10
+
11
+ def content
12
+ modified_lines = lines.map do |line|
13
+ if line.match(/Jets\.load_tasks/)
14
+ "Jets.application.load_tasks\n"
15
+ else
16
+ line
17
+ end
18
+ end.compact
19
+ modified_lines.join('')
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module JetsUpgrade::Rewrite
2
+ class ViewJs < Base
3
+ def initialize(path)
4
+ @path = path
5
+ end
6
+
7
+ def filename
8
+ @path # /full/path/to/app/controllers/posts_controller.rb
9
+ end
10
+
11
+ def not_marker
12
+ "javascript_pack_tag"
13
+ end
14
+
15
+ def content
16
+ modified_lines = lines.map do |line|
17
+ if line =~ /<%= javascript_pack_tag/
18
+ line = line.sub('<%= javascript_pack_tag', '<%= javascript_include_tag')
19
+ end
20
+ if line =~ /<%= stylesheet_pack_tag/
21
+ line = line.sub('<%= stylesheet_pack_tag', '<%= stylesheet_link_tag')
22
+ end
23
+ line
24
+ end
25
+ modified_lines.join('')
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module JetsUpgrade
2
+ module Rewrite
3
+ def run(options={})
4
+ Application.run
5
+ Rakefile.run
6
+ Gemfile.run(options)
7
+ rewrite_environment_files
8
+ rewrite_controllers
9
+ end
10
+
11
+ def rewrite_environment_files
12
+ %w[development test production].each do |env|
13
+ Environment.new(env).run
14
+ end
15
+ end
16
+
17
+ def rewrite_controllers
18
+ Dir.glob("app/controllers/**/*_controller.rb").each do |path|
19
+ next if path.include?("application_controller.rb")
20
+ Controller.new(path).run
21
+ end
22
+ end
23
+
24
+ extend self
25
+ end
26
+ end
27
+
@@ -0,0 +1,3 @@
1
+ module JetsUpgrade
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ $stdout.sync = true unless ENV["JETS_UPGRADE_STDOUT_SYNC"] == "0"
2
+
3
+ $:.unshift(File.expand_path("../", __FILE__))
4
+
5
+ require "jets_upgrade/autoloader"
6
+ JetsUpgrade::Autoloader.setup
7
+
8
+ require "active_support"
9
+ require "active_support/core_ext/string"
10
+ require "fileutils"
11
+ require "memoist"
12
+ require "rainbow/ext/string"
13
+
14
+ module JetsUpgrade
15
+ class Error < StandardError; end
16
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jets-upgrade
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: memoist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rainbow
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: zeitwerk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - tongueroo@gmail.com
142
+ executables:
143
+ - jets-upgrade
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - CHANGELOG.md
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - exe/jets-upgrade
154
+ - jets-upgrade.gemspec
155
+ - lib/jets-upgrade.rb
156
+ - lib/jets_upgrade.rb
157
+ - lib/jets_upgrade/autoloader.rb
158
+ - lib/jets_upgrade/cli.rb
159
+ - lib/jets_upgrade/cli/go.rb
160
+ - lib/jets_upgrade/cli/help.rb
161
+ - lib/jets_upgrade/cli/help/completion.md
162
+ - lib/jets_upgrade/cli/help/completion_script.md
163
+ - lib/jets_upgrade/command.rb
164
+ - lib/jets_upgrade/iam.rb
165
+ - lib/jets_upgrade/javascript.rb
166
+ - lib/jets_upgrade/javascript/base.rb
167
+ - lib/jets_upgrade/javascript/gitignore.rb
168
+ - lib/jets_upgrade/javascript/move.rb
169
+ - lib/jets_upgrade/javascript/remove.rb
170
+ - lib/jets_upgrade/public_index.rb
171
+ - lib/jets_upgrade/rewrite.rb
172
+ - lib/jets_upgrade/rewrite/application.rb
173
+ - lib/jets_upgrade/rewrite/base.rb
174
+ - lib/jets_upgrade/rewrite/controller.rb
175
+ - lib/jets_upgrade/rewrite/environment.rb
176
+ - lib/jets_upgrade/rewrite/gemfile.rb
177
+ - lib/jets_upgrade/rewrite/rakefile.rb
178
+ - lib/jets_upgrade/rewrite/view_js.rb
179
+ - lib/jets_upgrade/version.rb
180
+ homepage: https://github.com/rubyonjets/jets-upgrade
181
+ licenses:
182
+ - MIT
183
+ metadata: {}
184
+ post_install_message:
185
+ rdoc_options: []
186
+ require_paths:
187
+ - lib
188
+ required_ruby_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ required_rubygems_version: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ requirements: []
199
+ rubygems_version: 3.4.20
200
+ signing_key:
201
+ specification_version: 4
202
+ summary: Jets Upgrade Tool
203
+ test_files: []