diff --git a/src/initialize/src/classes/ProjectInitializer.ts b/src/initialize/src/classes/ProjectInitializer.ts index a315d874f..de9d3cea5 100644 --- a/src/initialize/src/classes/ProjectInitializer.ts +++ b/src/initialize/src/classes/ProjectInitializer.ts @@ -18,6 +18,7 @@ import { detectType } from '../controllers/typeDetectionController'; import { writeCwSettings } from '../controllers/cwSettingsController'; import { importProjectFromGitRepo } from '../controllers/gitController'; import { InitializeResult } from 'initializeTypes'; +import { importProjectFromTar } from '../controllers/tarController'; export default class ProjectInitializer { projectName: string; @@ -47,6 +48,16 @@ export default class ProjectInitializer { return this.initializeExistingProject(); } + async initializeProjectFromTar( + tarFile: string + ): Promise { + await importProjectFromTar(tarFile, + this.projectName, + this.projectMountDirectory + ); + return this.initializeExistingProject(); + } + copyFiles() { this.traverseFileSystem(this.projectMountDirectory); } diff --git a/src/initialize/src/controllers/tarController.ts b/src/initialize/src/controllers/tarController.ts new file mode 100644 index 000000000..151912f60 --- /dev/null +++ b/src/initialize/src/controllers/tarController.ts @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2019 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +import fs from 'fs-extra'; +import { exec } from 'child_process'; +import request from 'request'; + +export function importProjectFromTar(tarFile: string, name: string, dest: string): Promise { + + const fileName = `/tmp/${name}.tar.gz`; + const file = fs.createWriteStream(fileName); + + return new Promise((resolve, reject) => { + request(tarFile) + .pipe(file) + .on('finish', () => { + exec(`tar -xvf ${fileName} -C ${dest}`, (err: any, stdout: any, stderr: any) => { + try { + fs.removeSync(fileName); + } + catch (err) { + /* Logging error when removing temp dir fails */ + console.log(err); + } + if (err) { + return reject(err); + } + resolve(); + }); + }) + .on('error', (err) => { + try { + fs.removeSync(fileName); + } + catch (err) { + console.log(err); + } + reject(err); + }); + }); + +} diff --git a/src/initialize/src/main.ts b/src/initialize/src/main.ts index 95d3654cf..b88e25736 100644 --- a/src/initialize/src/main.ts +++ b/src/initialize/src/main.ts @@ -42,7 +42,9 @@ export async function initializeProject(projectName: string, projectDirectory: s const projectInitializer = new ProjectInitializer(projectName, projectDirectory); try { - if (isGitCloneRequired(projectName, gitRepository)) { + if (process.env.TAR_FILE) { + initializeResult = await projectInitializer.initializeProjectFromTar(process.env.TAR_FILE); + } else if (isGitCloneRequired(projectName, gitRepository)) { initializeResult = await projectInitializer.initializeProjectFromGit(gitRepository, gitBranch); } else { initializeResult = await projectInitializer.initializeExistingProject(); diff --git a/src/pfe/portal/modules/projectInitializer.js b/src/pfe/portal/modules/projectInitializer.js index a680c2073..255179509 100644 --- a/src/pfe/portal/modules/projectInitializer.js +++ b/src/pfe/portal/modules/projectInitializer.js @@ -189,12 +189,19 @@ const createEnvForContainerCreation = (user, projectName, gitInfo) => { env.push(`CW_EXTENSIONS=${JSON.stringify(cwExtensions)}`); if (projectName && gitInfo.repo) { - env.push( - `PROJ_NAME=${projectName}`, - `GIT_REPO=${gitInfo.repo}`, - ); - if (gitInfo.branch) { - env.push(`GIT_BRANCH=${gitInfo.branch}`); + if (gitInfo.repo.endsWith(".tar.gz")) { + env.push( + `PROJ_NAME=${projectName}`, + `TAR_FILE=${gitInfo.repo}`, + ); + } else { + env.push( + `PROJ_NAME=${projectName}`, + `GIT_REPO=${gitInfo.repo}`, + ); + if (gitInfo.branch) { + env.push(`GIT_BRANCH=${gitInfo.branch}`); + } } } return env;