Skip to content

Commit

Permalink
Fixed adding and removing of files, updated readme, and Guardfile tem…
Browse files Browse the repository at this point in the history
…plate
  • Loading branch information
imathis committed Aug 23, 2013
1 parent 2115026 commit be50f3b
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 92 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -18,3 +18,4 @@ tmp
.DS_Store
test/javascripts/*.js
_site
js/*.js
6 changes: 3 additions & 3 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,6 @@
- initial release

## 1.1.0
- Added compatibility with jekyll-stitch-plus
- Added compatibility with guard-jekyll-plus
- Changed default Guardfile to expect a config yaml file
- Added compatibility with jekyll-stitch-plus.
- Added compatibility with guard-jekyll-plus.
- Now handling file additions and removals correctly.
107 changes: 102 additions & 5 deletions README.md
@@ -1,24 +1,95 @@
# Guard::Stitchplus
# Guard Stitch Plus

Write a gem description
A guard compiling and uglifying javascripts, powered by [stitch-plus](https://github.com/imathis/stitch-plus).

## Installation

Add this line to your application's Gemfile:

gem 'guard-stitchplus'
gem 'guard-stitch-plus'

And then execute:

$ bundle

Or install it yourself as:

$ gem install guard-stitchplus
$ gem install guard-stitch-plus

## Usage

Write usage instructions here
Add this guard to your Guardfile with `guard init stitch-plus`. Here's a sample Guardfile.

```ruby
stitch_config = {
output: 'javascripts/site.js',
dependencies: [
'javascripts/lib/jquery.js',
'javascripts/lib'
],
paths: ['javascripts/modules'],
fingerprint: true,
uglify: true
}

guard 'stitch-plus', stitch_config do
watch /^javascripts/
end
```

Whenever the guard is loaded or when a javascript file changes which is being watched, stitch-plus will:

1. Add jquery.js, followed by any other javascript or coffeescript file in `javascripts/lib/`.
2. Add a Require function and wrap all files in `javascripts/modules` in a CommonJS wrapper.
3. Uglify the compiled javascript.
4. Write it to a fingerprinted file at `javascripts/site-f1408932717b4b16eb97969d34961213.js`.

### Using this with Jekyll?

If you're using Jekyll, be sure to check out [jekyll-stitch-plus](https://github.com/octopress/jekyll-stitch-plus) which integrates smoothly with jekyll and also plays
nicely with this guard plugin. In that case be sure to add your stitch-plus configuration to your Jekyll config file (usually _config.yml) and point your guard to that
file as well.

Your Jekyll YAML config file might look like this.

```yaml
stitch:
output: 'javascripts/site.js'
dependencies:
- 'javascripts/lib/jquery.js'
- 'javascripts/lib'
paths: ['javascripts/modules']
fingerprint: true
uglify: true
```

Then your Guardfile would look like this.

```ruby
guard 'stitch-plus', config: '_config.yml' do
watch /^(javascripts|_config.yml)/
end
```

Notice that the guard is also watching the Jekyll config file.

## Configuration

Configure stitch-plus by passing a hash of options or pointing it to a YAML config file.

| Config | Description | Default |
|:-----------------|:---------------------------------------------------------------------------|:------------|
| `config` | A path to a YAML file containing stitch configurations | nil |
| `dependencies` | Array of files/directories to be added first as global javascripts | nil |
| `paths` | Array of directories where javascripts will be wrapped as CommonJS modules | nil |
| `output` | A path to write the compiled javascript | 'all.js' |
| `fingerprint` | Add a fingerprint to the file name for super cache busting power | false |
| `cleanup` | Automatically remove previously compiled files | true |
| `uglify` | Smash javascript using the Uglifier gem | false |
| `uglify_options` | Options for the Uglifier gem. See the [Uglifier docs](https://github.com/lautis/uglifier#usage) for details. | {} |

Note: You can configure from the hash and a yaml file, but configurations loaded from a YAML file will override any settings in your config hash. For example, `{config: 'stitch.yml', uglify: false}` will be overwritten if the yaml file sets `uglify: true`.


## Contributing

Expand All @@ -27,3 +98,29 @@ Write usage instructions here
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

## License

Copyright (c) 2013 Brandon Mathis

MIT License

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:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

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.

26 changes: 24 additions & 2 deletions lib/guard/stitch-plus.rb
Expand Up @@ -12,7 +12,7 @@ def initialize (watchers=[], options={})
def start
@stitcher = ::StitchPlus.new(@options.merge({guard: true}))
ENV['GUARD_STITCH_PLUS'] = 'true'
ENV['GUARD_STITCH_PLUS_FILES'] = @stitcher.all_files.join(',')
@stitch_files = stitch_files
write
end

Expand All @@ -24,10 +24,24 @@ def run_all
write
end

def run_on_removals(paths)
if (paths & @stitch_files).size > 0
write
end
end

def run_on_additions(paths)
@stitch_files = stitch_files
if (paths & @stitch_files).size > 0
write
end
end

def run_on_changes(paths)
@stitch_files = stitch_files
if @options[:config] and paths.include? @options[:config]
start
elsif (paths & @stitcher.all_files).size > 0
elsif (paths & @stitch_files).size > 0
write
end
end
Expand All @@ -36,6 +50,14 @@ def write
@stitcher.write
ENV['GUARD_STITCH_PLUS_OUTPUT'] = @stitcher.last_write
end

private

def stitch_files
all_files = @stitcher.all_files
ENV['GUARD_STITCH_PLUS_FILES'] = all_files.join(',')
all_files
end
end
end

16 changes: 14 additions & 2 deletions lib/guard/stitch-plus/templates/Guardfile
@@ -1,4 +1,16 @@
guard 'stitch-plus', config: '_config.yml' do
watch %r{^javascripts/|_config\.yml}
# Read more about stitch-plus configuration at https://github.com/imathis/guard-stitch-plus
#
stitch_config = {
dependencies: [
'javascripts/lib/jquery.js',
'javascripts/lib'
],
paths: ['javascripts/modules'],
output: 'javascripts/site.js',
uglify: true
}

guard 'stitch-plus', stitch_config do
watch /javascripts/
end

2 changes: 1 addition & 1 deletion lib/guard/stitch-plus/version.rb
@@ -1,5 +1,5 @@
module Guard
module StitchPlusVersion
VERSION = "1.0.0"
VERSION = "1.1.0"
end
end
12 changes: 10 additions & 2 deletions test/Guardfile
Expand Up @@ -2,10 +2,18 @@
# More info at https://github.com/guard/guard#readme


guard 'stitch-plus', config: '_config.yml' do
watch %r{^js/|_config.yml}
# Add
stitch_config = {
dependencies: ['js/lib'],
paths: ['js/modules'],
output: 'js/site.js',
uglify: true
}
guard 'stitch-plus', stitch_config do
watch /js/
end


guard "jekyll-plus" do
watch /.*/
ignore %r{^_site/}
Expand Down
77 changes: 0 additions & 77 deletions test/js/site.js

This file was deleted.

0 comments on commit be50f3b

Please sign in to comment.