/
artifacts.ts
135 lines (122 loc) · 3.39 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
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
import { quote } from 'shlex';
import { dirname, join } from 'upath';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { ExecOptions, exec } from '../../util/exec';
import {
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../util/fs';
import { UpdateArtifact, UpdateArtifactsResult } from '../common';
import { getCocoaPodsHome } from './utils';
const pluginRegex = /^\s*plugin\s*(['"])(?<plugin>[^'"]+)\1/;
function getPluginCommands(content: string): string[] {
const result = new Set<string>();
const lines: string[] = content.split('\n');
lines.forEach((line) => {
const match = pluginRegex.exec(line);
if (match) {
const { plugin } = match.groups;
result.add(`gem install ${quote(plugin)}`);
}
});
return [...result];
}
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`cocoapods.getArtifacts(${packageFileName})`);
if (updatedDeps.length < 1) {
logger.debug('CocoaPods: empty update - returning null');
return null;
}
const lockFileName = getSiblingFileName(packageFileName, 'Podfile.lock');
try {
await writeLocalFile(packageFileName, newPackageFileContent);
} catch (err) {
logger.warn({ err }, 'Podfile could not be written');
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.message,
},
},
];
}
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug(`Lockfile not found: ${lockFileName}`);
return null;
}
const match = new RegExp(/^COCOAPODS: (?<cocoapodsVersion>.*)$/m).exec(
existingLockFileContent
);
const tagConstraint =
match && match.groups ? match.groups.cocoapodsVersion : null;
const cmd = [...getPluginCommands(newPackageFileContent), 'pod install'];
const execOptions: ExecOptions = {
cwdFile: packageFileName,
extraEnv: {
CP_HOME_DIR: await getCocoaPodsHome(config),
},
docker: {
image: 'renovate/cocoapods',
tagScheme: 'ruby',
tagConstraint,
},
};
try {
await exec(cmd, execOptions);
} catch (err) {
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.stderr || err.stdout || err.message,
},
},
];
}
const status = await platform.getRepoStatus();
if (!status.modified.includes(lockFileName)) {
return null;
}
logger.debug(`Returning updated lockfile: ${lockFileName}`);
const lockFileContent = await readLocalFile(lockFileName);
const res: UpdateArtifactsResult[] = [
{
file: {
name: lockFileName,
contents: lockFileContent,
},
},
];
const podsDir = join(dirname(packageFileName), 'Pods');
const podsManifestFileName = join(podsDir, 'Manifest.lock');
if (await readLocalFile(podsManifestFileName, 'utf8')) {
for (const f of status.modified.concat(status.not_added)) {
if (f.startsWith(podsDir)) {
res.push({
file: {
name: f,
contents: await readLocalFile(f),
},
});
}
}
for (const f of status.deleted || []) {
res.push({
file: {
name: '|delete|',
contents: f,
},
});
}
}
return res;
}