Skip to content

Commit

Permalink
chore: Added commands to toggle Lock File Parsing in the title bar. (#…
Browse files Browse the repository at this point in the history
…124)

* chore: Added commands to toggle Lock File Parsing in the title bar.

* chore: Toggle Lock File Parsing commands added to title bar

* chore: Update lock file parsing commands to use language-specific settings

* chore: Changed some naming
  • Loading branch information
kadirkaang authored Sep 5, 2024
1 parent f10635f commit d266aee
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 11 deletions.
8 changes: 8 additions & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to the "dependi" extension will be documented in this file.

## [v0.7.9](https://github.com/filllabs/dependi/compare/v0.7.8...v0.7.9)

### New Features

- Commands to toggle Lock File Parsing in the title bar.
- ![Lock Icon](https://www.dependi.io/screenshots/lock.png) Lock File Parsing enabled.
- ![Unlock Icon](https://www.dependi.io/screenshots/unlock.png) Lock File Parsing disabled.
- ![Doc-Lock Icon](https://www.dependi.io/screenshots/lock-doc.png) A lock file has been found and parsed.


### Improvements

- New advanced vulnerability report. (_Only for [Pro](https://dependi.io) version._)
Expand Down
36 changes: 36 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@
],
"menus": {
"editor/title": [
{
"when": "dependi.hasLockFile && resourceFilename != 'go.mod' && resourceFilename != 'requirements.txt'",
"command": "dependi.commands.lockFileParsed",
"group": "navigation"
},
{
"when": "!dependi.hasLockFile && dependi.isLockFileEnabled && resourceFilename in dependi.supportedFiles && resourceFilename != 'go.mod' && resourceFilename != 'requirements.txt'",
"command": "dependi.commands.disableLockFileParsing",
"group": "navigation"
},
{
"when": "!dependi.hasLockFile && !dependi.isLockFileEnabled && resourceFilename in dependi.supportedFiles && resourceFilename != 'go.mod' && resourceFilename != 'requirements.txt'",
"command": "dependi.commands.enableLockFileParsing",
"group": "navigation"
},
{
"when": "resourceFilename in dependi.supportedFiles",
"command": "dependi.commands.vulnerability.report",
Expand Down Expand Up @@ -200,6 +215,27 @@
"dark": "./icons/vuln-rep-dark.svg",
"light": "./icons/vuln-rep-light.svg"
}
},
{
"command": "dependi.commands.enableLockFileParsing",
"title": "Enable Lock File Parsing",
"category": "Dependi",
"icon": "$(unlock)"
},
{
"command": "dependi.commands.disableLockFileParsing",
"title": "Disable Lock File Parsing",
"category": "Dependi",
"icon": "$(lock)"
},
{
"command": "dependi.commands.lockFileParsed",
"title": "Disable Lock File Parsing",
"category": "Dependi",
"icon": {
"dark": "./icons/lock-doc-dark.svg",
"light": "./icons/lock-doc-light.svg"
}
}
],
"configuration": [
Expand Down
33 changes: 33 additions & 0 deletions vscode/src/commands/lock-file/disableLockFileParsing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { commands, TextEditor, TextEditorEdit, workspace } from "vscode";
import { Configs } from "../../config";
import listener from "../../core/listeners";
import { CurrentLanguage, Language } from "../../core/Language";
const config = workspace.getConfiguration("dependi");

export const disableLockFileParsing = commands.registerTextEditorCommand(
Configs.DISABLE_LOCK_FILE_PARSING,
(editor: TextEditor, edit: TextEditorEdit, info) => {
if (editor) {
switch (CurrentLanguage) {
case Language.Rust:
config.update(Configs.RUST_ENABLED_LOCK_FILE, false);
break;
case Language.JS:
config.update(Configs.NPM_ENABLED_LOCK_FILE, false);
break;
case Language.PHP:
config.update(Configs.PHP_ENABLED_LOCK_FILE, false);
break;
case Language.Python:
config.update(Configs.PYTHON_ENABLED_LOCK_FILE, false);
break;
default:
break;
}
commands.executeCommand("setContext", "dependi.isLockFileEnabled", false);
setTimeout(() => {
listener(editor);
}, 1000);
}
}
);
34 changes: 34 additions & 0 deletions vscode/src/commands/lock-file/enableLockFileParsing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { commands, TextEditor, TextEditorEdit, workspace } from "vscode";
import listener from "../../core/listeners";
import { Configs } from "../../config";
import { CurrentLanguage, Language } from "../../core/Language";

const config = workspace.getConfiguration("dependi");

export const enableLockFileParsing = commands.registerTextEditorCommand(
Configs.ENABLE_LOCK_FILE_PARSING,
(editor: TextEditor, edit: TextEditorEdit, info) => {
if (editor) {
switch (CurrentLanguage) {
case Language.Rust:
config.update(Configs.RUST_ENABLED_LOCK_FILE, true);
break;
case Language.JS:
config.update(Configs.NPM_ENABLED_LOCK_FILE, true);
break;
case Language.PHP:
config.update(Configs.PHP_ENABLED_LOCK_FILE, true);
break;
case Language.Python:
config.update(Configs.PYTHON_ENABLED_LOCK_FILE, true);
break;
default:
break;
}
commands.executeCommand("setContext", "dependi.isLockFileEnabled", true);
setTimeout(() => {
listener(editor);
}, 1000);
}
}
);
36 changes: 36 additions & 0 deletions vscode/src/commands/lock-file/lockFileParsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { commands, TextEditor, TextEditorEdit, workspace } from "vscode";
import listener from "../../core/listeners";
import { Configs } from "../../config";
import { CurrentLanguage, Language } from "../../core/Language";

const config = workspace.getConfiguration("dependi");

export const lockFileParsed = commands.registerTextEditorCommand(
Configs.LOCK_FILE_PARSED,
(editor: TextEditor, edit: TextEditorEdit, info) => {
if (editor) {
switch (CurrentLanguage) {
case Language.Rust:
config.update(Configs.RUST_ENABLED_LOCK_FILE, false);
break;
case Language.JS:
config.update(Configs.NPM_ENABLED_LOCK_FILE, false);
break;
case Language.PHP:
config.update(Configs.PHP_ENABLED_LOCK_FILE, false);
break;
case Language.Python:
config.update(Configs.PYTHON_ENABLED_LOCK_FILE, false);
break;
default:
break;
}

commands.executeCommand("setContext", "dependi.hasLockFile", false);
commands.executeCommand("setContext", "dependi.isLockFileEnabled", false);
setTimeout(() => {
listener(editor);
}, 1000);
}
}
);
3 changes: 3 additions & 0 deletions vscode/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export enum Configs {
// CREATE_CHANGELOG = `${DEPENDI}createChangelog`,
UPDATE_ALL = `${DEPENDI}commands.updateAll`,
RETRY = `${DEPENDI}commands.retry`,
ENABLE_LOCK_FILE_PARSING = `${DEPENDI}commands.enableLockFileParsing`,
DISABLE_LOCK_FILE_PARSING = `${DEPENDI}commands.disableLockFileParsing`,
LOCK_FILE_PARSED = `${DEPENDI}commands.lockFileParsed`,


//Storage
Expand Down
15 changes: 9 additions & 6 deletions vscode/src/core/Language.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import { sendTelemetry } from '../api/telemetry/telemetry';
import { env } from 'vscode';
import { commands, env } from 'vscode';
import { Settings } from '../config';

export enum Language {
None = 0,
Expand Down Expand Up @@ -30,25 +31,27 @@ export function setLanguage(file?: string) {
const filename = path.basename(file);
switch (filename.toLowerCase()) {
case "cargo.toml":
return setLanguageConfig(Language.Rust, "rust", filename, OCVEnvironment.Cratesio);
return setLanguageConfig(Language.Rust, "rust", filename, OCVEnvironment.Cratesio, Settings.rust.lockFileEnabled);
case "go.mod":
return setLanguageConfig(Language.Golang, "go", filename, OCVEnvironment.Go);
case "package.json":
return setLanguageConfig(Language.JS, "npm", filename, OCVEnvironment.Npm);
return setLanguageConfig(Language.JS, "npm", filename, OCVEnvironment.Npm, Settings.npm.lockFileEnabled);
case "requirements.txt":
return setLanguageConfig(Language.Python, "python", filename, OCVEnvironment.Pypi);
case "composer.json":
return setLanguageConfig(Language.PHP, "php", filename, OCVEnvironment.Packagist);
return setLanguageConfig(Language.PHP, "php", filename, OCVEnvironment.Packagist, Settings.php.lockFileEnabled);
case "pyproject.toml":
return setLanguageConfig(Language.Python, "python", filename, OCVEnvironment.Pypi);
return setLanguageConfig(Language.Python, "python", filename, OCVEnvironment.Pypi, Settings.python.lockFileEnabled);
}
};

function setLanguageConfig(language: Language, config: string, filename: string, OCVenv: OCVEnvironment) {
function setLanguageConfig(language: Language, config: string, filename: string, OCVenv: OCVEnvironment, isLockFileEnabled?: boolean) {
CurrentLanguage = language;
CurrentLanguageConfig = config;
CurrentEnvironment = OCVenv;
if (env.isTelemetryEnabled)
sendTelemetry({FileName: filename})
if (isLockFileEnabled !== undefined)
commands.executeCommand("setContext", "dependi.isEnableLockParsing", isLockFileEnabled);
return language;
}
3 changes: 2 additions & 1 deletion vscode/src/core/parsers/JsonParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TextDocument, TextLine, window } from "vscode";
import { commands, TextDocument, TextLine, window } from "vscode";
import Item from "../Item";
import { isQuote, shouldIgnoreLine } from "./utils";
import { Settings } from "../../config";
Expand Down Expand Up @@ -82,6 +82,7 @@ export class JsonParser {
const fileContent = fs.readFileSync(lockFilePath, "utf8");
const LockFileParser = this.lockParser;
item = LockFileParser.parse(fileContent, item);
commands.executeCommand("setContext", "dependi.hasLockFile", true);
}
} catch (err) {
console.error(err);
Expand Down
3 changes: 2 additions & 1 deletion vscode/src/core/parsers/TomlParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TextDocument, TextLine, window } from "vscode";
import { commands, TextDocument, TextLine, window } from "vscode";
import Item from "../Item";
import { Parser } from "./parser";
import { isQuote, shouldIgnoreLine } from "./utils";
Expand Down Expand Up @@ -286,6 +286,7 @@ function parseLockFile(item: Item[]): Item[] {
const fileContent = fs.readFileSync(lockFilePath, "utf8");
const LockFileParser = new TomlLockFileParser();
item = LockFileParser.parse(fileContent, item);
commands.executeCommand("setContext", "dependi.hasLockFile", true);
}
} catch (err) {
console.error(err);
Expand Down
7 changes: 7 additions & 0 deletions vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import listener from "./core/listeners";
import { ChangelogPanel } from "./panels/ChangelogPanel";
import { WelcomePagePanel } from "./panels/WelcomePanel";
import { ExtensionStorage } from "./storage";
import { disableLockFileParsing } from "./commands/lock-file/disableLockFileParsing";
import { enableLockFileParsing } from "./commands/lock-file/enableLockFileParsing";
import { lockFileParsed } from "./commands/lock-file/lockFileParsed";


export var Logger: OutputChannel;

Expand Down Expand Up @@ -83,6 +87,9 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(updateAll);
context.subscriptions.push(generateVulnerabilityReport(context));
context.subscriptions.push(generateCurrentVulnReport(context));
context.subscriptions.push(disableLockFileParsing);
context.subscriptions.push(enableLockFileParsing);
context.subscriptions.push(lockFileParsed);
}

export function deactivate() {
Expand Down
1 change: 1 addition & 0 deletions vscode/src/icons/lock-doc-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions vscode/src/icons/lock-doc-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions vscode/src/webviews/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export function markdownToHTML(markdown: string): string {
// Convert links to HTML
// Convert images to HTML
let html = markdown.replace(
/\[([^\]]+)\]\(([^)]+)\)/g,
'<a href="$2">$1</a>'
/!\[([^\]]+)\]\(([^)]+)\)/g,
'<img src="$2" alt="$1">'
);

// Convert links to HTML
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');

// Convert headings to HTML"
html = html.replace(/^#\s(.*)$/gm, "<h1>$1</h1>");
html = html.replace(/^##\s(.*)$/gm, "<h2>$1</h2>");
Expand Down

0 comments on commit d266aee

Please sign in to comment.