Skip to content

Instantly share code, notes, and snippets.

@dv
Last active August 29, 2015 14:17
Show Gist options
  • Save dv/0e5e8757ed09eddaac8e to your computer and use it in GitHub Desktop.
Save dv/0e5e8757ed09eddaac8e to your computer and use it in GitHub Desktop.
Run rails generator and open generated files in Sublime
#!/usr/bin/ruby
# Alias as follows:
#
# alias rg="~/scripts/rails_g_and_open_in_sublime.rb"
#
# Then use like this:
#
# $ rg model Post
#
require 'pty'
def extract_created_files(lines)
lines.map do |line|
command, file = colorless(line).split
file if command == "create"
end.compact.reverse
end
def colorless(str)
str.gsub /\033\[\d+m/, ""
end
command = "rails generate #{$*.join(" ")}"
lines = []
# Use PTY to force Thor to output coloured text
PTY.spawn(command) do |r, w, pid|
begin
while line = r.readline
puts line
lines << line
end
rescue EOFError
# noop
end
end
files = extract_created_files(lines)
if files.any?
puts "\nOpening files #{files.join(", ")}..."
exec("'/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl' #{files.join(" ")}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment