public
Fork of drnic/ruby-on-rails-tmbundle
Description: Ruby on Rails TextMate bundle [master branch is svn trunk; patches to drnicwilliams@gmail.com]
Homepage: http://macromates.com
Clone URL: git://github.com/Infininight/ruby-on-rails-tmbundle.git
Search Repo:
commit  c1160ccd8fd575a698a44d7b4a7094a1a7c973d8
tree    920c6eeed6657a0f348871e7186beeef8429dc7a
parent  4e2fe3ade86e6bbd8b4c47482a6e81dc8d85ff59
ruby-on-rails-tmbundle / Support / bin / generate.rb
100755 57 lines (48 sloc) 2.066 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
 
# Copyright:
# (c) 2006 syncPEOPLE, LLC.
# Visit us at http://syncpeople.com/
# Author: Duane Johnson (duane.johnson@gmail.com)
# Description:
# Asks what to generate and what name to use, then runs script/generate.
 
require 'rails_bundle_tools'
require 'fileutils'
require File.dirname(__FILE__) + "/../lib/rails/generate"
 
# Look for (created) files and return an array of them
def files_from_generator_output(output, type = 'create')
  output.to_a.map { |line| line.scan(/#{type}\s+([^\s]+)$/).flatten.first }.compact.select { |f| File.exist?(f) and !File.directory?(f) }
end
 
Generator.setup
 
if choice = TextMate.choose("Generate:", Generator.names.map { |name| Inflector.humanize name }, :title => "Rails Generator")
  arguments =
    TextMate.input(
      Generator.generators[choice].question,
      Generator.generators[choice].default_answer,
      :title => "#{Inflector.humanize Generator.generators[choice].name} Generator")
  if arguments
    options = ""
 
    case choice
    when 0
      options = TextMate.input("Name the new controller for the scaffold:", "", :title => "Scaffold Controller Name")
      options = "'#{options}'"
    when 1
      options = TextMate.input("List any actions you would like created for the controller:",
        "index new create edit update destroy", :title => "Controller Actions")
    end
 
    # add the --svn option, if needed
    proj_dir = ENV["TM_PROJECT_DIRECTORY"]
    if proj_dir and File.exist?(File.join(proj_dir, ".svn"))
      options << " --svn"
    end
 
    rails_root = RailsPath.new.rails_root
    FileUtils.cd rails_root
    command = "script/generate #{Generator.generators[choice].name} #{arguments} #{options}"
    $logger.debug "Command: #{command}"
 
    output = ruby(command)
    $logger.debug "Output: #{output}"
    TextMate.rescan_project
    files = files_from_generator_output(output)
    files.each { |f| TextMate.open(File.join(rails_root, f)) }
    TextMate.textbox("Done generating #{Generator.generators[choice].name}", output, :title => "Done")
  end
end