Skip to content

Commit

Permalink
12 hover provider for manifest files (#26)
Browse files Browse the repository at this point in the history
* updated globalState and refactored

* fixed undefined of repoData

* added scan for manifest when user creates a new file
  • Loading branch information
rajpreet-s authored Jul 29, 2024
1 parent a218abe commit a5cc26a
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/commands/debrickedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import { DebrickedCommands, Organization } from "../constants/index";
import { BaseCommandService, ScanService, FileService } from "../services";
import { Logger, GlobalState, Common, ErrorHandler } from "../helpers";
import { ManifestWatcher } from "helpers/manifestWatcher";

export class DebrickedCommand {
private static get globalState(): GlobalState {
Expand Down Expand Up @@ -41,8 +42,7 @@ export class DebrickedCommand {
}

// Add file watcher for all files found from 'debricked files find'
const foundFiles = (await FileService.findFilesService()) || [];
await ScanService.addWatcherToManifestFiles(foundFiles, context);
await ManifestWatcher.getInstance().setupWatchers(context);
} catch (error) {
ErrorHandler.handleError(error);
} finally {
Expand Down
1 change: 0 additions & 1 deletion src/constants/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class Organization {
static readonly seqIdKey = "sequenceID";
static readonly access = "access";
static readonly accessTokenKey = "accessToken";
static readonly repoDataKey = "repoData";
static readonly bearer = "bearer";
static readonly bearerTokenKey = "bearerToken";
static readonly userId = "userId";
Expand Down
7 changes: 4 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export async function activate(context: vscode.ExtensionContext) {
GlobalState.initialize(context);

const globalState = GlobalState.getInstance();
// For dev - Clears the globalData - uncomment to clear the globalData
// await globalState.clearAllGlobalData();
globalState.setGlobalData(Organization.seqIdKey, Common.generateHashCode());
progress.report({
message: "Activating VS Code Extension",
Expand All @@ -42,10 +44,9 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.window.registerTreeDataProvider(Organization.debrickedCommand, debCommandsProvider);

const currentVersion = await BaseCommandService.getCurrentExtensionVersion();
const storedVersion = globalState.getGlobalData(Organization.extensionVersionKey, Organization.baseVersion);
const isFirstActivation = globalState.getGlobalData(Organization.isFirstActivationKey, true);
const debrickedData: any = globalState.getGlobalData(Organization.debrickedDataKey, {});

if (currentVersion !== storedVersion || isFirstActivation) {
if (currentVersion !== debrickedData.extensionVersion || debrickedData.isFirstActivation) {
globalState.setGlobalData(Organization.seqIdKey, Common.generateHashCode());
progress.report({
message: "Installing Debricked cli",
Expand Down
17 changes: 11 additions & 6 deletions src/helpers/AuthHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class AuthHelper {
static async getToken(useDefaultToken: boolean = true, tokenKey: "access" | "bearer"): Promise<string | undefined> {
try {
let token: string | undefined;
const TOKEN_KEY = tokenKey === "access" ? Organization.accessTokenKey : Organization.bearerTokenKey;
const TOKEN_KEY =
tokenKey === Organization.access ? Organization.accessTokenKey : Organization.bearerTokenKey;
const defaultAccessToken: any = await AuthHelper.globalState.getSecretData(TOKEN_KEY);

if (useDefaultToken) {
Expand All @@ -25,19 +26,23 @@ export class AuthHelper {
Logger.logInfo("InputBox Opened for tokens");

token = await ShowInputBoxHelper.promptForInput({
prompt: tokenKey === "access" ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
prompt:
tokenKey === Organization.access ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
ignoreFocusOut: true,
password: true,
title: tokenKey === "access" ? Messages.ACCESS_TOKEN : Messages.BEARER_TOKEN,
placeHolder: tokenKey === "access" ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
title: tokenKey === Organization.access ? Messages.ACCESS_TOKEN : Messages.BEARER_TOKEN,
placeHolder:
tokenKey === Organization.access ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
});

if (token) {
await AuthHelper.globalState.setSecretData(TOKEN_KEY, token);
const message = tokenKey === "access" ? Messages.ACCESS_TOKEN_SAVED : Messages.BEARER_TOKEN_SAVED;
const message =
tokenKey === Organization.access ? Messages.ACCESS_TOKEN_SAVED : Messages.BEARER_TOKEN_SAVED;
StatusBarMessageHelper.showInformationMessage(message);
} else {
const message = tokenKey === "access" ? Messages.ACCESS_TOKEN_RQD : Messages.BEARER_TOKEN_RQD;
const message =
tokenKey === Organization.access ? Messages.ACCESS_TOKEN_RQD : Messages.BEARER_TOKEN_RQD;
throw new Error(message);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/commandHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Command {
): Promise<string> {
if (accessTokenRequired) {
const flags = DebrickedCommands.getCommandSpecificFlags("Debricked") || [];
const accessToken = await AuthHelper.getToken(true, "access");
const accessToken = await AuthHelper.getToken(true, Organization.access);

if (accessToken) {
cmdParams.push(flags[0].flag);
Expand Down Expand Up @@ -57,7 +57,7 @@ export class Command {

if (accessTokenRequired) {
const flags = DebrickedCommands.getCommandSpecificFlags("Debricked") || [];
const accessToken = await AuthHelper.getToken(true, "access");
const accessToken = await AuthHelper.getToken(true, Organization.access);

if (accessToken) {
command = `${command} ${flags[0].flag} ${accessToken}`;
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/commonHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export class Common {
*/
public static async checkUserId(): Promise<void> {
try {
const userId = await Common.globalState.getGlobalDataByKey(
const userId = await Common.globalState.getGlobalData(
Organization.debrickedDataKey,
"",
Organization.userId,
);
if (!userId) {
Expand Down
20 changes: 10 additions & 10 deletions src/helpers/gitHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageStatus, Organization } from "../constants/index";
import { MessageStatus } from "../constants/index";
import { Command, GlobalState, Logger, ShowInputBoxHelper } from "../helpers";

export class GitHelper {
Expand Down Expand Up @@ -60,21 +60,21 @@ export class GitHelper {
public static async setupGit(): Promise<void> {
const currentRepo = await GitHelper.getUpstream();
Logger.logMessageByStatus(MessageStatus.INFO, `Current repository: ${currentRepo}`);
const repoData: any = await GitHelper.globalState.getGlobalData(Organization.repoDataKey, {});
const selectedRepoName: string = await GitHelper.getRepositoryName();
let repoData: any = await GitHelper.globalState.getGlobalData(selectedRepoName, {});

if (selectedRepoName) {
if (!repoData[selectedRepoName]) {
repoData[selectedRepoName] = {};
if (!repoData) {
repoData = {};
}
repoData[selectedRepoName].repositoryName = selectedRepoName;
repoData.repositoryName = selectedRepoName;
}

repoData[selectedRepoName].userName = await GitHelper.getUsername();
repoData[selectedRepoName].email = await GitHelper.getEmail();
repoData[selectedRepoName].currentBranch = await GitHelper.getCurrentBranch();
repoData[selectedRepoName].commitID = await GitHelper.getCommitHash();
repoData.userName = await GitHelper.getUsername();
repoData.email = await GitHelper.getEmail();
repoData.currentBranch = await GitHelper.getCurrentBranch();
repoData.commitID = await GitHelper.getCommitHash();

await GitHelper.globalState.setGlobalData(Organization.repoDataKey, repoData);
await GitHelper.globalState.setGlobalData(selectedRepoName, repoData);
}
}
19 changes: 4 additions & 15 deletions src/helpers/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,18 @@ export class GlobalState {
return this.context.globalState.update(key, JSON.stringify(data));
}

public getGlobalData<T>(key: string, defaultValue?: T): T | undefined {
public getGlobalData(key: string, defaultValue?: any, attribute?: string) {
const storedValue = this.context.globalState.get<string>(key);
if (storedValue !== undefined) {
try {
return JSON.parse(storedValue) as T;
} catch (error) {
Logger.logMessageByStatus(MessageStatus.ERROR, `Error parsing stored value for key ${key}: ${error}`);
}
}
return defaultValue;
}

public getGlobalDataByKey(globalKey: string, key: string) {
const storedValue = this.context.globalState.get<string>(globalKey);
if (storedValue) {
try {
const data = JSON.parse(storedValue);
return data[key];

return attribute ? data[attribute] : data;
} catch (error) {
Logger.logMessageByStatus(MessageStatus.ERROR, `Error parsing stored value for key ${key}: ${error}`);
}
}
return storedValue;
return defaultValue;
}

public clearGlobalData(...keys: string[]): Thenable<void>[] {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/loggerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Logger {

private static async writeLog(message: string) {
const timestamp = new Date().toISOString();
const userId = await Logger.globalState.getGlobalDataByKey(Organization.debrickedDataKey, Organization.userId);
const userId = await Logger.globalState.getGlobalData(Organization.debrickedDataKey, "", Organization.userId);
const sequenceId = Logger.globalState.getGlobalData(Organization.seqIdKey)
? `[seq_id:${Logger.globalState.getGlobalData(Organization.seqIdKey)}]`
: "";
Expand Down
92 changes: 92 additions & 0 deletions src/helpers/manifestWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as vscode from "vscode";
import * as path from "path";
import { MessageStatus, Organization, DebrickedCommands } from "../constants";
import { ScanService, FileService } from "services";
import { Common, ErrorHandler, Logger, StatusMessage, StatusBarMessageHelper, GlobalState } from "../helpers";

export class ManifestWatcher {
private static instance: ManifestWatcher;
private globalWatcher: vscode.FileSystemWatcher | null = null;
private manifestWatchers: vscode.FileSystemWatcher[] = [];

private constructor() {}
private static get globalState(): GlobalState {
return GlobalState.getInstance();
}

public static getInstance(): ManifestWatcher {
if (!ManifestWatcher.instance) {
ManifestWatcher.instance = new ManifestWatcher();
}
return ManifestWatcher.instance;
}

public async setupWatchers(context: vscode.ExtensionContext): Promise<void> {
try {
Logger.logMessageByStatus(MessageStatus.INFO, "Setting up Manifest File Watchers");
ManifestWatcher.globalState.setGlobalData(Organization.seqIdKey, Common.generateHashCode());

// Setup global watcher if not already set
if (!this.globalWatcher) {
this.setupGlobalWatcher(context);
}

const filesToScan = (await FileService.findFilesService()) || [];
await this.updateManifestWatchers(filesToScan, context);

StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.COMPLETE, DebrickedCommands.SCAN.cli_command),
);
} catch (error: any) {
ErrorHandler.handleError(error);
} finally {
StatusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.FINISHED, DebrickedCommands.SCAN.cli_command),
);
Logger.logMessageByStatus(MessageStatus.INFO, "Watchers for Manifest files are now ready to scan.");
}
}

private setupGlobalWatcher(context: vscode.ExtensionContext): void {
this.globalWatcher = vscode.workspace.createFileSystemWatcher("**/*");
this.globalWatcher.onDidCreate(async () => {
await this.setupWatchers(context);
});
context.subscriptions.push(this.globalWatcher);
}

private async updateManifestWatchers(filesToScan: string[], context: vscode.ExtensionContext): Promise<void> {
// Dispose old watchers
this.manifestWatchers.forEach((watcher) => watcher.dispose());
this.manifestWatchers = [];

if (filesToScan.length > 0) {
const filesPattern = new RegExp(filesToScan.map((file) => `^${file}$`).join("|"));

vscode.window.onDidChangeActiveTextEditor((editor) => {
if (editor && filesPattern.test(path.basename(editor.document.fileName))) {
vscode.commands.executeCommand("setContext", "debrickedFilesToScan", true);
} else {
vscode.commands.executeCommand("setContext", "debrickedFilesToScan", false);
}
});

filesToScan.forEach((file: string) => {
const watcher = vscode.workspace.createFileSystemWatcher(`**/${file}`);
const runScan = async () => {
await ScanService.scanService();
};

watcher.onDidChange(runScan);
watcher.onDidCreate(runScan);
watcher.onDidDelete(runScan);
Logger.logMessageByStatus(MessageStatus.INFO, `Register watcher on ${file}`);
context.subscriptions.push(watcher);
this.manifestWatchers.push(watcher);
});
Logger.logInfo("Watchers added successfully");
} else {
Logger.logInfo("No manifest files found");
}
}
}
2 changes: 1 addition & 1 deletion src/helpers/terminalHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Terminal {
let command: string = `${Organization.debrickedCli}`;
if (accessTokenRequired) {
const flags = DebrickedCommands.getCommandSpecificFlags("Debricked") || [];
const accessToken = await AuthHelper.getToken(useDefaultAccessToken, "access");
const accessToken = await AuthHelper.getToken(useDefaultAccessToken, Organization.access);

if (accessToken) {
Logger.logMessageByStatus(
Expand Down
15 changes: 10 additions & 5 deletions src/services/baseCommandService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ export class BaseCommandService {
);

await installer.runInstallScript();
BaseCommandService.globalState.setGlobalData(Organization.isFirstActivationKey, false);
BaseCommandService.globalState.setGlobalData(Organization.extensionVersionKey, currentVersion);
const debrickedData: any = await BaseCommandService.globalState.getGlobalData(
Organization.debrickedDataKey,
{},
);
debrickedData[Organization.isFirstActivationKey] = false;
debrickedData[Organization.extensionVersionKey] = currentVersion;

BaseCommandService.globalState.setGlobalData(Organization.debrickedDataKey, debrickedData);
Logger.logMessageByStatus(
MessageStatus.INFO,
`${Organization.extensionVersionKey}: ${BaseCommandService.globalState.getGlobalData(Organization.extensionVersionKey, "")}`,
`${Organization.extensionVersionKey}: ${debrickedData[Organization.extensionVersionKey]}`,
);
} catch (error: any) {
ErrorHandler.handleError(error);
Expand All @@ -144,10 +149,10 @@ export class BaseCommandService {
}
switch (selectedSubCommand?.cli_command) {
case "accessToken":
AuthHelper.getToken(false, "access");
AuthHelper.getToken(false, Organization.access);
break;
case "bearerToken":
AuthHelper.getToken(false, "bearer");
AuthHelper.getToken(false, Organization.bearer);
break;
}
} catch (error: any) {
Expand Down
30 changes: 19 additions & 11 deletions src/services/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class FileService {
if (command.sub_commands && command.sub_commands.length > 0) {
selectedSubCommand = command.sub_commands[0];
if (selectedSubCommand && selectedSubCommand.cli_command) {
cmdParams.push(selectedSubCommand.cli_command);
cmdParams.push(selectedSubCommand.cli_command, "-j");
}
}

Expand All @@ -80,20 +80,28 @@ export class FileService {

progress.report({ message: "🚀Finding Files..." });

const foundFiles = await Command.executeAsyncCommand(`${Organization.debrickedCli} ${cmdParams.join(" ")}`);
const foundFilesArray: string[] = Common.stringToArray(foundFiles, "\n");
await GitHelper.setupGit();
const repoData: any = await FileService.globalState.getGlobalData(Organization.repoDataKey, {});
const selectedRepoName = await GitHelper.getRepositoryName();
const foundFiles = JSON.parse(
await Command.executeAsyncCommand(`${Organization.debrickedCli} ${cmdParams.join(" ")}`),
);
const foundFilesArray: string[] = foundFiles
.map((item: any) => item.manifestFile)
.filter((file: any) => file !== "");

await GitHelper.setupGit();
const selectedRepoName = await GitHelper.getRepositoryName();
let repoData: any = await FileService.globalState.getGlobalData(selectedRepoName, {});

if (selectedRepoName && !repoData[selectedRepoName]) {
repoData[selectedRepoName] = {};
if (!repoData) {
repoData = {};
}

repoData[selectedRepoName].filesToScan = foundFilesArray;
repoData.filesToScan = foundFilesArray;
progress.report({ message: "🏁 Found Files" });
await FileService.globalState.setGlobalData(Organization.repoDataKey, repoData);
Logger.logMessageByStatus(MessageStatus.INFO, `Found ${foundFilesArray.length} Files: ${foundFilesArray}`);
await FileService.globalState.setGlobalData(selectedRepoName, repoData);
Logger.logMessageByStatus(
MessageStatus.INFO,
`Found ${foundFilesArray.length} Files: ${foundFilesArray}`,
);
return foundFilesArray;
} catch (error: any) {
ErrorHandler.handleError(error);
Expand Down
Loading

0 comments on commit a5cc26a

Please sign in to comment.