forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
208 lines (188 loc) · 5.5 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { Stats } from 'fs';
import * as os from 'os';
import { chmod, stat } from 'fs-extra';
import upath from 'upath';
import { LANGUAGE_JAVA } from '../../constants/languages';
import * as datasourceMaven from '../../datasource/maven';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { ExecOptions, exec } from '../../util/exec';
import { BinarySource } from '../../util/exec/common';
import { readLocalFile } from '../../util/fs';
import * as gradleVersioning from '../../versioning/gradle';
import {
ExtractConfig,
PackageFile,
UpdateDependencyConfig,
Upgrade,
} from '../common';
import {
GradleDependency,
collectVersionVariables,
init,
updateGradleVersion,
} from './build-gradle';
import {
createRenovateGradlePlugin,
extractDependenciesFromUpdatesReport,
} from './gradle-updates-report';
export const GRADLE_DEPENDENCY_REPORT_OPTIONS =
'--init-script renovate-plugin.gradle renovate';
const TIMEOUT_CODE = 143;
export function gradleWrapperFileName(config: ExtractConfig): string {
if (
os.platform() === 'win32' &&
config?.binarySource !== BinarySource.Docker
) {
return 'gradlew.bat';
}
return './gradlew';
}
export async function prepareGradleCommand(
gradlewName: string,
cwd: string,
gradlew: Stats | null,
args: string | null
): Promise<string> {
/* eslint-disable no-bitwise */
// istanbul ignore if
if (gradlew?.isFile() === true) {
// if the file is not executable by others
if ((gradlew.mode & 0o1) === 0) {
// add the execution permission to the owner, group and others
await chmod(upath.join(cwd, gradlewName), gradlew.mode | 0o111);
}
if (args === null) {
return gradlewName;
}
return `${gradlewName} ${args}`;
}
/* eslint-enable no-bitwise */
return null;
}
async function prepareGradleCommandFallback(
gradlewName: string,
cwd: string,
gradlew: Stats | null,
args: string
): Promise<string> {
const cmd = await prepareGradleCommand(gradlewName, cwd, gradlew, args);
if (cmd === null) {
return `gradle ${args}`;
}
return cmd;
}
export async function executeGradle(
config: ExtractConfig,
cwd: string,
gradlew: Stats | null
): Promise<void> {
let stdout: string;
let stderr: string;
let timeout;
if (config.gradle?.timeout) {
timeout = config.gradle.timeout * 1000;
}
const cmd = await prepareGradleCommandFallback(
gradleWrapperFileName(config),
cwd,
gradlew,
GRADLE_DEPENDENCY_REPORT_OPTIONS
);
const execOptions: ExecOptions = {
timeout,
cwd,
docker: {
image: 'renovate/gradle',
},
};
try {
logger.debug({ cmd }, 'Start gradle command');
({ stdout, stderr } = await exec(cmd, execOptions));
} catch (err) /* istanbul ignore next */ {
if (err.code === TIMEOUT_CODE) {
throw new ExternalHostError(err, 'gradle');
}
logger.warn({ errMessage: err.message }, 'Gradle extraction failed');
return;
}
logger.debug(stdout + stderr);
logger.debug('Gradle report complete');
}
export async function extractAllPackageFiles(
config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFile[] | null> {
let rootBuildGradle: string | undefined;
let gradlew: Stats | null;
for (const packageFile of packageFiles) {
const dirname = upath.dirname(packageFile);
const gradlewPath = upath.join(dirname, gradleWrapperFileName(config));
gradlew = await stat(upath.join(config.localDir, gradlewPath)).catch(
() => null
);
if (['build.gradle', 'build.gradle.kts'].includes(packageFile)) {
rootBuildGradle = packageFile;
break;
}
// If there is gradlew in the same directory, the directory should be a Gradle project root
if (gradlew?.isFile() === true) {
rootBuildGradle = packageFile;
break;
}
}
if (!rootBuildGradle) {
logger.warn('No root build.gradle nor build.gradle.kts found - skipping');
return null;
}
logger.debug('Extracting dependencies from all gradle files');
const cwd = upath.join(config.localDir, upath.dirname(rootBuildGradle));
await createRenovateGradlePlugin(cwd);
await executeGradle(config, cwd, gradlew);
init();
const dependencies = await extractDependenciesFromUpdatesReport(cwd);
if (dependencies.length === 0) {
return [];
}
const gradleFiles: PackageFile[] = [];
for (const packageFile of packageFiles) {
const content = await readLocalFile(packageFile, 'utf8');
if (content) {
gradleFiles.push({
packageFile,
datasource: datasourceMaven.id,
deps: dependencies,
});
collectVersionVariables(dependencies, content);
} else {
// istanbul ignore next
logger.debug({ packageFile }, 'packageFile has no content');
}
}
return gradleFiles;
}
function buildGradleDependency(config: Upgrade): GradleDependency {
return {
group: config.depGroup,
name: config.name,
version: config.currentValue,
};
}
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string {
// prettier-ignore
logger.debug(`gradle.updateDependency(): packageFile:${upgrade.packageFile} depName:${upgrade.depName}, version:${upgrade.currentValue} ==> ${upgrade.newValue}`);
return updateGradleVersion(
fileContent,
buildGradleDependency(upgrade),
upgrade.newValue
);
}
export const language = LANGUAGE_JAVA;
export const defaultConfig = {
fileMatch: ['\\.gradle(\\.kts)?$', '(^|/)gradle.properties$'],
timeout: 600,
versioning: gradleVersioning.id,
};