|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import toml from '@iarna/toml'; |
1 | 4 | import execa from 'execa'; |
2 | 5 |
|
3 | 6 | export interface CargoMetadataRoot { |
@@ -128,3 +131,54 @@ export async function getCargoMetadata( |
128 | 131 |
|
129 | 132 | return JSON.parse(cargoMetaData) as CargoMetadataRoot; |
130 | 133 | } |
| 134 | + |
| 135 | +interface CargoConfig { |
| 136 | + env: Record<string, any>; |
| 137 | + cwd: string; |
| 138 | +} |
| 139 | + |
| 140 | +interface CargoBuildTarget { |
| 141 | + name?: string; |
| 142 | + path?: string; |
| 143 | +} |
| 144 | + |
| 145 | +interface CargoToml { |
| 146 | + bin?: CargoBuildTarget[]; |
| 147 | +} |
| 148 | + |
| 149 | +interface CargoWorkspace { |
| 150 | + toml: CargoToml; |
| 151 | + root: string; |
| 152 | +} |
| 153 | + |
| 154 | +export async function findCargoWorkspace( |
| 155 | + config: CargoConfig, |
| 156 | +): Promise<CargoWorkspace> { |
| 157 | + const { stdout: projectDescriptionStr } = await execa( |
| 158 | + 'cargo', |
| 159 | + ['locate-project'], |
| 160 | + config, |
| 161 | + ); |
| 162 | + const projectDescription = JSON.parse(projectDescriptionStr) as { |
| 163 | + root: string; |
| 164 | + }; |
| 165 | + return { |
| 166 | + toml: await toml.parse.stream(fs.createReadStream(projectDescription.root)), |
| 167 | + root: projectDescription.root, |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +export function findBinaryName( |
| 172 | + workspace: CargoWorkspace, |
| 173 | + entryPath: string, |
| 174 | +): string { |
| 175 | + const { bin } = workspace.toml; |
| 176 | + if (bin) { |
| 177 | + const relativePath = path.relative(path.dirname(workspace.root), entryPath); |
| 178 | + const entry = bin.find((binEntry) => binEntry.path === relativePath); |
| 179 | + if (entry?.name) { |
| 180 | + return entry.name; |
| 181 | + } |
| 182 | + } |
| 183 | + return path.basename(entryPath, '.rs').replace('[', '_').replace(']', '_'); |
| 184 | +} |
0 commit comments