Skip to content

Commit

Permalink
API 34 Support Gradle Tools project
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed May 10, 2024
1 parent 7009965 commit 48baf24
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 735 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ example
**/assets/www/cordova.js

/test/.externalNativeBuild

!/templates/project/gradle/wrapper/gradle-wrapper.jar
!/test/androidx/gradle/wrapper/gradle-wrapper.jar
/test/androidx/cdv-gradle-config.json

/test/androidx/tools
/test/assets/www/.tmp*
/test/assets/www/cordova.js
/test/bin
Expand Down
14 changes: 3 additions & 11 deletions lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const fs = require('fs-extra');
const path = require('path');
const execa = require('execa');
const glob = require('fast-glob');
const semver = require('semver');
const events = require('cordova-common').events;
const CordovaError = require('cordova-common').CordovaError;
const check_reqs = require('../check_reqs');
Expand All @@ -30,8 +29,6 @@ const { compareByAll, isWindows } = require('../utils');
const { createEditor } = require('properties-parser');
const CordovaGradleConfigParserFactory = require('../config/CordovaGradleConfigParserFactory');

const MIN_GRADLE_REQUIRED = '8.6';

const MARKER = 'YOUR CHANGES WILL BE ERASED!';
const SIGNING_PROPERTIES = '-signing.properties';
const TEMPLATE =
Expand Down Expand Up @@ -118,7 +115,7 @@ class ProjectBuilder {
}

getGradleWrapperPath () {
let wrapper = path.join(this.root, 'gradlew');
let wrapper = path.join(this.root, 'tools', 'gradlew');

if (isWindows()) {
wrapper += '.bat';
Expand All @@ -133,16 +130,11 @@ class ProjectBuilder {
* @returns {Promise<void>}
*/
async installGradleWrapper (gradleVersion) {
if (semver.compare(semver.coerce(MIN_GRADLE_REQUIRED), semver.coerce(gradleVersion)) === 1) {
throw new CordovaError(`Cannot install Gradle ${gradleVersion}. Minimum Required is ${MIN_GRADLE_REQUIRED}.`);
}

const wrapper = this.getGradleWrapperPath();
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(wrapper, ['-p', this.root, 'wrapper', '--gradle-distribution-url', process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL, '--validate-url'], { stdio: 'inherit' });
await execa('gradle', ['-p', path.join(this.root, 'tools'), 'wrapper', '--gradle-distribution-url', process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL, '--validate-url'], { stdio: 'inherit' });
} else {
await execa(wrapper, ['-p', this.root, 'wrapper', '--gradle-version', gradleVersion, '--validate-url'], { stdio: 'inherit' });
await execa('gradle', ['-p', path.join(this.root, 'tools'), 'wrapper', '--gradle-version', gradleVersion, '--validate-url'], { stdio: 'inherit' });
}
}

Expand Down
8 changes: 3 additions & 5 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function copyBuildRules (projectPath) {
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'));

copyGradleWrapper(projectPath);
copyGradleTools(projectPath);
}

function copyScripts (projectPath) {
Expand Down Expand Up @@ -171,12 +171,10 @@ function validateProjectName (project_name) {
return Promise.resolve();
}

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

fs.copySync(path.resolve(srcDir, 'gradle'), path.resolve(projectPath, 'gradle'));
fs.copySync(path.resolve(srcDir, 'gradlew'), path.resolve(projectPath, 'gradlew'));
fs.copySync(path.resolve(srcDir, 'gradlew.bat'), path.resolve(projectPath, 'gradlew.bat'));
fs.copySync(path.resolve(srcDir, 'tools'), path.resolve(projectPath, 'tools'));
}

/**
Expand Down
20 changes: 4 additions & 16 deletions spec/unit/builders/ProjectBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
under the License.
*/

const { CordovaError } = require('cordova-common');
const fs = require('fs-extra');
const path = require('path');
const rewire = require('rewire');
Expand Down Expand Up @@ -135,27 +134,16 @@ describe('ProjectBuilder', () => {
execaSpy.and.resolveTo();
});

let gradle = path.normalize('/root/gradlew');
if (isWindows()) {
gradle += '.bat';
}

it('should run gradle wrapper 8.7', async () => {
await builder.installGradleWrapper('8.7');
expect(execaSpy).toHaveBeenCalledWith(gradle, ['-p', '/root', 'wrapper', '--gradle-version', '8.7', '--validate-url'], jasmine.any(Object));
expect(execaSpy).toHaveBeenCalledWith('gradle', ['-p', path.normalize('/root/tools'), 'wrapper', '--gradle-version', '8.7', '--validate-url'], 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.7');
delete process.env.CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL;
expect(execaSpy).toHaveBeenCalledWith(gradle, ['-p', '/root', 'wrapper', '--gradle-distribution-url', 'https://dist.local', '--validate-url'], jasmine.any(Object));
});

it('should error if attempting to install an unacceptable gradle version', async () => {
const UNACCEPTABLE_VERSION = '8.5';
await expectAsync(builder.installGradleWrapper(UNACCEPTABLE_VERSION))
.toBeRejectedWithError(CordovaError, new RegExp(`^Cannot install Gradle ${UNACCEPTABLE_VERSION}.+`));
expect(execaSpy).toHaveBeenCalledWith('gradle', ['-p', path.normalize('/root/tools'), 'wrapper', '--gradle-distribution-url', 'https://dist.local', '--validate-url'], jasmine.any(Object));
});
});

Expand Down Expand Up @@ -188,7 +176,7 @@ describe('ProjectBuilder', () => {

builder.build({});

let gradle = path.join(rootDir, 'gradlew');
let gradle = path.join(rootDir, 'tools', 'gradlew');
if (isWindows()) {
gradle += '.bat';
}
Expand Down Expand Up @@ -250,7 +238,7 @@ describe('ProjectBuilder', () => {
const gradleArgs = ['test', 'args', '-f'];
builder.getArgs.and.returnValue(gradleArgs);

let gradle = path.join(rootDir, 'gradlew');
let gradle = path.join(rootDir, 'tools', 'gradlew');
if (isWindows()) {
gradle += '.bat';
}
Expand Down
Binary file removed templates/project/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 0 additions & 7 deletions templates/project/gradle/wrapper/gradle-wrapper.properties

This file was deleted.

249 changes: 0 additions & 249 deletions templates/project/gradlew

This file was deleted.

Loading

0 comments on commit 48baf24

Please sign in to comment.