rename 1.0.11 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd9bfc9678104ffa45d1ed46dd77175ec9477dd8ac9d78e2b311312821e93e7c
4
- data.tar.gz: 928bd83c721e29792092910a6f40b34215c43a1a7750be1eaf4e39a753a7de42
3
+ metadata.gz: f9103bd6950e096f966b148dd5a186d10deee5de51b33f659c992e102d7be6f0
4
+ data.tar.gz: 8cb7182693d49c6197b03cdcacf8081ebf5dbb29c3ed387704c8a58545958603
5
5
  SHA512:
6
- metadata.gz: 4cdb1e940a996bf0eddf75eaf12bad8542e2af9ced9f468761c72f8ec11dca3dc56b459b99074cb96b9f573df5384e223928a504d15e66db8e202d2810022efa
7
- data.tar.gz: 892261fd19eb9228bd0f945c8c2ab035ac7842a20356334df1a6d20c3dc321b507e916e9474aeee90af6c3ddce06ae81c9512542d655071f4320f2baa1ae89c1
6
+ metadata.gz: d5d6c617e7f321a063e18a3ed2f3e206956f47c606fcbb09d0b1f2fec60b091ebdac9bdcb78587beeaa02a7d3aba4fc1de9db71d59d4c6cbe778412f846cf815
7
+ data.tar.gz: ef0dcb01aa5d90c5e776e224e2eaec29601758effee2e156eb4c6debc5973584d2a3e2d035575cd4152b6284db4d128fa558f347e9f5b40686d40532513ee196
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This gem allows you to rename your Rails application by using a single command.
4
4
 
5
- Tested up to Ruby 3.2.2 and Rails 7.0.8
5
+ Tested up to Ruby 3.4.5 and Rails 8.0.2
6
6
 
7
7
  ## Installation
8
8
 
@@ -18,6 +18,52 @@ gem 'rename'
18
18
  rails g rename:into New-Name
