aboutsummaryrefslogtreecommitdiffstats
path: root/Cakefile
blob: d01ab6b321deca488506248c95d35285c02a7313 (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
fs      = require 'fs'
{exec, spawn}  = require 'child_process'
util    = require 'util'
{jsmin} = require 'jsmin'

targetName    = "psd"

###
CoffeeScript Options
###
strictMode    = false
csSrcDir      = "src"
csTargetDir   = "lib"

depsDir        = "deps"

targetCoffee  = "#{csSrcDir}/build"

targetCoreJS      = "#{csTargetDir}/#{targetName}.js"
targetCoreMinJS   = "#{csTargetDir}/#{targetName}.min.js"
coffeeCoreOpts    = "-j #{targetName}.js -o #{csTargetDir} -c #{targetCoffee}.coffee"

# All source files listed in include order
coffeeFiles   = [
  "psdassert"
  
  "psd"
  "psdcolor"
  "psddescriptor"
  "psdfile"
  "psdheader"
  "psdimage"
  "psdchannelimage"
  "psdlayer"
  "psdlayermask"

  "layerdata/blackwhite"
  "layerdata/brightnesscontrast"
  "layerdata/colorbalance"
  "layerdata/curves"
  "layerdata/exposure"
  "layerdata/gradient"
  "layerdata/huesaturation"
  "layerdata/invert"
  "layerdata/layereffect"
  "layerdata/levels"
  "layerdata/pattern"
  "layerdata/photofilter"
  "layerdata/posterize"
  "layerdata/selectivecolor"
  "layerdata/solidcolor"
  "layerdata/threshold"
  "layerdata/typetool"
  "layerdata/vibrance"

  "psdresource"
  "util"
  "log"
]

###
Event System
###
finishedCallback = {}
finished = (type) ->      
  finishedCallback[type]() if finishedCallback[type]?

finishListener = (type, cb) ->
  finishedCallback[type] = cb

###
Options
###
option '-f', '--file [FILE]', 'Test file to load (for debugging)'

###
Tasks
###
task 'debug', 'Run psd.js in debug mode with node-inspector', (opts) ->
  throw "Must specify a file with -f" if not opts.file

  debug = spawn 'coffee', ['--nodejs', '--debug-brk', opts.file]
  debug.stdout.on 'data', (data) -> console.log data

  insp = spawn 'node-inspector'
  insp.stdout.on 'data', (data) -> util.log data

  exec 'open http://127.0.0.1:8080/debug?port=5858'

task 'docs', 'Generates documentation for the coffee files', ->
  util.log 'Invoking docco on the CoffeeScript source files'
  
  files = coffeeFiles
  files[i] = "#{csSrcDir}/#{files[i]}.coffee" for i in [0...files.length]

  exec "./node_modules/docco/bin/docco #{files.join(' ')}", (err, stdout, stderr) ->
    util.log err if err
    util.log "Documentation built into docs/ folder."

task 'test', 'Run all unit tests', ->
  reporter = require('nodeunit').reporters.default
  process.chdir __dirname

  console.log "=> Running unit tests"
  reporter.run ['test/unit_tests'], null, ->

    {TargetPractice} = require './test/targetpractice'
    tp = new TargetPractice "psd.tp/**/*.json"

    console.log "\n=> Running TargetPractice"
    tp.runTests()
        
task 'watch', 'Automatically recompile the CoffeeScript files when updated', ->
  util.log "Watching for changes in #{csSrcDir}"
  
  for jsFile in coffeeFiles then do (jsFile) ->
    fs.watchFile "#{csSrcDir}/#{jsFile}.coffee", (curr, prev) ->
      if +curr.mtime isnt +prev.mtime
        util.log "#{csSrcDir}/#{jsFile}.coffee updated"
        invoke 'build'
        
task 'build', 'Compile and minify all CoffeeScript source files', ->
  finishListener 'js', -> invoke 'minify'
  invoke 'compile'

task 'compile', 'Compile all CoffeeScript source files', (opts) ->
  util.log "Building #{targetCoreJS}"
  contents = []
  remaining = coffeeFiles.length

  util.log "Appending #{coffeeFiles.length} files to #{targetCoffee}.coffee"
  
  for file, index in coffeeFiles then do (file, index) ->
    fs.readFile "#{csSrcDir}/#{file}.coffee", "utf8", (err, fileContents) ->
      util.log err if err
      
      contents[index] = fileContents
      util.log "[#{index + 1}] #{file}.coffee"
      process() if --remaining is 0
      
  process = ->
    contents.unshift "###\nEND DEPENDENCIES\n###\n\n"
    deps = fs.readdirSync depsDir
    for dep in deps
      util.log "Adding dependency #{dep}"
      contents.unshift "`" + fs.readFileSync("#{depsDir}/#{dep}", "utf8") + "`\n\n"

    contents.unshift fs.readFileSync("#{csSrcDir}/license.coffee") + "\n\n"
    contents.unshift "\"use strict\"" if strictMode
    core = contents.join("\n\n")

    fs.writeFile "#{targetCoffee}.coffee", core, "utf8", (err) ->
      util.log err if err
      
      exec "./node_modules/.bin/coffee #{coffeeCoreOpts}", (err, stdout, stderr) ->
        util.log err if err
        util.log "Compiled #{targetCoreJS}"
        fs.unlink "#{targetCoffee}.coffee"

        finished('js')
        
task 'minify', 'Minify the CoffeeScript files', ->
  util.log "Minifying #{targetCoreJS}"
  fs.readFile targetCoreJS, "utf8", (err, contents) ->
    fs.writeFile targetCoreMinJS, jsmin(contents), "utf8", (err) ->
      util.log err if err