Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function registerNotebookController(context: vscode.ExtensionContext): void {
'python',
'mojo',
"zig",
"powershell"
];

controller.executeHandler = (
Expand Down
15 changes: 13 additions & 2 deletions src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { processCellsTypescript } from './languages/typescript';
import { ChildProcessWithoutNullStreams, spawnSync, spawn } from 'child_process';
import { processShell as processShell } from './languages/shell';
import { processCellsPython } from './languages/python';
import { processPowerShell } from './languages/powershell';
import * as vscode from 'vscode';
import { homedir } from 'os';
import { processCellsMojo } from './languages/mojo';
Expand Down Expand Up @@ -360,7 +361,16 @@ export class Kernel {
}
lastRunLanguage = 'shell';
const nuResult = processShell(currentCell, 'nushell');
return { stream: nuResult.stream, clearOutput: nuResult.clearOutput };
return { stream: nuResult.stream, clearOutput: nuResult.clearOutput }

case 'powershell':
if (commandNotOnPath('powershell', 'https://learn.microsoft.com/en-us/powershell/scripting/install/install-powershell')) {
return null;
}
lastRunLanguage = 'powershell';
const psResult = processPowerShell(currentCell);
return { stream: psResult.stream, clearOutput: psResult.clearOutput };


default:
return null;
Expand Down Expand Up @@ -483,7 +493,8 @@ export class Kernel {
buf = Buffer.concat(arr);
const outputs = decoder.decode(buf).split(/!!output-start-cell[\n,""," "]/g);
const currentCellOutput =
lastRunLanguage === 'shell' ? outputs[1] : outputs[currentCellLang.index];
['shell','powershell'].includes(lastRunLanguage)
? outputs[1] : outputs[currentCellLang.index];

if (!clearOutput && currentCellOutput?.trim()) {
exec.replaceOutput([
Expand Down
35 changes: 35 additions & 0 deletions src/languages/powershell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { writeFileSync, mkdirSync } from 'fs';
import path from 'path';
import { NotebookCell } from 'vscode';
import { getTempPath } from '../config';
import { LanguageCommand } from '../types';

const tempDir = getTempPath();

export const processPowerShell = (
cell: NotebookCell
): { stream: ChildProcessWithoutNullStreams; clearOutput: boolean } => {
const prog = process.platform === 'win32' ? 'powershell' : 'pwsh';

const contents = cell.document.getText(); // optionally escape quotes

const main = `
Write-Host "!!output-start-cell"
& {${contents}}
`;

const filename = path.join(tempDir, 'main.ps1');
mkdirSync(tempDir, { recursive: true });
writeFileSync(filename, main);

let clearOutput = false;
if (cell.metadata?.command?.startsWith(LanguageCommand.clear)) {
clearOutput = true;
}

return {
stream: spawn(prog, ['-NoProfile', '-File', filename], { cwd: tempDir }),
clearOutput,
};
};
1 change: 1 addition & 0 deletions src/markdownParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const LANG_IDS = new Map([
['zsh', 'zsh'],
['openai', 'openai'],
['groq', 'groq'],
['powershell', 'powershell'],
]);

const LANG_ABBREVS = new Map(Array.from(LANG_IDS.keys()).map((k) => [LANG_IDS.get(k), k]));
Expand Down