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

Chore: Add notification on BitBake not found #16

Merged
merged 1 commit into from
Oct 12, 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
50 changes: 50 additions & 0 deletions client/src/ClientNotificationManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type Memento, commands, window } from 'vscode'
import { type LanguageClient, type Disposable } from 'vscode-languageclient/node'

export class ClientNotificationManager {
private readonly _client: LanguageClient
private readonly _memento: Memento

constructor (client: LanguageClient, memento: Memento) {
this._client = client
this._memento = memento
}

buildHandlers (): Disposable[] {
const handlers = [
this.buildBitBakeNotFoundHandler()
]

return handlers
}

private buildBitBakeNotFoundHandler (): Disposable {
deribaucourt marked this conversation as resolved.
Show resolved Hide resolved
const isNeverShowAgain = this.checkIsNeverShowAgain('custom/bitBakeNotFound')
if (isNeverShowAgain) {
return { dispose: () => {} }
}
return this._client.onNotification('custom/bitBakeNotFound', () => {
void window.showErrorMessage(
'BitBake folder could not be found. Please set its path in the settings. Optionally, also set an environment script.',
'Open Settings',
'Close',
'Never Show Again'
)
.then((item) => {
if (item === 'Open Settings') {
void commands.executeCommand('workbench.action.openSettings', 'bitbake')
} else if (item === 'Never Show Again') {
void this.neverShowAgain('custom/bitBakeNotFound')
}
})
})
}

private neverShowAgain (method: string): Thenable<void> {
return this._memento.update(`neverShowAgain/${method}`, true)
}

private checkIsNeverShowAgain (method: string): boolean {
return this._memento.get(`neverShowAgain/${method}`, false)
}
}
3 changes: 3 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { ExtensionContext } from 'vscode'
import type { LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node'

// import { legend, BitBakeDocumentSemanticTokensProvider } from './BitBakeDocumentSemanticTokensProvider'
import { ClientNotificationManager } from './ClientNotificationManager'

let client: LanguageClient
export async function activate (context: ExtensionContext): Promise<void> {
Expand Down Expand Up @@ -55,6 +56,8 @@ export async function activate (context: ExtensionContext): Promise<void> {
await client.start()

// context.subscriptions.push(languages.registerDocumentSemanticTokensProvider({ language: 'bitbake', scheme: 'file' }, new BitBakeDocumentSemanticTokensProvider(), legend))
const notificationManager = new ClientNotificationManager(client, context.globalState)
context.subscriptions.push(...notificationManager.buildHandlers())
}

export function deactivate (): Thenable<void> | undefined {
Expand Down
9 changes: 8 additions & 1 deletion server/src/BitBakeDocScanner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import fs from 'fs'
import logger from 'winston'

type SuffixType = 'layer' | 'providedItem' | undefined

Expand Down Expand Up @@ -50,7 +51,13 @@ export class BitBakeDocScanner {
}

parse (pathToBitbakeFolder: string): void {
const file = fs.readFileSync(path.join(pathToBitbakeFolder, variablesFolder), 'utf8')
let file = ''
const docPath = path.join(pathToBitbakeFolder, variablesFolder)
try {
file = fs.readFileSync(docPath, 'utf8')
} catch {
logger.warn(`BitBake doc file not found at ${docPath}`)
}
for (const match of file.matchAll(variablesRegexForDoc)) {
const name = match.groups?.name
// Naive silly inneficient incomplete conversion to markdown
Expand Down
13 changes: 10 additions & 3 deletions server/src/BitBakeProjectScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
import {
OutputParser
} from './OutputParser'
import { ServerNotificationManager } from './ServerNotificationManager'

interface ScannStatus {
scanIsRunning: boolean
Expand All @@ -42,13 +43,15 @@ export class BitBakeProjectScanner {
private _includes: ElementInfo[] = new Array < ElementInfo >()
private _recipes: ElementInfo[] = new Array < ElementInfo >()
private readonly _outputParser: OutputParser
private readonly _notificationManager: ServerNotificationManager
private _shouldDeepExamine: boolean = false
private _pathToBuildFolder: string = 'build'
private _pathToEnvScript: string = 'oe-init-build-env'
private _pathToBitbakeFolder: string = 'bitbake'

constructor (connection: Connection) {
this._outputParser = new OutputParser(connection)
this._notificationManager = new ServerNotificationManager(connection)
}

private readonly _scanStatus: ScannStatus = {
Expand Down Expand Up @@ -347,9 +350,13 @@ export class BitBakeProjectScanner {
const commandResult = this.executeCommand(scriptContent)

if (commandResult.status === 127) {
// Likely "bitbake: not found"
// TODO: Show a proper error message to help the user configuring the extension
throw new Error(commandResult.output.toString())
const output = commandResult.stderr.toString()
if (output.includes('bitbake')) {
// Seems like Bitbake could not be found.
// Are we sure this is the actual error?
this._notificationManager.sendBitBakeNotFound()
throw new Error(output)
}
}

return commandResult
Expand Down
20 changes: 20 additions & 0 deletions server/src/ServerNotificationManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type Connection } from 'vscode-languageserver'

export type NotificationType =
'custom/bitBakeNotFound'

export class ServerNotificationManager {
private readonly _connection: Connection

constructor (connection: Connection) {
this._connection = connection
}

send (type: NotificationType, message?: string): void {
void this._connection.sendNotification(type, message)
}

sendBitBakeNotFound (): void {
this.send('custom/bitBakeNotFound')
}
}