This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uic): add live execution support
- Loading branch information
1 parent
8c9d06e
commit a757644
Showing
7 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as assert from 'node:assert' | ||
import * as fs from 'node:fs' | ||
import * as path from 'node:path' | ||
import { | ||
E2E_TIMEOUT, | ||
setupE2EEnvironment, | ||
sleep, | ||
TEST_ASSETS_PATH, | ||
} from '../utils' | ||
|
||
suite('uic-live-execution/e2e', () => { | ||
const sampleFilenameNoExt = 'sample' | ||
|
||
const uiFilePath = path.resolve( | ||
TEST_ASSETS_PATH, | ||
'ui', | ||
`${sampleFilenameNoExt}.ui`, | ||
) | ||
|
||
let originalFullText: string | ||
|
||
suiteSetup(async function () { | ||
this.timeout(E2E_TIMEOUT) | ||
await setupE2EEnvironment() | ||
}) | ||
|
||
setup(async () => { | ||
originalFullText = fs.readFileSync(uiFilePath, { encoding: 'utf-8' }) | ||
|
||
removeGeneratedRcFile(sampleFilenameNoExt) | ||
}) | ||
|
||
teardown(() => { | ||
fs.writeFileSync(uiFilePath, originalFullText, { encoding: 'utf-8' }) | ||
|
||
removeGeneratedRcFile(sampleFilenameNoExt) | ||
}) | ||
|
||
test('should recompile UI file changed', async () => { | ||
fs.writeFileSync( | ||
uiFilePath, | ||
originalFullText.replace(/My Window Title/gi, 'My New Window Title'), | ||
) | ||
|
||
await sleep() // wait for file watcher to trigger | ||
|
||
assert.ok( | ||
fs.existsSync( | ||
path.resolve(TEST_ASSETS_PATH, 'ui', `ui_${sampleFilenameNoExt}.py`), | ||
), | ||
) | ||
}).timeout(E2E_TIMEOUT) | ||
}).timeout(E2E_TIMEOUT) | ||
|
||
function removeGeneratedRcFile(sampleFilenameNoExt: string) { | ||
return fs.rmSync( | ||
path.resolve(TEST_ASSETS_PATH, 'ui', `ui_${sampleFilenameNoExt}.py`), | ||
{ force: true, recursive: true }, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { ExtensionContext, Uri } from 'vscode' | ||
import { workspace } from 'vscode' | ||
import { EXTENSION_NAMESPACE } from '../constants' | ||
import type { ExecError, StdErrError } from '../run' | ||
import type { ErrorResult, SuccessResult } from '../types' | ||
import { compileUi } from './compile-ui' | ||
|
||
export function registerUicLiveExecution({ | ||
context: { extensionPath, subscriptions }, | ||
onResultReceived, | ||
}: RegisterUicLiveExecutionArgs) { | ||
const watcher = workspace.createFileSystemWatcher('**/*.ui') | ||
watcher.onDidChange(async uri => | ||
onResultReceived( | ||
await onUiFileUpdated({ uri, extensionPath: extensionPath }), | ||
), | ||
) | ||
watcher.onDidCreate(async uri => | ||
onResultReceived( | ||
await onUiFileUpdated({ uri, extensionPath: extensionPath }), | ||
), | ||
) | ||
subscriptions.push(watcher) | ||
} | ||
|
||
type RegisterUicLiveExecutionArgs = { | ||
readonly context: ExtensionContext | ||
readonly onResultReceived: (result: OnUiFileUpdatedResult) => void | ||
} | ||
|
||
async function onUiFileUpdated({ | ||
uri, | ||
extensionPath, | ||
}: OnUiFileUpdatedArgs): Promise<OnUiFileUpdatedResult> { | ||
const enabled = | ||
workspace | ||
.getConfiguration(`${EXTENSION_NAMESPACE}.uic`, uri) | ||
.get<boolean>('liveExecution') ?? true | ||
|
||
if (!enabled) return { kind: 'Success', value: 'Live execution disabled' } | ||
|
||
return compileUi({ extensionPath }, uri) | ||
} | ||
|
||
type OnUiFileUpdatedArgs = { | ||
readonly extensionPath: string | ||
readonly uri: Uri | ||
} | ||
|
||
type OnUiFileUpdatedResult = | ||
| SuccessResult<string> | ||
| ExecError | ||
| StdErrError | ||
| ErrorResult<'NotFound'> | ||
| ErrorResult<'Type'> |