/
artifacts.ts
91 lines (90 loc) · 3.15 KB
/
artifacts.ts
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
import { quote } from 'shlex';
import { logger } from '../../logger';
import { ExecOptions, exec } from '../../util/exec';
import {
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../util/gitfs';
import { UpdateArtifact, UpdateArtifactsResult } from '../common';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`cargo.updateArtifacts(${packageFileName})`);
if (updatedDeps === undefined || updatedDeps.length < 1) {
logger.debug('No updated cargo deps - returning null');
return null;
}
const lockFileName = getSiblingFileName(packageFileName, 'Cargo.lock');
const existingLockFileContent = await readLocalFile(lockFileName);
if (!existingLockFileContent) {
logger.debug('No Cargo.lock found');
return null;
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
logger.debug('Updating ' + lockFileName);
for (let i = 0; i < updatedDeps.length; i += 1) {
const dep = updatedDeps[i];
// Update dependency `${dep}` in Cargo.lock file corresponding to Cargo.toml file located
// at ${localPackageFileName} path
let cmd = `cargo update --manifest-path ${quote(
packageFileName
)} --package ${quote(dep)}`;
const execOptions: ExecOptions = {
docker: {
image: 'renovate/rust',
},
};
try {
await exec(cmd, execOptions);
} catch (err) /* istanbul ignore next */ {
// Two different versions of one dependency can be present in the same
// crate, and when that happens an attempt to update it with --package ${dep}
// key results in cargo exiting with error code `101` and an error mssage:
// "error: There are multiple `${dep}` packages in your project".
//
// If exception `err` was caused by this, we execute `updateAll` function
// instead of returning an error. `updateAll` function just executes
// "cargo update --manifest-path ${localPackageFileName}" without the `--package` key.
//
// If exception `err` was not caused by this, we just rethrow it. It will be caught
// by the outer try { } catch {} and processed normally.
const msgStart = 'error: There are multiple';
if (err.code === 101 && err.stderr.startsWith(msgStart)) {
cmd = cmd.replace(/ --package.*/, '');
await exec(cmd, execOptions);
} else {
throw err; // this is caught below
}
}
}
logger.debug('Returning updated Cargo.lock');
const newCargoLockContent = await readLocalFile(lockFileName);
if (existingLockFileContent === newCargoLockContent) {
logger.debug('Cargo.lock is unchanged');
return null;
}
return [
{
file: {
name: lockFileName,
contents: newCargoLockContent,
},
},
];
} catch (err) {
logger.warn({ err }, 'Failed to update Cargo lock file');
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.message,
},
},
];
}
}