<commit_before>require "fileutils"

def bail_with(project)
    puts ">>>> #{project} failed! <<<<"
    exit(1)
end

def rename_project_to(name)
  files = [ "AndroidIntelliJStarter.iml",
            ".idea/.name",
            "AndroidManifest.xml"] + Dir.glob('.idea/**/*.xml')
  puts "Searching: #{files.join(",")}\n"          
  files.each do |filename|
    replace(filename, "AndroidIntelliJStarter", name)
  end
  
  puts ">>> Renamed 'AndroidIntelliJStarter.iml' to '#{name}.iml'"
  File.rename "AndroidIntelliJStarter.iml", "#{name}.iml"

  # delete workspace.xml, which might hold bad references. It will be regenerated.
  File.delete ".idea/workspace.xml" if File.exists? ".idea/workspace.xml" 
  
  init_git_repo
end

def replace(filename, original_name, new_name) 
  text = File.read(filename)
  replaced = text.gsub!(original_name, new_name)
  File.open(filename, "w") {|file| file.puts text}
  puts ">>> Replaced '#{original_name}' in #{filename} with '#{new_name}'" if replaced
end

def init_git_repo
  puts "\n!!! Do you want to create a new git repository? "
  puts "!!! Type 'yes' to back up your .git directory and create a new git repository"
  print "> "
  should_init = gets.chomp!
  
  if should_init.downcase == "yes" 
    puts "!!! Moving .git to .git.bak. Delete this if you don't want it."
    FileUtils.mv ".git", ".git.bak"
    puts "!!! Initializing a new git repository!"
    system "git init ."
    system "echo '' > .gitmodules"
    system "git add ."
    system "git ci -am 'Initial Commit'"
    
    reset_robolectric
  else 
    puts "!!! You typed '#{should_init}'. Leaving existing git repository."
    puts "!!! Run ./script/init_git (or ruby script/init_git) to try again."
  end  
end

def reset_robolectric
  puts "Add Robolectric as a non-pushable submodule pointing at HEAD"
  
  system "git rm --cached submodules/robolectric"
  FileUtils.rm_rf "submodules"
  system "echo ''> .gitmodules"
  
  system "git submodule add git://github.com/pivotal/robolectric.git submodules/robolectric"
  system "git submodule update --init"
end  
<commit_msg>script tweak<commit_after>require "fileutils"

def bail_with(project)
    puts ">>>> #{project} failed! <<<<"
    exit(1)
end

def rename_project_to(name)
  files = [ "AndroidIntelliJStarter.iml",
            ".idea/.name",
            "AndroidManifest.xml"] + Dir.glob('.idea/**/*.xml')
  puts "Searching: #{files.join(",")}\n"          
  files.each do |filename|
    replace(filename, "AndroidIntelliJStarter", name)
  end
  
  puts ">>> Renamed 'AndroidIntelliJStarter.iml' to '#{name}.iml'"
  File.rename "AndroidIntelliJStarter.iml", "#{name}.iml"

  # delete workspace.xml, which might hold bad references. It will be regenerated.
  File.delete ".idea/workspace.xml" if File.exists? ".idea/workspace.xml" 
  
  init_git_repo
end

def replace(filename, original_name, new_name) 
  text = File.read(filename)
  replaced = text.gsub!(original_name, new_name)
  File.open(filename, "w") {|file| file.puts text}
  puts ">>> Replaced '#{original_name}' in #{filename} with '#{new_name}'" if replaced
end

def init_git_repo
  puts "\n!!! Do you want to create a new git repository? "
  puts "!!! Type 'yes' to back up your .git directory and create a new git repository"
  print "> "
  should_init = gets.chomp!
  
  if should_init.downcase == "yes" 
    puts "!!! Moving .git to .git.bak. Delete this if you don't want it."
    FileUtils.mv ".git", ".git.bak"
    puts "!!! Initializing a new git repository!"
    system "git init ."    

    reset_robolectric

    system "git add ."
    system "git ci -am 'Initial Commit'"
  else 
    puts "!!! You typed '#{should_init}'. Leaving existing git repository."
    puts "!!! Run ./script/init_git (or ruby script/init_git) to try again."
  end  
end

def reset_robolectric
  puts "Add Robolectric as a non-pushable submodule pointing at HEAD"
  
  system "git rm --cached submodules/robolectric"
  FileUtils.rm_rf "submodules"
  system "echo ''> .gitmodules"
  
  system "git submodule add git://github.com/pivotal/robolectric.git submodules/robolectric"
  system "git submodule update --init"
end  

{'fraction_non_alphanumeric': 0.13688029020556228, 'fraction_numerical': 0.0004836759371221282, 'mean_word_length': 3.433011789924973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5855156', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1389, 'n_words': 400}