class Escalator::Confuse

Constants

EXTNAMES

Attributes

command[RW]
keywords[RW]
project_path[RW]

Public Class Methods

run(command) click to toggle source
# File lib/escalator/confuse.rb, line 7
def run command
  @command = command
  Throw.note "Begin confuse #{File.basename File.dirname command.project_path} ..."
  prepareContext
  handleProject
  project_path
end

Private Class Methods

handleFile(file, keyword) click to toggle source
# File lib/escalator/confuse.rb, line 87
def handleFile file, keyword
  if EXTNAMES.include? file.path.split(".").last
    content = File.read file.real_path
    content = content.gsub(keyword.regexp) { |match|
      match == keyword.value ? keyword.first : keyword.second
    }
    File.write file.real_path, content
  end
end
handleGroup(group, keyword) click to toggle source
# File lib/escalator/confuse.rb, line 69
def handleGroup group, keyword
  group.anyGroups.each { |subGroup|
    handleGroup subGroup, keyword
  }
  group.files.each { |file|
    if command.verbose?
      puts "Confusing #{keyword.value} for #{file.path} ..."
    end
    if file.path.include? keyword.value
      dir = File.dirname(file.real_path)
      name = file.path.gsub keyword.regexp, keyword.first
      FileUtils.mv file.real_path, "#{dir}/#{name}"
      file.set_path name
    end
    handleFile file, keyword
  }
end
handleProject() click to toggle source
# File lib/escalator/confuse.rb, line 56
def handleProject
  project = Xcodeproj::Project.open project_path
  keywords.each { |keyword|
    Throw.note "Begin Confuse #{keyword.value} ..."
    handleGroup project.groups.first, keyword
  }
  project.save project_path
  if command.show_output?
    system "open #{command.output_path}"
  end
  Throw.note "Confuse Success!"
end
prepareContext() click to toggle source
# File lib/escalator/confuse.rb, line 25
def prepareContext
  Throw.note "Prepare confuse context ..."
  @keywords = []
  timestamp = Time.now.to_i.to_s
  command.keywords.each { |keyword|
    part1 = (Random.rand(25) + 65).chr
    part2 = OpenSSL::Digest.hexdigest("SHA1", keyword + timestamp)[0,7].upcase
    text1 = part1 + part2
    text2 = text1.sub(/[A-Z]*[0-9a-z]/) { |match| match.downcase }
    @keywords << OpenStruct.new(:first  => text1,
                                :second => text2,
                                :value  => keyword,
                                :regexp => Regexp.new(keyword, Regexp::IGNORECASE))
    }
  if command.verbose?
    puts keywords.map { |keyword|
      "#{keyword.value} ==> first: #{keyword.first} second: #{keyword.second}\n"
    }
  end
  source_path = File.expand_path("../", command.project_path)
  output_path = File.expand_path("./", command.output_path)
  project_name = File.basename(command.project_path)
  target_path = "#{output_path}/#{File.basename(source_path)}"
  @project_path = "#{target_path}/#{project_name}"
  FileUtils.mkdir_p output_path
  if File.exist? target_path
    FileUtils.rm_rf target_path
  end
  FileUtils.cp_r source_path, output_path
end