-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gradle): use java image for docker mode (#11316)
- Loading branch information
Showing
13 changed files
with
426 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,20 @@ | ||
import { GradleVersionDatasource } from '../../datasource/gradle-version'; | ||
import { logger } from '../../logger'; | ||
import { regEx } from '../../util/regex'; | ||
import * as gradleVersioning from '../../versioning/gradle'; | ||
import { id as versioning } from '../../versioning/gradle'; | ||
import type { PackageDependency, PackageFile } from '../types'; | ||
|
||
// https://regex101.com/r/1GaQ2X/1 | ||
const DISTRIBUTION_URL_REGEX = regEx( | ||
'^(?:distributionUrl\\s*=\\s*)\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?<type>bin|all)\\.zip\\s*$' | ||
); | ||
import { extractGradleVersion } from './utils'; | ||
|
||
export function extractPackageFile(fileContent: string): PackageFile | null { | ||
logger.debug('gradle-wrapper.extractPackageFile()'); | ||
const lines = fileContent.split('\n'); | ||
|
||
for (const line of lines) { | ||
const distributionUrlMatch = DISTRIBUTION_URL_REGEX.exec(line); | ||
if (distributionUrlMatch) { | ||
const dependency: PackageDependency = { | ||
depName: 'gradle', | ||
currentValue: distributionUrlMatch.groups.version, | ||
datasource: GradleVersionDatasource.id, | ||
versioning: gradleVersioning.id, | ||
}; | ||
logger.debug(dependency, 'Gradle Wrapper'); | ||
return { deps: [dependency] }; | ||
} | ||
logger.trace('gradle-wrapper.extractPackageFile()'); | ||
const currentValue = extractGradleVersion(fileContent); | ||
if (currentValue) { | ||
const dependency: PackageDependency = { | ||
depName: 'gradle', | ||
currentValue, | ||
datasource: GradleVersionDatasource.id, | ||
versioning, | ||
}; | ||
return { deps: [dependency] }; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { setGlobalConfig } from '../../config/global'; | ||
import { extractGradleVersion, getJavaContraint } from './utils'; | ||
|
||
describe('manager/gradle-wrapper/util', () => { | ||
describe('getJavaContraint()', () => { | ||
it('return null for global mode', () => { | ||
expect(getJavaContraint(undefined)).toBeNull(); | ||
}); | ||
|
||
it('return ^11.0.0 for docker mode and undefined gradle', () => { | ||
setGlobalConfig({ binarySource: 'docker' }); | ||
expect(getJavaContraint(undefined)).toEqual('^11.0.0'); | ||
}); | ||
|
||
it('return ^8.0.0 for docker gradle < 5', () => { | ||
setGlobalConfig({ binarySource: 'docker' }); | ||
expect(getJavaContraint('4.9')).toEqual('^8.0.0'); | ||
}); | ||
|
||
it('return ^11.0.0 for docker gradle >=5 && <7', () => { | ||
setGlobalConfig({ binarySource: 'docker' }); | ||
expect(getJavaContraint('6.0')).toEqual('^11.0.0'); | ||
}); | ||
|
||
it('return ^16.0.0 for docker gradle >= 7', () => { | ||
setGlobalConfig({ binarySource: 'docker' }); | ||
expect(getJavaContraint('7.0.1')).toEqual('^16.0.0'); | ||
}); | ||
}); | ||
|
||
describe('extractGradleVersion()', () => { | ||
it('works for undefined', () => { | ||
expect(extractGradleVersion(undefined)).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import type { Stats } from 'fs'; | ||
import os from 'os'; | ||
import upath from 'upath'; | ||
import { getGlobalConfig } from '../../config/global'; | ||
import { chmod } from '../../util/fs'; | ||
import { regEx } from '../../util/regex'; | ||
import gradleVersioning from '../../versioning/gradle'; | ||
import { id as npmVersioning } from '../../versioning/npm'; | ||
|
||
export const extraEnv = { | ||
GRADLE_OPTS: | ||
'-Dorg.gradle.parallel=true -Dorg.gradle.configureondemand=true -Dorg.gradle.daemon=false -Dorg.gradle.caching=false', | ||
}; | ||
|
||
export function gradleWrapperFileName(): string { | ||
if ( | ||
os.platform() === 'win32' && | ||
getGlobalConfig()?.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; | ||
} | ||
|
||
/** | ||
* Find compatible java version for gradle. | ||
* see https://docs.gradle.org/current/userguide/compatibility.html | ||
* @param gradleVersion current gradle version | ||
* @returns A Java semver range | ||
*/ | ||
export function getJavaContraint(gradleVersion: string): string | null { | ||
if (getGlobalConfig()?.binarySource !== 'docker') { | ||
// ignore | ||
return null; | ||
} | ||
|
||
const major = gradleVersioning.getMajor(gradleVersion); | ||
if (major >= 7) { | ||
return '^16.0.0'; | ||
} | ||
// first public gradle version was 2.0 | ||
if (major > 0 && major < 5) { | ||
return '^8.0.0'; | ||
} | ||
return '^11.0.0'; | ||
} | ||
|
||
export function getJavaVersioning(): string { | ||
return npmVersioning; | ||
} | ||
|
||
// https://regex101.com/r/1GaQ2X/1 | ||
const DISTRIBUTION_URL_REGEX = regEx( | ||
'^(?:distributionUrl\\s*=\\s*)\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?<type>bin|all)\\.zip\\s*$' | ||
); | ||
|
||
export function extractGradleVersion(fileContent: string): string | null { | ||
const lines = fileContent?.split('\n') ?? []; | ||
|
||
for (const line of lines) { | ||
const distributionUrlMatch = DISTRIBUTION_URL_REGEX.exec(line); | ||
if (distributionUrlMatch) { | ||
return distributionUrlMatch.groups.version; | ||
} | ||
} | ||
|
||
return null; | ||
} |
Oops, something went wrong.