Skip to content

Commit

Permalink
Chore: Move the scan for recipe environment in its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsonZiweiWang committed Jan 10, 2024
1 parent 714f8ba commit 089ef22
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 38 deletions.
66 changes: 66 additions & 0 deletions client/src/driver/BitbakeRecipeScanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode'
import { type LanguageClient } from 'vscode-languageclient/node'
import { logger } from '../lib/src/utils/OutputLogger'
import { runBitbakeTask } from '../ui/BitbakeCommands'
import { type BitbakeCustomExecution, type BitbakeTaskProvider } from '../ui/BitbakeTaskProvider'
import { RequestMethod } from '../lib/src/types/requests'

export class BitbakeRecipeScanner {
private isPending: boolean = false
private _languageClient: LanguageClient | undefined

public scanResults: string = ''

async scan (chosenRecipe: string, taskProvider: BitbakeTaskProvider): Promise<void> {
const scanRecipeEnvTask = new vscode.Task(
{ type: 'bitbake', recipes: [chosenRecipe], options: { parseOnly: true, env: true } },
vscode.TaskScope.Workspace,
'Bitbake: Scan recipe env',
'bitbake'
)

const runningTasks = vscode.tasks.taskExecutions
if (runningTasks.some((execution) => execution.task.name === scanRecipeEnvTask.name)) {
logger.debug('Bitbake parsing task is already running')
this.isPending = true
}

await runBitbakeTask(scanRecipeEnvTask, taskProvider)
}

subscribeToTaskEnd (context: vscode.ExtensionContext, taskProvider: BitbakeTaskProvider): void {
context.subscriptions.push(vscode.tasks.onDidEndTask(async (e) => {
if (e.execution.task.name === 'Bitbake: Scan recipe env') {
if (this.isPending) {
this.isPending = false
await this.scan(e.execution.task.definition.recipes?.[0] ?? '', taskProvider)
}

const executionEngine = e.execution.task.execution as BitbakeCustomExecution
if (executionEngine !== undefined) {
this.scanResults = executionEngine.pty?.outputDataString ?? ''
if (this._languageClient === undefined) {
logger.error('[onDidEndTask] Language client not set, unable to forward recipe environment to the server')
} else {
if (this.scanResults !== '') {
logger.debug('[onDidEndTask] Sending recipe environment to the server')
void this._languageClient.sendRequest(RequestMethod.ProcessRecipeScanResults, { scanResults: this.scanResults })
}
}
}
}
}))
}

setLanguageClient (client: LanguageClient): void {
this._languageClient = client
}
}

const bitbakeRecipeScanner = new BitbakeRecipeScanner()
export default bitbakeRecipeScanner
9 changes: 6 additions & 3 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { logger } from './lib/src/utils/OutputLogger'
import { activateLanguageServer, deactivateLanguageServer } from './language/languageClient'
import { BitbakeDriver } from './driver/BitbakeDriver'
import { BitbakeTaskProvider } from './ui/BitbakeTaskProvider'
import { registerBitbakeCommands, registerDevtoolCommands, setLanguageClient } from './ui/BitbakeCommands'
import { registerBitbakeCommands, registerDevtoolCommands } from './ui/BitbakeCommands'
import { BitbakeWorkspace } from './ui/BitbakeWorkspace'
import { BitbakeRecipesView } from './ui/BitbakeRecipesView'
import { BitbakeStatusBar } from './ui/BitbakeStatusBar'
Expand All @@ -20,8 +20,9 @@ import { BitbakeDocumentLinkProvider } from './documentLinkProvider'
import { DevtoolWorkspacesView } from './ui/DevtoolWorkspacesView'
import { bitbakeESDKMode, setBitbakeESDKMode } from './driver/BitbakeESDK'
import path from 'path'
import bitbakeRecipeScanner from './driver/BitbakeRecipeScanner'

export let client: LanguageClient
let client: LanguageClient
const bitbakeDriver: BitbakeDriver = new BitbakeDriver()
let bitbakeTaskProvider: BitbakeTaskProvider
let taskProvider: vscode.Disposable
Expand Down Expand Up @@ -88,10 +89,12 @@ export async function activate (context: vscode.ExtensionContext): Promise<void>
bitbakeTaskProvider = new BitbakeTaskProvider(bitbakeDriver)
client = await activateLanguageServer(context)
bitBakeProjectScanner.setClient(client)
setLanguageClient(client)

taskProvider = vscode.tasks.registerTaskProvider('bitbake', bitbakeTaskProvider)

bitbakeRecipeScanner.setLanguageClient(client)
bitbakeRecipeScanner.subscribeToTaskEnd(context, bitbakeTaskProvider)

