├── .gitignore ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── TO DO ├── lib ├── generators │ └── rename │ │ ├── app_to │ │ ├── USAGE │ │ └── app_to_generator.rb │ │ ├── into │ │ ├── USAGE │ │ └── into_generator.rb │ │ └── shared │ │ └── common_methods.rb ├── rename.rb └── rename │ └── version.rb ├── rename.gemspec └── tests ├── rename_test.rb └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | .idea 19 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in rename.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Morshed Alam 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rename 2 | 3 | This gem allows you to rename your Rails application by using a single command. 4 | 5 | Tested up to Ruby 3.2.2 and Rails 7.0.8 6 | 7 | ## Installation 8 | 9 | Add this line to your application's Gemfile: 10 | 11 | ```ruby 12 | gem 'rename' 13 | ``` 14 | 15 | ## Usage 16 | 17 | ``` 18 | rails g rename:into New-Name 19 | ``` 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 | app/views/layouts/application.html.erb 45 | app/views/layouts/application.html.haml 46 | app/views/layouts/application.html.slim 47 | README.md 48 | README.markdown 49 | README.mdown 50 | README.mkdn 51 | Search and replace underscore seperated module name to... 52 | config/initializers/session_store.rb 53 | config/database.yml 54 | config/cable.yml 55 | config/environments/production.rb 56 | package.json 57 | Removing references... 58 | .idea 59 | Renaming directory... 60 | ``` 61 | 62 | ## Contributing 63 | 64 | 1. Fork it 65 | 2. Create your feature branch (`git checkout -b my-new-feature`) 66 | 3. Commit your changes (`git commit -am 'Added some feature'`) 67 | 4. Push to the branch (`git push origin my-new-feature`) 68 | 5. Create new Pull Request 69 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | require "bundler/gem_tasks" 3 | -------------------------------------------------------------------------------- /TO DO: -------------------------------------------------------------------------------- 1 | == Rename github repository using API -------------------------------------------------------------------------------- /lib/generators/rename/app_to/USAGE: -------------------------------------------------------------------------------- 1 | rails g rename:app_to NewName 2 | -------------------------------------------------------------------------------- /lib/generators/rename/app_to/app_to_generator.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../shared/common_methods', File.dirname(__FILE__)) 2 | 3 | module Rename 4 | module Generators 5 | class AppToGenerator < Rails::Generators::Base 6 | include CommonMethods 7 | 8 | def app_to 9 | warn '[DEPRECATION] `app_to` is deprecated. Please use `into` instead.' 10 | perform 11 | end 12 | end 13 | end 14 | end -------------------------------------------------------------------------------- /lib/generators/rename/into/USAGE: -------------------------------------------------------------------------------- 1 | rails g rename:into NewName 2 | -------------------------------------------------------------------------------- /lib/generators/rename/into/into_generator.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../shared/common_methods', File.dirname(__FILE__)) 2 | 3 | module Rename 4 | module Generators 5 | class IntoGenerator < Rails::Generators::Base 6 | include CommonMethods 7 | 8 | def into 9 | perform 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/generators/rename/shared/common_methods.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/concern' 2 | 3 | module CommonMethods 4 | extend ActiveSupport::Concern 5 | 6 | included do 7 | desc 'Rename your Rails application' 8 | 9 | argument :new_name, :type => :string, :default => '' 10 | end 11 | 12 | protected 13 | 14 | def perform 15 | prepare_app_vars 16 | validate_name_and_path? 17 | apply_new_module_name 18 | remove_references 19 | rename_directory 20 | end 21 | 22 | def app_parent 23 | if Rails.version.to_f >= 3.3 24 | Rails.application.class.to_s.deconstantize 25 | else 26 | Rails.application.class.parent.name 27 | end 28 | end 29 | 30 | def prepare_app_vars 31 | @new_key = new_name.gsub(/\W/, '_') 32 | @old_module_name = app_parent 33 | @old_dir = File.basename(Dir.getwd) 34 | @new_module_name = @new_key.squeeze('_').camelize 35 | @new_dir = new_name.gsub(/[&%*@()!{}\[\]'\\\/"]+/, '') 36 | @new_path = Rails.root.to_s.split('/')[0...-1].push(@new_dir).join('/') 37 | end 38 | 39 | def validate_name_and_path? 40 | if new_name.blank? 41 | raise Thor::Error, "[Error] Application name can't be blank." 42 | elsif new_name =~ /^\d/ 43 | raise Thor::Error, '[Error] Please give a name which does not start with numbers.' 44 | elsif @new_module_name.size < 1 45 | raise Thor::Error, '[Error] Please enter at least one alphabet.' 46 | elsif reserved_names.include?(@new_module_name.downcase) 47 | raise Thor::Error, '[Error] Please give a name which does not match any of the reserved Rails keywords.' 48 | elsif Object.const_defined?(@new_module_name) 49 | raise Thor::Error, "[Error] Constant '#{@new_module_name}' is already in use, please choose another name." 50 | elsif file_exist?(@new_path) 51 | raise Thor::Error, "[Error] Folder '#{@new_dir}' already in use, please choose another name." 52 | end 53 | end 54 | 55 | # rename_app_to_new_app_module 56 | def apply_new_module_name 57 | in_root do 58 | puts 'Search and replace exact module name...' 59 | Dir['*', 'config/**/**/*.rb', '.{rvmrc}'].each do |file| 60 | # file = File.join(Dir.pwd, file) 61 | replace_into_file(file, /(#{@old_module_name}*)/m, @new_module_name) 62 | end 63 | #Application layout 64 | %w(erb haml slim).each do |ext| 65 | replace_into_file("app/views/layouts/application.html.#{ext}", /#{@old_module_name}/, @new_module_name) 66 | end 67 | #Readme 68 | %w(md markdown mdown mkdn).each do |ext| 69 | replace_into_file("README.#{ext}", /#{@old_module_name}/, @new_module_name) 70 | end 71 | 72 | puts 'Search and replace underscore seperated module name in files...' 73 | #session key 74 | replace_into_file('config/initializers/session_store.rb', /(('|")_.*_session('|"))/i, "'_#{@new_key}_session'") 75 | #database 76 | replace_into_file('config/database.yml', /#{@old_module_name.underscore}/i, @new_name.underscore) 77 | #Channel and job queue 78 | %w(config/cable.yml config/environments/production.rb).each do |file| 79 | replace_into_file(file, /#{@old_module_name.underscore}_production/, "#{@new_module_name.underscore}_production") 80 | end 81 | # package.json name entry 82 | old_package_name_regex = /\Wname\W *: *\W(?[-_\p{Alnum}]+)\W *, */i 83 | new_package_name = %("name":"#{@new_module_name.underscore}",) 84 | replace_into_file('package.json', old_package_name_regex, new_package_name) 85 | end 86 | end 87 | 88 | private 89 | 90 | def reserved_names 91 | @reserved_names = %w[application destroy benchmarker profiler plugin runner test] 92 | end 93 | 94 | def file_exist?(name) 95 | File.respond_to?(:exist?) ? File.exist?(name) : File.exists?(name) 96 | end 97 | 98 | def remove_references 99 | print 'Removing references...' 100 | 101 | begin 102 | FileUtils.rm_r('.idea') 103 | rescue Exception => ex 104 | end 105 | puts 'Done!' 106 | end 107 | 108 | def rename_directory 109 | print 'Renaming directory...' 110 | 111 | begin 112 | # FileUtils.mv Dir.pwd, app_path 113 | gem_set_file = '.ruby-gemset' 114 | replace_into_file(gem_set_file, @old_dir, @new_dir) if file_exist?(gem_set_file) 115 | File.rename(Rails.root.to_s, @new_path) 116 | puts 'Done!' 117 | puts "New application path is '#{@new_path}'" 118 | rescue Exception => ex 119 | puts "Error:#{ex.inspect}" 120 | end 121 | end 122 | 123 | def replace_into_file(file, search_exp, replace) 124 | return if File.directory?(file) || !file_exist?(file) 125 | 126 | begin 127 | gsub_file file, search_exp, replace 128 | rescue Exception => ex 129 | puts "Error: #{ex.message}" 130 | end 131 | end 132 | end 133 | -------------------------------------------------------------------------------- /lib/rename.rb: -------------------------------------------------------------------------------- 1 | module Rename 2 | autoload :VERSION, 'active_admin/version' 3 | end 4 | -------------------------------------------------------------------------------- /lib/rename/version.rb: -------------------------------------------------------------------------------- 1 | module Rename 2 | VERSION = '1.1.0' 3 | end 4 | -------------------------------------------------------------------------------- /rename.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/rename/version', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.name = 'rename' 6 | gem.version = Rename::VERSION 7 | gem.license = 'MIT' 8 | gem.authors = ['Morshed Alam'] 9 | gem.email = %w(morshed201@gmail.com) 10 | gem.homepage = 'https://github.com/morshedalam/rename' 11 | gem.summary = 'Rename your Rails application using a single command.' 12 | gem.required_ruby_version = ">= 1.8.7" 13 | 14 | gem.add_dependency 'rails', '>= 3.0.0' 15 | gem.add_dependency 'thor', '>= 0.19.1' 16 | gem.add_runtime_dependency 'activesupport' 17 | gem.rubyforge_project = 'rename' 18 | 19 | gem.files = `git ls-files`.split("\n") 20 | gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } 21 | gem.require_paths = %w(lib) 22 | end 23 | -------------------------------------------------------------------------------- /tests/rename_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class RenameTest < ActiveSupport::TestCase 4 | # Replace this with your real tests. 5 | test "the truth" do 6 | assert true 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /tests/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'test/unit' 3 | require 'active_support' 4 | --------------------------------------------------------------------------------