class DTRipperProjectCreator::ProjectReplacer

Public Class Methods

new(proj_name,proj_dir,zip_file_name,down_load_url,proj_root_folder,replace_rex) click to toggle source
# 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

Public Instance Methods

file_content_replace(path) click to toggle source
# File lib/DTRipperProjectCreator.rb, line 96
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) click to toggle source
# File lib/DTRipperProjectCreator.rb, line 116
def get_entries_name(path)
  full_path = File.expand_path(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() click to toggle source
# 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 false
  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!!"
  return true
end
replace_all_replacable_str(path) click to toggle source
# File lib/DTRipperProjectCreator.rb, line 64
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) click to toggle source
# File lib/DTRipperProjectCreator.rb, line 103
def unzip(path)
  full_path = File.expand_path(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