/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as loc from '../../localizedConstants'; import { INotebookService, Notebook } from '../../services/notebookService'; import { IToolsService } from '../../services/toolsService'; import { InputComponents, setModelValues } from '../modelViewUtils'; import { ResourceTypeModel } from '../resourceTypeModel'; import { ResourceTypeWizard } from '../resourceTypeWizard'; import { DeploymentType, NotebookWizardDeploymentProvider, NotebookWizardInfo } from '../../interfaces'; import { IPlatformService } from '../../services/platformService'; import { NotebookWizardAutoSummaryPage } from './notebookWizardAutoSummaryPage'; import { NotebookWizardPage } from './notebookWizardPage'; export class NotebookWizardModel extends ResourceTypeModel { private _inputComponents: InputComponents = {}; public get notebookService(): INotebookService { return this.wizard.notebookService; } public get platformService(): IPlatformService { return this.wizard.platformService; } public get toolsService(): IToolsService { return this.wizard.toolsService; } public get wizardInfo(): NotebookWizardInfo { return this.notebookProvider.notebookWizard; } public get inputComponents(): InputComponents { return this._inputComponents; } constructor(public notebookProvider: NotebookWizardDeploymentProvider, wizard: ResourceTypeWizard) { super(notebookProvider, wizard); if (this.notebookProvider.notebookWizard.codeCellInsertionPosition === undefined) { this.notebookProvider.notebookWizard.codeCellInsertionPosition = 0; } this.wizard.wizardObject.title = this.notebookProvider.notebookWizard.title; this.wizard.wizardObject.doneButton.label = this.notebookProvider.notebookWizard.doneAction?.label || loc.deployNotebook; this.wizard.wizardObject.generateScriptButton.label = this.notebookProvider.notebookWizard.scriptAction?.label || loc.scriptToNotebook; } public get deploymentType(): DeploymentType | undefined { return this.notebookProvider.notebookWizard.type; } public initialize(): void { this.wizard.setPages(this.getPages()); } public onCancel(): void { } public async onGenerateScript(): Promise { const notebook = await this.prepareNotebookAndEnvironment(); await this.openNotebook(notebook); } public async onOk(): Promise { const notebook = await this.prepareNotebookAndEnvironment(); const openedNotebook = await this.openNotebook(notebook); openedNotebook.runAllCells(); } private async openNotebook(notebook: Notebook) { const notebookPath = this.notebookService.getNotebookPath(this.wizardInfo.notebook); return await this.notebookService.openNotebookWithContent(notebookPath, JSON.stringify(notebook, undefined, 4)); } private async prepareNotebookAndEnvironment() { await setModelValues(this.inputComponents, this); const env: NodeJS.ProcessEnv = process.env; this.setEnvironmentVariables(env, (varName) => { const isPassword = !!this.inputComponents[varName]?.isPassword; return isPassword; }); const notebook: Notebook = await this.notebookService.getNotebook(this.wizardInfo.notebook); // generate python code statements for all variables captured by the wizard const statements = this.getCodeCellContentForNotebook( this.toolsService.toolsForCurrentProvider, (varName) => { const isPassword = !!this.inputComponents[varName]?.isPassword; return !isPassword; } ); // insert generated code statements into the notebook. notebook.cells.splice( this.wizardInfo.codeCellInsertionPosition ?? 0, 0, { cell_type: 'code', source: statements, metadata: {}, outputs: [], execution_count: 0 } ); return notebook; } private getPages(): NotebookWizardPage[] { const pages: NotebookWizardPage[] = []; for (let pageIndex: number = 0; pageIndex < this.wizardInfo.pages.length; pageIndex++) { if (this.wizardInfo.pages[pageIndex].isSummaryPage && this.wizardInfo.isSummaryPageAutoGenerated) { // If we are auto-generating the summary page pages.push(new NotebookWizardAutoSummaryPage(this, pageIndex)); } else { pages.push(new NotebookWizardPage(this, pageIndex)); } } return pages; } }