rename 1.0.3 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da1e3eaf1e62827e6562036b535ec15fbe79a66c
4
- data.tar.gz: 1ada5e52d2937365dc6c98a7fb81435a2f1d7bbb
3
+ metadata.gz: d491da7b4f286f0a39f91bc3cd17eeedaadd7cda
4
+ data.tar.gz: b3f084d31ae1c9fa50d91f2ffcdccf2f3bdd9ea4
5
5
  SHA512:
6
- metadata.gz: 80f35bdc31a513c22c286a4c057766af2bf2a353edccb9c6c21a83bc775eafe0b268b884d66f43c74f1c9c0eeb4422d4e6035778eefd5506fdcc6c4d7e94ea0c
7
- data.tar.gz: b92ebd0b7dcf0b1565399f55703448592fc9e49c3746dd1e8a6c1f10810fd84a81ec8fab3cf87721464c3ed22e21aaeebe14ad98febffd6f8f8fb525179023a8
6
+ metadata.gz: 06cfc9c772360112a4e0c5c102fbd2e9f31812557a78d9dd56bb6d22c904608f3935b0bbb92eaa130f34c9d00d8573fa9ea0f8a232734e8c8a2734d14743c0b2
7
+ data.tar.gz: 494786f8972e13004f8b97050936a75a18dc650a364d9e539d440a9810d23eb9cc68ffc94c3bd8716af17b24377fbb888f9bf562cb21547d5f28fbbb57e43909
@@ -1,19 +1,13 @@
1
- require File.expand_path('../../../rename/common_methods', File.dirname(__FILE__))
1
+ require File.expand_path('../shared/common_methods', File.dirname(__FILE__))
2
2
 
3
3
  module Rename
4
4
  module Generators
5
- class Error < Thor::Error
6
- end
7
-
8
5
  class AppToGenerator < Rails::Generators::Base
9
6
  include CommonMethods
10
7
 
11
8
  def app_to
12
- warn "[DEPRECATION] `app_to` is deprecated. Please use `into` instead."
13
-
14
- valid?
15
- new_app_module
16
- new_app_directory
9
+ warn '[DEPRECATION] `app_to` is deprecated. Please use `into` instead.'
10
+ perform
17
11
  end
18
12
  end
19
13
  end
@@ -1,17 +1,12 @@
1
- require File.expand_path('../../../rename/common_methods', File.dirname(__FILE__))
1
+ require File.expand_path('../shared/common_methods', File.dirname(__FILE__))
2
2
 
3
3
  module Rename
4
4
  module Generators
5
- class Error < Thor::Error
6
- end
7
-
8
5
  class IntoGenerator < Rails::Generators::Base
9
6
  include CommonMethods
10
7
 
11
8
  def into
12
- valid?
13
- new_app_module
14
- new_app_directory
9
+ perform
15
10
  end
16
11
  end
17
12
  end
