-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathupdateArches.js
More file actions
146 lines (118 loc) · 4.72 KB
/
updateArches.js
File metadata and controls
146 lines (118 loc) · 4.72 KB
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
const { execFileSync } = require('child_process');
const { readFileSync, readdirSync, writeFileSync } = require('fs');
const path = require('path');
const nodeDirRegex = /^\d+$/;
// Given a name and a tag, this returns an array of architectures that it supports
const fetchImageArches = (repoTag) => execFileSync('bashbrew', [
'cat', repoTag,
], { encoding: 'utf8' }).split('\n')
.find((line) => line.startsWith('Architectures:'))
.split(':')[1]
.trim()
.split(/\s*,\s*/);
// Parses an "architectures" file into an object like:
// {
// arch1: ['variant1', 'variant2'],
// //...
// }
const parseArchitecturesFile = (file) => Object.fromEntries(
[...readFileSync(file, 'utf8').matchAll(/^(?<arch>\S+)\s+(?<variants>\S+)$/mg)]
.slice(1)
.map(({ groups: { arch, variants } }) => [arch, variants.split(',')]),
);
// Takes in an object like:
// {
// arch1: ['variant1', 'variant2'],
// // ...
// }
// and returns an object like
// {
// variant1: ['arch1', 'arch2'],
// // ...
// }
const invertObject = (obj) => Object.entries(obj)
.reduce((acc, [key, vals]) => vals.reduce((valAcc, val) => {
const { [val]: keys, ...rest } = valAcc;
return {
...rest,
[val]: keys
? [...keys, key]
: [key],
};
}, acc), {});
// Returns a list of the child directories in the given path
const getChildDirectories = (parent) => readdirSync(parent, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map(({ name }) => path.resolve(parent, name));
const getNodeVerionDirs = (base) => getChildDirectories(base)
.filter((childPath) => nodeDirRegex.test(path.basename(childPath)));
// Assume no duplicates
const areArraysEquilivant = (arches1, arches2) => arches1.length === arches2.length
&& arches1.every((arch) => arches2.includes(arch));
// Returns the paths of Dockerfiles that are at: base/*/Dockerfile
const getDockerfilesInChildDirs = (base) => getChildDirectories(base)
.map((childDir) => path.resolve(childDir, 'Dockerfile'));
// Given a path to a Dockerfile like .../14/variant/Dockerfile, this will return "variant"
const getVariantFromPath = (file) => path.dirname(file).split(path.sep).slice(-1);
const getBaseImageFromDockerfile = (file) => readFileSync(file, 'utf8')
.match(/^FROM (\S+)/m)[1];
// Given a dockerfile, this function returns an array like [variant, [arch1, arch2, ...]]
const getVariantAndArches = (dockerfile) => {
const variant = getVariantFromPath(dockerfile);
const baseImage = getBaseImageFromDockerfile(dockerfile);
const arches = fetchImageArches(baseImage);
// TODO: filter by arches node supports
return [variant, arches];
};
const getStoredVariantArches = (file) => {
const storedArchVariants = parseArchitecturesFile(file);
return invertObject(storedArchVariants);
};
const areVariantArchesEquilivant = (current, stored) => Object.keys(current).length
=== Object.keys(stored).length
&& Object.entries(current).every(
([variant, arches]) => stored[variant] && areArraysEquilivant(arches, stored[variant]),
);
const formatEntry = ([arch, variants], variantOffset) => `${arch}${' '.repeat(variantOffset - arch.length)}${variants.join(',')}`;
const sortObjectKeys = (obj) => Object.keys(obj)
.sort()
.reduce((acc, key) => ({
...acc,
[key]: obj[key]
}), {});
const storeArchitectures = (variantArches, architecturesFile) => {
const archVariants = sortObjectKeys(invertObject(variantArches));
const data = {
'bashbrew-arch': ['variants'],
...archVariants,
};
const maxKeyLength = Math.max(...Object.keys(data).map((key) => key.length));
// Variants start 2 spaces after the longest key
const variantOffset = maxKeyLength + 2;
const str = Object.entries(data)
.map((entry) => formatEntry(entry, variantOffset))
.join('\n') + '\n';
writeFileSync(architecturesFile, str);
// Just here for debugging purposes
console.log(str);
console.log('\n\n');
};
const updateNodeDirArches = (nodeDir) => {
const dockerfiles = getDockerfilesInChildDirs(nodeDir);
const currentVariantArches = Object.fromEntries(dockerfiles.map(getVariantAndArches));
const architecturesFile = path.resolve(nodeDir, 'architectures');
const storedVariantArches = getStoredVariantArches(architecturesFile);
if (areVariantArchesEquilivant(currentVariantArches, storedVariantArches)) {
console.log('Architectures up-to-date: ', nodeDir);
return false;
}
console.log('Architectures outdated: ', nodeDir);
storeArchitectures(currentVariantArches, architecturesFile);
return true;
};
const updateArchitectures = () => {
const nodeDirs = getNodeVerionDirs(__dirname);
const dirsUpdated = nodeDirs.map(updateNodeDirArches);
return dirsUpdated.some((updated) => updated);
};
module.exports = updateArchitectures;