Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build-infrastructure): Add setVersion API #22919

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import type { Opaque } from 'type-fest';
import type { PackageJson as PackageJson_2 } from 'type-fest';
import * as semver from 'semver';
import type { SetRequired } from 'type-fest';
import { SimpleGit } from 'simple-git';

Expand Down Expand Up @@ -234,6 +235,9 @@ export interface Reloadable {
reload(): void;
}

// @public
export function setVersion<J extends PackageJson>(packages: IPackage[], version: semver.SemVer): Promise<void>;

// @public
export interface WorkspaceDefinition {
directory: string;
Expand Down
28 changes: 1 addition & 27 deletions build-tools/packages/build-infrastructure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,4 @@ export type {
IPackageManager,
} from "./types.js";
export { isIPackage, isIReleaseGroup } from "./types.js";

// export {
// filterPackages,
// type FilterablePackage,
// selectAndFilterPackages,
// type GlobString,
// AllPackagesSelectionCriteria,
// EmptySelectionCriteria,
// type PackageSelectionCriteria,
// type PackageFilterOptions,
// } from "./filter.js";
// export {
// FluidRepo as FluidRepoBase,
// getAllDependenciesInRepo,
// loadFluidRepo,
// } from "./fluidRepo.js";
// export {
// getFiles,
// findGitRootSync,
// getMergeBaseRemote,
// getRemote,
// getChangedSinceRef,
// } from "./git.js";
// export { PackageBase } from "./package.js";
// export { updatePackageJsonFile, updatePackageJsonFileAsync } from "./packageJsonUtils.js";
// export { createPackageManager } from "./packageManagers.js";
// export { setVersion } from "./versions.js";
export { setVersion } from "./versions.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import { strict as assert } from "node:assert";
import path from "node:path";

import { expect } from "chai";
import { afterEach, describe, it } from "mocha";
import * as semver from "semver";
import { simpleGit } from "simple-git";

import { loadFluidRepo } from "../fluidRepo.js";
import type { ReleaseGroupName, WorkspaceName } from "../types.js";
import { setVersion } from "../versions.js";

import { testDataPath, testRepoRoot } from "./init.js";

const repo = loadFluidRepo(path.join(testDataPath, "./testRepo"));
const main = repo.releaseGroups.get("main" as ReleaseGroupName);
assert(main !== undefined);

const group2 = repo.releaseGroups.get("group2" as ReleaseGroupName);
assert(group2 !== undefined);

const group3 = repo.releaseGroups.get("group3" as ReleaseGroupName);
assert(group3 !== undefined);

const secondWorkspace = repo.workspaces.get("second" as WorkspaceName);
assert(secondWorkspace !== undefined);

/**
* A git client rooted in the test repo. Used for resetting tests.
*/
const git = simpleGit(testRepoRoot);

describe("setVersion", () => {
afterEach(async () => {
await git.checkout(["HEAD", "--", testRepoRoot]);
repo.reload();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to re-assign the release groups and workspaces after reloading the repo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I don't think so. The reload operation just reloads the packages' data from disk. It doesn't actually load the IFluid repo from config again. That is, this is not the same as creating a new IFluidRepo. So the references should all stay the same after reload.

});

it("release group", async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await setVersion(main.packages, semver.parse("1.2.1")!);
repo.reload();

const allCorrect = main.packages.every((pkg) => pkg.version === "1.2.1");
expect(main.version).to.equal("1.2.1");
expect(allCorrect).to.be.true;
});

it("workspace", async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await setVersion(secondWorkspace.packages, semver.parse("2.2.1")!);
repo.reload();

const allCorrect = secondWorkspace.packages.every((pkg) => pkg.version === "2.2.1");
expect(allCorrect).to.be.true;
});

it("repo", async () => {
const packages = [...repo.packages.values()];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await setVersion(packages, semver.parse("1.2.1")!);
repo.reload();

const allCorrect = packages.every((pkg) => pkg.version === "1.2.1");
expect(allCorrect).to.be.true;
});
});
30 changes: 30 additions & 0 deletions build-tools/packages/build-infrastructure/src/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import * as semver from "semver";

import { updatePackageJsonFile } from "./packageJsonUtils.js";
import type { IPackage, PackageJson } from "./types.js";

/**
* Sets the version of a group of packages.
*
* Note that any loaded objects such as an IFluidRepo instance may need to be reloaded after calling this function.
*
* @param fluidRepo - The {@link IFluidRepo}.
tylerbutler marked this conversation as resolved.
Show resolved Hide resolved
* @param packages - An array of objects whose version should be updated.
* @param version - The version to set.
*/
export async function setVersion<J extends PackageJson>(
packages: IPackage[],
version: semver.SemVer,
): Promise<void> {
const translatedVersion = version;
for (const pkg of packages) {
updatePackageJsonFile<J>(pkg.directory, (json) => {
tylerbutler marked this conversation as resolved.
Show resolved Hide resolved
json.version = translatedVersion.version;
});
}
}
Loading