Skip to content

Commit

Permalink
Merge pull request #47 from andreban/paths-with-space-fix
Browse files Browse the repository at this point in the history
Paths with space fix
  • Loading branch information
andreban authored Dec 5, 2019
2 parents e068937 + fa63f1a commit 40d6dec
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 57 deletions.
23 changes: 13 additions & 10 deletions src/lib/androidSdk/AndroidSdkTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class AndroidSdkTools {
this.process = process;
this.config = config;
this.jdkHelper = jdkHelper;
if (this.process.platform === 'win32') {
this.pathJoin = path.win32.join;
} else {
this.pathJoin = path.posix.join;
}
}

// Runs <path-to-sdk-tools>/tools/bin/sdkmanager --install "build-tools;29.0.2"
Expand All @@ -37,30 +42,28 @@ class AndroidSdkTools {

console.log('Installing Build Tools');
await util.execInteractive(
path.join(this.getAndroidHome(), '/tools/bin/sdkmanager'),
this.pathJoin(this.getAndroidHome(), '/tools/bin/sdkmanager'),
['--install',
`"build-tools;${BUILD_TOOLS_VERSION}"`],
env,
);
}

async checkBuildTools() {
const buildToolsPath = path.join(this.getAndroidHome(), '/build-tools/', BUILD_TOOLS_VERSION);
const buildToolsPath =
this.pathJoin(this.getAndroidHome(), '/build-tools/', BUILD_TOOLS_VERSION);
return fs.existsSync(buildToolsPath);
}

async writeLicenseFile() {
const licensesPath = path.join(this.getAndroidHome(), '/licenses/');
const licensesPath = this.pathJoin(this.getAndroidHome(), '/licenses/');
await fs.promises.mkdir(licensesPath, {recursive: true});
const androidSdkLicenseFile = path.join(licensesPath, '/android-sdk-license');
const androidSdkLicenseFile = this.pathJoin(licensesPath, '/android-sdk-license');
await fs.promises.writeFile(androidSdkLicenseFile, '24333f8a63b6825ea9c5514f83c2829b004d1fee');
}

getAndroidHome() {
if (this.process.platform === 'win32') {
return path.win32.join(this.config.androidSdkPath, '/');
}
return path.posix.join(this.config.androidSdkPath, '/');
return this.pathJoin(this.config.androidSdkPath, '/');
}

