ninjs 0.16.3 → 0.16.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/VERSION +1 -1
  2. data/lib/sprockets/.gitignore +4 -0
  3. data/lib/sprockets/CHANGELOG +22 -0
  4. data/lib/sprockets/LICENSE +20 -0
  5. data/lib/sprockets/README.markdown +230 -0
  6. data/lib/sprockets/bin/sprocketize +54 -0
  7. data/lib/sprockets/ext/nph-sprockets.cgi +127 -0
  8. data/lib/sprockets/lib/sprockets.rb +42 -0
  9. data/lib/sprockets/lib/sprockets/concatenation.rb +36 -0
  10. data/lib/sprockets/lib/sprockets/environment.rb +52 -0
  11. data/lib/sprockets/lib/sprockets/error.rb +5 -0
  12. data/lib/sprockets/lib/sprockets/pathname.rb +37 -0
  13. data/lib/sprockets/lib/sprockets/preprocessor.rb +96 -0
  14. data/lib/sprockets/lib/sprockets/secretary.rb +110 -0
  15. data/lib/sprockets/lib/sprockets/source_file.rb +56 -0
  16. data/lib/sprockets/lib/sprockets/source_line.rb +85 -0
  17. data/lib/sprockets/lib/sprockets/version.rb +9 -0
  18. data/lib/sprockets/sprockets.gemspec +15 -0
  19. data/lib/sprockets/test/fixtures/assets/images/script_with_assets/one.png +1 -0
  20. data/lib/sprockets/test/fixtures/assets/images/script_with_assets/two.png +1 -0
  21. data/lib/sprockets/test/fixtures/assets/stylesheets/script_with_assets.css +1 -0
  22. data/lib/sprockets/test/fixtures/constants.yml +1 -0
  23. data/lib/sprockets/test/fixtures/double_slash_comments_that_are_not_requires_should_be_ignored_when_strip_comments_is_false.js +8 -0
  24. data/lib/sprockets/test/fixtures/double_slash_comments_that_are_not_requires_should_be_removed_by_default.js +2 -0
  25. data/lib/sprockets/test/fixtures/multiline_comments_should_be_removed_by_default.js +4 -0
  26. data/lib/sprockets/test/fixtures/requiring_a_file_after_it_has_already_been_required_should_do_nothing.js +5 -0
  27. data/lib/sprockets/test/fixtures/requiring_a_file_that_does_not_exist_should_raise_an_error.js +1 -0
  28. data/lib/sprockets/test/fixtures/requiring_a_single_file_should_replace_the_require_comment_with_the_file_contents.js +3 -0
  29. data/lib/sprockets/test/fixtures/requiring_the_current_file_should_do_nothing.js +1 -0
  30. data/lib/sprockets/test/fixtures/src/constants.yml +3 -0
  31. data/lib/sprockets/test/fixtures/src/foo.js +1 -0
  32. data/lib/sprockets/test/fixtures/src/foo/bar.js +4 -0
  33. data/lib/sprockets/test/fixtures/src/foo/foo.js +1 -0
  34. data/lib/sprockets/test/fixtures/src/script_with_assets.js +3 -0
  35. data/lib/sprockets/test/fixtures/src/script_with_comments.js +13 -0
  36. data/lib/sprockets/test/test_concatenation.rb +28 -0
  37. data/lib/sprockets/test/test_environment.rb +64 -0
  38. data/lib/sprockets/test/test_helper.rb +55 -0
  39. data/lib/sprockets/test/test_pathname.rb +43 -0
  40. data/lib/sprockets/test/test_preprocessor.rb +107 -0
  41. data/lib/sprockets/test/test_secretary.rb +89 -0
  42. data/lib/sprockets/test/test_source_file.rb +34 -0
  43. data/lib/sprockets/test/test_source_line.rb +89 -0
  44. data/ninjs.gemspec +55 -13
  45. metadata +46 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.16.3
