summaryrefslogtreecommitdiff
path: root/tools/release/helpers/deno_workspace.ts
blob: d15209adc33f39cb3b88483b251ef87b13eb3094 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

import * as path from "https://deno.land/std@0.105.0/path/mod.ts";
import * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts";
import * as cargo from "./cargo.ts";
import { getCratesIoMetadata } from "./crates_io.ts";
import { withRetries } from "./helpers.ts";

export class DenoWorkspace {
  #workspaceCrates: readonly DenoWorkspaceCrate[];
  #workspaceRootDirPath: string;

  static get rootDirPath() {
    const currentDirPath = path.dirname(path.fromFileUrl(import.meta.url));
    return path.resolve(currentDirPath, "../../../");
  }

  static async load(): Promise<DenoWorkspace> {
    return new DenoWorkspace(
      await cargo.getMetadata(DenoWorkspace.rootDirPath),
    );
  }

  private constructor(metadata: cargo.CargoMetadata) {
    const crates = [];
    for (const memberId of metadata.workspace_members) {
      const pkg = metadata.packages.find((pkg) => pkg.id === memberId);
      if (!pkg) {
        throw new Error(`Could not find package with id ${memberId}`);
      }
      crates.push(new DenoWorkspaceCrate(this, pkg));
    }

    this.#workspaceCrates = crates;
    this.#workspaceRootDirPath = metadata.workspace_root;
  }

  get crates() {
    return this.#workspaceCrates;
  }

  /** Gets the dependency crates used for the first part of the release process. */
  getDependencyCrates() {
    return [
      this.getBenchUtilCrate(),
      this.getSerdeV8Crate(),
      this.getCoreCrate(),
      ...this.getExtCrates(),
      this.getRuntimeCrate(),
    ];
  }

  getSerdeV8Crate() {
    return this.getCrateByNameOrThrow("serde_v8");
  }

  getCliCrate() {
    return this.getCrateByNameOrThrow("deno");
  }

  getCoreCrate() {
    return this.getCrateByNameOrThrow("deno_core");
  }

  getRuntimeCrate() {
    return this.getCrateByNameOrThrow("deno_runtime");
  }

  getBenchUtilCrate() {
    return this.getCrateByNameOrThrow("deno_bench_util");
  }

  getExtCrates() {
    const extPath = path.join(this.#workspaceRootDirPath, "ext");
    return this.#workspaceCrates.filter((c) =>
      c.manifestPath.startsWith(extPath)
    );
  }

  getCrateByNameOrThrow(name: string) {
    const crate = this.#workspaceCrates.find((c) => c.name === name);
    if (!crate) {
      throw new Error(`Could not find crate: ${name}`);
    }
    return crate;
  }

  build() {
    return cargo.build(DenoWorkspace.rootDirPath);
  }

  updateLockFile() {
    return cargo.check(DenoWorkspace.rootDirPath);
  }
}

export class DenoWorkspaceCrate {
  #workspace: DenoWorkspace;
  #pkg: cargo.CargoPackageMetadata;
  #isUpdatingManifest = false;

  constructor(workspace: DenoWorkspace, pkg: cargo.CargoPackageMetadata) {
    this.#workspace = workspace;
    this.#pkg = pkg;
  }

  get manifestPath() {
    return this.#pkg.manifest_path;
  }

  get directoryPath() {
    return path.dirname(this.#pkg.manifest_path);
  }

  get name() {
    return this.#pkg.name;
  }

  get version() {
    return this.#pkg.version;
  }

  getDependencies() {
    const dependencies = [];
    for (const dependency of this.#pkg.dependencies) {
      const crate = this.#workspace.crates.find((c) =>
        c.name === dependency.name
      );
      if (crate != null) {
        dependencies.push(crate);
      }
    }
    return dependencies;
  }

  async isPublished() {
    const cratesIoMetadata = await getCratesIoMetadata(this.name);
    return cratesIoMetadata.versions.some((v) => v.num === this.version);
  }

  async publish() {
    if (await this.isPublished()) {
      console.log(`Already published ${this.name} ${this.version}`);
      return false;
    }

    console.log(`Publishing ${this.name} ${this.version}...`);

    // Sometimes a publish may fail due to the crates.io index
    // not being updated yet. Usually it will be resolved after
    // retrying, so try a few times before failing hard.
    return await withRetries({
      action: async () => {
        await cargo.publishCrate(this.directoryPath);
        return true;
      },
      retryCount: 5,
      retryDelaySeconds: 10,
    });
  }

  build() {
    return cargo.build(this.directoryPath);
  }

  updateLockFile() {
    return cargo.check(this.directoryPath);
  }

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

  async setVersion(version: string) {
    console.log(`Setting ${this.name} to ${version}...`);
    for (const crate of this.#workspace.crates) {
      await crate.setDependencyVersion(this.name, version);
    }
    await this.#updateManifestVersion(version);
  }

  async setDependencyVersion(dependencyName: string, version: string) {
    const dependency = this.#pkg.dependencies.find((d) =>
      d.name === dependencyName
    );
    if (dependency != null) {
      await this.#updateManifestFile((fileText) => {
        // simple for now...
        const findRegex = new RegExp(
          `^(\\b${dependencyName}\\b\\s.*)"([=\\^])?[0-9]+[^"]+"`,
          "gm",
        );
        return fileText.replace(findRegex, `$1"${version}"`);
      });

      dependency.req = `^${version}`;
    }
  }

  async #updateManifestVersion(version: string) {
    await this.#updateManifestFile((fileText) => {
      const findRegex = new RegExp(
        `^(version\\s*=\\s*)"${this.#pkg.version}"$`,
        "m",
      );
      return fileText.replace(findRegex, `$1"${version}"`);
    });
    this.#pkg.version = version;
  }

  async #updateManifestFile(action: (fileText: string) => string) {
    if (this.#isUpdatingManifest) {
      throw new Error("Cannot update manifest while updating manifest.");
    }
    this.#isUpdatingManifest = true;
    try {
      const originalText = await Deno.readTextFile(this.#pkg.manifest_path);
      const newText = action(originalText);
      if (originalText === newText) {
        throw new Error(`The file didn't change: ${this.manifestPath}`);
      }
      await Deno.writeTextFile(this.manifestPath, newText);
    } finally {
      this.#isUpdatingManifest = false;
    }
  }
}