phrases_changer_gem 0.0.98

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/phrases_changer.rb +225 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b93f9ded86d6bb09bd043519787e7a3fc62c379ab08f24db9d92466fcf10a46
4
+ data.tar.gz: 32e754006317a8ba08329ac1db1856b5557810b5d0e8df65ca68d702c84d468c
5
+ SHA512:
6
+ metadata.gz: df4d87e7b018dc5718b319209d545bb4d2adf028fb0f9e2b4eba580b5d9727887a5d4a0cc5e7bf811d930f0562dc20dfc7dd782c138db37dc68bb4fa7b5c619b
7
+ data.tar.gz: 73ad32f06a075840981580dccf4cc42ff819d411db640624f2cc0e1d177cb225deb445a46bbcbfa2d0dcb49f918ed89b4b6bcbf36aef2ac0f92c398a36abb432
@@ -0,0 +1,225 @@
1
+ # *** Phrases Changer ***
2
+ #
3
+ # The most comfortable CONSOLE FEATURE written in Ruby to change phrases in directory recursively,
4
+ # in the safely way, without mistakes, with log what have been changed.
5
+ #
6
+ # This console feature in comparison to phrase changes options in many IDE ensure, you don`t be afraid
7
+ # that something will be changed even if you don`t wish unwanted changes in some places.
8
+
9
+ # Fill the documentation section below.
10
+ # To run the script just use command as below in console in the same directory as this script:
11
+ #
12
+ # $ ruby phrases_changer.rb
13
+
14
+ #***** CONFIGURATION - the only section you have to change for your own using the script **************
15
+ #
16
+ #
17
+ PATH_TO_DIRECTORY = 'example_files' # <-- YOUR CONF Path to directory with files to reworks
18
+ WHAT_I_WANT_TO_CHANGE = 'commodore64' # <-- YOUR CONF This is what you are changing
19
+ WHAT_I_WANT_TO_RETRIEVE = 'Amiga CD 32' # <-- YOUR CONF This is result phrase that you want to retrive.
20
+ #
21
+ #
22
+ #***** end of CONFIGURATION *** Thats all what you have to specify *************************************
23
+
24
+
25
+
26
+ $conf = {
27
+ argv0: WHAT_I_WANT_TO_CHANGE,
28
+ argv1: WHAT_I_WANT_TO_RETRIEVE,
29
+ dir_path: File.join(Dir.pwd, PATH_TO_DIRECTORY),
30
+ log_path: File.join(Dir.pwd, 'logs'),
31
+ }
32
+
33
+ # === This class manage of process: searches and iterate files, save changes to logs.
34
+ # Regarding to the logic of making changes in files is reposnible class ChangeFile located in the same file.
35
+ class PhrasesChanger
36
+
37
+ DIRECTORY_PATH = $conf[:dir_path]
38
+ LOG_PATH = $conf[:log_path]
39
+
40
+ def initialize phrase_we_have_now, phrase_we_want_to_have
41
+ @phrase_we_have_now = phrase_we_have_now
42
+ @phrase_we_want_to_have = phrase_we_want_to_have
43
+ main
44
+ end
45
+
46
+ # @return
47
+ # void
48
+ def main
49
+ files_list = search_in_project @phrase_we_have_now
50
+ objChangeFile = ChangeFile.new @phrase_we_have_now
51
+ files_list.each do |file_path|
52
+ file_before = file_content file_path
53
+ log_hash = { file_path: file_path, phrase_we_have_now: @phrase_we_have_now }
54
+ several_file_phrases = objChangeFile.iterate_several_file file_path
55
+ several_file_phrases.each do |line_to_change|
56
+ let_me_change = objChangeFile.ask_for_several_change_in_file line_to_change, file_path
57
+ if let_me_change
58
+ make_check = objChangeFile.make line_to_change, @phrase_we_have_now, @phrase_we_want_to_have, file_path
59
+ raise "The problem with 'make' function occurs!" unless make_check
60
+ puts "\n\e[39mThe phrase \e[31m'#{@phrase_we_have_now}' \e[39mis currently changed to \e[32m'#{@phrase_we_want_to_have}'\e[34m in file:\e[30m"
61
+ puts "#{file_path}\n\n"
62
+ else
63
+ puts "No file has been changed\n\n"
64
+ end
65
+ end
66
+ #If you will not stop the script and finish the changes, you will have log with all changes.
67
+ #Otherwise log file will not created.
68
+ puts "\e[39m* *"
69
+ puts '* Process complete *'
70
+ puts '* You can see changes in log file *'
71
+ puts "******************************************\n"
72
+ puts "Log file path: #{File.join(LOG_PATH, adjust_log_name(@phrase_we_have_now))}\n\n\n\n\n\n\n"
73
+ file_after = file_content file_path
74
+ append_to_log log_hash, file_before, file_after
75
+ end
76
+ end
77
+
78
+ # === Return files where occurs: phrase_we_have_now
79
+ # @return Array
80
+ def search_in_project phrase_we_have_now
81
+ result_files_with_phrase = []
82
+ path_to_files = File.join(DIRECTORY_PATH, '**/*.rb')
83
+ files_to_check = []
84
+ Dir.glob(path_to_files) do |rb_file|
85
+ files_to_check << rb_file
86
+ end
87
+ raise "files_to_check is empty !" if files_to_check.length == 0
88
+ #Looking for files where occurs: phrase_we_have_now
89
+ files_to_check.each do |one_file|
90
+ file = File.open(one_file, "r") do |f|
91
+ f.each_line do |line|
92
+ reg = /.*#{@phrase_we_have_now}.*/
93
+ if line.match?(reg)
94
+ result_files_with_phrase << one_file
95
+ end
96
+ end
97
+ end
98
+ end
99
+ if result_files_with_phrase.length == 0
100
+ puts "\n\e[31m\e[1;4mThe phrase: '#{@phrase_we_have_now}' not found in files.\e[31m"
101
+ exit
102
+ end
103
+ result_files_with_phrase.uniq.sort
104
+ end
105
+
106
+ # @return
107
+ # void
108
+ def append_to_log log_hash, file_before, file_after
109
+ raise "log_hash is not a Hash!" unless log_hash.is_a?(Hash)
110
+ data = "\n\n+++++++++++++++++++++++++++++++++ #{Time.now.to_s} ++++++++++++++++++++++++++++++++++\n"
111
+ data += "#{log_hash[:file_path]}\n"
112
+ data += "BEFORE: \n" + file_before + "\n----------------------------------------------\nAFTER:\n" + file_after
113
+ File.open(File.join(LOG_PATH, adjust_log_name(log_hash[:phrase_we_have_now])), "a") do |f|
114
+ f.write(data)
115
+ end
116
+ end
117
+
118
+ # @return String
119
+ def file_content file_path
120
+ data = File.read(file_path)
121
+ end
122
+
123
+ private
124
+
125
+ # @return String
126
+ def adjust_log_name name
127
+ change = name.strip.gsub!(':', '_')
128
+ return change if change
129
+ return name
130
+ end
131
+ end
132
+
133
+
134
+ # === Class responsible of the logic of making changes process.
135
+ class ChangeFile
136
+
137
+ def initialize phrase_we_have_now
138
+ @phrase_we_have_now = phrase_we_have_now
139
+ @change_all = false
140
+ end
141
+
142
+ # === Return array which contains lines with searched phrase for indicated file.
143
+ # @return Array
144
+ def iterate_several_file file_path
145
+ #Iterate file line by line
146
+ result_lines_in_file = []
147
+ reg = /.*#{@phrase_we_have_now}.*/
148
+ file = File.open(file_path, "r") do |f|
149
+ f.each_line do |line|
150
+ if line.match?(reg)
151
+ result_lines_in_file << line
152
+ end
153
+ end
154
+ end
155
+ result_lines_in_file
156
+ end
157
+
158
+ # This method ask you for confirmation if you want to make indicated change.
159
+ # @return Boolean
160
+ def ask_for_several_change_in_file line_to_change, file_path
161
+ if @change_all
162
+ puts "\n\e[39m******************************************"
163
+ puts '* *'
164
+ puts '* Automatically changes has been started *'
165
+ puts '* *'
166
+ puts "\nFile: #{file_path}\n\n"
167
+ return true
168
+ end
169
+ puts "\n\e[39m******************************************"
170
+ puts '* *'
171
+ puts '* Follow the instructions below to *'
172
+ puts '* make changes in file *'
173
+ puts "\nPhrase:\n #{line_to_change}\nfound in file:\n#{file_path}\n"
174
+ print "\e[31m\e[1;4m\nDo I have to change this phrase [y/n/all] (default \e[32m\e[1;4mYes\e[31m) ?\n"
175
+ choise = STDIN.gets.chomp.upcase
176
+ res = false
177
+ if choise == 'Y' || choise.length == 0
178
+ res = true
179
+ elsif choise == 'A'
180
+ res = true
181
+ @change_all = true
182
+ end
183
+ res
184
+ end
185
+
186
+ # Returns bool value for info if changes made correctly or rather problem occurs.
187
+ # @return Boolean
188
+ def make line_to_change, phrase_we_have_now, phrase_we_want_to_have, file_path
189
+ verbose = $conf[:verbose]
190
+ if verbose
191
+ puts "\e[39m-----------------------------"
192
+ puts "I'm changing:\n#{phrase_we_have_now}"
193
+ puts '-----------------------------'
194
+ puts "to:\n#{phrase_we_want_to_have}"
195
+ puts '-----------------------------'
196
+ end
197
+
198
+ #Change every occurence
199
+ puts "in file:\n#{file_path}" if verbose
200
+ data = File.read(file_path)
201
+ puts "\n\e[31m++Old version: \e[30m\n" if verbose
202
+ puts data + "\n" if verbose
203
+
204
+ line_changed = line_to_change.gsub(phrase_we_have_now, phrase_we_want_to_have)
205
+ data.sub! line_to_change, line_changed
206
+
207
+ puts "\n\e[32m++New version: \e[30m\n" if verbose
208
+ puts data + "\n" if verbose
209
+ puts "\e[31mOld line:\n #{line_to_change}\e[32m\nNew line:\n#{line_changed}\e[30m" #Standard info. verbose = true -> shows all file
210
+ #Write the file
211
+ res = false
212
+ res = File.open(file_path, "w") do |f|
213
+ f.write(data)
214
+ end
215
+ if res
216
+ return true
217
+ end
218
+ false
219
+ end
220
+ end
221
+
222
+ PhrasesChanger.new $conf[:argv0], $conf[:argv1]
223
+
224
+ # Ruby 2.5.1
225
+ # 2018, Arkadiusz Mazur, gmail address: programmer.rails
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phrases_changer_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.98
5
+ platform: ruby
6
+ authors:
7
+ - Arkadiusz Mazur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - phrases_changer.rb
20
+ homepage: https://github.com/arkadiusz-mazur/ruby_phrases_changer/blob/master/phrases_changer.rb
21
+ licenses:
22
+ - Apache-2.0
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.7.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: 'DEPRECATED. Please use gitHub version by link below: ...................
44
+ https://github.com/arkadiusz-mazur/ruby_phrases_changer/blob/master/phrases_changer.rb'
45
+ test_files: []