This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
175 lines (134 loc) · 4.49 KB
/
Cakefile
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
167
168
169
170
171
172
173
174
175
fs = require 'fs'
url = require 'url'
path = require 'path'
crypto = require 'crypto'
{exec, spawn} = require 'child_process'
# Uses the same keys as [michael's substance](https://github.com/michael/substance)
try
config = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
catch _
process.stderr.write "You must write a `config.json` file for some tasks. Use `config.json.example` for reference."
if config
parts = url.parse config.couchdb_url
db_host = parts.hostname
db_port = parts.port
db_name = parts.pathname.slice(1)
if auth = parts.auth
db_user = auth.split(':')[0]
db_pass = auth.split(':')[1]
# Build
# =====
task 'build:parser', ->
jison = exec 'jison src/parser/karol.yy src/parser/karol.l'
connectStd jison
jison.on 'exit', ->
fs.rename 'karol.js', 'lib/parser/parser.js'
###
task 'compress', 'Bundle and compress all JavaScript files', ->
{parser,uglify} = require 'uglify-js'
extractScripts = (html) ->
scriptTag = /<script src="\/f\/([a-zA-Z0-9_.-\/]+)"><\/script>/g
scripts = []
scripts.push match[1] while match = scriptTag.exec html
scripts
searchFile = (filename) ->
for dir in ['/lib/', '/public/']
filepath = __dirname + dir + filename
return filepath if path.existsSync filepath
readFile = (filepath) ->
fs.readFileSync filepath, 'utf-8'
compress = (code) ->
ast = parser.parse code
ast = uglify.ast_mangle ast
ast = uglify.ast_squeeze ast
uglify.gen_code ast
compressed = extractScripts(readFile 'couchapp/templates/layout.html')
.map(searchFile)
.map(readFile)
.map(compress)
.join()
# TODO: write compressed
###
# Test
# ====
task 'test', ->
require './test/run_tests'
# doesn't work somehow
jasmine = exec 'jasmine-node test/spec'
connectStd jasmine
# Push/Sync CouchApp
# ==================
# Note to myself: Start CouchDB with `sudo /etc/init.d/couchdb start`
createCouchApp = (callback) ->
couchapp = require 'couchapp'
couchapp.createApp require('./couchapp/app.js'), config.couchdb_url, callback
task 'push', 'Push the couchapp to the server', ->
createCouchApp (app) -> app.push()
task 'sync', 'Push and watch local files for changes', ->
createCouchApp (app) -> app.sync()
STATIC_DIR = "#{__dirname}/couchapp/static"
task 'upload:static', ->
db = openDBWithAuth db_user, db_pass
fs.readdirSync(STATIC_DIR)
.forEach (file) ->
name = file.replace(/\.html$/, '')
body = fs.readFileSync("#{STATIC_DIR}/#{file}", 'utf-8')
title = if m = body.match(/<h2>(.*)<\/h2>/) then m[1] else "Untitled Page"
db.save name,
title: title
body: body
# Examples
# ========
DEFAULT_WORLD = "#{__dirname}/examples/default_world.kdw"
task 'upload:examples', ->
invoke 'upload:old-examples'
invoke 'upload:new-examples'
uploadExamplesDir = (dir, description) ->
db = openDBWithExamples()
fs.readdirSync(dir)
.forEach (file) ->
language = if file.match(/\.js$/)
'javascript'
else if file.match(/\.kdp$/)
'karol'
else
null
return unless language
codeFile = "#{dir}/#{file}"
worldFile = codeFile.replace(/\.[a-z]+$/, '.kdw')
worldFile = DEFAULT_WORLD unless path.existsSync worldFile
db.save sha1(codeFile),
type: 'project'
author: "examples"
title: path.basename(codeFile).replace(/\.[a-z]+$/, '')
world: fs.readFileSync worldFile, 'utf-8'
code: fs.readFileSync codeFile, 'utf-8'
language: language
description: description
, (err) -> console.error err if err
task 'upload:old-examples', "Upload the old examples that come bundled with Robot Karol", ->
uploadExamplesDir("#{__dirname}/examples/old", "This is one of the examples that come bundled with Robot Karol.")
task 'upload:new-examples', "Upload the new examples", ->
uploadExamplesDir("#{__dirname}/examples/new", "")
# Helpers
# -------
sha1 = (str) ->
hash = crypto.createHash('sha1')
hash.update(str)
hash.digest('hex')
openDBWithAuth = (username, password) ->
auth = if username and password
username: username
password: password
else
undefined
cradle = require 'cradle'
connection = new cradle.Connection
host: db_host
port: db_port
auth: auth
connection.database db_name
openDBWithExamples = -> openDBWithAuth 'examples', config.examples_password
connectStd = (child) ->
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)