Repository navigation
Expand file tree
/
Copy pathIncrementalBuildInfo.ts
More file actions
212 lines (185 loc) · 7.32 KB
/
Copy pathIncrementalBuildInfo.ts
File metadata and controls
212 lines (185 loc) · 7.32 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { FileSystem, Path } from '@rushstack/node-core-library';
/**
* Information about an incremental build. This information is used to determine which files need to be rebuilt.
* @beta
*/
export interface IIncrementalBuildInfo {
/**
* A string that represents the configuration inputs for the build.
* If the configuration changes, the old build info object should be discarded.
*/
configHash: string;
/**
* A map of absolute input file paths to their version strings.
* The version string should change if the file changes.
*/
inputFileVersions: Map<string, string>;
/**
* A map of absolute output file paths to the input files they were computed from.
*/
fileDependencies?: Map<string, string[]>;
}
/**
* Serialized version of {@link IIncrementalBuildInfo}.
* @beta
*/
export interface ISerializedIncrementalBuildInfo {
/**
* A string that represents the configuration inputs for the build.
* If the configuration changes, the old build info object should be discarded.
*/
configHash: string;
/**
* A map of input files to their version strings.
* File paths are specified relative to the folder containing the build info file.
*/
inputFileVersions: Record<string, string>;
/**
* Map of output file names to the corresponding index in `Object.entries(inputFileVersions)`.
* File paths are specified relative to the folder containing the build info file.
*/
fileDependencies?: Record<string, number[]>;
}
/**
* Converts an absolute path to a path relative to a base path.
*/
export const makePathRelative: (absolutePath: string, basePath: string) => string =
process.platform === 'win32'
? (absolutePath: string, basePath: string) => {
// On Windows, need to normalize slashes
return Path.convertToSlashes(path.win32.relative(basePath, absolutePath));
}
: (absolutePath: string, basePath: string) => {
// On POSIX, can preserve existing slashes
return path.posix.relative(basePath, absolutePath);
};
/**
* Serializes a build info object to a portable format that can be written to disk.
* @param state - The build info to serialize
* @param makePathPortable - A function that converts an absolute path to a portable path. This is a separate argument to support cross-platform tests.
* @returns The serialized build info
* @beta
*/
export function serializeBuildInfo(
state: IIncrementalBuildInfo,
makePathPortable: (absolutePath: string) => string
): ISerializedIncrementalBuildInfo {
const fileIndices: Map<string, number> = new Map();
const inputFileVersions: Record<string, string> = {};
for (const [absolutePath, version] of state.inputFileVersions) {
const relativePath: string = makePathPortable(absolutePath);
fileIndices.set(absolutePath, fileIndices.size);
inputFileVersions[relativePath] = version;
}
const { fileDependencies: newFileDependencies } = state;
let fileDependencies: Record<string, number[]> | undefined;
if (newFileDependencies) {
fileDependencies = {};
for (const [absolutePath, dependencies] of newFileDependencies) {
const relativePath: string = makePathPortable(absolutePath);
const indices: number[] = [];
for (const dependency of dependencies) {
const index: number | undefined = fileIndices.get(dependency);
if (index === undefined) {
throw new Error(`Dependency not found: ${dependency}`);
}
indices.push(index);
}
fileDependencies[relativePath] = indices;
}
}
const serializedBuildInfo: ISerializedIncrementalBuildInfo = {
configHash: state.configHash,
inputFileVersions,
fileDependencies
};
return serializedBuildInfo;
}
/**
* Deserializes a build info object from its portable format.
* @param serializedBuildInfo - The build info to deserialize
* @param makePathAbsolute - A function that converts a portable path to an absolute path. This is a separate argument to support cross-platform tests.
* @returns The deserialized build info
*/
export function deserializeBuildInfo(
serializedBuildInfo: ISerializedIncrementalBuildInfo,
makePathAbsolute: (relativePath: string) => string
): IIncrementalBuildInfo {
const inputFileVersions: Map<string, string> = new Map();
const absolutePathByIndex: string[] = [];
for (const [relativePath, version] of Object.entries(serializedBuildInfo.inputFileVersions)) {
const absolutePath: string = makePathAbsolute(relativePath);
absolutePathByIndex.push(absolutePath);
inputFileVersions.set(absolutePath, version);
}
let fileDependencies: Map<string, string[]> | undefined;
const { fileDependencies: serializedFileDependencies } = serializedBuildInfo;
if (serializedFileDependencies) {
fileDependencies = new Map();
for (const [relativeOutputFile, indices] of Object.entries(serializedFileDependencies)) {
const absoluteOutputFile: string = makePathAbsolute(relativeOutputFile);
const dependencies: string[] = [];
for (const index of Array.isArray(indices) ? indices : [indices]) {
const dependencyAbsolutePath: string | undefined = absolutePathByIndex[index];
if (dependencyAbsolutePath === undefined) {
throw new Error(`Dependency index not found: ${index}`);
}
dependencies.push(dependencyAbsolutePath);
}
fileDependencies.set(absoluteOutputFile, dependencies);
}
}
const buildInfo: IIncrementalBuildInfo = {
configHash: serializedBuildInfo.configHash,
inputFileVersions,
fileDependencies
};
return buildInfo;
}
/**
* Writes a build info object to disk.
* @param state - The build info to write
* @param filePath - The file path to write the build info to
* @beta
*/
export async function writeBuildInfoAsync(state: IIncrementalBuildInfo, filePath: string): Promise<void> {
const basePath: string = path.dirname(filePath);
const serializedBuildInfo: ISerializedIncrementalBuildInfo = serializeBuildInfo(
state,
(absolutePath: string) => {
return makePathRelative(absolutePath, basePath);
}
);
// This file is meant only for machine reading, so don't pretty-print it.
const stringified: string = JSON.stringify(serializedBuildInfo);
await FileSystem.writeFileAsync(filePath, stringified, { ensureFolderExists: true });
}
/**
* Reads a build info object from disk.
* @param filePath - The file path to read the build info from
* @returns The build info object, or undefined if the file does not exist or cannot be parsed
* @beta
*/
export async function tryReadBuildInfoAsync(filePath: string): Promise<IIncrementalBuildInfo | undefined> {
let serializedBuildInfo: ISerializedIncrementalBuildInfo | undefined;
try {
const fileContents: string = await FileSystem.readFileAsync(filePath);
serializedBuildInfo = JSON.parse(fileContents) as ISerializedIncrementalBuildInfo;
} catch (error) {
if (FileSystem.isNotExistError(error)) {
return;
}
throw error;
}
const basePath: string = path.dirname(filePath);
const buildInfo: IIncrementalBuildInfo = deserializeBuildInfo(
serializedBuildInfo,
(relativePath: string) => {
return path.resolve(basePath, relativePath);
}
);
return buildInfo;
}