Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitbake terminal refactor #33

Merged
merged 15 commits into from
Dec 15, 2023
Merged
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 __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const languages = {
createWebviewPanel: jest.fn(),
registerTreeDataProvider: jest.fn(),
createTreeView: jest.fn(),
createTerminal: jest.fn(),
};

const workspace = {
Expand Down
55 changes: 55 additions & 0 deletions client/images/yocto-light-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 2 additions & 5 deletions client/src/documentLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ export class BitbakeDocumentLinkProvider implements vscode.DocumentLinkProvider
const filenames = linksData.map(link => link.value.split(';')[0])
const filenamesRegex = '{' + filenames.join(',') + '}'
const parentDir = document.uri.path.split('/').slice(0, -1).join('/')
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri)
const pnDir = path.join(parentDir, extractRecipeName(document.uri.path) as string)
const pnDirRelative = pnDir.replace(workspaceFolder?.uri.path + '/', '')
const filesDir = path.join(parentDir, 'files')
const filesDirRelative = filesDir.replace(workspaceFolder?.uri.path + '/', '')
return [...(await vscode.workspace.findFiles(pnDirRelative + '/**/' + filenamesRegex, undefined, filenames.length, token)),
...(await vscode.workspace.findFiles(filesDirRelative + '/**/' + filenamesRegex, undefined, filenames.length, token))]
return [...(await vscode.workspace.findFiles(new vscode.RelativePattern(pnDir, '**/' + filenamesRegex), undefined, filenames.length, token)),
...(await vscode.workspace.findFiles(new vscode.RelativePattern(filesDir, '**/' + filenamesRegex), undefined, filenames.length, token))]
}

