Inital Commit
[oweals/finalsclub.git] / node_modules / jade / benchmarks / haml / spec / lib / jspec.growl.js
1
2 // JSpec - Growl - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
3
4 ;(function(){
5   
6   Growl = {
7     
8     // --- Version
9     
10     version: '1.0.0',
11     
12     /**
13      * Execute the given _cmd_, returning an array of lines from stdout.
14      *
15      * Examples:
16      *
17      *   Growl.exec('growlnotify', '-m', msg)
18      *
19      * @param  {string ...} cmd
20      * @return {array}
21      * @api public
22      */
23
24     exec: function(cmd) {
25       var lines = [], line
26       with (JavaImporter(java.lang, java.io)) {
27         var proccess = Runtime.getRuntime().exec(Array.prototype.slice.call(arguments))
28         var stream = new DataInputStream(proccess.getInputStream())
29         while (line = stream.readLine())
30           lines.push(line + '')
31         stream.close()    
32       }
33       return lines
34     },
35     
36     /**
37      * Return the extension of the given _path_ or null.
38      *
39      * @param  {string} path
40      * @return {string}
41      * @api private
42      */
43     
44     extname: function(path) {
45       return path.lastIndexOf('.') != -1 ? 
46         path.slice(path.lastIndexOf('.') + 1, path.length) :
47           null
48     },
49
50     /**
51      * Version of the 'growlnotify' binary.
52      *
53      * @return {string}
54      * @api private
55      */
56
57     binVersion: function() {
58       try { return this.exec('growlnotify', '-v')[0].split(' ')[1] } catch (e) {}
59     },
60
61     /**
62      * Send growl notification _msg_ with _options_.
63      *
64      * Options:
65      *
66      *  - title   Notification title
67      *  - sticky  Make the notification stick (defaults to false)
68      *  - name    Application name (defaults to growlnotify)
69      *  - image
70      *    - path to an icon sets --iconpath
71      *    - path to an image sets --image
72      *    - capitalized word sets --appIcon
73      *    - filename uses extname as --icon
74      *    - otherwise treated as --icon
75      *
76      * Examples:
77      *
78      *   Growl.notify('New email')
79      *   Growl.notify('5 new emails', { title: 'Thunderbird' })
80      *
81      * @param {string} msg
82      * @param {options} hash
83      * @api public
84      */
85
86     notify: function(msg, options) {
87       options = options || {}
88       var args = ['growlnotify', '-m', msg]
89       if (!this.binVersion()) throw new Error('growlnotify executable is required')
90       if (image = options.image) {
91         var flag, ext = this.extname(image)
92         flag = flag || ext == 'icns' && 'iconpath'
93         flag = flag || /^[A-Z]/.test(image) && 'appIcon'
94         flag = flag || /^png|gif|jpe?g$/.test(ext) && 'image'
95         flag = flag || ext && (image = ext) && 'icon'
96         flag = flag || 'icon'
97         args.push('--' + flag, image)
98       }
99       if (options.sticky) args.push('--sticky')
100       if (options.name) args.push('--name', options.name)
101       if (options.title) args.push(options.title)
102       this.exec.apply(this, args)
103     }
104   }
105   
106   JSpec.include({
107     name: 'Growl',
108     reporting: function(options){
109       var stats = JSpec.stats
110       if (stats.failures) Growl.notify('failed ' + stats.failures + ' assertions', { title: 'JSpec'})
111       else Growl.notify('passed ' + stats.passes + ' assertions', { title: 'JSpec' })
112     }
113   })
114   
115 })()