Skip to content
This repository
Newer
Older
100755 128 lines (115 sloc) 3.969 kb
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
1 #!/usr/bin/env ruby
2
3 # This is a simple CGI wrapper around Sprockets.
4 #
5 # Copy it into a directory on your site with CGI enabled. When invoked, the
6 # script will search its directory and parent directories for a YAML file named
7 # "config/sprockets.yml" in order to load configuration information.
8 #
9a89c253 »
2009-02-18 Change the CGI output generation environment variable name to "sprock…
9 # If you set the environment variable "sprockets_generate_output_file" to
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
10 # "true" the concatenation will be cached to disk. Use it in conjunction with
11 # URL rewriting to cache your Sprockets output on the first request.
12 #
13 # Assuming a site layout like this:
14 #
15 # mysite/
16 # config/
17 # sprockets.yml
18 # javascripts/
19 # mysite.js
20 # ...
21 # public/
22 # index.html
23 # nph-sprockets.cgi (this file)
24 # vendor/
25 # sprockets/
26 # prototype/ -> ...
27 # scriptaculous/ -> ...
28 #
29 # mysite/config/sprockets.yml might look like this:
30 #
31 # :load_path:
32 # - javascripts
33 # - vendor/sprockets/*/src
34 # :source_files:
35 # - javascripts/mysite.js
36 # - javascripts/*.js
37 # :output_file: public/sprockets.js
38 #
39 # The <script> tag in mysite/public/index.html could look like this:
40 #
41 # <script type="text/javascript" src="/sprockets.js"></script>
42 #
43 # And you might have the following Apache configuration:
44 #
45 # <VirtualHost ...>
46 # ServerName mysite.example.org
47 # DocumentRoot "/path/to/mysite/public"
48 #
49 # <Directory "/path/to/mysite/public">
50 # Options +ExecCGI +FollowSymLinks
51 # AddHandler cgi-script .cgi
52 #
53 # RewriteEngine on
54 # RewriteCond /sprockets.js !-f
55 # RewriteRule ^sprockets\.js /nph-sprockets.cgi [P,L]
56 # </Directory>
57 # </VirtualHost>
58 #
59 # All requests to /sprockets.js will transparently proxy /nph-sprockets.cgi if
60 # mysite/public/sprockets.js does not exist. In production, you can add
61 #
9a89c253 »
2009-02-18 Change the CGI output generation environment variable name to "sprock…
62 # SetEnv sprockets_generate_output_file true
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
63 #
64 # to your Apache configuration and mysites/public/sprockets.js will be cached
65 # on the first request to /sprockets.js.
66
67 require "yaml"
68 require "fileutils"
69
70 def respond_with(options = {})
6ff59fc5 »
2009-02-18 Prefer "vendor/gems/sprockets/lib" to "vendor/sprockets/lib" when loo…
71 options = { :code => 200, :content => "", :type => "text/plain" }.merge(options)
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
72 print "HTTP/1.0 #{options[:code]}\r\n"
73 print "Content-Type: #{options[:type]}\r\n"
74 print "Content-Length: #{options[:content].length}\r\n"
75 print "\r\n#{options[:content]}"
76 $stdout.flush
77 exit!
78 end
79
80 def search_upwards_for(filename)
81 pwd = original_pwd = Dir.pwd
82 loop do
83 return File.expand_path(filename) if File.file?(filename)
84 Dir.chdir("..")
85 respond_with(:code => 500, :content => "couldn't find config/sprockets.yml") if Dir.pwd == pwd
86 pwd = Dir.pwd
87 end
88 ensure
89 Dir.chdir(original_pwd)
90 end
91
92 def generate_output_file?
9a89c253 »
2009-02-18 Change the CGI output generation environment variable name to "sprock…
93 (ENV["REDIRECT_sprockets_generate_output_file"] || ENV["sprockets_generate_output_file"]) =~ /true/i
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
94 end
95
96 configuration_file = search_upwards_for("config/sprockets.yml")
97 sprockets_root = File.dirname(File.dirname(configuration_file))
98 configuration = YAML.load(IO.read(configuration_file))
99
100 begin
6ff59fc5 »
2009-02-18 Prefer "vendor/gems/sprockets/lib" to "vendor/sprockets/lib" when loo…
101 if File.directory?(sprockets_dir = File.join(sprockets_root, "vendor/gems/sprockets/lib"))
102 $:.unshift sprockets_dir
103 elsif File.directory?(sprockets_dir = File.join(sprockets_root, "vendor/sprockets/lib"))
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
104 $:.unshift sprockets_dir
105 else
106 require "rubygems"
107 end
108
109 require "sprockets"
110
111 rescue Exception => e
112 respond_with(:code => 500, :content => "couldn't find sprockets: #{e}")
113 end
114
115 begin
116 secretary = Sprockets::Secretary.new(
117 :root => sprockets_root,
118 :load_path => configuration[:load_path],
119 :source_files => configuration[:source_files]
120 )
121
9a89c253 »
2009-02-18 Change the CGI output generation environment variable name to "sprock…
122 secretary.concatenation.save_to(File.join(sprockets_root, configuration[:output_file])) if generate_output_file?
37421309 »
2009-02-15 Add a simple CGI wrapper around Sprockets
123 respond_with(:content => secretary.concatenation.to_s, :type => "text/javascript")
124
125 rescue Exception => e
126 respond_with(:code => 500, :content => "couldn't generate concatenated javascript: #{e}")
127 end
Something went wrong with that request. Please try again.