Skip to content

Commit 0918657

Browse files
Fixed build:all output on Windows (#8089)
* Fixed handling for paths with windows separator during build * Fixed icon index order on windows during build * Updated icon search index Removed obsolete icon search index
1 parent 0e75aef commit 0918657

File tree

4 files changed

+89
-12
lines changed

4 files changed

+89
-12
lines changed

src/overrides/assets/javascripts/iconsearch_index.json

-1
This file was deleted.

tools/build/_/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import * as chokidar from "chokidar"
2424
import * as fs from "fs/promises"
25+
import * as path from "path"
2526
import {
2627
EMPTY,
2728
Observable,
@@ -120,7 +121,7 @@ export function resolve(
120121

121122
/* Build overrides */
122123
!process.argv.includes("--all")
123-
? filter(file => !file.startsWith(".overrides/"))
124+
? filter(file => !file.startsWith(`.overrides${path.sep}`))
124125
: identity,
125126
)
126127
}

tools/build/index.ts

+85-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
concat,
2828
defer,
2929
from,
30+
identity,
3031
map,
3132
merge,
3233
mergeMap,
@@ -112,6 +113,75 @@ function minsvg(data: string): string {
112113
return result.data
113114
}
114115

116+
/**
117+
* Return a path with POSIX style separators
118+
*
119+
* The default function assumes UNIX system, so it just returns the path.
120+
*
121+
* @param p - string path
122+
* @returns String path
123+
*/
124+
let assurePosixSep = function (p: string): string {
125+
return p
126+
};
127+
128+
/**
129+
* Return a path with POSIX style separators
130+
*
131+
* The Windows variant of this function replaces the separator with regex.
132+
*
133+
* @param p - string path
134+
* @returns String path
135+
*/
136+
function assurePosixSepWin(p: string): string {
137+
return p.replace(winSepRegex, path.posix.sep)
138+
};
139+
140+
const winSepRegex = new RegExp(`\\${path.win32.sep}`, "g");
141+
142+
if (path.sep === path.win32.sep) {
143+
assurePosixSep = assurePosixSepWin;
144+
}
145+
146+
/**
147+
* Compare two path strings to decide on the order
148+
*
149+
* On Windows the default order of paths containing `_` from the resolve function
150+
* is different than on macOS. This function restores the order to the usual.
151+
* Implementation adapted based on https://t.ly/VJp78
152+
*
153+
* Note: The paths should have no extension like .svg, just the name. Otherwise
154+
* it won't check the compare.startsWith(reference) properly.
155+
*
156+
* @param reference Left string to compare
157+
* @param compare Right string to compare against
158+
* @returns Number for the sort function to define the order
159+
*/
160+
function sortOrderForWindows(reference: string, compare: string): number {
161+
reference = reference.toLowerCase();
162+
compare = compare.toLowerCase();
163+
164+
const length = Math.min(reference.length, compare.length);
165+
166+
for (let i = 0; i < length; i++) {
167+
const leftChar = reference[i];
168+
const rightChar = compare[i];
169+
170+
if (leftChar !== rightChar)
171+
return customAlphabet.indexOf(leftChar) - customAlphabet.indexOf(rightChar);
172+
}
173+
174+
if (reference.length !== compare.length) {
175+
if (compare.startsWith(reference) && compare[reference.length] === "-")
176+
return 1;
177+
return reference.length - compare.length;
178+
}
179+
180+
return 0;
181+
}
182+
183+
const customAlphabet: string = "_,-./0123456789abcdefghijklmnopqrstuvwxyz";
184+
115185
/* ----------------------------------------------------------------------------
116186
* Tasks
117187
* ------------------------------------------------------------------------- */
@@ -187,7 +257,7 @@ const sources$ = copyAll("**/*.py", {
187257
const stylesheets$ = resolve("**/[!_]*.scss", { cwd: "src" })
188258
.pipe(
189259
mergeMap(file => zip(
190-
of(ext(file, ".css").replace(/(overrides|templates)\//, "")),
260+
of(ext(file, ".css").replace(new RegExp(`(overrides|templates)\\${path.sep}`), "")),
191261
transformStyle({
192262
from: `src/${file}`,
193263
to: ext(`${base}/${file}`, ".css")
@@ -199,7 +269,7 @@ const stylesheets$ = resolve("**/[!_]*.scss", { cwd: "src" })
199269
const javascripts$ = resolve("**/{custom,bundle,search}.ts", { cwd: "src" })
200270
.pipe(
201271
mergeMap(file => zip(
202-
of(ext(file, ".js").replace(/(overrides|templates)\//, "")),
272+
of(ext(file, ".js").replace(new RegExp(`(overrides|templates)\\${path.sep}`), "")),
203273
transformScript({
204274
from: `src/${file}`,
205275
to: ext(`${base}/${file}`, ".js")
@@ -229,10 +299,10 @@ const manifest$ = merge(
229299
.pipe(
230300
scan((prev, mapping) => (
231301
mapping.reduce((next, [key, value]) => (
232-
next.set(key, value.replace(
233-
new RegExp(`${base}\\/(overrides|templates)\\/`),
302+
next.set(assurePosixSep(key), assurePosixSep(value.replace(
303+
new RegExp(`${base}\\/(overrides|templates)\\${path.sep}`),
234304
""
235-
))
305+
)))
236306
), prev)
237307
), new Map<string, string>()),
238308
)
@@ -291,9 +361,15 @@ const icons$ = defer(() => resolve("**/*.svg", {
291361
}))
292362
.pipe(
293363
reduce((index, file) => index.set(
294-
file.replace(/\.svg$/, "").replace(/\//g, "-"),
295-
file
296-
), new Map<string, string>())
364+
file.replace(/\.svg$/, "").replace(new RegExp(`\\${path.sep}`, "g"), "-"),
365+
assurePosixSep(file)
366+
), new Map<string, string>()),
367+
// The icons are stored in the index file, and the output needs to be OS
368+
// agnostic. Some icons contain the `_` character, which has different order
369+
// in the glob output on Windows.
370+
(path.sep === path.win32.sep) ? map(icons => new Map(
371+
[...icons].sort((a, b) => sortOrderForWindows(a[0], b[0]))
372+
)) : identity
297373
)
298374

299375
/* Compute emoji mappings (based on Twemoji) */
@@ -372,7 +448,7 @@ const schema$ = merge(
372448
"reference/icons-emojis/#search"
373449
].join("/"),
374450
"type": "string",
375-
"enum": icons.map(icon => icon.replace(".svg", ""))
451+
"enum": icons.map(icon => assurePosixSep(icon.replace(".svg", "")))
376452
})),
377453
switchMap(data => write(
378454
"docs/schema/assets/icons.json",

tools/build/transform/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ interface TransformOptions {
6060
/**
6161
* Base directory for source map resolution
6262
*/
63-
const root = new RegExp(`file://${path.resolve(".")}/`, "g")
63+
const currentPath = path.resolve(".").replace(new RegExp(`\\${path.win32.sep}`, "g"), path.posix.sep)
64+
const root = path.sep === path.posix.sep ? new RegExp(`file://${currentPath}/`, "g") : new RegExp(`file:///${currentPath}/`, "g")
6465

6566
/* ----------------------------------------------------------------------------
6667
* Helper functions

0 commit comments

Comments
 (0)