Skip to content

Commit

Permalink
feat(SystemExecutor): add outputchannel field to log when downloading…
Browse files Browse the repository at this point in the history
… the scanner

Signed-off-by: Jinan Jeong <[email protected]>
  • Loading branch information
incipienstation committed Jul 31, 2024
1 parent bd1b5e7 commit 20a51ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
33 changes: 17 additions & 16 deletions fosslight-scanner/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { removeAnsiEscapeCodes } from "./utils/parseLog";
let outputChannel = vscode.window.createOutputChannel("Fosslight Scanner");

export async function activate(context: vscode.ExtensionContext) {
const systemExecuter = SystemExecuter.getInstance();
const systemExecuter = SystemExecuter.getInstance({ outputChannel });
await systemExecuter.setUpVenv();
console.log(
'Congratulations, your extension "fosslight-scanner-extension" is now active!'
Expand All @@ -26,7 +26,7 @@ export async function activate(context: vscode.ExtensionContext) {
title: "Running Fosslight Scanner...",
cancellable: false,
},
async (progress) => {
async (_) => {
const handleLog = (log: string) => {
outputChannel.appendLine(removeAnsiEscapeCodes(log));
};
Expand All @@ -39,35 +39,40 @@ export async function activate(context: vscode.ExtensionContext) {
};

const printOutput = async (outputDirPath: string) => {
const outputFilePath = (
await findFiles(outputDirPath, /fosslight_report_all_\d{6}_\d{4}\.yaml/)
).sort((a, b) => b.localeCompare(a))[0];
const filePaths = await findFilePaths(
outputDirPath,
/fosslight_report_all_\d{6}_\d{4}\.yaml/
);
const outputFilePath = filePaths.sort((a, b) => b.localeCompare(a))[0];
console.log("Found the latest report file: ", outputFilePath);
const output = fs.readFileSync(outputFilePath, "utf8");
outputChannel.appendLine("Analysis result:\n");
outputChannel.appendLine(output);
};

const findFiles = async (dir: string, regex: RegExp): Promise<string[]> => {
let files: string[] = [];
const findFilePaths = async (
dir: string,
regex: RegExp
): Promise<string[]> => {
let filePaths: string[] = [];
const entries = await fse.readdir(dir);

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

if (stat.isDirectory()) {
files = files.concat(await findFiles(entryPath, regex));
filePaths = filePaths.concat(await findFilePaths(entryPath, regex));
} else if (path.basename(entry).match(regex)) {
files.push(entryPath);
filePaths.push(entryPath);
}
}

return files;
return filePaths;
};

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

vscode.window.showInformationMessage(
" Analysis completed on the root directory."
"Analysis completed on the root directory."
);

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

try {
if (await fse.exists(tempDirPath)) {
throw new Error("Temporary directory './.temp' already exists.");
}
// Create the temporary directory
await fse.emptyDir(tempDirPath);

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

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

const args = commandParser.parseCmd2Args({
Expand Down
28 changes: 22 additions & 6 deletions fosslight-scanner/src/services/SystemExecuter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as fs from "fs";
import * as path from "path";
import * as util from "util";
import * as vscode from "vscode";
import { exec, spawn, ChildProcess } from "child_process";

interface SystemExecuterProps {
outputChannel: vscode.OutputChannel;
}

class SystemExecuter {
private static instance: SystemExecuter;
private readonly venvPath = path.join(__dirname, "resources", "venv");
Expand All @@ -16,21 +21,32 @@ class SystemExecuter {
: path.join(this.venvPath, "bin", "activate");
private logHandlers: ((data: any) => void)[] = [];
private child: ChildProcess | null = null;
private outputChannel: vscode.OutputChannel;

private constructor() {}
private constructor({ outputChannel }: SystemExecuterProps) {
this.outputChannel = outputChannel;
}

public static getInstance(): SystemExecuter {
public static getInstance({
outputChannel,
}: {
outputChannel: vscode.OutputChannel;
}): SystemExecuter {
if (!SystemExecuter.instance) {
SystemExecuter.instance = new SystemExecuter();
SystemExecuter.instance = new SystemExecuter({ outputChannel });
}
return SystemExecuter.instance;
}

public async setUpVenv() {
const arg = !this.checkVenv() ? "false" : undefined; // assign any string is fine
console.log("Waiting for setting venv and Fosslight Scanner");
this.outputChannel.clear();
this.outputChannel.show();
this.outputChannel.appendLine(
"Waiting for setting venv and Fosslight Scanner"
);
const progressInterval = setInterval(() => {
process.stdout.write(".");
this.outputChannel.append(".");
}, 500); // print '.' every 500ms while setting

// Will take a long time (about 3 min) when the first install the venv and fs.
Expand All @@ -40,7 +56,7 @@ class SystemExecuter {
"[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."
);
} else {
console.log("Fosslight Scanner is ready to use.");
this.outputChannel.appendLine("Fosslight Scanner is ready to use.");
}
clearInterval(progressInterval); // stop printing '.'
}
Expand Down

0 comments on commit 20a51ee

Please sign in to comment.