From 0f60e2b9ec02120d852c133369429e4f86ce508f Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 22 Nov 2023 12:27:09 -0500 Subject: [PATCH] feat: update deps and make updating cargo.toml sync --- crate.ts | 66 ++++++++++++++++------------------------ deps.ts | 6 ++-- tasks/bump_deno_deps.ts | 6 ++-- tasks/publish_release.ts | 4 +-- 4 files changed, 34 insertions(+), 48 deletions(-) diff --git a/crate.ts b/crate.ts index 55e6656..cdd3651 100644 --- a/crate.ts +++ b/crate.ts @@ -12,8 +12,6 @@ export interface CrateDep { export class Crate { #pkg: CargoPackageMetadata; - #isUpdatingManifest = false; - #isUpdatingRootManifest = false; constructor( public readonly repo: Repo, @@ -79,17 +77,17 @@ 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", @@ -97,30 +95,34 @@ export class Crate { 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]+[^"]+"`, @@ -133,8 +135,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", @@ -145,7 +147,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 = @@ -155,7 +157,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 = @@ -271,21 +273,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"); @@ -295,26 +289,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); } diff --git a/deps.ts b/deps.ts index 9e2dc60..5f50950 100644 --- a/deps.ts +++ b/deps.ts @@ -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"; diff --git a/tasks/bump_deno_deps.ts b/tasks/bump_deno_deps.ts index 8210958..af39e95 100644 --- a/tasks/bump_deno_deps.ts +++ b/tasks/bump_deno_deps.ts @@ -28,8 +28,8 @@ import { $, 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, @@ -54,7 +54,7 @@ for (const crate of repo.crates) { } $.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}`); } } diff --git a/tasks/publish_release.ts b/tasks/publish_release.ts index dc252a4..2d3933f 100644 --- a/tasks/publish_release.ts +++ b/tasks/publish_release.ts @@ -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,