async provideDocumentLinks (document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.DocumentLink[]> {
Expand Down
4 changes: 3 additions & 1 deletion client/src/driver/BitBakeProjectScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import type {
import { type BitbakeDriver } from './BitbakeDriver'
import { type LanguageClient } from 'vscode-languageclient/node'
import fs from 'fs'
import { runBitbakeTerminalCustomCommand } from '../ui/BitbakeTerminal'
import { finishProcessExecution } from '../lib/src/utils/ProcessUtils'

interface ScannStatus {
scanIsRunning: boolean
Expand Down Expand Up @@ -400,7 +402,7 @@ export class BitBakeProjectScanner {
if (this._bitbakeDriver === undefined) {
throw new Error('Bitbake driver is not set')
}
return await this._bitbakeDriver.spawnBitbakeProcessSync(command)
return await finishProcessExecution(runBitbakeTerminalCustomCommand(this._bitbakeDriver, command, 'BitBake: Scan Project', true))
}
}

Expand Down
53 changes: 35 additions & 18 deletions client/src/driver/BitbakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import fs from 'fs'
import { logger } from '../lib/src/utils/OutputLogger'
import { type BitbakeSettings, loadBitbakeSettings } from '../lib/src/BitbakeSettings'
import { clientNotificationManager } from '../ui/ClientNotificationManager'
import { type BitbakeTaskDefinition } from '../ui/BitbakeTaskProvider'
import { runBitbakeTerminalCustomCommand } from '../ui/BitbakeTerminal'
import { finishProcessExecution } from '../lib/src/utils/ProcessUtils'

/// This class is responsible for wrapping up all bitbake classes and exposing them to the extension
export class BitbakeDriver {
Expand Down Expand Up @@ -48,21 +51,6 @@ export class BitbakeDriver {
return child
}

/// Execute a command in the bitbake environment and wait for completion
async spawnBitbakeProcessSync (command: string): Promise<childProcess.SpawnSyncReturns<Buffer>> {
const { shell, script } = this.prepareCommand(command)
await this.waitForBitbakeToFinish()
logger.debug(`Executing Bitbake command (sync) with ${shell}: ${script}`)
this.bitbakeActive = true
const ret = childProcess.spawnSync(script, {
shell,
cwd: this.bitbakeSettings.workingDirectory,
env: { ...process.env, ...this.bitbakeSettings.shellEnv }
})
this.bitbakeActive = false
return ret
}

private prepareCommand (command: string): {
shell: string
script: string
Expand Down Expand Up @@ -116,13 +104,42 @@ export class BitbakeDriver {
return false
}

// eslint-disable-next-line @typescript-eslint/await-thenable
const ret = await this.spawnBitbakeProcessSync('which bitbake')
const command = 'which bitbake'
const process = runBitbakeTerminalCustomCommand(this, command, 'Bitbake: Sanity test', true)
const ret = await finishProcessExecution(process)
if (ret.status !== 0) {
clientNotificationManager.showBitbakeError('Command "which bitbake" failed: \n' + ret.stdout.toString() + ret.stderr.toString())
const errorMsg = `Command "${command}" returned ${ret.status}.\n See Bitbake Terminal for command output.`
// The BitbakeTerminal focuses on it's own on error
clientNotificationManager.showBitbakeError(errorMsg)
return false
}

return true
}

composeBitbakeCommand (bitbakeTaskDefinition: BitbakeTaskDefinition): string {
function appendCommandParam (command: string, param: string): string {
return command + ' ' + param
}

let command = 'bitbake'

bitbakeTaskDefinition.recipes?.forEach(recipe => {
command = appendCommandParam(command, `${recipe}`)
})
if (bitbakeTaskDefinition.task !== undefined) {
command = appendCommandParam(command, `-c ${bitbakeTaskDefinition.task}`)
}
if (bitbakeTaskDefinition.options?.continue === true) {
command = appendCommandParam(command, '-k')
}
if (bitbakeTaskDefinition.options?.force === true) {
command = appendCommandParam(command, '-f')
}
if (bitbakeTaskDefinition.options?.parseOnly === true) {
command = appendCommandParam(command, '-p')
}

return command
}
}
2 changes: 1 addition & 1 deletion client/src/lib/src/BitbakeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function expandWorkspaceFolder (configuration: string, workspaceFolder: string):
}

/// Santitize a string to be passed in a shell command (remove special characters)
function sanitizeForShell (command: string | undefined): string | undefined {
export function sanitizeForShell (command: string | undefined): string | undefined {
if (command === undefined) {
return undefined
}
Expand Down
37 changes: 37 additions & 0 deletions client/src/lib/src/utils/ProcessUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* --------------------------------------------------------------------------------------------
* 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 type childProcess from 'child_process'

/// Wait for an asynchronous process to finish and return its output
export async function finishProcessExecution (process: Promise<childProcess.ChildProcess>): Promise<childProcess.SpawnSyncReturns<Buffer>> {
return await new Promise<childProcess.SpawnSyncReturns<Buffer>>((resolve, reject) => {
process.then((child) => {
let stdout = ''
let stderr = ''
child.stdout?.on('data', (data) => {
stdout += data
})
child.stderr?.on('data', (data) => {
stderr += data
})
child.on('close', (code) => {
const stdoutBuffer = Buffer.from(stdout)
const stderrBuffer = Buffer.from(stderr)
resolve({
pid: -1,
output: [stdoutBuffer, stderrBuffer],
stdout: stdoutBuffer,
stderr: stderrBuffer,
status: code,
signal: null
})
})
},
(error) => {
reject(error)
})
})
}
73 changes: 39 additions & 34 deletions client/src/ui/BitbakeCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@ import * as vscode from 'vscode'

import { logger } from '../lib/src/utils/OutputLogger'
import { type BitbakeWorkspace } from './BitbakeWorkspace'
import { type BitbakeTaskProvider } from './BitbakeTaskProvider'
import path from 'path'
import { BitbakeRecipeTreeItem } from './BitbakeRecipesView'
import { type BitBakeProjectScanner } from '../driver/BitBakeProjectScanner'
import { extractRecipeName } from '../lib/src/utils/files'
import { runBitbakeTerminal } from './BitbakeTerminal'
import { type BitbakeDriver } from '../driver/BitbakeDriver'
import { sanitizeForShell } from '../lib/src/BitbakeSettings'
import { type BitbakeTaskDefinition, type BitbakeTaskProvider } from './BitbakeTaskProvider'

let parsingPending = false

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, uri) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.clean-recipe', async (uri) => { await cleanRecipeCommand(bitbakeWorkspace, bitbakeTaskProvider, uri) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.run-task', async (uri, task) => { await runTaskCommand(bitbakeWorkspace, bitbakeTaskProvider, uri, task) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.build-recipe', async (uri) => { await buildRecipeCommand(bitbakeWorkspace, bitbakeTaskProvider.bitbakeDriver, uri) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.clean-recipe', async (uri) => { await cleanRecipeCommand(bitbakeWorkspace, bitbakeTaskProvider.bitbakeDriver, uri) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.run-task', async (uri, task) => { await runTaskCommand(bitbakeWorkspace, bitbakeTaskProvider.bitbakeDriver, uri, task) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.drop-recipe', async (uri) => { await dropRecipe(bitbakeWorkspace, uri) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.watch-recipe', async (recipe) => { await addActiveRecipe(bitbakeWorkspace, recipe) }))
context.subscriptions.push(vscode.commands.registerCommand('bitbake.rescan-project', async () => { await rescanProject(bitbakeProjectScanner) }))

// Handles enqueued parsing requests (onSave)
context.subscriptions.push(
vscode.tasks.onDidEndTask((e) => {
if (e.execution.task.name === 'Parse all recipes') {
if (e.execution.task.name === 'Bitbake: Parse') {
if (parsingPending) {
parsingPending = false
void parseAllrecipes(bitbakeWorkspace, bitbakeTaskProvider)
Expand All @@ -46,50 +49,52 @@ async function parseAllrecipes (bitbakeWorkspace: BitbakeWorkspace, taskProvider
return
}

// We have to use tasks instead of BitbakeTerminal because we want the problemMatchers to detect parsing errors
const parseAllRecipesTask = new vscode.Task(
{ type: 'bitbake', options: { parseOnly: true } },
vscode.TaskScope.Workspace,
'Parse all recipes',
'Bitbake: Parse',
'bitbake'
)
const runningTasks = vscode.tasks.taskExecutions
if (runningTasks.some((execution) => execution.task.name === parseAllRecipesTask.name)) {
logger.debug('Parse all recipes task is already running')
logger.debug('Bitbake parsing task is already running')
parsingPending = true
return
}
await runBitbakeTask(parseAllRecipesTask, taskProvider)
}

async function buildRecipeCommand (bitbakeWorkspace: BitbakeWorkspace, taskProvider: vscode.TaskProvider, uri?: any): Promise<void> {
async function buildRecipeCommand (bitbakeWorkspace: BitbakeWorkspace, bitbakeDriver: BitbakeDriver, uri?: any): Promise<void> {
const chosenRecipe = await selectRecipe(bitbakeWorkspace, uri)
if (chosenRecipe !== undefined) {
logger.debug(`Command: build-recipe: ${chosenRecipe}`)
const task = new vscode.Task(
{ type: 'bitbake', recipes: [chosenRecipe] },
vscode.TaskScope.Workspace,
`Build recipe: ${chosenRecipe}`,
'bitbake'
)
await runBitbakeTask(task, taskProvider)
await runBitbakeTerminal(
bitbakeDriver,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
{
recipes: [chosenRecipe]
} as BitbakeTaskDefinition,
`Bitbake: Build: ${chosenRecipe}`)
}
}

async function cleanRecipeCommand (bitbakeWorkspace: BitbakeWorkspace, taskProvider: vscode.TaskProvider, uri?: any): Promise<void> {
async function cleanRecipeCommand (bitbakeWorkspace: BitbakeWorkspace, bitbakeDriver: BitbakeDriver, uri?: any): Promise<void> {
const chosenRecipe = await selectRecipe(bitbakeWorkspace, uri)
if (chosenRecipe !== undefined) {
logger.debug(`Command: clean-recipe: ${chosenRecipe}`)
const task = new vscode.Task(
{ type: 'bitbake', recipes: [chosenRecipe], task: 'clean' },
vscode.TaskScope.Workspace,
`Clean recipe: ${chosenRecipe}`,
'bitbake'
)
await runBitbakeTask(task, taskProvider)
await runBitbakeTerminal(
bitbakeDriver,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
{
recipes: [chosenRecipe],
task: 'clean'
} as BitbakeTaskDefinition,
`Bitbake: Clean: ${chosenRecipe}`)
}
}

async function runTaskCommand (bitbakeWorkspace: BitbakeWorkspace, taskProvider: vscode.TaskProvider, uri?: any, task?: any): Promise<void> {
async function runTaskCommand (bitbakeWorkspace: BitbakeWorkspace, bitbakeDriver: BitbakeDriver, uri?: any, task?: any): Promise<void> {
const chosenRecipe = await selectRecipe(bitbakeWorkspace, uri)
if (chosenRecipe !== undefined) {
let chosenTask: string | undefined
Expand All @@ -99,21 +104,21 @@ async function runTaskCommand (bitbakeWorkspace: BitbakeWorkspace, taskProvider:
chosenTask = await selectTask()
}
if (chosenTask !== undefined) {
logger.debug(`Command: run-task: ${chosenRecipe} -c ${chosenTask}`)
const task = new vscode.Task(
{ type: 'bitbake', recipes: [chosenRecipe], task: chosenTask },
vscode.TaskScope.Workspace,
`Run task ${chosenTask} for ${chosenRecipe}`,
'bitbake'
)
await runBitbakeTask(task, taskProvider)
logger.debug(`Command: run-task: ${chosenRecipe}, ${chosenTask}`)
await runBitbakeTerminal(bitbakeDriver,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
{
recipes: [chosenRecipe],
task: chosenTask
} as BitbakeTaskDefinition,
`Bitbake: Task: ${chosenTask}: ${chosenRecipe}`)
}
}
}

async function selectTask (): Promise<string | undefined> {
const chosenTask = await vscode.window.showInputBox({ placeHolder: 'Bitbake task to run (bitbake -c)' })
return chosenTask
return sanitizeForShell(chosenTask)
}

async function selectRecipe (bitbakeWorkspace: BitbakeWorkspace, uri?: any, canCreate: boolean = true): Promise<string | undefined> {
Expand Down Expand Up @@ -154,7 +159,7 @@ async function addActiveRecipe (bitbakeWorkspace: BitbakeWorkspace, recipe?: str
}
let chosenRecipe = await vscode.window.showInputBox({ placeHolder: 'Recipe name to add' })
if (chosenRecipe !== undefined) {
chosenRecipe = extractRecipeName(chosenRecipe) as string
chosenRecipe = sanitizeForShell(extractRecipeName(chosenRecipe) as string) as string
bitbakeWorkspace.addActiveRecipe(chosenRecipe)
}
return chosenRecipe
Expand Down
8 changes: 4 additions & 4 deletions client/src/ui/BitbakeStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode'

import { type BitbakeScanResult } from '../lib/src/types/BitbakeScanResult'
import { lastParsingExitCode } from './BitbakeTaskProvider'
import { type BitbakeCustomExecution } from './BitbakeTaskProvider'
import { type BitBakeProjectScanner } from '../driver/BitBakeProjectScanner'

export class BitbakeStatusBar {
Expand Down Expand Up @@ -34,15 +34,15 @@ export class BitbakeStatusBar {
})

vscode.tasks.onDidStartTask((e: vscode.TaskStartEvent) => {
if (e.execution.task.name === 'Parse all recipes' && e.execution.task.source === 'bitbake') {
if (e.execution.task.name === 'Bitbake: Parse' && e.execution.task.source === 'bitbake') {
this.parsingInProgress = true
this.updateStatusBar()
}
})
vscode.tasks.onDidEndTask((e: vscode.TaskEndEvent) => {
if (e.execution.task.name === 'Parse all recipes' && e.execution.task.source === 'bitbake') {
if (e.execution.task.name === 'Bitbake: Parse' && e.execution.task.source === 'bitbake') {
this.parsingInProgress = false
this.scanExitCode = lastParsingExitCode
this.scanExitCode = (e.execution.task.execution as BitbakeCustomExecution).pty?.lastExitCode ?? -1
this.updateStatusBar()
}
})
Expand Down
Loading
Loading