diff --git a/Commands/Recent Migrations.tmCommand b/Commands/Recent Migrations.tmCommand new file mode 100644 index 00000000..9a17b2b9 --- /dev/null +++ b/Commands/Recent Migrations.tmCommand @@ -0,0 +1,101 @@ + + + + + command + #!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -wKU +require 'time' + +def list_latest_migrations_from_path(path) + Dir.chdir(path) + migrations = File.directory?(path) ? Dir.glob('*.rb').reverse.first(50) : [] + + out = <<-EOT + <style> + h1 { border-bottom: 1px solid black; color: green; font-weight: bold; } + li { margin-top: 10px; font-size: 1.2em; } + a { text-decoration: none; } + small { color: #666; margin-top: 2px } + </style> + <h1>Recent Migrations</h1> + EOT + + if migrations.empty? + out << '<h2">Error: No migration dir found!</h2>' + return out + end + + out << '<ol>' + link_index = 0 + migrations.each do |migration| + migration_time = Time.parse("#{migration[0..3]}-#{migration[4..5]}-#{migration[6..7]} #{migration[8..9]}:#{migration[10..11]}:#{migration[12..13]}").strftime("%b %d, %Y %I:%M%p") rescue nil + migration_name = migration[15..-1] + migration_version = migration[0...14] + out << <<-EOT + <li> + <a id="link_#{link_index += 1}" href="txmt://open?url=file://#{path}/#{migration}" onclick="javascript:window.close();">#{migration_name}</a> <small>(#{migration_version})</small><br/> + <small>#{migration_time}</small> + </li> + EOT + end + + out << '</ol>' + + #javascript to capture keystrokes for shortcuts (ie typing 1, opens link 1) + key_code_for_1 = 49 + out << <<-EOT + + <script> + document.onkeyup = TriggerShortcutKeys; + function TriggerShortcutKeys(e) { + var keyId = (window.event) ? event.keyCode : e.keyCode; + if(keyId >= #{key_code_for_1} && keyId < #{key_code_for_1 + [migrations.size, 9].min}) + { + location.href = document.getElementById("link_" + (keyId - #{key_code_for_1 - 1}).toString()).getAttribute('href'); + window.close(); + } + } + </script> + EOT + return out +end + +def output_migrations + path = "#{ENV['TM_PROJECT_DIRECTORY']}/db/migrate" + + if File.directory?(path) + return list_latest_migrations_from_path(path) + exit + else + dirs = Dir.getwd.split("/") + migrations_path = '' + loop do + path = dirs.join("/") + "migrate" + if File.directory?(path) + migrations_path = path + break + end + dirs.pop + break if dirs.empty? + end + list_latest_migrations_from_path(migrations_path) + end +end + +puts output_migrations + input + none + keyEquivalent + ~M + name + Recent Migrations + outputFormat + html + outputLocation + newWindow + scope + source.ruby.rails + uuid + 36830DAF-CDA0-4E7F-B475-A1230C02FBDD + +