-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGruntfile.coffee
230 lines (184 loc) · 6.2 KB
/
Gruntfile.coffee
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
fs = require 'fs'
path = require 'path'
module.exports = ->
# Project configuration
@initConfig
pkg: @file.readJSON 'package.json'
# CoffeeScript compilation
coffee:
src:
expand: true
cwd: 'src'
src: ['**.coffee']
dest: 'src'
ext: '.js'
schema:
expand: true
cwd: 'schema'
src: ['**.coffee']
dest: 'schema'
ext: '.js'
test:
expand: true
cwd: 'test/schema/'
src: ['**.coffee']
dest: 'test/schema/'
ext: '.js'
convert:
yaml:
files: [
expand: true
cwd: 'schema/yaml'
src: ['*.yml']
dest: 'schema/json/'
ext: '.json'
]
# Automated recompilation and testing when developing
watch:
test:
files: ['test/**/*.coffee', 'src/*.coffee', 'schema/*.coffee']
tasks: ['test']
yaml:
files: ['schema/yaml/**/*.yml']
tasks: ['convert', 'json-to-js', 'test']
mochaTest:
test:
src: ['test/schema/*.js']
options:
greph: process.env.TESTS
# FBP Network Protocol tests
exec:
fbp_test: 'node bin/fbp-test --colors'
spechtml: 'node ./node_modules/.bin/showdown makehtml -i spec/protocol.md -o dist/index.html'
# Deploying
'gh-pages':
options:
base: 'dist/',
user:
name: 'fbp-protocol bot',
silent: true
repo: 'https://' + process.env.GH_TOKEN + '@github.com/flowbased/fbp-protocol.git'
src: '**/*'
# Grunt plugins used for testing
@loadNpmTasks 'grunt-contrib-coffee'
@loadNpmTasks 'grunt-contrib-watch'
@loadNpmTasks 'grunt-exec'
@loadNpmTasks 'grunt-mocha-test'
# For deploying
@loadNpmTasks 'grunt-gh-pages'
# Create json schemas from yaml
@loadNpmTasks 'grunt-convert'
# Our local tasks
@registerTask 'build', ['coffee', 'convert', 'json-to-js', 'build-markdown', 'exec:spechtml']
@registerTask 'test', ['build', 'mochaTest'] # FIXME: enable 'exec:fbp_test'
@registerTask 'default', ['test']
@registerTask 'json-to-js', ->
schemaJs = "module.exports = #{JSON.stringify getSchemas()}"
fs.writeFileSync './schema/schemas.js', schemaJs, 'utf8'
schemas = null
getSchemas = ->
unless schemas
schemas = {}
dir = './schema/json/'
for jsonFile in fs.readdirSync dir
if jsonFile not in ['.', '..']
name = jsonFile.split('.')[0]
filename = path.join dir, jsonFile
schema = JSON.parse fs.readFileSync filename
schemas[name] = schema
return schemas
# transforms schemas into format better suited to be added to docs
getDescriptions = ->
tv4 = require './schema/index.js'
schemas = getSchemas()
fillRefs = (obj) ->
if typeof obj isnt 'object'
return obj
else if (typeof obj) is 'object' and obj.length?
return obj.map (item) -> fillRefs item
newObj = {}
for own key, value of obj
if key is '$ref'
refObj = fillRefs tv4.getSchema(value)
for own refKey, refValue of refObj
newObj[refKey] = refValue
else
newObj[key] = fillRefs value
return newObj
mergeAllOf = (obj) ->
unless typeof obj is "object" and not obj.length?
return obj
newObj = {}
for key, value of obj
if key is 'allOf'
for mergeObj in obj[key]
for propName, prop of mergeObj
newObj[propName] ?= prop
else if typeof value is "object" and not value.length?
newObj[key] = mergeAllOf value
else
newObj[key] = value
return newObj
desc = {}
protocols =
runtime: ['input', 'output']
graph: ['input', 'output']
component: ['input', 'output']
network: ['input', 'output']
for protocol, categories of protocols
messages = {}
desc[protocol] =
title: schemas[protocol].title
description: schemas[protocol].description
messages: messages
for category in categories
for event, schema of schemas[protocol][category]
schema = fillRefs schema
message =
id: schema.id
description: schema.description
if schema.allOf?
for key, value of schema.allOf[1].properties.payload
message[key] = value
messages[event] = mergeAllOf message
return desc
@registerTask 'build-markdown', ->
lines = []
p = (line) -> lines.push line
for protocol, protocolProps of getDescriptions()
p "<a id=\"#{protocol}\"></a>"
p "## #{protocolProps.title}\n"
p "#{protocolProps.description}\n"
for messageType, message of protocolProps.messages
p "### `#{messageType}`\n"
p "#{message.description}"
for messagePropName, messageProp of message.properties
line = "* `#{messagePropName}`: #{messageProp.description}"
items = messageProp.items
if messageProp.type is 'object' and messageProp.properties?
p line
for subPropName, subProp of messageProp.properties
p " - `#{subPropName}`: #{subProp.description}"
if items?.type is 'object'
line += ", each containing"
p line
for itemPropName, itemProp of items.properties
if itemProp.type is 'object'
p " * `#{itemPropName}`: #{itemProp.description}"
for itemSubPropName, itemSubProp of itemProp.properties
p " - `#{itemSubPropName}`: #{itemSubProp.description}"
else
p " - `#{itemPropName}`: #{itemProp.description}"
if items?.type is 'string' and items?._enumDescriptions
line += " Options include:"
p line
for enumDescription in items._enumDescriptions
p " - `#{enumDescription.name}`: #{enumDescription.description}"
else
p line
p '\n'
marker = "<%= descriptions %>\n"
file = fs.readFileSync 'spec/protocol.js.md', 'utf8'
file = file.replace marker, lines.join('\n')
fs.writeFileSync 'spec/protocol.md', file, 'utf8'