@@ -0,0 +1,111 @@
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
+ change_app_directory
19
+ end
20
+
21
+ def prepare_app_vars
22
+ @new_key = new_name.gsub(/\W/, '_')
23
+ @old_module_name = Rails.application.class.parent.to_s
24
+ @new_module_name = @new_key.squeeze('_').camelize
25
+ @new_dir = new_name.gsub(/[&%*@()!{}\[\]'\\\/"]+/, '')
26
+ @new_path = Rails.root.to_s.split('/')[0...-1].push(@new_dir).join('/')
27
+ end
28
+
29
+ def validate_name_and_path?
30
+ if new_name.blank?
31
+ raise Thor::Error, "[Error] Application name can't be blank."
32
+ elsif new_name =~ /^\d/
33
+ raise Thor::Error, '[Error] Please give a name which does not start with numbers.'
34
+ elsif @new_module_name.size < 1
35
+ raise Thor::Error, '[Error] Please enter at least one alphabet.'
36
+ elsif reserved_names.include?(@new_module_name.downcase)
37
+ raise Thor::Error, '[Error] Please give a name which does not match any of the reserved Rails keywords.'
38
+ elsif Object.const_defined?(@new_module_name)
39
+ raise Thor::Error, "[Error] Constant #{@new_module_name} is already in use, please choose another name."
40
+ elsif File.exists?(@new_path)
41
+ raise Thor::Error, '[Error] Already in use, please choose another name.'
42
+ end
43
+ end
44
+
45
+ # rename_app_to_new_app_module
46
+ def apply_new_module_name
47
+ in_root do
48
+ puts 'Search and replace module in...'
49
+
50
+ #Search and replace module in to file
51
+ Dir['*', 'config/**/**/*.rb', '.{rvmrc}'].each do |file|
52
+ # file = File.join(Dir.pwd, file)
53
+ replace_into_file(file, /(#{@old_module_name}*)/m, @new_module_name)
54
+ end
55
+
56
+ #Rename session key
57
+ replace_into_file('config/initializers/session_store.rb', /(('|")_.*_session('|"))/i, "'_#{@new_key}_session'")
58
+ #Rename database
59
+ replace_into_file('config/database.yml', /#{@old_module_name.underscore}/i, @new_name.underscore)
60
+ end
61
+ end
62
+
63
+ # rename_app_to_new_app_directory
64
+ def change_app_directory
65
+ rename_references
66
+ rename_directory
67
+ end
68
+
69
+ private
70
+
71
+ def reserved_names
72
+ @reserved_names = %w[application destroy benchmarker profiler plugin runner test]
73
+ end
74
+
75
+ def rename_references
76
+ puts 'Renaming references...'
77
+ old_basename = File.basename(Dir.getwd)
78
+
79
+ in_root do
80
+ Dir.glob('.idea/*', File::FNM_DOTMATCH).each do |file|
81
+ replace_into_file(file, old_basename, @new_dir)
82
+ end
83
+
84
+ gem_set_file = '.ruby-gemset'
85
+ replace_into_file(gem_set_file, old_basename, @new_dir) if File.exist?(gem_set_file)
86
+ end
87
+ end
88
+
89
+ def rename_directory
90
+ print 'Renaming directory...'
91
+
92
+ begin
93
+ # FileUtils.mv Dir.pwd, app_path
94
+ File.rename(Rails.root.to_s, @new_path)
95
+ puts 'Done!'
96
+ puts "New application path is '#{@new_path}'"
97
+ rescue Exception => ex
98
+ puts "Error:#{ex.inspect}"
99
+ end
100
+ end
101
+
102
+ def replace_into_file(file, search_exp, replace)
103
+ return if File.directory?(file)
104
+
105
+ begin
106
+ gsub_file file, search_exp, replace
107
+ rescue Exception => ex
108
+ puts "Error: #{ex.message}"
109
+ end
110
+ end
111
+ end
@@ -1,3 +1,3 @@
1
1
  module Rename
2
- VERSION = '1.0.3'
2
+ VERSION = '1.0.5'
3
3
  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.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morshed Alam
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-01 00:00:00.000000000 Z
11
+ date: 2017-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -69,8 +69,8 @@ files:
69
69
  - lib/generators/rename/app_to/app_to_generator.rb
70
70
  - lib/generators/rename/into/USAGE
71
71
  - lib/generators/rename/into/into_generator.rb
72
+ - lib/generators/rename/shared/common_methods.rb
72
73
  - lib/rename.rb
73
- - lib/rename/common_methods.rb
74
74
  - lib/rename/version.rb
75
75
  - rename.gemspec
76
76
  - tests/rename_test.rb
@@ -1,121 +0,0 @@
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 app_name
15
- @app_name = new_name.gsub(/\W/, '_').squeeze('_').camelize
16
- end
17
-
18
- def app_key
19
- @app_key = new_name.gsub(/\W/, '_').downcase
20
- end
21
-
22
- def app_dir
23
- @app_dir = new_name.gsub(/[&%*@()!{}\[\]'\\\/"]+/, '')
24
- end
25
-
26
- def app_path
27
- @app_path = Rails.root.to_s.split('/')[0...-1].push(app_dir).join('/')
28
- end
29
-
30
- def reserved_names
31
- @reserved_names = %w[application destroy benchmarker profiler plugin runner test]
32
- end
33
-
34
- def valid?
35
- if new_name.blank?
36
- raise Error, "Application name can't be blank."
37
- elsif new_name =~ /^\d/
38
- raise Error, "Invalid application name #{new_name}. Please give a name which does not start with numbers."
39
- end
40
-
41
- valid_new_app_name?
42
- valid_new_app_dir?
43
- end
44
-
45
- def valid_new_app_name?
46
- if app_name.size < 1
47
- raise Error, "Invalid application name #{app_name}. Please enter at least one alphabet."
48
- elsif reserved_names.include?(app_name.downcase)
49
- raise Error, "Invalid application name #{app_name}. Please give a name which does not match one of the reserved Rails keywords."
50
- elsif Object.const_defined?(app_name)
51
- raise Error, "Invalid application name #{app_name}, constant #{app_name} is already in use. Please choose another application name."
52
- end
53
- end
54
-
55
- def valid_new_app_dir?
56
- if File.exists?(app_path)
57
- raise Error, "Invalid application name #{app_dir}, already in use. Please choose another application name."
58
- end
59
- end
60
-
61
- # rename_app_to_new_app_module
62
- def new_app_module
63
- mod = "#{Rails.application.class.parent}"
64
-
65
- in_root do
66
- puts 'Search and replace module in...'
67
-
68
- #Search and replace module in to file
69
- Dir['*', 'config/**/**/*.rb', '.{rvmrc}'].each do |file|
70
- replace_into_file(file, /(#{mod}*)/m, app_name)
71
- end
72
-
73
- #Rename session key
74
- session_key_file = 'config/initializers/session_store.rb'
75
- search_exp = /(('|")_.*_session('|"))/i
76
- session_key = "'_#{app_key}_session'"
77
- replace_into_file(session_key_file, search_exp, session_key)
78
- end
79
- end
80
-
81
- # rename_app_to_new_app_directory
82
- def new_app_directory
83
- rename_references
84
- rename_directory
85
- end
86
-
87
- def rename_references
88
- puts 'Renaming references...'
89
- old_basename = File.basename(Dir.getwd)
90
-
91
- in_root do
92
- Dir.glob('.idea/*', File::FNM_DOTMATCH).each do |file|
93
- replace_into_file(file, old_basename, app_dir)
94
- end
95
-
96
- gem_set_file = '.ruby-gemset'
97
- replace_into_file(gem_set_file, old_basename, app_dir) if File.exist?(gem_set_file)
98
- end
99
- end
100
-
101
- def rename_directory
102
- print 'Renaming directory...'
103
- begin
104
- File.rename(Rails.root.to_s, app_path)
105
- puts 'Done!'
106
- puts "New application path is '#{app_path}'"
107
- rescue Exception => ex
108
- puts "Error:#{ex.inspect}"
109
- end
110
- end
111
-
112
- def replace_into_file(file, search_exp, replace)
113
- return if File.directory?(file)
114
-
115
- begin
116
- gsub_file file, search_exp, replace
117
- rescue Exception => ex
118
- puts "Error: #{ex.message}"
119
- end
120
- end
121
- end