resource deployment ext implementation -wip (#5508)

* resource types

* implement the dialog

* remove unused method

* fix issues

* formatting

* 5-17

* address comments and more tests
This commit is contained in:
Alan Ren
2019-05-17 20:24:02 -07:00
committed by GitHub
parent a59d1d3c05
commit 586fe10525
36 changed files with 2208 additions and 21 deletions

View File

@@ -0,0 +1,79 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { NotebookInfo } from '../interfaces';
import { isString } from 'util';
import * as os from 'os';
import * as path from 'path';
import * as nls from 'vscode-nls';
import { IPlatformService } from './platformService';
const localize = nls.loadMessageBundle();
export interface INotebookService {
launchNotebook(notebook: string | NotebookInfo): void;
}
export class NotebookService implements INotebookService {
constructor(private platformService: IPlatformService) { }
/**
* Copy the notebook to the user's home directory and launch the notebook from there.
* @param notebook the path of the notebook
*/
launchNotebook(notebook: string | NotebookInfo): void {
const notebookRelativePath = this.getNotebook(notebook);
const notebookFullPath = path.join(__dirname, '../../', notebookRelativePath);
if (notebookRelativePath && this.platformService.fileExists(notebookFullPath)) {
const targetFileName = this.getTargetNotebookFileName(notebookFullPath, os.homedir());
this.platformService.copyFile(notebookFullPath, targetFileName);
this.platformService.openFile(targetFileName);
}
else {
this.platformService.showErrorMessage(localize('resourceDeployment.notebookNotFound', 'The notebook {0} does not exist', notebookFullPath));
}
}
/**
* get the notebook path for current platform
* @param notebook the notebook path
*/
getNotebook(notebook: string | NotebookInfo): string {
let notebookPath;
if (notebook && !isString(notebook)) {
const platform = this.platformService.platform();
if (platform === 'win32') {
notebookPath = notebook.win32;
} else if (platform === 'darwin') {
notebookPath = notebook.darwin;
} else {
notebookPath = notebook.linux;
}
} else {
notebookPath = notebook;
}
return notebookPath;
}
/**
* Get a file name that is not already used in the target directory
* @param notebook source notebook file name
* @param targetDirectory target directory
*/
getTargetNotebookFileName(notebook: string, targetDirectory: string): string {
const notebookFileExtension = '.ipynb';
const baseName = path.basename(notebook, notebookFileExtension);
let targetFileName;
let idx = 0;
do {
const suffix = idx === 0 ? '' : `-${idx}`;
targetFileName = path.join(targetDirectory, `${baseName}${suffix}${notebookFileExtension}`);
idx++;
} while (this.platformService.fileExists(targetFileName));
return targetFileName;
}
}