public
Description: JavaScript dependency management and concatenation
Homepage: http://getsprockets.org/
Clone URL: git://github.com/sstephenson/sprockets.git
sstephenson (author)
Thu Feb 19 07:19:28 -0800 2009
commit  e0ddeaf4c2f1e9e175df6dc909afd78057326a42
tree    01afb834874ad9465dfd89719cce3e15da0c18c2
parent  83bd5093bdc1799b8be66152e28f19f369ec7bf9
sprockets / ext / nph-sprockets.cgi
100755 128 lines (115 sloc) 3.969 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby
 
# This is a simple CGI wrapper around Sprockets.
#
# Copy it into a directory on your site with CGI enabled. When invoked, the
# script will search its directory and parent directories for a YAML file named
# "config/sprockets.yml" in order to load configuration information.
#
# If you set the environment variable "sprockets_generate_output_file" to
# "true" the concatenation will be cached to disk. Use it in conjunction with
# URL rewriting to cache your Sprockets output on the first request.
#
# Assuming a site layout like this:
#
# mysite/
# config/
# sprockets.yml
# javascripts/
# mysite.js
# ...
# public/
# index.html
# nph-sprockets.cgi (this file)
# vendor/
# sprockets/
# prototype/ -> ...
# scriptaculous/ -> ...
#
# mysite/config/sprockets.yml might look like this:
#
# :load_path:
# - javascripts
# - vendor/sprockets/*/src
# :source_files:
# - javascripts/mysite.js
# - javascripts/*.js
# :output_file: public/sprockets.js
#
# The <script> tag in mysite/public/index.html could look like this:
#
# <script type="text/javascript" src="/sprockets.js"></script>
#
# And you might have the following Apache configuration:
#
# <VirtualHost ...>
# ServerName mysite.example.org
# DocumentRoot "/path/to/mysite/public"
#
# <Directory "/path/to/mysite/public">
# Options +ExecCGI +FollowSymLinks
# AddHandler cgi-script .cgi
#
# RewriteEngine on
# RewriteCond /sprockets.js !-f
# RewriteRule ^sprockets\.js /nph-sprockets.cgi [P,L]
# </Directory>
# </VirtualHost>
#
# All requests to /sprockets.js will transparently proxy /nph-sprockets.cgi if
# mysite/public/sprockets.js does not exist. In production, you can add
#
# SetEnv sprockets_generate_output_file true
#
# to your Apache configuration and mysites/public/sprockets.js will be cached
# on the first request to /sprockets.js.
 
require "yaml"
require "fileutils"
 
def respond_with(options = {})
  options = { :code => 200, :content => "", :type => "text/plain" }.merge(options)
  print "HTTP/1.0 #{options[:code]}\r\n"
  print "Content-Type: #{options[:type]}\r\n"
  print "Content-Length: #{options[:content].length}\r\n"
  print "\r\n#{options[:content]}"
  $stdout.flush
  exit!
end
 
def search_upwards_for(filename)
  pwd = original_pwd = Dir.pwd
  loop do
    return File.expand_path(filename) if File.file?(filename)
    Dir.chdir("..")
    respond_with(:code => 500, :content => "couldn't find config/sprockets.yml") if Dir.pwd == pwd
    pwd = Dir.pwd
  end
ensure
  Dir.chdir(original_pwd)
end
 
def generate_output_file?
  (ENV["REDIRECT_sprockets_generate_output_file"] || ENV["sprockets_generate_output_file"]) =~ /true/i
end
 
configuration_file = search_upwards_for("config/sprockets.yml")
sprockets_root = File.dirname(File.dirname(configuration_file))
configuration = YAML.load(IO.read(configuration_file))
 
begin
  if File.directory?(sprockets_dir = File.join(sprockets_root, "vendor/gems/sprockets/lib"))
    $:.unshift sprockets_dir
  elsif File.directory?(sprockets_dir = File.join(sprockets_root, "vendor/sprockets/lib"))
    $:.unshift sprockets_dir
  else
    require "rubygems"
  end
  
  require "sprockets"
  
rescue Exception => e
  respond_with(:code => 500, :content => "couldn't find sprockets: #{e}")
end
 
begin
  secretary = Sprockets::Secretary.new(
    :root => sprockets_root,
    :load_path => configuration[:load_path],
    :source_files => configuration[:source_files]
  )
  
  secretary.concatenation.save_to(File.join(sprockets_root, configuration[:output_file])) if generate_output_file?
  respond_with(:content => secretary.concatenation.to_s, :type => "text/javascript")
  
rescue Exception => e
  respond_with(:code => 500, :content => "couldn't generate concatenated javascript: #{e}")
end