19
19
  ```
20
20
 
21
+ ## Applied
22
+
23
+ ```
24
+ Search and replace exact module name to...
25
+ Gemfile
26
+ Gemfile.lock
27
+ README.md
28
+ Rakefile
29
+ config.ru
30
+ config/application.rb
31
+ config/boot.rb
32
+ config/environment.rb
33
+ config/environments/development.rb
34
+ config/environments/production.rb
35
+ config/environments/test.rb
36
+ config/importmap.rb
37
+ config/initializers/assets.rb
38
+ config/initializers/content_security_policy.rb
39
+ config/initializers/filter_parameter_logging.rb
40
+ config/initializers/inflections.rb
41
+ config/initializers/permissions_policy.rb
42
+ config/puma.rb
43
+ config/routes.rb
44
+ config/deploy.yml (Rails 8)
45
+ app/views/layouts/application.html.erb
46
+ app/views/layouts/application.html.haml
47
+ app/views/layouts/application.html.slim
48
+ app/views/pwa/manifest.json.erb (Rails 8)
49
+ README.md
50
+ README.markdown
51
+ README.mdown
52
+ README.mkdn
53
+ Dockerfile (if present)
54
+ Search and replace underscore seperated module name to...
55
+ config/initializers/session_store.rb (if present)
56
+ config/database.yml
57
+ config/database.yml (Rails 8 multi-db: cache, queue, cable)
58
+ config/database.yml (environment variables)
59
+ config/cable.yml
60
+ config/environments/production.rb
61
+ config/deploy.yml (Rails 8 Kamal service names)
62
+ package.json
63
+ Removing references...
64
+ .idea
65
+ Renaming directory...
66
+ ```
21
67
 
22
68
  ## Contributing
23
69
 
@@ -15,7 +15,8 @@ module CommonMethods
15
15
  prepare_app_vars
16
16
  validate_name_and_path?
17
17
  apply_new_module_name
18
- change_app_directory
18
+ remove_references
19
+ rename_directory
19
20
  end
20
21
 
21
22
  def app_parent
@@ -27,10 +28,17 @@ module CommonMethods
27
28
  end
28
29
 
29
30
  def prepare_app_vars
30
- @new_key = new_name.gsub(/\W/, '_')
31
31
  @old_module_name = app_parent
32
32
  @old_dir = File.basename(Dir.getwd)
33
- @new_module_name = @new_key.squeeze('_').camelize
33
+ @old_app_name = detect_app_name || @old_module_name.underscore
34
+
35
+ # Normalize old directory name using ActiveSupport to handle hyphens
36
+ # If the directory has hyphens, the database name would have underscores
37
+ @old_app_name_from_dir = @old_dir.parameterize(separator: '_')
38
+
39
+ @new_app_name = new_name.parameterize(separator: '_').gsub('-', '_')
40
+ @new_module_name = @new_app_name.camelize
41
+ @new_key = @new_app_name
34
42
  @new_dir = new_name.gsub(/[&%*@()!{}\[\]'\\\/"]+/, '')
35
43
  @new_path = Rails.root.to_s.split('/')[0...-1].push(@new_dir).join('/')
36
44
  end
@@ -46,7 +54,7 @@ module CommonMethods
46
54
  raise Thor::Error, '[Error] Please give a name which does not match any of the reserved Rails keywords.'
47
55
  elsif Object.const_defined?(@new_module_name)
48
56
  raise Thor::Error, "[Error] Constant '#{@new_module_name}' is already in use, please choose another name."
49
- elsif file_exist?(@new_path)
57
+ elsif file_exist?(@new_path) && @new_path != Rails.root.to_s
50
58
  raise Thor::Error, "[Error] Folder '#{@new_dir}' already in use, please choose another name."
51
59
  end
52
60
  end
@@ -54,42 +62,61 @@ module CommonMethods
54
62
  # rename_app_to_new_app_module
55
63
  def apply_new_module_name
56
64
  in_root do
57
- puts 'Search and replace module in files...'
58
-
59
- #Search and replace module in to file
60
- Dir['*', 'config/**/**/*.rb', '.{rvmrc}'].each do |file|
61
- # file = File.join(Dir.pwd, file)
62
- replace_into_file(file, /(#{@old_module_name}*)/m, @new_module_name)
65
+ puts 'Search and replace exact module name...'
66
+ Dir['*', 'config/**/**/*.{rb,yml}', '.{rvmrc}', 'app/views/pwa/*.json.erb'].each do |file|
67
+ replace_into_file(file, /#{@old_module_name}/, @new_module_name)
68
+ end
69
+ #Application layout
70
+ %w(erb haml slim).each do |ext|
71
+ replace_into_file("app/views/layouts/application.html.#{ext}", /#{@old_module_name}/, @new_module_name)
72
+ replace_into_file("app/views/layouts/application.html.#{ext}", /#{@old_module_name.underscore.humanize}/, @new_module_name.underscore.humanize)
73
+ end
74
+ #Readme
75
+ %w(md markdown mdown mkdn).each do |ext|
76
+ replace_into_file("README.#{ext}", /#{@old_module_name}/, @new_module_name)
63
77
  end
64
78
 
65
- #Rename session key
66
- replace_into_file('config/initializers/session_store.rb', /(('|")_.*_session('|"))/i, "'_#{@new_key}_session'")
67
- #Rename database
68
- replace_into_file('config/database.yml', /#{@old_module_name.underscore}/i, @new_name.underscore)
69
- #Rename into channel and job queue
79
+ puts 'Search and replace underscore separated module name in files...'
80
+ #session key
81
+ safe_replace_into_file('config/initializers/session_store.rb', /(('|")_.*_session('|"))/i, "'_#{@new_key}_session'")
82
+ #database
83
+ # Try both the detected app name and the directory-based name (with underscores)
84
+ replace_into_file('config/database.yml', /#{@old_app_name}/i, @new_app_name)
85
+ replace_into_file('config/database.yml', /#{@old_app_name_from_dir}/i, @new_app_name) if @old_app_name != @old_app_name_from_dir
86
+ replace_into_file('config/database.yml', /#{@old_app_name}_(production|development|test)(_cache|_queue|_cable)?/, "#{@new_app_name}_\\1\\2")
87
+ replace_into_file('config/database.yml', /#{@old_app_name_from_dir}_(production|development|test)(_cache|_queue|_cable)?/, "#{@new_app_name}_\\1\\2") if @old_app_name != @old_app_name_from_dir
88
+ replace_into_file('config/database.yml', /#{@old_app_name.upcase}_DATABASE_PASSWORD/, "#{@new_app_name.upcase}_DATABASE_PASSWORD")
89
+ replace_into_file('config/database.yml', /#{@old_app_name_from_dir.upcase}_DATABASE_PASSWORD/, "#{@new_app_name.upcase}_DATABASE_PASSWORD") if @old_app_name != @old_app_name_from_dir
90
+ #Channel and job queue
70
91
  %w(config/cable.yml config/environments/production.rb).each do |file|
71
- replace_into_file(file, /#{@old_module_name.underscore}_production/, "#{@new_module_name.underscore}_production")
92
+ replace_into_file(file, /#{@old_app_name}_production/, "#{@new_app_name}_production")
93
+ replace_into_file(file, /#{@old_app_name_from_dir}_production/, "#{@new_app_name}_production") if @old_app_name != @old_app_name_from_dir
72
94
  end
73
- #Application layout
74
- %w(erb haml).each do |file|
75
- replace_into_file("app/views/layouts/application.html.#{file}", /#{@old_module_name}/, @new_module_name)
76
- end
77
- # Update package.json name entry
95
+ # package.json name entry
78
96
  old_package_name_regex = /\Wname\W *: *\W(?<name>[-_\p{Alnum}]+)\W *, */i
79
- new_package_name = %("name":"#{@new_module_name.underscore}",)
97
+ new_package_name = %("name":"#{@new_app_name}",)
80
98
  replace_into_file('package.json', old_package_name_regex, new_package_name)
81
99
 
82
- puts 'Search and replace module in environment variables...'
83
- #Rename database
84
- replace_into_file('config/database.yml', /#{@old_module_name.underscore.upcase}/, @new_module_name.underscore.upcase)
85
- end
86
- end
87
100
 
88
- # rename_app_to_new_app_directory
89
- def change_app_directory
90
- rename_references
91
- remove_references
92
- rename_directory
101
+ # Rails 8 specific files
102
+ # Kamal deployment configuration
103
+ safe_replace_into_file('config/deploy.yml', /service: #{@old_app_name}/, "service: #{@new_app_name}")
104
+ safe_replace_into_file('config/deploy.yml', /service: #{@old_app_name_from_dir}/, "service: #{@new_app_name}") if @old_app_name != @old_app_name_from_dir
105
+ safe_replace_into_file('config/deploy.yml', /image: (.+)\/#{@old_app_name}/, "image: \\1/#{@new_app_name}")
106
+ safe_replace_into_file('config/deploy.yml', /image: (.+)\/#{@old_app_name_from_dir}/, "image: \\1/#{@new_app_name}") if @old_app_name != @old_app_name_from_dir
107
+ safe_replace_into_file('config/deploy.yml', /"#{@old_app_name}_/, "\"#{@new_app_name}_")
108
+ safe_replace_into_file('config/deploy.yml', /"#{@old_app_name_from_dir}_/, "\"#{@new_app_name}_") if @old_app_name != @old_app_name_from_dir
109
+ safe_replace_into_file('config/deploy.yml', /#{@old_app_name}-db/, "#{@new_app_name}-db")
110
+ safe_replace_into_file('config/deploy.yml', /#{@old_app_name_from_dir}-db/, "#{@new_app_name}-db") if @old_app_name != @old_app_name_from_dir
111
+
112
+ # PWA manifest
113
+ safe_replace_into_file('app/views/pwa/manifest.json.erb', /"name": "#{@old_module_name}"/, "\"name\": \"#{@new_module_name}\"")
114
+ safe_replace_into_file('app/views/pwa/manifest.json.erb', /"description": "#{@old_module_name}/, "\"description\": \"#{@new_module_name}")
115
+
116
+ # Dockerfile
117
+ safe_replace_into_file('Dockerfile', /#{@old_module_name}/, @new_module_name)
118
+ safe_replace_into_file('Dockerfile', /#{@old_app_name}/, @new_app_name)
119
+ end
93
120
  end
94
121
 
95
122
  private
@@ -98,18 +125,24 @@ module CommonMethods
98
125
  @reserved_names = %w[application destroy benchmarker profiler plugin runner test]
99
126
  end
100
127
 
101
- def file_exist?(name)
102
- File.respond_to?(:exist?) ? File.exist?(name) : File.exists?(name)
103
- end
128
+ def detect_app_name
129
+ return nil unless file_exist?('config/database.yml')
104
130
 
105
- def rename_references
106
- print 'Renaming references...'
131
+ database_content = File.read('config/database.yml')
107
132
 
108
- in_root do
109
- gem_set_file = '.ruby-gemset'
110
- replace_into_file(gem_set_file, @old_dir, @new_dir) if file_exist?(gem_set_file)
133
+ if match = database_content.match(/^\s*database:\s*(\w+)_development/m)
134
+ app_name_candidate = match[1]
135
+
136
+ if app_name_candidate.camelize == @old_module_name
137
+ return app_name_candidate
138
+ end
111
139
  end
112
- puts 'Done!'
140
+
141
+ nil
142
+ end
143
+
144
+ def file_exist?(name)
145
+ File.respond_to?(:exist?) ? File.exist?(name) : File.exists?(name)
113
146
  end
114
147
 
115
148
  def remove_references
@@ -127,6 +160,8 @@ module CommonMethods
127
160
 
128
161
  begin
129
162
  # FileUtils.mv Dir.pwd, app_path
163
+ gem_set_file = '.ruby-gemset'
164
+ replace_into_file(gem_set_file, @old_dir, @new_dir) if file_exist?(gem_set_file)
130
165
  File.rename(Rails.root.to_s, @new_path)
131
166
  puts 'Done!'
132
167
  puts "New application path is '#{@new_path}'"
@@ -144,4 +179,9 @@ module CommonMethods
144
179
  puts "Error: #{ex.message}"
145
180
  end
146
181
  end
182
+
183
+ def safe_replace_into_file(file, search_exp, replace)
184
+ return unless file_exist?(file) && !File.directory?(file)
185
+ replace_into_file(file, search_exp, replace)
186
+ end
147
187
  end
@@ -1,3 +1,3 @@
1
1
  module Rename
2
- VERSION = '1.0.11'
2
+ VERSION = '1.1.1'
3
3
  end
@@ -0,0 +1,126 @@
1
+ require 'test_helper'
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ class HyphenHandlingTest < ActiveSupport::TestCase
6
+ def setup
7
+ @temp_dir = Dir.mktmpdir
8
+ @original_dir = Dir.pwd
9
+ end
10
+
11
+ def teardown
12
+ Dir.chdir(@original_dir)
13
+ FileUtils.rm_rf(@temp_dir)
14
+ end
15
+
16
+ test "handles app names with hyphens correctly" do
17
+ # Create a mock Rails app structure with hyphenated name
18
+ app_dir = File.join(@temp_dir, 'api-proxy')
19
+ FileUtils.mkdir_p(File.join(app_dir, 'config'))
20
+
21
+ # Create database.yml with underscore naming (as Rails would)
22
+ database_yml = <<~YAML
23
+ default: &default
24
+ adapter: postgresql
25
+ encoding: unicode
26
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
27
+
28
+ development:
29
+ <<: *default
30
+ database: api_proxy_development
31
+
32
+ test:
33
+ <<: *default
34
+ database: api_proxy_test
35
+
36
+ production:
37
+ <<: *default
38
+ database: api_proxy_production
39
+ username: api_proxy
40
+ password: <%= ENV["API_PROXY_DATABASE_PASSWORD"] %>
41
+ YAML
42
+
43
+ File.write(File.join(app_dir, 'config', 'database.yml'), database_yml)
44
+
45
+ # Create cable.yml
46
+ cable_yml = <<~YAML
47
+ production:
48
+ adapter: redis
49
+ url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
50
+ channel_prefix: api_proxy_production
51
+ YAML
52
+
53
+ File.write(File.join(app_dir, 'config', 'cable.yml'), cable_yml)
54
+
55
+ Dir.chdir(app_dir) do
56
+ # Simulate the prepare_app_vars method
57
+ old_dir = File.basename(Dir.getwd)
58
+ assert_equal 'api-proxy', old_dir
59
+
60
+ # Test the normalization
61
+ old_app_name_from_dir = old_dir.parameterize(separator: '_')
62
+ assert_equal 'api_proxy', old_app_name_from_dir
63
+
64
+ # Verify the fix would work
65
+ new_app_name = 'citadel'.parameterize(separator: '_')
66
+ assert_equal 'citadel', new_app_name
67
+
68
+ # Read the database.yml and verify it contains the underscore version
69
+ db_content = File.read(File.join('config', 'database.yml'))
70
+ assert db_content.include?('api_proxy_development')
71
+ assert db_content.include?('api_proxy_test')
72
+ assert db_content.include?('api_proxy_production')
73
+ assert db_content.include?('API_PROXY_DATABASE_PASSWORD')
74
+
75
+ # Read cable.yml
76
+ cable_content = File.read(File.join('config', 'cable.yml'))
77
+ assert cable_content.include?('api_proxy_production')
78
+ end
79
+ end
80
+
81
+ test "handles normal app names without hyphens" do
82
+ app_dir = File.join(@temp_dir, 'myapp')
83
+ FileUtils.mkdir_p(File.join(app_dir, 'config'))
84
+
85
+ database_yml = <<~YAML
86
+ development:
87
+ database: myapp_development
88
+ YAML
89
+
90
+ File.write(File.join(app_dir, 'config', 'database.yml'), database_yml)
91
+
92
+ Dir.chdir(app_dir) do
93
+ old_dir = File.basename(Dir.getwd)
94
+ assert_equal 'myapp', old_dir
95
+
96
+ old_app_name_from_dir = old_dir.parameterize(separator: '_')
97
+ assert_equal 'myapp', old_app_name_from_dir
98
+
99
+ # In this case, old_app_name and old_app_name_from_dir should be the same
100
+ # So the conditional replacements won't run
101
+ end
102
+ end
103
+
104
+ test "handles complex app names with multiple hyphens" do
105
+ app_dir = File.join(@temp_dir, 'my-awesome-api-proxy')
106
+ FileUtils.mkdir_p(File.join(app_dir, 'config'))
107
+
108
+ database_yml = <<~YAML
109
+ development:
110
+ database: my_awesome_api_proxy_development
111
+ YAML
112
+
113
+ File.write(File.join(app_dir, 'config', 'database.yml'), database_yml)
114
+
115
+ Dir.chdir(app_dir) do
116
+ old_dir = File.basename(Dir.getwd)
117
+ assert_equal 'my-awesome-api-proxy', old_dir
118
+
119
+ old_app_name_from_dir = old_dir.parameterize(separator: '_')
120
+ assert_equal 'my_awesome_api_proxy', old_app_name_from_dir
121
+
122
+ db_content = File.read(File.join('config', 'database.yml'))
123
+ assert db_content.include?('my_awesome_api_proxy_development')
124
+ end
125
+ end
126
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rename
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.11
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morshed Alam
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-14 00:00:00.000000000 Z
11
+ date: 2026-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -73,6 +73,7 @@ files:
73
73
  - lib/rename.rb
74
74
  - lib/rename/version.rb
75
75
  - rename.gemspec
76
+ - tests/hyphen_handling_test.rb
76
77
  - tests/rename_test.rb
77
78
  - tests/test_helper.rb
78
79
  homepage: https://github.com/morshedalam/rename
@@ -94,8 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  - !ruby/object:Gem::Version
95
96
  version: '0'
96
97
  requirements: []
97
- rubyforge_project: rename
98
- rubygems_version: 2.7.11
98
+ rubygems_version: 3.5.11
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: Rename your Rails application using a single command.