Skip to content

Commit

Permalink
Merge pull request #2880 from patrick-rodgers/version-4
Browse files Browse the repository at this point in the history
updating buildsystem to single timeline
  • Loading branch information
patrick-rodgers authored Dec 29, 2023
2 parents 58662a9 + fb0985a commit 5363318
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 111 deletions.
54 changes: 26 additions & 28 deletions tools/buildsystem/bin/buildsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,8 @@ BuildSystem.prepare({}, function (env) {
// setup other context values from config
context.distRoot = config[0].distFolder || "./dist/packages";

const baseTimeline = new BuildTimeline();

// now we apply all our configs
if (activeConfig.behaviors) {
baseTimeline.using(...activeConfig.behaviors);
}

// read in any moment defined observers
for (let key in BuildMoments) {
if (activeConfig[key]) {
baseTimeline.on[key](...activeConfig[key]);
}
}

// now we make an array of timelines 1/target
const timelines = config[0].targets.map(tsconfigPath => {
context.targets = config[0].targets.map(tsconfigPath => {

const tsconfigRoot = resolve(dirname(tsconfigPath));
const parsedTSConfig: TSConfig = importJSON(tsconfigPath);
Expand All @@ -93,22 +79,34 @@ BuildSystem.prepare({}, function (env) {
resolvedPkgSrcRoot: tsconfigRoot,
resolvedPkgOutRoot: resolvedOutDir,
resolvedPkgDistRoot: context.distRoot,
relativePkgDistModulePath: context.distRoot,
relativePkgDistModulePath: context.distRoot,
});
}

return Object.assign({}, context, {
target: {
tsconfigPath,
tsconfigRoot,
parsedTSConfig,
resolvedOutDir,
packages,
}
});
}).map(context => new BuildTimeline(baseTimeline, context));

return {
tsconfigPath,
tsconfigRoot,
parsedTSConfig,
resolvedOutDir,
packages,
};
})

const timeline = new BuildTimeline(context);

// now we apply all our configs
if (activeConfig.behaviors) {
timeline.using(...activeConfig.behaviors);
}

// read in any moment defined observers
for (let key in BuildMoments) {
if (activeConfig[key]) {
timeline.on[key](...activeConfig[key]);
}
}

// we start one timeline per target
await Promise.all(timelines.map(tl => tl.start()));
await timeline.start();
});
});
20 changes: 10 additions & 10 deletions tools/buildsystem/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tools/buildsystem/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pnp/buildsystem",
"version": "4.0.0-beta8",
"version": "4.0.0-beta9",
"bin": {
"pnpbuild": "bin/buildsystem.js"
},
Expand All @@ -9,7 +9,7 @@
"type": "module",
"typings": "./index",
"dependencies": {
"@pnp/core": "^4.0.0-alpha0-v4nightly.20231227",
"@pnp/core": "^4.0.0-alpha0-v4nightly.20231229",
"globby": "^14.0.0",
"liftoff": "^4.0.0",
"webpack": "^5.89.0",
Expand Down
27 changes: 15 additions & 12 deletions tools/buildsystem/src/behaviors/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ export function Build(flags?: string[]): TimelinePipe {

instance.on.build(async function (this: BuildTimeline) {

const { tsconfigPath } = this.context.target;
const { targets } = this.context;

this.log(`Starting Build for target "${tsconfigPath}"`, 1);
await Promise.all(targets.map((target) => {

return new Promise<void>((res, reject) => {
this.log(`Starting Build for target "${target.tsconfigPath}"`, 1);

exec(`${tscPath} -b ${tsconfigPath} ${stringFlags}`, (error, stdout, _stderr) => {
return new Promise<void>((res, reject) => {

if (error === null) {
this.log(`Completing Build for target "${tsconfigPath}"`, 1);
res();
} else {
this.log(`Error in Build for target "${tsconfigPath}"`, 3);
reject(stdout);
}
exec(`${tscPath} -b ${target.tsconfigPath} ${stringFlags}`, (error, stdout, _stderr) => {

if (error === null) {
this.log(`Completing Build for target "${target.tsconfigPath}"`, 1);
res();
} else {
this.log(`Error in Build for target "${target.tsconfigPath}"`, 3);
reject(stdout);
}
});
});
});
}));
});

return instance;
Expand Down
2 changes: 1 addition & 1 deletion tools/buildsystem/src/behaviors/copy-asset-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function CopyAssetFiles(path: string, pattern: string[]): TimelinePipe {

this.log(`CopyAssetFiles found ${temp.length} files for pattern ${stringPattern} in path '${resolvedPath}'`);

const files = await this.context.target.packages.reduce((p, pkg) => {
const files = await this.context.targets[0].packages.reduce((p, pkg) => {

return p.then(async (a) => {

Expand Down
56 changes: 30 additions & 26 deletions tools/buildsystem/src/behaviors/copy-package-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,37 @@ export function CopyPackageFiles(source: "src" | "built", pattern: string[]): Ti

instance.on.package(async function (this: BuildTimeline) {

this.log(`Starting CopyPackageFiles with pattern ${stringPattern} on target '${this.context.target.tsconfigPath}'`);

const files = await this.context.target.packages.reduce((p, pkg) => {

const fileSourceRoot = resolve(source === "src" ? pkg.resolvedPkgSrcRoot : pkg.resolvedPkgOutRoot);

return p.then(async (a) => {

const temp = await (<any>globby)(pattern, {
cwd: fileSourceRoot,
this.context.targets.forEach(async (target) => {

this.log(`Starting CopyPackageFiles with pattern ${stringPattern} on target '${target.tsconfigPath}'`);

const files = await target.packages.reduce((p, pkg) => {

const fileSourceRoot = resolve(source === "src" ? pkg.resolvedPkgSrcRoot : pkg.resolvedPkgOutRoot);

return p.then(async (a) => {

const temp = await (<any>globby)(pattern, {
cwd: fileSourceRoot,
});

a.push(...temp.map(t => ({
src: resolve(fileSourceRoot, t),
dest: resolve(pkg.resolvedPkgDistRoot, source === "built" ? pkg.relativePkgDistModulePath : "", t),
})));

return a;
});

a.push(...temp.map(t => ({
src: resolve(fileSourceRoot, t),
dest: resolve(pkg.resolvedPkgDistRoot, source === "built" ? pkg.relativePkgDistModulePath : "", t),
})));

return a;
});

}, Promise.resolve<{ src: string, dest: string }[]>([]));

this.log(`CopyPackageFiles found ${files.length} files for pattern ${stringPattern} in target '${this.context.target.tsconfigPath}'`);

await Promise.all(files.map(f => buildCopyFile(f.src, f.dest)));

this.log(`Completing CopyPackageFiles with pattern ${stringPattern} on target '${this.context.target.tsconfigPath}'`);

}, Promise.resolve<{ src: string, dest: string }[]>([]));

this.log(`CopyPackageFiles found ${files.length} files for pattern ${stringPattern} in target '${target.tsconfigPath}'`);

await Promise.all(files.map(f => buildCopyFile(f.src, f.dest)));

this.log(`Completing CopyPackageFiles with pattern ${stringPattern} on target '${target.tsconfigPath}'`);

});
});

return instance;
Expand Down
23 changes: 13 additions & 10 deletions tools/buildsystem/src/behaviors/create-resolution-package-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ export function CreateResolutionPackageFiles(): TimelinePipe {

return (instance: BuildTimeline) => {

instance.on.package(async function (this: BuildTimeline) {
instance.on.package(async function (this: BuildTimeline) {

this.log("Creating Resolution package.json files.", 1);

const { target } = this.context;
const { targets } = this.context;

const promises = [];

target.packages.forEach((pkg) => {
targets.forEach((target) => {

const filePath = resolve(pkg.resolvedPkgDistRoot, pkg.relativePkgDistModulePath, "package.json");
target.packages.forEach((pkg) => {

let pkgFile = <any>{
name: pkg.name,
type: /commonjs/i.test(target.parsedTSConfig.compilerOptions.module) ? "commonjs" : "module",
}
const filePath = resolve(pkg.resolvedPkgDistRoot, pkg.relativePkgDistModulePath, "package.json");

this.log(`Writing module resolution package.json for ${filePath} as ${pkgFile.type}`, 0);
let pkgFile = <any>{
name: pkg.name,
type: /commonjs/i.test(target.parsedTSConfig.compilerOptions.module) ? "commonjs" : "module",
}

promises.push(buildWriteFile(filePath, JSON.stringify(pkgFile)));
this.log(`Writing module resolution package.json for ${filePath} as ${pkgFile.type}`, 0);

promises.push(buildWriteFile(filePath, JSON.stringify(pkgFile)));
});
});

await Promise.all(promises);
Expand Down
4 changes: 2 additions & 2 deletions tools/buildsystem/src/behaviors/publish-nightly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export function PublishNightly(flags: string[], nightlyName: "v3nightly" | "v4ni
// this updates all the package.json versions to the nightly pattern
instance.on.prePublish(async function (this: BuildTimeline) {

const { target } = this.context;
const { targets } = this.context;
const date = new Date();

const versionStr = `-${nightlyName}.${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, "0")}${date.getDate().toString().padStart(2, "0")}`;

this.log(`Updating nightly package.json version to ${versionStr}`);

await Promise.all(target.packages.map(pkg => {
await Promise.all(targets[0].packages.map(pkg => {

const packageJsonPath = resolve(pkg.resolvedPkgDistRoot, "package.json");
const packageJson = importJSON(packageJsonPath);
Expand Down
4 changes: 2 additions & 2 deletions tools/buildsystem/src/behaviors/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export function Publish(flags?: string[]): TimelinePipe {

instance.on.publish(async function (this: BuildTimeline) {

const { target } = this.context;
const { targets } = this.context;

const promises: Promise<void>[] = [];

target.packages.forEach(pkg => {
targets[0].packages.forEach(pkg => {

promises.push(new Promise((resolve, reject) => {

Expand Down
19 changes: 11 additions & 8 deletions tools/buildsystem/src/behaviors/replace-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ export function ReplaceVersion(paths: string[], options?: IReplaceVersionOptions

instance.on.postBuild(async function (this: BuildTimeline) {

const { version, target } = this.context;
const { version, targets } = this.context;

this.log(`Replacing package version for target "${target.tsconfigPath}"`, 1);
targets.forEach((target) => {

paths.forEach(async (path) => {
this.log(`Replacing package version for target "${target.tsconfigPath}"`, 1);

const resolvedPath = options?.pathsResolved ? path : resolve(target.resolvedOutDir, path);
this.log(`Resolving path '${path}' to '${resolvedPath}'.`, 0);
const file = await readFile(resolve(resolvedPath));
await buildWriteFile(resolvedPath, file.toString().replace(options.versionMask, version));
});
paths.forEach(async (path) => {

const resolvedPath = options?.pathsResolved ? path : resolve(target.resolvedOutDir, path);
this.log(`Resolving path '${path}' to '${resolvedPath}'.`, 0);
const file = await readFile(resolve(resolvedPath));
await buildWriteFile(resolvedPath, file.toString().replace(options.versionMask, version));
});
});
});

return instance;
Expand Down
4 changes: 2 additions & 2 deletions tools/buildsystem/src/behaviors/write-packagejson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export function WritePackageJSON(transform?: (p: any) => typeof p): TimelinePipe

instance.on.postBuild(async function (this: BuildTimeline) {

const { version, target } = this.context;
const { version, targets } = this.context;

const promises = [];

target.packages.forEach((pkg) => {
targets[0].packages.forEach((pkg) => {

let pkgFile = importJSON(resolve(pkg.resolvedPkgSrcRoot, "package.json"));

Expand Down
7 changes: 1 addition & 6 deletions tools/buildsystem/src/build-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ export class BuildTimeline extends Timeline<typeof BuildMoments> {
protected InternalResolve = Symbol.for("Queryable_Resolve");
protected InternalReject = Symbol.for("Queryable_Reject");

constructor(templateTimeline?: BuildTimeline, protected context?: Partial<IBuildContext>) {
constructor(protected context?: Partial<IBuildContext>) {
super(BuildMoments);

if (typeof templateTimeline !== "undefined") {
this.observers = templateTimeline.observers;
this._inheritingObservers = true;
}
}

public start(): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions tools/buildsystem/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IBuildContext {
version: string;
configName: string;
distRoot: string;
target: {
targets: {
tsconfigPath: string;
tsconfigRoot: string;
parsedTSConfig: TSConfig;
Expand All @@ -46,5 +46,5 @@ export interface IBuildContext {
resolvedPkgDistRoot: string;
relativePkgDistModulePath: string;
}[];
};
}[];
}

0 comments on commit 5363318

Please sign in to comment.