Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JdkInstaller #315

Merged
merged 7 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/cli/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import {join} from 'path';
import {homedir} from 'os';
import {Config, Log, ConsoleLog} from '@bubblewrap/core';
import {Config, Log, ConsoleLog, JdkInstaller} from '@bubblewrap/core';
import * as inquirer from 'inquirer';
import {existsSync} from 'fs';
import {promises as fsPromises} from 'fs';
Expand All @@ -31,17 +31,31 @@ const LEGACY_CONFIG_FILE_PATH = join(LEGACY_CONFIG_FOLDER, LEGACY_CONFIG_NAME);

async function createConfig(): Promise<Config> {
const result = await inquirer.prompt([
{
type: 'confirm',
name: 'jdkExists',
message: 'Do you have JDK 8 installed?',
default: false,
},
{
name: 'jdkPath',
message: 'Path to the JDK:',
message: 'Path to the JDK. If not installed, enter the desired install location:',
validate: existsSync,
}, {
name: 'androidSdkPath',
message: 'Path to the Android SDK:',
validate: existsSync,
},
]);
return new Config(result.jdkPath, result.androidSdkPath);

let jdkPath = result.jdkPath;
if (!result.jdkExists) {
console.log('Downloading JDK 8');
const jdkInstaller = new JdkInstaller(process);
jdkPath = await jdkInstaller.install(result.jdkPath);
}

return new Config(jdkPath, result.androidSdkPath);
}

async function renameConfigIfNeeded(log: Log): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Log, ConsoleLog} from './lib/Log';
import {MockLog} from './spec/mock/MockLog';
import {JarSigner} from './lib/jdk/JarSigner';
import {JdkHelper} from './lib/jdk/JdkHelper';
import {JdkInstaller} from './lib/jdk/JdkInstaller';
import {KeyTool} from './lib/jdk/KeyTool';
import {TwaManifest, DisplayModes, DisplayMode, asDisplayMode, SigningKeyInfo}
from './lib/TwaManifest';
Expand All @@ -35,6 +36,7 @@ export {AndroidSdkTools,
GradleWrapper,
JarSigner,
JdkHelper,
JdkInstaller,
KeyTool,
Log,
ConsoleLog,
Expand Down
96 changes: 96 additions & 0 deletions packages/core/src/lib/jdk/JdkInstaller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2019 Google Inc. All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wrong year for the copyright header.

Suggested change
* Copyright 2019 Google Inc. All Rights Reserved.
* 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.
*/

'use strict';

import * as path from 'path';
import util = require('../util');

const JDK_VERSION = '8u265-b01';
const JDK_DIR = `jdk${JDK_VERSION}`;
const DOWNLOAD_JDK_BIN_ROOT = `https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk${JDK_VERSION}/`;
const DOWNLOAD_JDK_SRC_ROOT = 'https://github.com/AdoptOpenJDK/openjdk-jdk8u/archive/';
const JDK_BIN_VERSION = JDK_VERSION.replace('-', '');
const JDK_FILE_NAME_MAC = `OpenJDK8U-jdk_x64_mac_hotspot_${JDK_BIN_VERSION}.tar.gz`;
const JDK_FILE_NAME_WIN32 = `OpenJDK8U-jdk_x86-32_windows_hotspot_${JDK_BIN_VERSION}.zip`;
const JDK_FILE_NAME_LINUX64 = `OpenJDK8U-jdk_x64_linux_hotspot_${JDK_BIN_VERSION}.tar.gz`;
const JDK_SRC_ZIP = `jdk${JDK_VERSION}.zip`;

/**
* Checks whether JDK 8 is installed. If not installed,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is wrong isn't it? I can't see where this class checks whether JDK 8 is installed.

* download the binary and source code and decompress at
joycetoh8 marked this conversation as resolved.
Show resolved Hide resolved
* path given by user.
joycetoh8 marked this conversation as resolved.
Show resolved Hide resolved
*/
export class JdkInstaller {
private process: NodeJS.Process;
private downloadFile: string;
private unzipFunction: (srcPath: string, dstPath: string, deleteWhenDone: boolean)
=> Promise<void>;
private joinPath: (...paths: string[]) => string;
/**
joycetoh8 marked this conversation as resolved.
Show resolved Hide resolved
* Constructs a new instance of JdkInstaller
joycetoh8 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param process {NodeJS.Process} process information from the OS process
*/
constructor(process: NodeJS.Process) {
this.process = process;
this.unzipFunction = util.untar;
this.joinPath = path.posix.join;
switch (process.platform) {
case 'win32': {
this.downloadFile = JDK_FILE_NAME_WIN32;
this.unzipFunction = util.unzipFile;
this.joinPath = path.win32.join;
break;
}
case 'darwin': {
this.downloadFile = JDK_FILE_NAME_MAC;
break;
}
case 'linux': {
this.downloadFile = JDK_FILE_NAME_LINUX64;
break;
}
default:
this.downloadFile = '';
console.log('Platform not found. Cannot download appropriate JDK.');
joycetoh8 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Downloads the platform-appropriate version of JDK 8, including
* binary and source code.
*
* @param installPath {string} path to install JDK at
*/
async install(installPath: string): Promise<string> {
if (this.downloadFile === '') {
throw new Error(`Platform not found or unsupported: ${this.process.platform}.`);
}
const dstPath = path.resolve(installPath);
const downloadSrcUrl = DOWNLOAD_JDK_SRC_ROOT + JDK_SRC_ZIP;
const localSrcZipPath = this.joinPath(dstPath, JDK_SRC_ZIP);
await util.downloadFile(downloadSrcUrl, localSrcZipPath);
await util.unzipFile(localSrcZipPath, dstPath, true);

const downloadBinUrl = DOWNLOAD_JDK_BIN_ROOT + this.downloadFile;
const localBinPath = this.joinPath(dstPath, this.downloadFile);
await util.downloadFile(downloadBinUrl, localBinPath);
await this.unzipFunction(localBinPath, dstPath, true);

return this.joinPath(dstPath, JDK_DIR);
}
}