Skip to content

Commit

Permalink
feat: update deps and make updating cargo.toml sync (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 22, 2023
1 parent d5ed024 commit c427daf
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 55 deletions.
68 changes: 28 additions & 40 deletions crate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export interface CrateDep {

export class Crate {
#pkg: CargoPackageMetadata;
#isUpdatingManifest = false;
#isUpdatingRootManifest = false;

constructor(
public readonly repo: Repo,
Expand Down Expand Up @@ -79,48 +77,54 @@ export class Crate {
}

increment(part: "major" | "minor" | "patch") {
const newVersion = semver.parse(this.version)!.increment(part).toString();
return this.setVersion(newVersion);
const newVersion = semver.format(
semver.increment(semver.parse(this.version), part),
);
this.setVersion(newVersion);
}

async setVersion(version: string) {
setVersion(version: string) {
$.logStep(`Setting ${this.name} to ${version}...`);

// sets the version of any usages of this crate in the root Cargo.toml
if (!this.repo.folderPath.equals(this.folderPath)) {
const rootpath = this.repo.folderPath.join("Cargo.toml");
const originalText = await rootpath.readText();
const originalText = rootpath.readTextSync();
const findRegex = new RegExp(
`^(\\b${this.name}\\b\\s.*)"([=\\^])?[0-9]+[^"]+"`,
"gm",
);

const newText = originalText.replace(findRegex, `$1"${version}"`);
if (originalText !== newText) {
await rootpath.writeText(newText);
rootpath.writeTextSync(newText);
} else {
// in this case, the repo does not keep the version
// inside the root cargo.toml file
for (const crate of this.repo.crates) {
await crate.setDependencyVersion(this.name, version);
crate.setDependencyVersion(this.name, version);
}
}
}

await this.#updateManifestVersion(version);
this.#updateManifestVersion(version);
}

static async getLatestVersion(crateName: string) {
return (await getCratesIoMetadata(crateName))?.crate.max_stable_version;
}

/** Gets the latest version from crates.io or returns undefined if not exists. */
async getLatestVersion() {
return (await getCratesIoMetadata(this.name))?.crate.max_stable_version;
getLatestVersion() {
return Crate.getLatestVersion(this.name);
}

async setDependencyVersion(dependencyName: string, version: string) {
setDependencyVersion(dependencyName: string, version: string) {
const dependency = this.#pkg.dependencies.find((d) =>
d.name === dependencyName
);
if (dependency != null && dependency.req !== "*") {
await this.#updateManifestFile((_filePath, fileText) => {
this.#updateManifestFile((_filePath, fileText) => {
// simple for now...
const findRegex = new RegExp(
`^(\\b${dependencyName}\\b\\s.*)"([=\\^])?[0-9]+[^"]+"`,
Expand All @@ -133,8 +137,8 @@ export class Crate {
}
}

async #updateManifestVersion(version: string) {
await this.#updateManifestFile((_filePath, fileText) => {
#updateManifestVersion(version: string) {
this.#updateManifestFile((_filePath, fileText) => {
const findRegex = new RegExp(
`^(version\\s*=\\s*)"${this.#pkg.version}"$`,
"m",
Expand All @@ -145,7 +149,7 @@ export class Crate {
}

toLocalSource(crate: Crate) {
return this.#updateRootManifestFile((filePath, fileText) => {
this.#updateRootManifestFile((filePath, fileText) => {
const relativePath = filePath.parentOrThrow().relative(crate.folderPath)
.replace(/\\/g, "/");
const newText =
Expand All @@ -155,7 +159,7 @@ export class Crate {
}

revertLocalSource(crate: Crate) {
return this.#updateRootManifestFile((filePath, fileText) => {
this.#updateRootManifestFile((filePath, fileText) => {
const relativePath = filePath.parentOrThrow().relative(crate.folderPath)
.replace(/\\/g, "/");
const newText =
Expand Down Expand Up @@ -271,21 +275,13 @@ export class Crate {
.cwd(this.folderPath);
}

async #updateManifestFile(
#updateManifestFile(
action: (filePath: PathRef, fileText: string) => string,
) {
if (this.#isUpdatingManifest) {
throw new Error("Cannot update manifest while updating manifest.");
}
this.#isUpdatingManifest = true;
try {
await updateFileEnsureChange(this.manifestPath, action);
} finally {
this.#isUpdatingManifest = false;
}
updateFileEnsureChange(this.manifestPath, action);
}

async #updateRootManifestFile(
#updateRootManifestFile(
action: (filePath: PathRef, fileText: string) => string,
) {
const rootManifestFilePath = this.repo.folderPath.join("Cargo.toml");
Expand All @@ -295,26 +291,18 @@ export class Crate {
) {
return this.#updateManifestFile(action);
}
if (this.#isUpdatingRootManifest) {
throw new Error("Cannot update root manifest while updating it.");
}
this.#isUpdatingRootManifest = true;
try {
await updateFileEnsureChange(rootManifestFilePath, action);
} finally {
this.#isUpdatingRootManifest = false;
}
updateFileEnsureChange(rootManifestFilePath, action);
}
}

async function updateFileEnsureChange(
function updateFileEnsureChange(
filePath: PathRef,
action: (filePath: PathRef, fileText: string) => string,
) {
const originalText = await filePath.readText();
const originalText = filePath.readTextSync();
const newText = action(filePath, originalText);
if (originalText === newText) {
throw new Error(`The file didn't change: ${filePath}`);
}
await filePath.writeText(newText);
filePath.writeTextSync(newText);
}
6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

export * as semver from "https://deno.land/std@0.179.0/semver/mod.ts";
export { default as $, PathRef } from "https://deno.land/x/dax@0.30.0/mod.ts";
export * as dax from "https://deno.land/x/dax@0.30.0/mod.ts";
export * as semver from "https://deno.land/std@0.201.0/semver/mod.ts";
export { default as $, PathRef } from "https://deno.land/x/dax@0.35.0/mod.ts";
export * as dax from "https://deno.land/x/dax@0.35.0/mod.ts";
2 changes: 1 addition & 1 deletion github_actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { Octokit } from "https://cdn.skypack.dev/@octokit/core@3?dts";
import { Octokit } from "npm:octokit@^3.1";

export function getGitHubRepository() {
const repoEnvVar = getEnvVarOrThrow("GITHUB_REPOSITORY");
Expand Down
6 changes: 3 additions & 3 deletions helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class GitTags {
/** Gets if the most recent version tag uses a `v` prefix. */
usesVersionVPrefix() {
const versionTags = this.getGitVersionTags();
versionTags.sort((a, b) => a.version.compare(b.version));
versionTags.sort((a, b) => semver.compare(a.version, b.version));
const mostRecentTag = versionTags[versionTags.length - 1];
return mostRecentTag?.name.startsWith("v") ?? false;
}
Expand All @@ -89,8 +89,8 @@ export class GitTags {
let previousVersion;
for (const tagInfo of this.getGitVersionTags()) {
const isGtPrevious = previousVersion == null ||
previousVersion.version.compare(tagInfo.version) < 0;
if (isGtPrevious && tagInfo.version.compare(v) < 0) {
semver.compare(previousVersion.version, tagInfo.version) < 0;
if (isGtPrevious && semver.compare(tagInfo.version, v) < 0) {
previousVersion = tagInfo;
}
}
Expand Down
11 changes: 5 additions & 6 deletions tasks/bump_deno_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
// deno run -A <url-to-this-module>
// ```

import { $, Repo } from "../mod.ts";
import { $, Crate, Repo } from "../mod.ts";
import { createOctoKit, getGitHubRepository } from "../github_actions.ts";
import { CratesIoCache } from "../crates_io.ts";

const cwd = $.path.resolve(".");
const repoName = $.path.basename(cwd);
const cwd = $.path(".").resolve();
const repoName = cwd.basename();
const repo = await Repo.load({
name: repoName,
path: cwd,
Expand All @@ -47,14 +47,13 @@ for (const crate of repo.crates) {
}

if (await cratesIo.hasDenoLandOwner(dep.name)) {
const latestVersion =
(await cratesIo.getMetadata(dep.name))?.crate.max_stable_version;
const latestVersion = await Crate.getLatestVersion(dep.name);
if (latestVersion == null) {
throw new Error(`Could not find crate version for ${dep.name}`);
}

$.logStep(`Updating ${dep.name} from ${dep.req} to ${latestVersion}...`);
await crate.setDependencyVersion(dep.name, latestVersion);
crate.setDependencyVersion(dep.name, latestVersion);
updates.add(`${dep.name} ${latestVersion}`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/publish_release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ import { $, Repo } from "../mod.ts";
import { createOctoKit, getGitHubRepository } from "../github_actions.ts";

const cliArgs = getCliArgs();
const cwd = $.path.resolve(".");
const repoName = $.path.basename(cwd);
const cwd = $.path(".").resolve();
const repoName = cwd.basename();
const repo = await Repo.load({
name: repoName,
path: cwd,
Expand Down

0 comments on commit c427daf

Please sign in to comment.