Skip to content

Commit

Permalink
Merge pull request #241 from andreban/bundle-release
Browse files Browse the repository at this point in the history
GradleWrapper can call bundleRelease
  • Loading branch information
andreban authored Jul 17, 2020
2 parents df71c5d + 73a1b29 commit fa03925
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
20 changes: 12 additions & 8 deletions packages/core/src/lib/GradleWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
* limitations under the License.
*/

import * as util from 'util';
import {exec} from 'child_process';
import {executeFile} from './util';
import {AndroidSdkTools} from './androidSdk/AndroidSdkTools';

const execPromise = util.promisify(exec);

/**
* A Wrapper around the Gradle commands.
*/
Expand Down Expand Up @@ -48,14 +45,21 @@ export class GradleWrapper {
}
}

/**
* Invokes `gradle bundleRelease` for the Android project.
*/
async bundleRelease(): Promise<void> {
const env = this.androidSdkTools.getEnv();
await executeFile(
this.gradleCmd, ['bundleRelease', '--stacktrace'], env, undefined, this.projectLocation);
}

/**
* Invokes `gradle assembleRelease` for the Android project.
*/
async assembleRelease(): Promise<void> {
const env = this.androidSdkTools.getEnv();
await execPromise(`${this.gradleCmd} assembleRelease --stacktrace`, {
env: env,
cwd: this.projectLocation,
});
await executeFile(
this.gradleCmd, ['assembleRelease', '--stacktrace'], env, undefined, this.projectLocation);
}
}
4 changes: 2 additions & 2 deletions packages/core/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export async function execute(
}

export async function executeFile(
cmd: string, args: string[], env: NodeJS.ProcessEnv, log?: Log,
cmd: string, args: string[], env: NodeJS.ProcessEnv, log?: Log, cwd?: string,
): Promise<{stdout: string; stderr: string}> {
if (log) {
log.debug(`Executing command ${cmd} with args ${args}`);
}
return await execFilePromise(cmd, args, {env: env});
return await execFilePromise(cmd, args, {env: env, cwd: cwd});
}

export async function downloadFile(url: string, path: string): Promise<void> {
Expand Down
62 changes: 62 additions & 0 deletions packages/core/src/spec/lib/GradleWrapperSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {GradleWrapper} from '../../lib/GradleWrapper';
import {Config} from '../../lib/Config';
import {JdkHelper} from '../../lib/jdk/JdkHelper';
import {AndroidSdkTools} from '../../lib/androidSdk/AndroidSdkTools';
import * as util from '../../lib/util';
import * as fs from 'fs';

describe('GradleWrapper', () => {
let gradleWrapper: GradleWrapper;
let androidSdkTools: AndroidSdkTools;

const cwd = '/path/to/twa-project/';
const process = {
platform: 'linux',
env: {
'PATH': '',
},
cwd: () => cwd,
} as unknown as NodeJS.Process;

beforeEach(() => {
spyOn(fs, 'existsSync').and.returnValue(true);
const config = new Config('/home/user/jdk8', '/home/user/sdktools');
const jdkHelper = new JdkHelper(process, config);
androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
gradleWrapper = new GradleWrapper(process, androidSdkTools);
});

describe('#bundleRelease', () => {
it('Calls "gradle bundleRelease --stacktrace"', async () => {
spyOn(util, 'executeFile').and.stub();
await gradleWrapper.bundleRelease();
expect(util.executeFile).toHaveBeenCalledWith('./gradlew',
['bundleRelease', '--stacktrace'], androidSdkTools.getEnv(), undefined, cwd);
});
});

describe('#assembleRelease', async () => {
it('Calls "gradle assembleRelease --stacktrace"', async () => {
spyOn(util, 'executeFile').and.stub();
await gradleWrapper.assembleRelease();
expect(util.executeFile).toHaveBeenCalledWith('./gradlew',
['assembleRelease', '--stacktrace'], androidSdkTools.getEnv(), undefined, cwd);
});
});
});

0 comments on commit fa03925

Please sign in to comment.