Class: DTRipperProjectCreator::ProjectReplacer
- Inherits:
-
Object
- Object
- DTRipperProjectCreator::ProjectReplacer
- Defined in:
- lib/DTRipperProjectCreator.rb
Instance Method Summary collapse
- #file_content_replace(path) ⇒ Object
- #get_entries_name(path) ⇒ Object
-
#initialize(proj_name, proj_dir, zip_file_name, down_load_url, proj_root_folder, replace_rex) ⇒ ProjectReplacer
constructor
A new instance of ProjectReplacer.
- #replace ⇒ Object
- #replace_all_replacable_str(path) ⇒ Object
- #unzip(path) ⇒ Object
Constructor Details
#initialize(proj_name, proj_dir, zip_file_name, down_load_url, proj_root_folder, replace_rex) ⇒ ProjectReplacer
Returns a new instance of ProjectReplacer.
21 22 23 24 25 26 27 28 |
# File 'lib/DTRipperProjectCreator.rb', line 21 def initialize(proj_name,proj_dir,zip_file_name,down_load_url,proj_root_folder,replace_rex) @proj_name = proj_name @proj_dir = proj_dir @zip_file_name = zip_file_name @down_load_url = down_load_url @proj_root_folder = proj_root_folder @replace_rex = replace_rex end |
Instance Method Details
#file_content_replace(path) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/DTRipperProjectCreator.rb', line 95 def file_content_replace(path) File.open(path,"r:utf-8") do |fr| buffer = fr.read.gsub(@replace_rex,@proj_name) # puts buffer File.open(path, "w:utf-8") { |fw| fw.write(buffer) } end end |
#get_entries_name(path) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/DTRipperProjectCreator.rb', line 115 def get_entries_name(path) full_path = File.(path) entries = [] Zip::InputStream::open(full_path) do |io| while (entry = io.get_next_entry) h = Hash["entry"=>entry.name , "dest_path"=>File.join(@proj_dir,entry.name)] entries << h end end entries end |
#replace ⇒ Object
29 30 31 32 33 34 35 36 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 62 |
# File 'lib/DTRipperProjectCreator.rb', line 29 def replace if File.exists?(File.join(@proj_dir,@proj_name)) puts "Already have project named '#{@proj_name}',If you want to create new one,delete old one please" return end unzip_file_path = File.join(@proj_dir,@zip_file_name) if File.exists?(unzip_file_path) puts "templete project zip file is already exist" File.delete(unzip_file_path) end puts "Downloading templete project...." File.open(unzip_file_path, 'wb') {|f| f.write(open(@down_load_url) {|f1| f1.read})} puts "Templete project download success!!" puts "Start unzip templete project...." unzip(unzip_file_path) puts "Unzip templete project success!!" File.delete(unzip_file_path) puts "Delete templete project zip file success!!" File.rename(File.join(@proj_dir,@proj_root_folder),File.join(@proj_dir,@proj_name)) replace_all_replacable_str File.join(@proj_dir,@proj_name) puts "Replace finished!!" end |
#replace_all_replacable_str(path) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/DTRipperProjectCreator.rb', line 63 def replace_all_replacable_str(path) dir = Dir.new(path) dir.each do |sub_dir| if sub_dir == "." || sub_dir == ".." next end if sub_dir.to_s.match(@replace_rex) changed_path = File.join(path,sub_dir.to_s.gsub(@replace_rex,@proj_name)) File.rename(File.join(path,sub_dir.to_s),changed_path) if File.ftype(changed_path) == "file" file_content_replace changed_path else replace_all_replacable_str changed_path end else orgin_path = File.join(path,sub_dir) if File.ftype(orgin_path) == "file" file_content_replace orgin_path elsif replace_all_replacable_str orgin_path end end end end |
#unzip(path) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/DTRipperProjectCreator.rb', line 102 def unzip(path) full_path = File.(path) entries = get_entries_name(path) Zip::File.open(full_path) do |f| entries.each do |entry| f.extract(entry["entry"],entry["dest_path"]) {true} #puts "unzip #{entry} succeed!" end end rescue =>e puts(e) exit 3 end |