This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
postBuildStep.ts
140 lines (119 loc) · 5.07 KB
/
postBuildStep.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as fse from "fs-extra";
import path from "path";
import { applyContentSecurityPolicyPatch } from "./src/host/polyfills/inspectorContentPolicy";
import {
applyDrawerTabLocationPatch,
applyInspectorCommonCssPatch,
applyMainTabTabLocationPatch,
applySelectTabPatch,
applyShowTabElement,
} from "./src/host/polyfills/simpleView";
async function copyFile(srcDir: string, outDir: string, name: string) {
await fse.copy(
path.join(srcDir, name),
path.join(outDir, name),
);
}
async function copyStaticFiles(debugMode: boolean) {
// Copy the static css file to the out directory
const commonSrcDir = "./src/common/";
const commonOutDir = "./out/common/";
await fse.ensureDir(commonOutDir);
await copyFile(commonSrcDir, commonOutDir, "styles.css");
// Must set environment variables EDGE_CHROMIUM_PATH and EDGE_CHROMIUM_OUT_DIR
// E.g. set EDGE_CHROMIUM_PATH=F:/git/Edge/src
// set EDGE_CHROMIUM_OUT_DIR=debug_x64
// See CONTRIBUTING.md for more details
const toolsSrcDir =
`${process.env.EDGE_CHROMIUM_PATH}/third_party/devtools-frontend/src/front_end/`;
if (!isDirectory(toolsSrcDir)) {
throw new Error(`Could not find Microsoft Edge (Chromium) DevTools path at '${toolsSrcDir}'. ` +
"Did you set the EDGE_CHROMIUM_PATH environment variable?");
}
const toolsGenDir =
`${process.env.EDGE_CHROMIUM_PATH}/out/${process.env.EDGE_CHROMIUM_OUT_DIR}/gen/devtools/`;
if (!isDirectory(toolsGenDir)) {
throw new Error(`Could not find Microsoft Edge (Chromium) output path at '${toolsGenDir}'. ` +
"Did you set the EDGE_CHROMIUM_OUT_DIR environment variable?");
}
const toolsResDir =
`${process.env.EDGE_CHROMIUM_PATH}/out/${process.env.EDGE_CHROMIUM_OUT_DIR}/resources/inspector/`;
// Copy the devtools to the out directory
const toolsOutDir = "./out/tools/front_end/";
await fse.ensureDir(toolsOutDir);
await fse.copy(toolsSrcDir, toolsOutDir);
// Copy the devtools generated files to the out directory
await fse.copy(toolsGenDir, toolsOutDir);
// Copy the optional devtools resource files to the out directory
if (isDirectory(toolsResDir)) {
await copyFile(toolsResDir, toolsOutDir, "InspectorBackendCommands.js");
await copyFile(toolsResDir, toolsOutDir, "SupportedCSSProperties.js");
}
// Patch older versions of the webview with our workarounds
await patchFilesForWebView(toolsOutDir, debugMode);
}
async function patchFilesForWebView(toolsOutDir: string, debugMode: boolean) {
if (!debugMode) {
// tslint:disable-next-line:no-console
console.log('Patching files for release version');
await patchFileForWebView("shell.js", toolsOutDir, true, [
applyInspectorCommonCssPatch
]);
await patchFileForWebView("inspector.html", toolsOutDir, true, [applyContentSecurityPolicyPatch]);
await patchFileForWebView("ui/TabbedPane.js", toolsOutDir, false, [applySelectTabPatch, applyShowTabElement]);
await patchFileForWebView("ui/InspectorView.js", toolsOutDir, false, [
applyDrawerTabLocationPatch,
applyMainTabTabLocationPatch,
]);
} else {
await patchFileForWebView("inspector.html", toolsOutDir, true, [applyContentSecurityPolicyPatch]);
await patchFileForWebView("ui/TabbedPane.js", toolsOutDir, false, [applySelectTabPatch, applyShowTabElement]);
await patchFileForWebView("ui/InspectorView.js", toolsOutDir, false, [
applyDrawerTabLocationPatch,
applyMainTabTabLocationPatch,
]);
}
}
async function patchFileForWebView(
filename: string,
dir: string,
isRelease: boolean,
patches: Array<(content: string, isRelease?: boolean) => string | null>) {
const file = path.join(dir, filename);
// Ignore missing files
if (!await fse.pathExists(file)) {
const template = `An expected file was not found: ${file}`;
throw new Error(template);
}
// Read in the file
let content = (await fse.readFile(file)).toString();
// Apply each patch in order
patches.forEach((patchFunction) => {
const patchResult: string | null = patchFunction(content, isRelease);
if (patchResult) {
content = patchResult;
} else {
const template = `An expected function was not patched correctly: ${patchFunction} on file: ${filename}`;
throw new Error(template);
}
});
// Write out the final content
await fse.writeFile(file, content);
}
function isDirectory(fullPath: string) {
try {
return fse.statSync(fullPath).isDirectory();
} catch {
return false;
}
}
function main() {
let debugMode = false;
if (process.argv && process.argv.length === 3 && process.argv[2] === "debug") {
debugMode = true;
}
copyStaticFiles(debugMode);
}
main();