|
| 1 | +import * as TOML from 'smol-toml'; |
| 2 | +import { promises as fs } from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { forceRunAsync } from '../run.js'; |
| 5 | + |
| 6 | +export default function updateCrates() { |
| 7 | + return { |
| 8 | + title: 'Update Rust crates', |
| 9 | + skip: (ctx) => ctx.newVersion.majorMinor < 136, |
| 10 | + task: (ctx, task) => { |
| 11 | + return task.newListr([ |
| 12 | + enumerateTemporalDependencies(), |
| 13 | + removeExistingCrates(), |
| 14 | + buildManifest(), |
| 15 | + vendorCrates(), |
| 16 | + generateLockfile(), |
| 17 | + updateGYP(), |
| 18 | + commitChanges() |
| 19 | + ]); |
| 20 | + } |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +const chromiumCratesDir = 'deps/v8/third_party/rust/chromium_crates_io'; |
| 25 | +const nodeCratesDir = 'deps/crates'; |
| 26 | + |
| 27 | +function enumerateTemporalDependencies() { |
| 28 | + return { |
| 29 | + title: 'Enumerate Temporal dependency tree', |
| 30 | + task: async(ctx) => { |
| 31 | + const tree = await forceRunAsync( |
| 32 | + ctx.cargo, |
| 33 | + ['tree', '-Z', 'bindeps', '--package', 'temporal_capi', '--prefix', 'none'], |
| 34 | + { |
| 35 | + ignoreFailure: false, |
| 36 | + captureStdout: true, |
| 37 | + spawnArgs: { cwd: path.join(ctx.nodeDir, chromiumCratesDir) } |
| 38 | + } |
| 39 | + ); |
| 40 | + ctx.temporalCrates = tree.trimEnd().split('\n').sort().map((crate) => crate.split(' ', 1)[0]); |
| 41 | + }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function removeExistingCrates() { |
| 46 | + return { |
| 47 | + title: 'Remove existing crates', |
| 48 | + task: async(ctx) => { |
| 49 | + await Promise.all([ |
| 50 | + fs.rm(path.join(ctx.nodeDir, nodeCratesDir, 'Cargo.lock'), { force: true }), |
| 51 | + fs.rm(path.join(ctx.nodeDir, nodeCratesDir, 'Cargo.toml'), { force: true }), |
| 52 | + fs.rm(path.join(ctx.nodeDir, nodeCratesDir, 'vendor'), { force: true, recursive: true }), |
| 53 | + ]); |
| 54 | + } |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function buildManifest() { |
| 59 | + return { |
| 60 | + title: 'Build new manifest', |
| 61 | + task: async(ctx) => { |
| 62 | + const sourceManifest = TOML.parse( |
| 63 | + await fs.readFile(path.join(ctx.nodeDir, chromiumCratesDir, 'Cargo.toml'), 'utf8') |
| 64 | + ); |
| 65 | + |
| 66 | + const header = |
| 67 | + '# This manifest is generated by node-core-utils. Do not modify it directly.\n\n'; |
| 68 | + const manifest = { |
| 69 | + package: { |
| 70 | + edition: sourceManifest.package.edition, |
| 71 | + name: 'node_crates', |
| 72 | + version: `${ctx.newVersion.major}.${ctx.newVersion.minor}.${ctx.newVersion.build}` |
| 73 | + }, |
| 74 | + lib: { |
| 75 | + 'crate-type': ['staticlib'] |
| 76 | + }, |
| 77 | + dependencies: {} |
| 78 | + }; |
| 79 | + for (const crate of ctx.temporalCrates) { |
| 80 | + if (crate === 'temporal_capi' || typeof sourceManifest.dependencies[crate] === 'object') { |
| 81 | + manifest.dependencies[crate] = sourceManifest.dependencies[crate]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const cargoTOML = header + TOML.stringify(manifest); |
| 86 | + await fs.writeFile(path.join(ctx.nodeDir, nodeCratesDir, 'Cargo.toml'), cargoTOML); |
| 87 | + }, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +// Note that the crates vendored in third_party/rust/chromium_crates_io already have any custom |
| 92 | +// Chromium patches applied by gnrt, so we don't need to worry about patching them ourselves. |
| 93 | +function vendorCrates() { |
| 94 | + return { |
| 95 | + title: 'Copy vendored crates', |
| 96 | + task: async(ctx, task) => { |
| 97 | + const chromiumVendor = path.join(ctx.nodeDir, chromiumCratesDir, 'vendor'); |
| 98 | + const nodeVendor = path.join(ctx.nodeDir, nodeCratesDir, 'vendor'); |
| 99 | + await fs.mkdir(nodeVendor); |
| 100 | + const subtasks = []; |
| 101 | + for (const crate of await fs.readdir(chromiumVendor)) { |
| 102 | + // gnrt's vendored directories are of the form "name-vN", "name-v0_N", or "name-v0_0_N" |
| 103 | + const name = crate.match(/^(.+)-v(?:0_){0,2}\d+$/)?.[1]; |
| 104 | + if (!name || !ctx.temporalCrates.includes(name)) continue; |
| 105 | + if (name === 'temporal_capi') { |
| 106 | + ctx.temporalCAPIDirectory = crate; |
| 107 | + } |
| 108 | + subtasks.push({ |
| 109 | + title: name, |
| 110 | + task: async(ctx) => { |
| 111 | + await fs.cp( |
| 112 | + path.join(chromiumVendor, crate), |
| 113 | + path.join(nodeVendor, crate), |
| 114 | + { recursive: true } |
| 115 | + ); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + return task.newListr(subtasks, { concurrent: ctx.concurrent }); |
| 120 | + } |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +function generateLockfile() { |
| 125 | + return { |
| 126 | + title: 'Generate lockfile', |
| 127 | + task: async(ctx) => { |
| 128 | + await forceRunAsync(ctx.cargo, ['generate-lockfile', '--quiet', '--offline'], { |
| 129 | + ignoreFailure: false, |
| 130 | + spawnArgs: { cwd: path.join(ctx.nodeDir, nodeCratesDir) } |
| 131 | + }); |
| 132 | + } |
| 133 | + }; |
| 134 | +} |
| 135 | + |
| 136 | +function updateGYP() { |
| 137 | + return { |
| 138 | + title: 'Update include path in crates.gyp', |
| 139 | + task: async(ctx, task) => { |
| 140 | + const filePath = path.join(ctx.nodeDir, nodeCratesDir, 'crates.gyp'); |
| 141 | + let gyp = await fs.readFile(filePath, 'utf8'); |
| 142 | + const [definition, currentDirectory] = gyp.match(/'temporal_capi_dir': '(.+?)'/); |
| 143 | + if (currentDirectory === ctx.temporalCAPIDirectory) { |
| 144 | + task.skip('crates.gyp already up-to-date'); |
| 145 | + return; |
| 146 | + } |
| 147 | + gyp = gyp.replace(definition, `'temporal_capi_dir': '${ctx.temporalCAPIDirectory}'`); |
| 148 | + await fs.writeFile(filePath, gyp); |
| 149 | + } |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +function commitChanges() { |
| 154 | + return { |
| 155 | + title: 'Commit changes', |
| 156 | + task: async(ctx) => { |
| 157 | + await ctx.execGitNode('add', ['deps/crates']); |
| 158 | + await ctx.execGitNode( |
| 159 | + 'commit', |
| 160 | + ['-m', `deps: update Rust crates for V8 ${ctx.newVersion}`] |
| 161 | + ); |
| 162 | + } |
| 163 | + }; |
| 164 | +} |
0 commit comments