Skip to content

Commit

Permalink
fix(windows): Gradle paths
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed May 9, 2024
1 parent 9683461 commit 5cf27f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
18 changes: 14 additions & 4 deletions lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const events = require('cordova-common').events;
const CordovaError = require('cordova-common').CordovaError;
const check_reqs = require('../check_reqs');
const PackageType = require('../PackageType');
const { compareByAll } = require('../utils');
const { compareByAll, isWindows } = require('../utils');
const { createEditor } = require('properties-parser');
const CordovaGradleConfigParserFactory = require('../config/CordovaGradleConfigParserFactory');

Expand Down Expand Up @@ -117,6 +117,16 @@ class ProjectBuilder {
return args;
}

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

if (isWindows()) {
wrapper += '.bat';
}

return wrapper;
}

/**
* Installs/updates the gradle wrapper
* @param {string} gradleVersion The gradle version to install. Ignored if CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL environment variable is defined
Expand All @@ -127,7 +137,7 @@ class ProjectBuilder {
throw new CordovaError(`Cannot install Gradle ${gradleVersion}. Minimum Required is ${MIN_GRADLE_REQUIRED}.`);
}

const wrapper = path.join(this.root, 'gradlew');
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' });
Expand Down Expand Up @@ -315,7 +325,7 @@ class ProjectBuilder {
* Returns a promise.
*/
async build (opts) {
const wrapper = path.join(this.root, 'gradlew');
const wrapper = this.getGradleWrapperPath();
const args = this.getArgs(opts.buildType === 'debug' ? 'debug' : 'release', opts);

events.emit('verbose', `Running Gradle Build: ${wrapper} ${args.join(' ')}`);
Expand All @@ -336,7 +346,7 @@ class ProjectBuilder {
}

clean (opts) {
const wrapper = path.join(this.root, 'gradlew');
const wrapper = this.getGradleWrapperPath();
const args = this.getArgs('clean', opts);
return execa(wrapper, args, { stdio: 'inherit', cwd: path.resolve(this.root) })
.then(() => {
Expand Down
24 changes: 20 additions & 4 deletions spec/unit/builders/ProjectBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const { CordovaError } = require('cordova-common');
const fs = require('fs-extra');
const path = require('path');
const rewire = require('rewire');
const { isWindows } = require('../../../lib/utils');

describe('ProjectBuilder', () => {
const rootDir = '/root';
Expand Down Expand Up @@ -134,16 +135,21 @@ 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(path.normalize('/root/gradlew'), ['-p', '/root', 'wrapper', '--gradle-version', '8.7', '--validate-url'], jasmine.any(Object));
expect(execaSpy).toHaveBeenCalledWith(gradle, ['-p', '/root', '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(path.normalize('/root/gradlew'), ['-p', '/root', 'wrapper', '--gradle-distribution-url', 'https://dist.local', '--validate-url'], jasmine.any(Object));
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 () => {
Expand Down Expand Up @@ -182,7 +188,12 @@ describe('ProjectBuilder', () => {

builder.build({});

expect(execaSpy).toHaveBeenCalledWith(path.join(rootDir, 'gradlew'), testArgs, jasmine.anything());
let gradle = path.join(rootDir, 'gradlew');
if (isWindows()) {
gradle += '.bat';
}

expect(execaSpy).toHaveBeenCalledWith(gradle, testArgs, jasmine.anything());
});

it('should reject if the spawn fails', () => {
Expand Down Expand Up @@ -239,8 +250,13 @@ describe('ProjectBuilder', () => {
const gradleArgs = ['test', 'args', '-f'];
builder.getArgs.and.returnValue(gradleArgs);

let gradle = path.join(rootDir, 'gradlew');
if (isWindows()) {
gradle += '.bat';
}

return builder.clean(opts).then(() => {
expect(execaSpy).toHaveBeenCalledWith(path.join(rootDir, 'gradlew'), gradleArgs, jasmine.anything());
expect(execaSpy).toHaveBeenCalledWith(gradle, gradleArgs, jasmine.anything());
});
});

Expand Down

0 comments on commit 5cf27f2

Please sign in to comment.