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

Refactor to a common bitbake lib #41

Merged
merged 11 commits into from
Nov 2, 2023
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/client/out/**/*.js"
"${workspaceRoot}/client/out/**/*.js",
],
"preLaunchTask": "watch:client"
},
Expand All @@ -23,7 +23,7 @@
"port": 6009,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/client/server/**/*.js"
"${workspaceRoot}/client/server/**/*.js",
],
"preLaunchTask": "watch:server"
},
Expand Down
27 changes: 27 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,8 @@
"devDependencies": {
"@types/node": "^20.6.2",
"@types/vscode": "^1.75.1"
}
},
"workspaces": [
"src/lib"
]
}
63 changes: 0 additions & 63 deletions client/src/__tests__/unit-tests/bitbake-settings.test.ts

This file was deleted.

26 changes: 16 additions & 10 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import * as vscode from 'vscode'
import { type LanguageClient } from 'vscode-languageclient/node'

import { ClientNotificationManager } from './ui/ClientNotificationManager'
import { logger } from './ui/OutputLogger'
import { logger } from './lib/src/utils/OutputLogger'
import { activateLanguageServer, deactivateLanguageServer } from './language/languageClient'
import { BitbakeDriver } from './driver/BitbakeDriver'
import { BitbakeDriver } from './lib/src/BitbakeDriver'
import { BitbakeTaskProvider } from './ui/BitbakeTaskProvider'
import { registerBitbakeCommands } from './ui/BitbakeCommands'
import { BitbakeWorkspace } from './ui/BitbakeWorkspace'
Expand All @@ -21,11 +21,16 @@ let taskProvider: vscode.Disposable
const bitbakeWorkspace: BitbakeWorkspace = new BitbakeWorkspace()
export let bitbakeExtensionContext: vscode.ExtensionContext

function loadLoggerSettings (): void {
logger.level = vscode.workspace.getConfiguration('bitbake').get('loggingLevel') ?? 'info'
logger.info('Bitbake logging level: ' + logger.level)
}

export async function activate (context: vscode.ExtensionContext): Promise<void> {
logger.setOutputChannel(vscode.window.createOutputChannel('BitBake'))
logger.loadSettings()
logger.outputChannel = vscode.window.createOutputChannel('BitBake')
loadLoggerSettings()
bitbakeExtensionContext = context
bitbakeDriver.loadSettings()
bitbakeDriver.loadSettings(vscode.workspace.getConfiguration('bitbake'), vscode.workspace.workspaceFolders?.[0].uri.fsPath)
bitbakeWorkspace.loadBitbakeWorkspace(context.workspaceState)
bitbakeTaskProvider = new BitbakeTaskProvider(bitbakeDriver)
client = await activateLanguageServer(context)
Expand All @@ -38,17 +43,17 @@ export async function activate (context: vscode.ExtensionContext): Promise<void>
// Handle settings change for bitbake driver
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('bitbake')) {
bitbakeDriver.loadSettings()
bitbakeDriver.loadSettings(vscode.workspace.getConfiguration('bitbake'), vscode.workspace.workspaceFolders?.[0].uri.fsPath)
logger.debug('Bitbake settings changed')
}
if (event.affectsConfiguration('bitbake.loggingLevel')) {
logger.loadSettings()
loadLoggerSettings()
}
}))
context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders((event) => {
logger.debug('Bitbake workspace changed: ' + JSON.stringify(event))
logger.loadSettings()
bitbakeDriver.loadSettings()
loadLoggerSettings()
bitbakeDriver.loadSettings(vscode.workspace.getConfiguration('bitbake'), vscode.workspace.workspaceFolders?.[0].uri.fsPath)
bitbakeWorkspace.loadBitbakeWorkspace(context.workspaceState)
}))

Expand All @@ -58,6 +63,7 @@ export async function activate (context: vscode.ExtensionContext): Promise<void>
}

export function deactivate (): Thenable<void> | undefined {
taskProvider.dispose()
taskProvider.dispose();
(logger.outputChannel as vscode.OutputChannel).dispose()
return deactivateLanguageServer(client)
}
19 changes: 19 additions & 0 deletions client/src/lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "vscode-bitbake-lib",
"description": "vscode-bitbake common library.",
"version": "1.1.2",
"author": "Savoir-faire Linux",
"license": "MIT",
"engines": {
"node": "*"
},
"bugs": {
"url": "https://github.com/savoirfairelinux/vscode-bitbake/issues"
},
"homepage": "https://github.com/savoirfairelinux/vscode-bitbake",
"repository": {
"type": "git",
"url": "https://github.com/savoirfairelinux/vscode-bitbake.git"
},
"scripts": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@

import childProcess from 'child_process'

import { logger } from '../ui/OutputLogger'
import { logger } from './utils/OutputLogger'
import { type BitbakeSettings, loadBitbakeSettings } from './BitbakeSettings'

