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