Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2013 22:30
Show Gist options
  • Save anonymous/8075952 to your computer and use it in GitHub Desktop.
Save anonymous/8075952 to your computer and use it in GitHub Desktop.
Tags files and directories per Brett Terpstra's approach.
#!/usr/bin/env ruby
def package?(file)
results = `mdls -name kMDItemContentTypeTree "#{file}" | grep com.apple.package`
results.length > 0
end
base = ARGV[0]
if !base
script = File.basename("#{__FILE__}")
puts "Usage: #{script} <base tag>"
puts "#{script} \"#work\""
puts "#{script} \"#play,:toplevel\""
exit
end
base += ',' if !base.include?(':')
packages = []
tags = {}
Dir.glob('**/*') do |file|
parts = file.split('/')
if parts.length == 1
parent_dir = ""
else
parent_dir = parts[0..-2].join('/')
end
if File.directory?(file)
if (package?(file))
packages << "#{file}/"
full_tag = tags[parent_dir] || base
`tag -s "#{full_tag}" "#{file}"`
else
if !packages.find { |pkg| file.start_with?(pkg) }
file_tag = parts[-1].downcase.gsub(/[^a-zA-Z0-9\-]*/,'')
full_tag = base
if parent_dir.length() > 0
full_tag = "#{tags[parent_dir]}:#{file_tag}"
else
full_tag = "#{base}:#{file_tag}"
end
tags[file] = full_tag
`tag -s "@#{file_tag}" "#{file}"`
end
end
else
if !packages.find { |pkg| file.start_with?(pkg) }
full_tag = tags[parent_dir] || base
`tag -s "#{full_tag}" "#{file}"`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment