diff --git a/packages/core/src/lib/GradleWrapper.ts b/packages/core/src/lib/GradleWrapper.ts index 5f7fad9d..db9c5462 100644 --- a/packages/core/src/lib/GradleWrapper.ts +++ b/packages/core/src/lib/GradleWrapper.ts @@ -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. */ @@ -48,14 +45,21 @@ export class GradleWrapper { } } + /** + * Invokes `gradle bundleRelease` for the Android project. + */ + async bundleRelease(): Promise { + 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 { 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); } } diff --git a/packages/core/src/lib/util.ts b/packages/core/src/lib/util.ts index 66d2dd5a..bbef381e 100644 --- a/packages/core/src/lib/util.ts +++ b/packages/core/src/lib/util.ts @@ -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 { diff --git a/packages/core/src/spec/lib/GradleWrapperSpec.ts b/packages/core/src/spec/lib/GradleWrapperSpec.ts new file mode 100644 index 00000000..4ad5f36d --- /dev/null +++ b/packages/core/src/spec/lib/GradleWrapperSpec.ts @@ -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); + }); + }); +});