clientNotificationManager.setMemento(context.workspaceState)
bitbakeRecipesView = new BitbakeRecipesView(bitbakeWorkspace, bitBakeProjectScanner)
bitbakeRecipesView.registerView(context)
Expand Down
40 changes: 5 additions & 35 deletions client/src/ui/BitbakeCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,18 @@ import { extractRecipeName } from '../lib/src/utils/files'
import { runBitbakeTerminal, runBitbakeTerminalCustomCommand } from './BitbakeTerminal'
import { type BitbakeDriver } from '../driver/BitbakeDriver'
import { sanitizeForShell } from '../lib/src/BitbakeSettings'
import { type BitbakeCustomExecution, type BitbakeTaskDefinition, type BitbakeTaskProvider } from './BitbakeTaskProvider'
import { type BitbakeTaskDefinition, type BitbakeTaskProvider } from './BitbakeTaskProvider'
import { type LayerInfo } from '../lib/src/types/BitbakeScanResult'
import { DevtoolWorkspaceTreeItem } from './DevtoolWorkspacesView'
import { finishProcessExecution } from '../lib/src/utils/ProcessUtils'
import { type SpawnSyncReturns } from 'child_process'
import { clientNotificationManager } from './ClientNotificationManager'
import { configureDevtoolSDKFallback } from '../driver/BitbakeESDK'
import { type LanguageClient } from 'vscode-languageclient/node'
import { RequestMethod } from '../lib/src/types/requests'
import bitbakeRecipeScanner from '../driver/BitbakeRecipeScanner'

let parsingPending = false
let bitbakeSanity = false

let _languageClient: LanguageClient

export function setLanguageClient (client: LanguageClient): void {
_languageClient = client
}

export function registerBitbakeCommands (context: vscode.ExtensionContext, bitbakeWorkspace: BitbakeWorkspace, bitbakeTaskProvider: BitbakeTaskProvider, bitBakeProjectScanner: BitBakeProjectScanner): void {
context.subscriptions.push(vscode.commands.registerCommand('bitbake.parse-recipes', async () => { await parseAllrecipes(bitbakeWorkspace, bitbakeTaskProvider) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.build-recipe', async (uri) => { await buildRecipeCommand(bitbakeWorkspace, bitbakeTaskProvider.bitbakeDriver, uri) }))
Expand All @@ -54,15 +47,6 @@ export function registerBitbakeCommands (context: vscode.ExtensionContext, bitba
void parseAllrecipes(bitbakeWorkspace, bitbakeTaskProvider)
}
}
if (e.execution.task.name === 'Bitbake: Scan recipe env') {
const executionEngine = e.execution.task.execution as BitbakeCustomExecution
if (executionEngine !== undefined) {
const scanResults = executionEngine.pty?.outputDataString
_languageClient === undefined && logger.error('Language client not set, unable to send request to the server to process scan results')
logger.debug('[onDidEndTask] Sending recipe environment to the server')
void _languageClient?.sendRequest(RequestMethod.ProcessRecipeScanResults, { scanResults })
}
}
})
)
}
Expand Down Expand Up @@ -144,27 +128,13 @@ async function scanRecipeCommand (bitbakeWorkspace: BitbakeWorkspace, taskProvid
logger.debug('Command: scan-recipe-env')

if (!bitbakeSanity && !(await taskProvider.bitbakeDriver?.checkBitbakeSettingsSanity())) {
logger.warn('bitbake settings are not sane, skip parse')
logger.warn('bitbake settings are not sane, Abort scan')
return
}

bitbakeSanity = true

const scanRecipeEnvTask = new vscode.Task(
{ type: 'bitbake', recipes: [chosenRecipe], options: { parseOnly: true, env: true } },
vscode.TaskScope.Workspace,
'Bitbake: Scan recipe env',
'bitbake'
)

const runningTasks = vscode.tasks.taskExecutions
if (runningTasks.some((execution) => execution.task.name === scanRecipeEnvTask.name)) {
logger.debug('Bitbake parsing task is already running')
parsingPending = true
return
}

await runBitbakeTask(scanRecipeEnvTask, taskProvider)
await bitbakeRecipeScanner.scan(chosenRecipe, taskProvider)
}

async function runTaskCommand (bitbakeWorkspace: BitbakeWorkspace, bitbakeDriver: BitbakeDriver, uri?: any, task?: any): Promise<void> {
Expand Down Expand Up @@ -248,7 +218,7 @@ async function dropRecipe (bitbakeWorkspace: BitbakeWorkspace, uri?: string): Pr
}
}

async function runBitbakeTask (task: vscode.Task, taskProvider: vscode.TaskProvider): Promise<void> {
export async function runBitbakeTask (task: vscode.Task, taskProvider: vscode.TaskProvider): Promise<void> {
let resolvedTask = taskProvider.resolveTask(task, new vscode.CancellationTokenSource().token)
if (resolvedTask instanceof Promise) {
resolvedTask = await resolvedTask
Expand Down

0 comments on commit 089ef22

Please sign in to comment.