-
Notifications
You must be signed in to change notification settings - Fork 0
/
don.js
executable file
·166 lines (133 loc) · 4.04 KB
/
don.js
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
#! /usr/bin/env node
const { parse } = require("./parse.js")
const { readFileSync } = require("fs")
const path = require("path")
function checkDependencyReferences(donTree) {
const undefinedDependencies = [
// {
// target: "target",
// dependency: "dep",
// }
]
for (const [target, commands] of Object.entries(donTree)) {
for (const command of commands) {
if (command.type === "dependency") {
for (const { name: dep } of command.dependencies) {
if (!donTree.hasOwnProperty(dep)) {
undefinedDependencies.push({
target,
dependency: dep,
})
}
}
}
}
}
return undefinedDependencies
}
function checkCircular(donTree, target, commands, stack = new Set(), circularDeps = {}) {
let commandNum = 0
let foundCircular = null
for (const command of commands) {
commandNum++
if (command.type === "dependency") {
for (const { name: depName, type } of command.dependencies) {
let depDepth = stack.size
let dependencyStack = new Set(stack)
dependencyStack.add(depName)
// Treat circular dependency special
if (type === "circular") {
// We've already been down this rabbit hole, skip
if (circularDeps[depName]) {
continue
}
circularDeps[depName] = true
dependencyStack = new Set([depName])
}
if (dependencyStack.size === depDepth + 1) {
// No circular reference for this dep, check further dependencies
foundCircular = checkCircular(donTree, target, donTree[depName], dependencyStack, circularDeps)
} else {
// This dep is circular
if (circularDeps.hasOwnProperty(depName)) {
// This dep is marked circular
} else {
// It's not marked circular
return {
target,
via: Array.from(dependencyStack).slice(-1)[0],
dependency: depName,
commandNumber: commandNum,
}
}
}
}
if (foundCircular) {
return foundCircular
}
}
}
return null
}
function donParse(source, absFile, error, exit = () => {}) {
try {
const { targets: parsed } = parse(source)
// Check for undefined dependencies
let undefinedDependencies = checkDependencyReferences(parsed)
if (undefinedDependencies.length) {
error(absFile + "\nError:\n")
for (const undep of undefinedDependencies) {
error(` Target "${ undep.target }" references undefined dependency "${ undep.dependency }"`)
}
exit(1)
}
let circularDeps = []
for (const [target, commands] of Object.entries(parsed)) {
const targetCircularDeps = checkCircular(parsed, target, commands, new Set([target]))
if (targetCircularDeps) {
circularDeps.push(targetCircularDeps)
}
}
if (circularDeps.length) {
error(absFile + "\nError:")
for (const cirdep of circularDeps) {
error(` Target "${ cirdep.target }" has circular dependency "${ cirdep.dependency }" via its dependency "${ cirdep.via }" on command ${ cirdep.commandNumber }`)
}
exit()
}
return parsed
} catch(e) {
if (!e.location) {
error(e.toString())
return exit()
}
const loc = e.location.start
error(`${ absFile }:${ loc.line }:${ loc.column }\n${ e.name }: ${ e.message }`)
exit()
}
}
module.exports = {
parse(source, filePath = "internal") {
let error = ""
const result = donParse(source, filePath, err => {
error += err
})
if (error) {
throw new Error(error)
}
return result
}
}
if (require.main === module) {
const file = process.argv[2]
const absFile = path.resolve(__dirname, file)
const source = readFileSync(file, "utf8")
let error = ""
const result = donParse(source, absFile, err => {
error += err + "\n"
}, () => {
process.stderr.write(error)
process.exit(1)
})
console.log(JSON.stringify(result, null, 4))
}