Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
feat!: allow users to set glob pattern for live execution
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace config rcc|uic.liveExecution with rcc|uic.liveExecution.enabled

Fix #278
  • Loading branch information
seanwu1105 committed Feb 25, 2023
1 parent 1098731 commit 90ac3f0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,18 @@
"markdownDescription": "The options passed to Qt `qmlls` executable. See [here](https://github.com/seanwu1105/vscode-qt-for-python#predefined-variables) for a detailed list of predefined variables.",
"scope": "window"
},
"qtForPython.rcc.liveExecution": {
"qtForPython.rcc.liveExecution.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "Enable live execution of Qt `rcc` executable. This will automatically compile the resource file when it is saved.",
"scope": "resource"
},
"qtForPython.rcc.liveExecution.glob": {
"type": "string",
"default": "**/*.qrc",
"markdownDescription": "The glob pattern used to match the resource files to be compiled. See [here](https://code.visualstudio.com/api/references/vscode-api#GlobPattern) for a detailed list of glob patterns.",
"scope": "resource"
},
"qtForPython.rcc.path": {
"type": "string",
"default": "",
Expand All @@ -262,11 +268,17 @@
"markdownDescription": "The options passed to Qt `rcc` executable. See [here](https://github.com/seanwu1105/vscode-qt-for-python#predefined-variables) for a detailed list of predefined variables.",
"scope": "resource"
},
"qtForPython.uic.liveExecution": {
"qtForPython.uic.liveExecution.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "Automatically recompile Qt UI files when any `*.ui` file has changed or created."
},
"qtForPython.uic.liveExecution.glob": {
"type": "string",
"default": "**/*.ui",
"markdownDescription": "The glob pattern used to match the UI files to be compiled. See [here](https://code.visualstudio.com/api/references/vscode-api#GlobPattern) for a detailed list of glob patterns.",
"scope": "resource"
},
"qtForPython.uic.path": {
"type": "string",
"default": "",
Expand Down
40 changes: 40 additions & 0 deletions src/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ type GetEnabledFromConfigArgs = {

type SupportedSwitchableTool = Extract<SupportedTool, 'qmlls'>

export function getLiveExecutionEnabledFromConfig$({
tool,
resource,
}: GetLiveExecutionEnabledFromConfigArgs) {
return getConfiguration$<boolean>({
section: `${EXTENSION_NAMESPACE}.${tool}.liveExecution`,
key: 'enabled',
defaultValue: DEFAULT_LIVE_EXECUTION_ENABLED,
resource,
})
}

const DEFAULT_LIVE_EXECUTION_ENABLED = true

type GetLiveExecutionEnabledFromConfigArgs = {
readonly tool: SupportedLiveExecutionTool
readonly resource: URI | undefined
}

export function getLiveExecutionGlobFromConfig$({
tool,
resource,
defaultValue,
}: GetLiveExecutionGlobFromConfigArgs) {
return getConfiguration$<string>({
section: `${EXTENSION_NAMESPACE}.${tool}.liveExecution`,
key: 'glob',
defaultValue,
resource,
})
}

type GetLiveExecutionGlobFromConfigArgs = {
readonly tool: SupportedLiveExecutionTool
readonly resource: URI | undefined
readonly defaultValue: string
}

type SupportedLiveExecutionTool = Extract<SupportedTool, 'uic' | 'rcc'>

export function getConfiguration$<T>({
section,
key,
Expand Down
30 changes: 20 additions & 10 deletions src/rcc/rcc-live-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,36 @@ import {
mergeMap,
of,
startWith,
switchMap,
} from 'rxjs'
import { RelativePattern, workspace } from 'vscode'
import { URI } from 'vscode-uri'
import { z } from 'zod'
import { getConfiguration$ } from '../configurations'
import { EXTENSION_NAMESPACE } from '../constants'
import {
getLiveExecutionEnabledFromConfig$,
getLiveExecutionGlobFromConfig$,
} from '../configurations'
import type { ErrorResult, SuccessResult } from '../types'
import { getWatcher$ } from '../watcher'
import { compileResource } from './compile-resource'

export function registerRccLiveExecution$({
extensionUri,
}: RegisterRccLiveExecutionArgs) {
const qrcFiles$ = defer(async () =>
workspace.findFiles('**/*.qrc', '**/site-packages/**'),
).pipe(concatMap(uris => from(uris)))
const glob$ = getLiveExecutionGlobFromConfig$({
tool: 'rcc',
resource: undefined,
defaultValue: '**/*.qrc',
})

const qrcFiles$ = glob$.pipe(
concatMap(glob => workspace.findFiles(glob, '**/site-packages/**')),
concatMap(uris => from(uris)),
)

const watcher$ = glob$.pipe(switchMap(glob => getWatcher$(glob)))

return merge(qrcFiles$, getWatcher$('**/*.qrc')).pipe(
return merge(qrcFiles$, watcher$).pipe(
mergeMap(qrcUri =>
registerResourcesLiveExecution$({ extensionUri, qrcUri }),
),
Expand All @@ -52,10 +64,8 @@ function registerResourcesLiveExecution$({
startWith(undefined), // Trigger compilation on resource file changes
concatMap(() =>
firstValueFrom(
getConfiguration$({
section: `${EXTENSION_NAMESPACE}.rcc`,
key: 'liveExecution',
defaultValue: true,
getLiveExecutionEnabledFromConfig$({
tool: 'rcc',
resource: qrcUri,
}),
),
Expand Down
26 changes: 13 additions & 13 deletions src/uic/uic-live-execution.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { concatMap, firstValueFrom } from 'rxjs'
import { concatMap, firstValueFrom, switchMap } from 'rxjs'
import type { URI } from 'vscode-uri'
import { getConfiguration$ } from '../configurations'
import { EXTENSION_NAMESPACE } from '../constants'
import {
getLiveExecutionEnabledFromConfig$,
getLiveExecutionGlobFromConfig$,
} from '../configurations'
import type { SupportedTool } from '../types'
import { getWatcher$ } from '../watcher'
import { compileUi } from './compile-ui'

export function registerUicLiveExecution$({
extensionUri,
}: GetUicLiveExecutionArgs) {
return getWatcher$('**/*.ui').pipe(
return getLiveExecutionGlobFromConfig$({
tool: 'uic',
resource: undefined,
defaultValue: '**/*.ui',
}).pipe(
switchMap(glob => getWatcher$(glob)),
concatMap(uri => onUiFileUpdated({ uri, extensionUri })),
)
}

type GetUicLiveExecutionArgs = {
readonly extensionUri: URI
}
type GetUicLiveExecutionArgs = { readonly extensionUri: URI }

async function onUiFileUpdated({ uri, extensionUri }: OnUiFileUpdatedArgs) {
const tool: SupportedTool = 'uic'
const enabled = await firstValueFrom(
getConfiguration$({
section: `${EXTENSION_NAMESPACE}.${tool}`,
key: 'liveExecution',
defaultValue: true,
resource: uri,
}),
getLiveExecutionEnabledFromConfig$({ tool, resource: uri }),
)

if (!enabled)
Expand Down

0 comments on commit 90ac3f0

Please sign in to comment.