Skip to content

Commit 20a51ee

Browse files
feat(SystemExecutor): add outputchannel field to log when downloading the scanner
Signed-off-by: Jinan Jeong <[email protected]>
1 parent bd1b5e7 commit 20a51ee

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

fosslight-scanner/src/extension.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { removeAnsiEscapeCodes } from "./utils/parseLog";
1313
let outputChannel = vscode.window.createOutputChannel("Fosslight Scanner");
1414

1515
export async function activate(context: vscode.ExtensionContext) {
16-
const systemExecuter = SystemExecuter.getInstance();
16+
const systemExecuter = SystemExecuter.getInstance({ outputChannel });
1717
await systemExecuter.setUpVenv();
1818
console.log(
1919
'Congratulations, your extension "fosslight-scanner-extension" is now active!'
@@ -26,7 +26,7 @@ export async function activate(context: vscode.ExtensionContext) {
2626
title: "Running Fosslight Scanner...",
2727
cancellable: false,
2828
},
29-
async (progress) => {
29+
async (_) => {
3030
const handleLog = (log: string) => {
3131
outputChannel.appendLine(removeAnsiEscapeCodes(log));
3232
};
@@ -39,35 +39,40 @@ export async function activate(context: vscode.ExtensionContext) {
3939
};
4040

4141
const printOutput = async (outputDirPath: string) => {
42-
const outputFilePath = (
43-
await findFiles(outputDirPath, /fosslight_report_all_\d{6}_\d{4}\.yaml/)
44-
).sort((a, b) => b.localeCompare(a))[0];
42+
const filePaths = await findFilePaths(
43+
outputDirPath,
44+
/fosslight_report_all_\d{6}_\d{4}\.yaml/
45+
);
46+
const outputFilePath = filePaths.sort((a, b) => b.localeCompare(a))[0];
4547
console.log("Found the latest report file: ", outputFilePath);
4648
const output = fs.readFileSync(outputFilePath, "utf8");
4749
outputChannel.appendLine("Analysis result:\n");
4850
outputChannel.appendLine(output);
4951
};
5052

51-
const findFiles = async (dir: string, regex: RegExp): Promise<string[]> => {
52-
let files: string[] = [];
53+
const findFilePaths = async (
54+
dir: string,
55+
regex: RegExp
56+
): Promise<string[]> => {
57+
let filePaths: string[] = [];
5358
const entries = await fse.readdir(dir);
5459

5560
for (const entry of entries) {
5661
const entryPath = path.join(dir, entry);
5762
const stat = await fse.stat(entryPath);
5863

5964
if (stat.isDirectory()) {
60-
files = files.concat(await findFiles(entryPath, regex));
65+
filePaths = filePaths.concat(await findFilePaths(entryPath, regex));
6166
} else if (path.basename(entry).match(regex)) {
62-
files.push(entryPath);
67+
filePaths.push(entryPath);
6368
}
6469
}
6570

66-
return files;
71+
return filePaths;
6772
};
6873

6974
const runCompareModeIfSbomExists = async (folderPath: string) => {
70-
const sbomFiles = await findFiles(folderPath, /sbom-info.yaml/);
75+
const sbomFiles = await findFilePaths(folderPath, /sbom-info.yaml/);
7176
if (sbomFiles.length > 0) {
7277
for (const sbomFilePath of sbomFiles) {
7378
console.log(
@@ -144,7 +149,7 @@ export async function activate(context: vscode.ExtensionContext) {
144149
await runFosslightScanner(yamlArgs);
145150

146151
vscode.window.showInformationMessage(
147-
" Analysis completed on the root directory."
152+
"Analysis completed on the root directory."
148153
);
149154

150155
const outputDirPath = path.join(rootDirPath, "fosslight_report");
@@ -177,16 +182,12 @@ export async function activate(context: vscode.ExtensionContext) {
177182
const tempFilePath = path.join(tempDirPath, path.basename(filePath));
178183

179184
try {
180-
if (await fse.exists(tempDirPath)) {
181-
throw new Error("Temporary directory './.temp' already exists.");
182-
}
183185
// Create the temporary directory
184186
await fse.emptyDir(tempDirPath);
185187

186188
// Copy the current file to the temporary directory
187189
await fse.copy(filePath, tempFilePath);
188190

189-
console.log("Copied file to temporary directory: ", tempFilePath);
190191
outputChannel.appendLine(`Analysis Subject: ${filePath}\n`);
191192

192193
const args = commandParser.parseCmd2Args({

fosslight-scanner/src/services/SystemExecuter.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import * as util from "util";
4+
import * as vscode from "vscode";
45
import { exec, spawn, ChildProcess } from "child_process";
56

7+
interface SystemExecuterProps {
8+
outputChannel: vscode.OutputChannel;
9+
}
10+
611
class SystemExecuter {
712
private static instance: SystemExecuter;
813
private readonly venvPath = path.join(__dirname, "resources", "venv");
@@ -16,21 +21,32 @@ class SystemExecuter {
1621
: path.join(this.venvPath, "bin", "activate");
1722
private logHandlers: ((data: any) => void)[] = [];
1823
private child: ChildProcess | null = null;
24+
private outputChannel: vscode.OutputChannel;
1925

20-
private constructor() {}
26+
private constructor({ outputChannel }: SystemExecuterProps) {
27+
this.outputChannel = outputChannel;
28+
}
2129

22-
public static getInstance(): SystemExecuter {
30+
public static getInstance({
31+
outputChannel,
32+
}: {
33+
outputChannel: vscode.OutputChannel;
34+
}): SystemExecuter {
2335
if (!SystemExecuter.instance) {
24-
SystemExecuter.instance = new SystemExecuter();
36+
SystemExecuter.instance = new SystemExecuter({ outputChannel });
2537
}
2638
return SystemExecuter.instance;
2739
}
2840

2941
public async setUpVenv() {
3042
const arg = !this.checkVenv() ? "false" : undefined; // assign any string is fine
31-
console.log("Waiting for setting venv and Fosslight Scanner");
43+
this.outputChannel.clear();
44+
this.outputChannel.show();
45+
this.outputChannel.appendLine(
46+
"Waiting for setting venv and Fosslight Scanner"
47+
);
3248
const progressInterval = setInterval(() => {
33-
process.stdout.write(".");
49+
this.outputChannel.append(".");
3450
}, 500); // print '.' every 500ms while setting
3551

3652
// Will take a long time (about 3 min) when the first install the venv and fs.
@@ -40,7 +56,7 @@ class SystemExecuter {
4056
"[Error]: Failed to set venv and install Fosslight Scanner.\n\t Please check the resources folder and files are in initial condition.\n\t Or try to reinstall this app."
4157
);
4258
} else {
43-
console.log("Fosslight Scanner is ready to use.");
59+
this.outputChannel.appendLine("Fosslight Scanner is ready to use.");
4460
}
4561
clearInterval(progressInterval); // stop printing '.'
4662
}

0 commit comments

Comments
 (0)