Created
June 16, 2011 14:03
-
-
Save Overbryd/1029278 to your computer and use it in GitHub Desktop.
This ruby script can mix your Textmate project file into a new one with file ignores from a .gitignore file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'plist' | |
require 'fileutils' | |
plist = ARGV[0] || Dir.pwd.split('/').last | |
new_plist = "#{plist}.new.tmproj" | |
gitignore = ARGV[1] || '.gitignore' | |
if File.exist?(plist) && File.exist?(gitignore) | |
# create a new plist file to preserve the original tmproj | |
FileUtils.cp(plist, new_plist) | |
properties = Plist::parse_xml(new_plist) | |
file_filter = [] | |
folder_filter = [] | |
File.read(gitignore).split("\n").each do |exclude| | |
# all file extension filters | |
if exclude =~ /^\*([\.\w]+)$/ | |
file_filter << Regexp.escape($1) | |
# other file and directory filters | |
elsif exclude =~ /(.+)(\/\*)?$/ | |
folder_filter << Regexp.escape($1) | |
end | |
end | |
# update the filter properties | |
document = properties['documents'].shift | |
document.merge!( | |
'regexFileFilter' => "!(#{file_filter.join('|')})$", | |
'regexFolderFilter' => "!.*/(\\.[^/]*|#{folder_filter.join('|')})" | |
) | |
properties['documents'].unshift(document) | |
File.open(new_plist, 'w') do |file| | |
file.write(Plist::Emit.dump(properties)) | |
end | |
puts "Successfully merged .gitignore into file/folder filters of #{plist}" | |
else | |
puts " Error: Could not read #{plist} or .gitignore." | |
puts | |
puts " Usage: gitignore2tmproj.rb [your.tmproj] [.gitignore]" | |
puts " Defaults to: gitignore2tmproj.rb working_directory_name.tmproj .gitignore" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment