-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathverify.js
165 lines (139 loc) · 3.87 KB
/
verify.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
// Copyright 2018 Sourcerer Inc.
// Author: Alexander Surkov ([email protected])
(function() {
'use strict';
/**
* Validates librariy files, located in 'libs' directory, and generates
* technologies.json.
*/
const fs = require('fs');
const path = require('path');
const dir = 'libs';
const libInfo = new Map([
[ 'cpp.json', 'C++' ],
[ 'crystal.json', 'Crystal' ],
[ 'csharp.json', 'C#' ],
[ 'dart.json', 'Dart' ],
[ 'elixir.json', 'Elixir' ],
[ 'go.json', 'Go' ],
[ 'java.json', 'Java' ],
[ 'javascript.json', 'JavaScript'],
[ 'kotlin.json', 'Kotlin'],
[ 'objectivec.json', 'Objective-C' ],
[ 'perl.json', 'Perl' ],
[ 'php.json', 'PHP' ],
[ 'plpgsql.json', 'PL/pgSQL' ],
[ 'python.json', 'Python' ],
[ 'ruby.json', 'Ruby' ],
[ 'rust.json', 'Rust' ],
[ 'scala.json', 'Scala' ],
[ 'swift.json', 'Swift' ],
]);
let libStats = new Map();
let techs = new Map();
let files = fs.readdirSync(dir);
for (let filename of files) {
let ids = new Set();
let repos = new Map();
let libFile = path.join(dir, filename);
let content = fs.readFileSync(libFile, 'utf-8');
let libs = null;
try {
libs = JSON.parse(content);
}
catch(e) {
fail(`${filename} not valid JSON; ${e.toString()}`);
return;
}
if (libInfo.has(filename)) {
libStats.set(libInfo.get(filename), libs.length);
}
for (let lib of libs) {
if (!Array.isArray(lib.tech) || lib.tech.length == 0) {
fail(`${filename}: no tech for ${lib.id}`);
}
if (!filename.startsWith('devops') && (!Array.isArray(lib.tags) || lib.tags.length == 0)) {
fail(`${filename}: no tags for ${lib.id}`);
}
let id = lib.id.toLowerCase();
if (ids.has(id)) {
fail(`${filename} file: '${id}' library id duplication`);
}
ids.add(id);
let repo = lib.repo.toLowerCase();
if (repo) {
if (repos.has(repo)) {
let repo_ids = repos.get(repo);
repo_ids.push(lib.id);
fail(
`${filename} file: '${repo}' repo duplication for ${repo_ids.map(i => `'${i}'`).join(', ')} libraries`
);
}
repos.set(repo, [ lib.id ]);
}
else if (!filename.startsWith('devops') && (!lib.examples || lib.examples.length == 0)) {
fail(`${filename} file: '${lib.id}' lib has neither repo or examples provided`);
}
if (/\s/.test(repo)) {
fail(`${filename} file: '${repo}' repo path has unexpected characters`);
}
if (repo.includes('github.com')) {
fail(`${filename} file: '${repo}' repo invalid path format, github.com hosted repos need relative paths`);
}
let tech = lib.tech[0];
if (!techs.has(tech)) {
techs.set(tech, new Set());
}
let tags = techs.get(tech);
for (let tag of lib.tags) {
if (!tags.has(tag)) {
tags.add(tag);
}
}
}
libs.sort( // Sort libs by tech, id.
(a, b) => {
if (a.tech[0] < b.tech[0]) {
return -1;
}
if (a.tech[0] > b.tech[0]) {
return 1;
}
if (a.id < b.id) {
return -1;
}
if (a.id > b.id) {
return 1;
}
return 0;
}
);
content = `[${libs.map(v => ('\n ' + JSON.stringify(v)))}
]
`;
fs.writeFileSync(libFile, content, 'utf-8');
}
let techs_json = {};
let tech_names = Array.from(techs.keys()).sort();
for (let tech of tech_names) {
let tags = techs.get(tech);
techs_json[tech] = { tags: Array.from(tags).sort() };
}
let output = `${JSON.stringify(techs_json, null, ' ')}
`;
fs.writeFileSync(`technologies.json`, output);
fs.writeFileSync(
`tools/coverage/libstats.js`,
`(function() {
'use strict';
// Do not change: the file is autogenerated by verify.js.
window.libStats = ${JSON.stringify(Array.from(libStats.entries()), null, ' ')};
}());
`
);
console.log(`\x1b[42mSUCCESS!\x1b[0m technologies.json was updated; libs were sorted`);
function fail(msg)
{
console.error(`\x1b[41m\x1b[37m!Failed:\x1b[0m ${msg}`);
}
}());