Skip to content

Commit

Permalink
feat: improve Gradle installation via wrapper
Browse files Browse the repository at this point in the history
From upstream (apache#1678):

Gradle -b flag

This flag has been deprecated and scheduled to be removed
in gradle's next major release. The gradle projects must follow a
standard structure now. Cordova's generated project already follows the
standard structure thus for cordova build commands the -b append can
simply just be removed, we don't need to explicitly declare the
build.gradle file.

It was also used against the wrapper.gradle file which changes are noted
below.

gradle.wrapper

The "intentionally empty" gradle.wrapper file has been removed.

We no longer manually manage gradle versions. Instead, we let gradle do
it for us by utilising their --gradle-version argument. A new function
was introduced to replace the old runGradleWrapper to:

 1. setup the gradle wrapper if not already present
 2. install/update the gradle wrapper to the specified version

By default, this will be 8.4 as defined in the defaults json config
file, but may be overwritten by GradleVersion preference, or
CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL environment variable.

If CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL is defined, the preference
will be ignored and --distribution-url will be used instead, maintaining
the ability for corporations to install their own gradle wrapper from
their own source.

References https://outsystemsrd.atlassian.net/browse/RNMT-6402
  • Loading branch information
EiyuuZack committed Nov 29, 2023
1 parent 9cb3b1b commit 0dcad27
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 70 deletions.
40 changes: 15 additions & 25 deletions lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ class ProjectBuilder {
}

getArgs (cmd, opts) {
let args = [
'-b', path.join(this.root, 'build.gradle')
];
let args = [];
if (opts.extraArgs) {
args = args.concat(opts.extraArgs);
}
Expand Down Expand Up @@ -116,16 +114,17 @@ class ProjectBuilder {
return args;
}

/*
* This returns a promise
*/
runGradleWrapper (gradle_cmd) {
const gradlePath = path.join(this.root, 'gradlew');
const wrapperGradle = path.join(this.root, 'wrapper.gradle');
if (fs.existsSync(gradlePath)) {
// Literally do nothing, for some reason this works, while !fs.existsSync didn't on Windows
/**
* Installs/updates the gradle wrapper
* @param {string} gradleVersion The gradle version to install. Ignored if CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL environment variable is defined
* @returns {Promise<void>}
*/
async installGradleWrapper (gradleVersion) {
if (process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL) {
events.emit('verbose', `Overriding Gradle Version via CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL (${process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL})`);
await execa('gradle', ['-p', this.root, 'wrapper', '--gradle-distribution-url', process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL], { stdio: 'inherit' });
} else {
return execa(gradle_cmd, ['-p', this.root, 'wrapper', '-b', wrapperGradle], { stdio: 'inherit' });
await execa('gradle', ['-p', this.root, 'wrapper', '--gradle-version', gradleVersion], { stdio: 'inherit' });
}
}

Expand Down Expand Up @@ -275,23 +274,14 @@ class ProjectBuilder {

prepEnv (opts) {
const self = this;
const config = this._getCordovaConfig();
return check_reqs.check_gradle()
.then(function (gradlePath) {
return self.runGradleWrapper(gradlePath);
.then(function () {
events.emit('verbose', `Using Gradle: ${config.GRADLE_VERSION}`);
return self.installGradleWrapper(config.GRADLE_VERSION);
}).then(function () {
return self.prepBuildFiles();
}).then(() => {
const config = this._getCordovaConfig();
// update/set the distributionUrl in the gradle-wrapper.properties
const gradleWrapperPropertiesPath = path.join(self.root, 'gradle/wrapper/gradle-wrapper.properties');
const gradleWrapperProperties = createEditor(gradleWrapperPropertiesPath);
const distributionUrl = process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL || `https://services.gradle.org/distributions/gradle-${config.GRADLE_VERSION}-all.zip`;
gradleWrapperProperties.set('distributionUrl', distributionUrl);
gradleWrapperProperties.save();

events.emit('verbose', `Gradle Distribution URL: ${distributionUrl}`);
})
.then(() => {
const signingPropertiesPath = path.join(self.root, `${opts.buildType}${SIGNING_PROPERTIES}`);

if (fs.existsSync(signingPropertiesPath)) fs.removeSync(signingPropertiesPath);
Expand Down
17 changes: 5 additions & 12 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,13 @@ function prepBuildFiles (projectPath) {
buildModule.getBuilder(projectPath).prepBuildFiles();
}

function copyBuildRules (projectPath, isLegacy) {
function copyBuildRules (projectPath) {
const srcDir = path.join(ROOT, 'templates', 'project');

if (isLegacy) {
// The project's build.gradle is identical to the earlier build.gradle, so it should still work
fs.copySync(path.join(srcDir, 'legacy', 'build.gradle'), path.join(projectPath, 'legacy', 'build.gradle'));
fs.copySync(path.join(srcDir, 'wrapper.gradle'), path.join(projectPath, 'wrapper.gradle'));
} else {
fs.copySync(path.join(srcDir, 'build.gradle'), path.join(projectPath, 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'build.gradle'), path.join(projectPath, 'app', 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'repositories.gradle'), path.join(projectPath, 'app', 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'repositories.gradle'), path.join(projectPath, 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'wrapper.gradle'), path.join(projectPath, 'wrapper.gradle'));
}
fs.copySync(path.join(srcDir, 'build.gradle'), path.join(projectPath, 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'build.gradle'), path.join(projectPath, 'app', 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'repositories.gradle'), path.join(projectPath, 'app', 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'repositories.gradle'), path.join(projectPath, 'repositories.gradle'));
}

function copyScripts (projectPath) {
Expand Down
23 changes: 14 additions & 9 deletions spec/unit/builders/ProjectBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,24 @@ describe('ProjectBuilder', () => {
});
});

describe('runGradleWrapper', () => {
it('should run the provided gradle command if a gradle wrapper does not already exist', () => {
spyOn(fs, 'existsSync').and.returnValue(false);
builder.runGradleWrapper('/my/sweet/gradle');
expect(execaSpy).toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
describe('installGradleWrapper', () => {
beforeEach(() => {
execaSpy.and.resolveTo();
});

it('should do nothing if a gradle wrapper exists in the project directory', () => {
spyOn(fs, 'existsSync').and.returnValue(true);
builder.runGradleWrapper('/my/sweet/gradle');
expect(execaSpy).not.toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
it('should run gradle wrapper 8.3', async () => {
await builder.installGradleWrapper('8.3');
expect(execaSpy).toHaveBeenCalledWith('gradle', ['-p', '/root', 'wrapper', '--gradle-version', '8.3'], jasmine.any(Object));
});

it('CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL should override gradle version', async () => {
process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL = 'https://dist.local';
await builder.installGradleWrapper('8.3');
delete process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL;
expect(execaSpy).toHaveBeenCalledWith('gradle', ['-p', '/root', 'wrapper', '--gradle-distribution-url', 'https://dist.local'], jasmine.any(Object));
});
});

describe('build', () => {
beforeEach(() => {
spyOn(builder, 'getArgs');
Expand Down
1 change: 0 additions & 1 deletion templates/project/wrapper.gradle

This file was deleted.

22 changes: 0 additions & 22 deletions test/androidx/wrapper.gradle

This file was deleted.

12 changes: 11 additions & 1 deletion test/run_java_unit_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ class AndroidTestRunner {
);
}

_getGradleVersion () {
const config = JSON.parse(
fs.readFileSync(path.resolve(this.projectDir, '../../framework/cdv-gradle-config-defaults.json'), {
encoding: 'utf-8'
})
);

return config.GRADLE_VERSION;
}

_createProjectBuilder () {
return new ProjectBuilder(this.projectDir).runGradleWrapper('gradle');
return new ProjectBuilder(this.projectDir).installGradleWrapper(this._getGradleVersion());
}

run () {
Expand Down

0 comments on commit 0dcad27

Please sign in to comment.