1
+ 0.16.4
@@ -0,0 +1,4 @@
1
+ doc
2
+ pkg
3
+ tmp
4
+ README.html
@@ -0,0 +1,22 @@
1
+ * Add a :strip_comments option to Secretary that is passed through to its Preprocessor. [#12 state:resolved]
2
+
3
+
4
+ *1.0.2* (February 24, 2009)
5
+
6
+ * Update the sprockets-rails installation instructions to use `script/plugin`. [#7 state:resolved]
7
+
8
+ * Fix Secretary#source_last_modified not to raise an exception when one of the preprocessor's source files has been moved or deleted. (Jonathan Goldman) [#10 state:resolved]
9
+
10
+ * Avoid hanging when preprocessing minified source files. (Maciej Pasternacki) [#9 state:resolved]
11
+
12
+
13
+ *1.0.1* (February 19, 2009)
14
+
15
+ * Change the CGI output generation environment variable name to "sprockets_generate_output_file" and use the right path in Concatenation#save_to.
16
+
17
+ * Prefer "vendor/gems/sprockets/lib" to "vendor/sprockets/lib" when looking for an externaled Sprockets checkout in nph-sprockets.cgi.
18
+
19
+
20
+ *1.0.0* (February 17, 2009)
21
+
22
+ * Initial public release.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sam Stephenson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,230 @@
1
+ Sprockets
2
+ =========
3
+
4
+ [http://getsprockets.org/](http://getsprockets.org/)
5
+
6
+ Sprockets is a Ruby library that preprocesses and concatenates JavaScript source files. It takes any number of source files and preprocesses them line-by-line in order to build a single concatenation. Specially formatted lines act as directives to the Sprockets preprocessor, telling it to require the contents of another file or library first or to provide a set of asset files (such as images or stylesheets) to the document root. Sprockets attempts to fulfill required dependencies by searching a set of directories called the load path.
7
+
8
+
9
+ ## Why use Sprockets?
10
+
11
+ **Extract reusable code and share it across multiple web sites or applications.** Sprockets makes it easy to extract JavaScript plugins from your site or application and share them across your portfolio. Use your SCM to check out plugin repositories directly into your site or application. Then tell Sprockets to add the plugins to your load path. Support for asset provisioning lets you bundle CSS and images with JavaScript plugins, too.
12
+
13
+ **Speed up your site by automatically concatenating JavaScript into a single file for production.** Concatenating your site's JavaScript means all your source code is cached in the browser on the first hit. It also means you reduce the number of HTTP requests necessary to load a single page. When combined with gzip compression on the web server, concatenation is the fastest way to serve JavaScript to your users.
14
+
15
+ **Organize your JavaScript source code into multiple commented files and directories.** Finally, an end to navigating long, difficult-to-maintain JavaScript source files. With Sprockets, JavaScript source code can live anywhere on your system, even outside your site's or application's document root. You're free to split source code up into multiple files and organize those files into directories during development. You can also add as many comments as you want—they'll be stripped from the resulting output.
16
+
17
+ **Use bleeding-edge framework and library code in your application.** If you use and contribute to open-source JavaScript frameworks and libraries that use Sprockets, like [Prototype](http://www.prototypejs.org/) and [script.aculo.us](http://script.aculo.us/), the build processes for those scripts can be integrated directly into your application. That makes it possible to track the latest development versions of your framework and library dependencies by adding their repositories to your application's Sprockets load path.
18
+
19
+ **Sprockets is compatible with the PDoc JavaScript documentation system and the JavaScript framework of your choice.** If you document your JavaScript source code with [PDoc](http://www.pdoc.org/), Sprockets will automatically strip documentation comments from the resulting concatenated output. You can use any JavaScript framework you like in your site or application—Sprockets is framework-agnostic.
20
+
21
+
22
+ ## Installing Sprockets
23
+
24
+ Sprockets is written in Ruby and has no runtime dependencies apart from the Ruby standard library. You can install it with RubyGems:
25
+
26
+ $ gem install --remote sprockets
27
+
28
+ This will also install the `sprocketize` command-line utility.
29
+
30
+
31
+ ## Sprocketizing your source code
32
+
33
+ Sprockets takes any number of _source files_ and preprocesses them line-by-line in order to build a single _concatenation_. Specially formatted lines act as _directives_ to the Sprockets preprocessor, telling it to _require_ the contents of another file or library first or to _provide_ a set of asset files to the document root. Sprockets attempts to fulfill required dependencies by searching a set of directories called the _load path_.
34
+
35
+ ### How Sprockets handles comments
36
+
37
+ Use single-line <nobr>(`//`)</nobr> comments in JavaScript source files for comments that don't need to appear in the resulting concatenated output. Use multiple-line <nobr>(`/* ... */`)</nobr> comments for comments that _should_ appear in the resulting concatenated output, like copyright notices or descriptive headers. PDoc <nobr>(`/** ... **/`)</nobr> documentation comments will not be included in the resulting concatenation.
38
+
39
+ Comments beginning with `//=` are treated by Sprockets as _directives_. Sprockets currently understands two directives, `require` and `provide`.
40
+
41
+ ### Specifying dependencies with the `require` directive
42
+
43
+ Use the `require` directive to tell Sprockets that another JavaScript source file should be inserted into the concatenation before continuing to preprocess the current source file. If the specified source file has already been required, Sprockets ignores the directive.
44
+
45
+ The format of a `require` directive determines how Sprockets looks for the dependent source file. If you place the name of the source file in angle brackets:
46
+
47
+ //= require <prototype>
48
+
49
+ Sprockets will search your load path, in order, for a file named `prototype.js`, and begin preprocessing the first match it finds. (An error will be raised if a matching file can't be found.) If you place the name of the source file in quotes:
50
+
51
+ //= require "date_helper"
52
+
53
+ Sprockets will _not_ search the load path, but will instead look for a file named `date_helper.js` in the same directory as the current source file. In general, it is a good idea to use quotes to refer to related files, and angle brackets to refer to packages, libraries, or third-party code that may live in a different location.
54
+
55
+ You can refer to files in subdirectories with the `require` directive. For example:
56
+
57
+ //= require <behavior/hover_observer>
58
+
59
+ Sprockets will search the load path for a file named `hover_observer.js` in a directory named `behavior`.
60
+
61
+ ### Bundling assets with the `provide` directive
62
+
63
+ Sometimes it is necessary to include associated stylesheets, images, or even HTML files with a JavaScript plugin. Sprockets lets you specify that a JavaScript source file depends on a set of assets, and offers a routine for copying all dependent assets into the document root.
64
+
65
+ The `provide` directive tells Sprockets that the current source file depends on the set of assets in the named directory. For example, say you have a plugin with the following directory structure:
66
+
67
+ plugins/color_picker/assets/images/color_picker/arrow.png
68
+ plugins/color_picker/assets/images/color_picker/circle.png
69
+ plugins/color_picker/assets/images/color_picker/hue.png
70
+ plugins/color_picker/assets/images/color_picker/saturation_and_brightness.png
71
+ plugins/color_picker/assets/stylesheets/color_picker.css
72
+ plugins/color_picker/src/color.js
73
+ plugins/color_picker/src/color_picker.js
74
+
75
+ Assume `plugins/color_picker/src/` is in your Sprockets load path. `plugins/color_picker/src/color_picker.js` might look like this:
76
+
77
+ //= require "color"
78
+ //= provide "../assets"
79
+
80
+ When `<color_picker>` is required in your application, its `provide` directive will tell Sprockets that all files in the `plugins/color_picker/assets/` directory should be copied into the web server's document root.
81
+
82
+ ### Inserting string constants with `<%= ... %>`
83
+
84
+ You may need to parameterize and insert constants into your source code. Sprockets lets you define such constants in a special file called `constants.yml` that lives in your load path. This file is formatted as a [YAML](http://yaml.org/spec/1.2/) hash.
85
+
86
+ Continuing the `color_picker` example, assume `plugins/color_picker/src/constants.yml` contains the following:
87
+
88
+ COLOR_PICKER_VERSION: 1.0.0
89
+ COLOR_PICKER_AUTHOR: Sam Stephenson <sam@example.org>
90
+
91
+ The constants are specified in a single place, and you can now insert them into your source code without repetition:
92
+
93
+ /* Color Picker plugin, version <%= COLOR_PICKER_VERSION %>
94
+ * (c) 2009 <%= COLOR_PICKER_AUTHOR %>
95
+ * Distributed under the terms of an MIT-style license */
96
+
97
+ var ColorPicker = {
98
+ VERSION: '<%= COLOR_PICKER_VERSION %>',
99
+ ...
100
+ };
101
+
102
+ The resulting concatenated output will have the constant values substituted in place:
103
+
104
+ /* Color Picker plugin, version 1.0.0
105
+ * (c) 2009 Sam Stephenson <sam@example.org>
106
+ * Distributed under the terms of an MIT-style license */
107
+
108
+ var ColorPicker = {
109
+ VERSION: '1.0.0',
110
+ ...
111
+ };
112
+
113
+ Constants share a global namespace, so you can refer to constants defined anywhere in your load path. If a constant is not found, Sprockets raises an error and halts further preprocessing.
114
+
115
+
116
+ ## Using Sprockets
117
+
118
+ Sprockets is distributed as a Ruby library. It comes with a command-line tool called `sprocketize` for generating concatenations and installing provided assets. You can also use the `sprockets-rails` plugin to sprocketize your Rails application. A simple CGI is bundled with Sprockets for use in other environments.
119
+
120
+ ### Sprockets as a Ruby library
121
+
122
+ The simplest way to use Sprockets from Ruby is with the `Sprockets::Secretary` class. A `Secretary` handles the job of setting up a Sprockets environment, creating a preprocessor, loading your application's source files, and generating the resulting concatenation.
123
+
124
+ You can pass the following options to `Sprockets::Secretary.new`:
125
+
126
+ * `:root` - Specifies the Sprockets root, or the base location for all directories specified in the load path and source files required by the preprocessor. Defaults to `"."` (the current working directory).
127
+ * `:asset_root` - Specifies the application's document root, or the directory from which the web server serves its files.
128
+ * `:load_path` - An ordered array of directory names (either absolute paths, or paths relative to the Sprockets root) where Sprockets will search for required JavaScript dependencies.
129
+ * `:source_files` - An ordered array of JavaScript source files (either absolute paths, or paths relative to the Sprockets root) that Sprockets will require one-by-one to build the resulting concatenation.
130
+ * `:expand_paths` - Specifies whether or not Sprockets will expand filenames in the `load_path` and `source_files` arrays according to shell glob rules (e.g. `:load_path => ["vendor/sprockets/*/src"]` or `:source_files => ["app/javascripts/**/*.js"]`). Defaults to `true`; set it to `false` if you do _not_ want shell expansion applied to paths.
131
+
132
+ Once you have a `Secretary` object, you can call its `concatenation` method to get a `Sprockets::Concatenation` object back. You can also call its `install_assets` method to install provided assets into the directory specified by the `:asset_root` option.
133
+
134
+ Example:
135
+
136
+ secretary = Sprockets::Secretary.new(
137
+ :asset_root => "public",
138
+ :load_path => ["vendor/sprockets/*/src", "vendor/plugins/*/javascripts"],
139
+ :source_files => ["app/javascripts/application.js", "app/javascripts/**/*.js"]
140
+ )
141
+
142
+ # Generate a Sprockets::Concatenation object from the source files
143
+ concatenation = secretary.concatenation
144
+ # Write the concatenation to disk
145
+ concatenation.save_to("public/sprockets.js")
146
+
147
+ # Install provided assets into the asset root
148
+ secretary.install_assets
149
+
150
+ You can ask the `Secretary` for the most recent last-modified time of all the source files that make up the concatenation with the `source_last_modified` method. Use this in conjunction with the `reset!` method to reuse a `Secretary` instance across multiple requests and only regenerate concatenations when the source file has changed.
151
+
152
+ ### Using `sprocketize` from the command line
153
+
154
+ The `sprocketize` command is a simple wrapper around `Sprockets::Secretary`. It takes any number of source files (specified as command-line arguments) and preprocesses them according to the options you pass. Then it prints the resulting concatenation to standard output, where it can be redirected to a file or piped to another program for further processing.
155
+
156
+ You can pass the following command-line options to `sprocketize`:
157
+
158
+ -C, --directory=DIRECTORY Change to DIRECTORY before doing anything
159
+ -I, --include-dir=DIRECTORY Adds the directory to the Sprockets load path
160
+ -a, --asset-root=DIRECTORY Copy provided assets into DIRECTORY
161
+ -h, --help Shows this help message
162
+ -v, --version Shows version
163
+
164
+ Example:
165
+
166
+ $ sprocketize -I app/javascripts \
167
+ -I vendor/sprockets/prototype/src \
168
+ -I vendor/sprockets/color_picker/src \
169
+ --asset-root=public \
170
+ app/javascripts/*.js > public/sprockets.js
171
+
172
+ ### Using the `sprockets-rails` plugin in your Rails application
173
+
174
+ The [`sprockets-rails`](http://github.com/sstephenson/sprockets-rails/tree/master) plugin (distributed separately) sets up your Rails application for use with Sprockets. To install it, first install the `sprockets` RubyGem, then check out a copy of the `sprockets-rails` repository into your `vendor/plugins/` directory. When you run the bundled `install.rb` script, `sprockets-rails` will create two new directories in your application and copy a configuration file into your `config/` directory.
175
+
176
+ `sprockets-rails` includes a controller named `SprocketsController` that renders your application's Sprockets concatenation. When caching is enabled, e.g. in production mode, `SprocketsController` uses Rails page caching to save the concatenated output to `public/sprockets.js` the first time it is requested. When caching is disabled, e.g. in development mode, `SprocketsController` will render a fresh concatenation any time a source file changes.
177
+
178
+ To source Sprockets' JavaScript concatenation from your HTML templates, use the provided `sprockets_include_tag` helper.
179
+
180
+ `sprockets-rails` also includes a set of Rake tasks for generating the concatenation (`rake sprockets:install_script`) and installing provided assets (`rake sprockets:install_assets`). Run `sprockets:install_assets` any time you add or update a Sprockets plugin in your application. Add `sprockets:install_script` as a [Capistrano](http://www.capify.org/) post-deploy hook to generate the Sprockets concatenation on your servers automatically at deploy time.
181
+
182
+ Here's a walkthrough of the installation process:
183
+
184
+ 1. `gem install --remote sprockets`
185
+
186
+ 2. `script/plugin install git://github.com/sstephenson/sprockets-rails.git`
187
+
188
+ You now have `app/javascripts/` and `vendor/sprockets/` directories in your application, as well as a `config/sprockets.yml` file.
189
+
190
+ 3. Edit your `config/routes.rb` file to add routes for `SprocketsController`:
191
+
192
+ ActionController::Routing::Routes.draw do |map|
193
+ # Add the following line:
194
+ SprocketsApplication.routes(map)
195
+ ...
196
+ end
197
+
198
+ 4. Move your JavaScript source files from `public/javascripts/` into `app/javascripts/`. All files in all subdirectories of `app/javascripts/` will be required by Sprockets in alphabetical order, with the exception of `app/javascripts/application.js`, which is required _before any other file_. (You can change this behavior by editing the `source_files` line of `config/sprockets.yml`.)
199
+
200
+ 5. Adjust your HTML templates to call `<%= sprockets_include_tag %>` instead of `<%= javascript_include_tag ... %>`.
201
+
202
+ Once `sprockets-rails` is installed, you can check out Sprockets plugins into the `vendor/sprockets/` directory. By default, `sprockets-rails` configures Sprockets' load path to search `vendor/sprockets/*/src/`, as well as `vendor/plugins/*/javascripts/`. This means that the `javascripts/` directories of Rails plugins are automatically installed into your Sprockets load path.
203
+
204
+ ### Using the bundled Sprockets CGI script
205
+
206
+ Sprockets comes with a simple CGI script for serving JavaScript outside of a Ruby environment. You can find it, along with brief documentation and installation instructions, as `ext/nph-sprockets.cgi` in your copy of the Sprockets source code. (If you installed Sprockets with RubyGems, you can find the location of the Sprockets source code with the `gem which sprockets` command.)
207
+
208
+
209
+ ## Contributing to Sprockets
210
+
211
+ The Sprockets source code is hosted on [GitHub](http://github.com/). Check out a working copy with [Git](http://git-scm.com/):
212
+
213
+ $ git clone git://github.com/sstephenson/sprockets.git
214
+
215
+ You can fork the Sprockets project on GitHub, commit your changes, and [send a pull request](http://github.com/guides/pull-requests) if you'd like your feature or bug fix to be considered for the next release. Please make sure to update the unit tests as well.
216
+
217
+ ### Reporting bugs
218
+
219
+ If you find a bug in Sprockets and aren't feeling motivated to fix it yourself, you can file a ticket at the [Sprockets Lighthouse project](http://prototype.lighthouseapp.com/projects/8888-sprockets).
220
+
221
+
222
+ ## License
223
+
224
+ Copyright &copy; 2009 Sam Stephenson.
225
+
226
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
227
+
228
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
229
+
230
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), *%w".. lib sprockets")
4
+ require "optparse"
5
+
6
+ load_path = (ENV["SPROCKETS_PATH"] || "").split(":")
7
+ filenames = []
8
+ options = { :load_path => load_path, :source_files => filenames, :expand_paths => false }
9
+
10
+ OptionParser.new do |opts|
11
+ opts.summary_width = 28
12
+ opts.banner = "Usage: sprocketize [options] filename [filename ...]"
13
+
14
+ def opts.show_usage
15
+ puts self
16
+ exit
17
+ end
18
+
19
+ opts.on("-C DIRECTORY", "--directory=DIRECTORY", "Change to DIRECTORY before doing anything") do |directory|
20
+ Dir.chdir(directory)
21
+ end
22
+
23
+ opts.on("-I DIRECTORY", "--include-dir=DIRECTORY", "Adds the directory to the Sprockets load path") do |directory|
24
+ load_path << directory
25
+ end
26
+
27
+ opts.on("-a DIRECTORY", "--asset-root=DIRECTORY", "Copy provided assets into DIRECTORY") do |directory|
28
+ options[:asset_root] = directory
29
+ end
30
+
31
+ opts.on_tail("-h", "--help", "Shows this help message") do
32
+ opts.show_usage
33
+ end
34
+
35
+ opts.on_tail("-v", "--version", "Shows version") do
36
+ puts Sprockets::Version::STRING
37
+ exit
38
+ end
39
+
40
+ opts.show_usage if ARGV.empty?
41
+
42
+ begin
43
+ opts.order(ARGV) do |filename|
44
+ filenames << filename
45
+ end
46
+ rescue OptionParser::ParseError => e
47
+ opts.warn e.message
48
+ opts.show_usage
49
+ end
50
+ end
51
+
52
+ secretary = Sprockets::Secretary.new(options)
53
+ secretary.install_assets if options[:asset_root]
54
+ print secretary.concatenation
@@ -0,0 +1,127 @@
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
+ #
9
+ # If you set the environment variable "sprockets_generate_output_file" to
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
+ #
62
+ # SetEnv sprockets_generate_output_file true
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 = {})
71
+ options = { :code => 200, :content => "", :type => "text/plain" }.merge(options)
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?
93
+ (ENV["REDIRECT_sprockets_generate_output_file"] || ENV["sprockets_generate_output_file"]) =~ /true/i
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
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"))
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
+
122
+ secretary.concatenation.save_to(File.join(sprockets_root, configuration[:output_file])) if generate_output_file?
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