Class: CookieGen::Generator
- Inherits:
-
Object
- Object
- CookieGen::Generator
- Defined in:
- lib/cookiegen/generator.rb
Class Method Summary collapse
- .change_path(path, replacements) ⇒ Object
- .cookiecutter_string(key) ⇒ Object
- .create_cookie(json_path) ⇒ Object
- .should_ignore_path(path, ignore) ⇒ Object
- .update_files(replacements, ignore) ⇒ Object
Class Method Details
.change_path(path, replacements) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cookiegen/generator.rb', line 16 def self.change_path(path, replacements) path_arr = path.split("/") last_path = path_arr.pop path_suffix = $search_folder.chomp + (path_arr.length > 0 ? "/" + path_arr.join("/") : "") replacements.keys.each do |key| old_last_path = last_path.dup while last_path.include? key start_index = last_path.index(key) last_path[start_index, key.length] = Generator.(replacements[key]) old_path = path_suffix + "/" + old_last_path new_path = path_suffix + "/" + last_path puts "\nUpdating Path:" puts "From: #{old_path}" puts "To: #{new_path}" File.rename "#{old_path}", "#{new_path}" old_last_path = last_path.dup end end return path_suffix + "/" + last_path end |
.cookiecutter_string(key) ⇒ Object
7 8 9 |
# File 'lib/cookiegen/generator.rb', line 7 def self.(key) return "{{ cookiecutter.#{key} }}" end |
.create_cookie(json_path) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/cookiegen/generator.rb', line 63 def self.(json_path) json_file = File.read(json_path) jsonHash = JSON.parse(json_file) replacements = jsonHash["replace"] ignore = jsonHash["ignore"] Generator.update_files(replacements, ignore) end |
.should_ignore_path(path, ignore) ⇒ Object
11 12 13 14 |
# File 'lib/cookiegen/generator.rb', line 11 def self.should_ignore_path(path, ignore) intersection = ignore & path.split("/") return intersection.length > 0 end |
.update_files(replacements, ignore) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cookiegen/generator.rb', line 37 def self.update_files(replacements, ignore) # Fetch list of all files in the folder, rejecting all the ignored files # 'reverse' is to make sure we're updating subfolders/subfiles before main folders list_of_files = Dir.glob("**/*", File::FNM_DOTMATCH).reverse.reject do |path| Generator.should_ignore_path(path, ignore) end list_of_files.each do |path| changed_path = Generator.change_path(path, replacements) if !File.directory?(changed_path) puts "\nFile: #{changed_path}" replacements.keys.each do |key| begin text = File.read(changed_path) new_contents = text.gsub(/#{key}/, "#{Generator.(replacements[key])}") # To write changes to the file, use: File.open(changed_path, "w") {|file| file.puts new_contents } rescue => e puts "\nError modifying file: #{changed_path}" puts "Error: #{e}" next end end end end end |