/// This class is responsible for wrapping up all bitbake classes and exposing them to the extension
export class BitbakeDriver {
bitbakeSettings: BitbakeSettings = { pathToBitbakeFolder: '', pathToBuildFolder: '', pathToEnvScript: '' }

loadSettings (): void {
this.bitbakeSettings = loadBitbakeSettings()
loadSettings (settings: any, workspaceFolder: string = ''): void {
this.bitbakeSettings = loadBitbakeSettings(settings, workspaceFolder)
logger.debug('BitbakeDriver settings updated: ' + JSON.stringify(this.bitbakeSettings))
}

/// Execute a command in the bitbake environment
spawnBitbakeProcess (command: string): childProcess.ChildProcess {
const shell = process.env.SHELL ?? '/bin/sh'
const { shell, script } = this.prepareCommand(command)
logger.debug(`Executing Bitbake command: ${shell} -c ${script}`)
return childProcess.spawn(script, {
shell
})
}

command = this.composeBitbakeScript(command)
logger.debug(`Executing Bitbake command: ${shell} -c ${command}`)
return childProcess.spawn(command, {
/// Execute a command in the bitbake environment and wait for completion
spawnBitbakeProcessSync (command: string): childProcess.SpawnSyncReturns<Buffer> {
const { shell, script } = this.prepareCommand(command)
logger.debug(`Executing Bitbake command (sync): ${shell} -c ${command}`)
return childProcess.spawnSync(script, {
shell
})
}

private prepareCommand (command: string): {
shell: string
script: string
} {
const shell = process.env.SHELL ?? '/bin/sh'
const script = this.composeBitbakeScript(command)
return { shell, script }
}

private composeBitbakeScript (command: string): string {
let script = ''
command = sanitizeForShell(command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode'

import path from 'path'

/// Defines the context of a bitbake workspace with all information to call bitbake
Expand All @@ -14,25 +12,17 @@ export interface BitbakeSettings {
pathToEnvScript: string
}

export function loadBitbakeSettings (): BitbakeSettings {
const settings = vscode.workspace.getConfiguration('bitbake')

const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
let workspacePath = ''
if (workspaceFolder !== undefined) {
workspacePath = workspaceFolder.uri.fsPath
}

export function loadBitbakeSettings (settings: any, workspaceFolder: string = ''): BitbakeSettings {
/* eslint no-template-curly-in-string: "off" */
// The default values are defined in package.json
let pathToBitbakeFolder: string = settings.get<string>('pathToBitbakeFolder', '')
pathToBitbakeFolder = pathToBitbakeFolder.replace('${workspaceFolder}', workspacePath)
let pathToBitbakeFolder: string = settings.pathToBitbakeFolder
pathToBitbakeFolder = pathToBitbakeFolder.replace('${workspaceFolder}', workspaceFolder)
pathToBitbakeFolder = path.resolve(pathToBitbakeFolder)
let pathToBuildFolder: string = settings.get<string>('pathToBuildFolder', '')
pathToBuildFolder = pathToBuildFolder.replace('${workspaceFolder}', workspacePath)
let pathToBuildFolder: string = settings.pathToBuildFolder
pathToBuildFolder = pathToBuildFolder.replace('${workspaceFolder}', workspaceFolder)
pathToBuildFolder = path.resolve(pathToBuildFolder)
let pathToEnvScript: string = settings.get<string>('pathToEnvScript', '')
pathToEnvScript = pathToEnvScript.replace('${workspaceFolder}', workspacePath)
let pathToEnvScript: string = settings.pathToEnvScript
pathToEnvScript = pathToEnvScript.replace('${workspaceFolder}', workspaceFolder)
pathToEnvScript = path.resolve(pathToEnvScript)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,7 @@
* ------------------------------------------------------------------------------------------ */

import * as fs from 'fs'
import * as vscode from 'vscode'
import { BitbakeDriver } from '../../driver/BitbakeDriver'

jest.mock('vscode')

const mockBitbakeConfiguration = (values: Record<string, string>): void => {
vscode.workspace.getConfiguration = jest.fn().mockImplementation(() => ({
get: (key: string): string | undefined => {
if (key === undefined || values[key] === undefined) {
return ''
}
return values[key]
}
}))
}
import { BitbakeDriver } from '../../BitbakeDriver'

describe('BitbakeDriver Tests', () => {
it('should protect from shell injections', (done) => {
Expand All @@ -39,12 +25,12 @@ describe('BitbakeDriver Tests', () => {
const fakeEnvScriptPath = '/tmp/bitbake-vscode-test/envsetup.sh'
const fakeBuildPath = '/tmp/bitbake-vscode-test'

mockBitbakeConfiguration({
const driver = new BitbakeDriver()
driver.loadSettings({
pathToEnvScript: fakeEnvScriptPath,
pathToBuildFolder: fakeBuildPath
pathToBuildFolder: fakeBuildPath,
pathToBitbakeFolder: ''
})
const driver = new BitbakeDriver()
driver.loadSettings()

fs.mkdirSync(fakeBuildPath, { recursive: true })
const content = 'cd ' + fakeBuildPath + '; echo FINDME'
Expand Down
22 changes: 22 additions & 0 deletions client/src/lib/src/__tests__/driver/bitbake-settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* --------------------------------------------------------------------------------------------
* 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 { loadBitbakeSettings } from '../../BitbakeSettings'

describe('BitbakeSettings Tests', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('should expand workspaceFolder', () => {
const settings = loadBitbakeSettings({
pathToBitbakeFolder: '',
pathToEnvScript: '',
// eslint-disable-next-line no-template-curly-in-string
pathToBuildFolder: '${workspaceFolder}/build'
}, '/home/user/workspace')
expect(settings.pathToBuildFolder).toEqual('/home/user/workspace/build')
})
})
8 changes: 8 additions & 0 deletions client/src/lib/src/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

test('two plus four is six', () => {
expect(2 + 4).toBe(6)
})
Loading