getEnv() {
Expand All @@ -72,7 +75,7 @@ class AndroidSdkTools {
async zipalign(input, output) {
const env = this.getEnv();
const zipalignCmd = [
path.join(this.getAndroidHome(), '/build-tools/29.0.2/zipalign'),
`"${this.pathJoin(this.getAndroidHome(), '/build-tools/29.0.2/zipalign')}"`,
'-v -f -p 4',
input,
output,
Expand All @@ -83,7 +86,7 @@ class AndroidSdkTools {
async apksigner(keystore, ksPass, alias, keyPass, input, output) {
const env = this.getEnv();
const apksignerCmd = [
path.join(this.getAndroidHome(), '/build-tools/29.0.2/apksigner'),
`"${this.pathJoin(this.getAndroidHome(), '/build-tools/29.0.2/apksigner')}"`,
`sign --ks ${keystore}`,
`--ks-key-alias ${alias}`,
`--ks-pass pass:${ksPass}`,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function untar(tarFile, destinationPath, deleteZipWhenDone = false) {

function execInteractive(cwd, args, env) {
const {spawn} = require('child_process');
const shell = spawn(cwd, args, {
const shell = spawn(`"${cwd}"`, args, {
stdio: 'inherit',
env: env,
shell: true,
Expand Down
134 changes: 88 additions & 46 deletions src/spec/lib/androidSdk/AndroidSdkToolsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,100 @@

const JdkHelper = require('../../../lib/jdk/JdkHelper');
const AndroidSdkTools = require('../../../lib/androidSdk/AndroidSdkTools');
const util = require('../../../lib/util');

function buildMockConfig(platform) {
if (platform === 'linux' || platform == 'darwin') {
return {
jdkPath: '/home/user/jdk8',
androidSdkPath: '/home/user/android-sdk',
};
}

if (platform === 'win32') {
return {
jdkPath: 'C:\\Users\\user\\jdk8',
androidSdkPath: 'C:\\Users\\user\\android-sdk',
};
}

throw new Error('Unsupported Platform: ' + platform);
}

function buildMockProcess(platform) {
if (platform === 'linux') {
return {
platform: 'linux',
env: {
'PATH': '',
},
};
}

if (platform === 'darwin') {
return {
platform: 'darwin',
env: {
'PATH': '',
},
};
}

if (platform === 'win32') {
return {
platform: 'win32',
env: {
'PATH': '',
},
};
}

throw new Error('Unsupported Platform: ' + platform);
}

describe('AndroidSdkTools', () => {
describe('#getEnv()', () => {
it('Sets the correct ANDROID_HOME on Linux', () => {
const config = {
jdkPath: '/home/user/jdk8',
androidSdkPath: '/home/user/android-sdk',
};
const process = {
platform: 'linux',
env: {
'PATH': '',
},
};
const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
const env = androidSdkTools.getEnv();
expect(env['ANDROID_HOME']).toBe('/home/user/android-sdk/');
});
const tests = [
{platform: 'linux', expectedAndroidHome: '/home/user/android-sdk/'},
{platform: 'darwin', expectedAndroidHome: '/home/user/android-sdk/'},
{platform: 'win32', expectedAndroidHome: 'C:\\Users\\user\\android-sdk\\'},
];

it('Sets the correct ANDROID_HOME on MacOSX', () => {
const config = {
jdkPath: '/home/user/jdk8',
androidSdkPath: '/home/user/android-sdk',
};
const process = {
platform: 'darwin',
env: {
'PATH': '',
},
};
const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
const env = androidSdkTools.getEnv();
expect(env['ANDROID_HOME']).toBe('/home/user/android-sdk/');
tests.forEach((test) => {
it(`Sets the correct ANDROID_HOME on ${test.platform}`, () => {
const config = buildMockConfig(test.platform);
const process = buildMockProcess(test.platform);
const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
const env = androidSdkTools.getEnv();
expect(env['ANDROID_HOME']).toBe(test.expectedAndroidHome);
});
});
});

describe('#installBuildTools', () => {
const tests = [
{platform: 'linux',
expectedAndroidHome: '/home/user/android-sdk/tools/bin/sdkmanager'},
{platform: 'darwin',
expectedAndroidHome: '/home/user/android-sdk/tools/bin/sdkmanager'},
{platform: 'win32',
expectedAndroidHome: 'C:\\Users\\user\\android-sdk\\tools\\bin\\sdkmanager'},
];

it('Sets the correct ANDROID_HOME on Windows', () => {
const config = {
jdkPath: 'C:\\Users\\user\\jdk8',
androidSdkPath: 'C:\\Users\\user\\android-sdk',
};
const process = {
platform: 'win32',
env: {
'PATH': '',
},
};
const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
const env = androidSdkTools.getEnv();
expect(env['ANDROID_HOME']).toBe('C:\\Users\\user\\android-sdk\\');
tests.forEach((test) => {
it(`Build the correct command-line on ${test.platform}`, () => {
const config = buildMockConfig(test.platform);
const process = buildMockProcess(test.platform);
const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
spyOn(util, 'execInteractive').and.stub();
androidSdkTools.installBuildTools();
expect(util.execInteractive).toHaveBeenCalledWith(
test.expectedAndroidHome,
['--install', '"build-tools;29.0.2"'],
androidSdkTools.getEnv());
});
});
});
});

0 comments on commit 40d6dec

Please sign in to comment.