Skip to content

Commit

Permalink
PackageInfo refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
maziac committed Feb 19, 2021
1 parent 803ace1 commit 7fc56c4
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

# 1.7.2
- Internal refactoring.

# 1.7.1
- Solved issue #54: Using "# " as comment instead of "#".

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "asm-code-lens",
"displayName": "ASM Code Lens",
"version": "1.7.1",
"version": "1.7.2",
"publisher": "maziac",
"description": "A language server that enables code lens, references, hover information, symbol renaming and the outline view for assembler files.",
"author": {
Expand Down
Binary file added releases/asm-code-lens-1.7.2.vsix
Binary file not shown.
8 changes: 4 additions & 4 deletions src/HexCalcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class HexCalcProvider implements vscode.WebviewViewProvider {
return;

// Add the html styles etc.
const extPath = PackageInfo.extensionPath;
const extPath = PackageInfo.extension.extensionPath;
const mainHtmlFile = path.join(extPath, 'html/hexcalc.html');
let mainHtml = readFileSync(mainHtmlFile).toString();
// Exchange local path
Expand Down Expand Up @@ -81,7 +81,7 @@ let hexPrefix = "${hexPrefix}";`
const vscodePanel = vscode.window.createWebviewPanel('', '', {preserveFocus: true, viewColumn: vscode.ViewColumn.Nine});
vscodePanel.title = 'Donate...';
// Read the file
const extPath = PackageInfo.extensionPath;
const extPath = PackageInfo.extension.extensionPath;
const htmlFile = path.join(extPath, 'html/donate.html');
let html = readFileSync(htmlFile).toString();
// Exchange local path
Expand All @@ -95,9 +95,9 @@ let hexPrefix = "${hexPrefix}";`
switch (message.command) {
case 'showExtension':
// Switch to Extension Manager
vscode.commands.executeCommand("workbench.extensions.search", PackageInfo.publisher)
vscode.commands.executeCommand("workbench.extensions.search", PackageInfo.extension.packageJSON.publisher)
// And select the given extension
const extensionName = PackageInfo.publisher + '.' + message.data;
const extensionName = PackageInfo.extension.packageJSON.publisher + '.' + message.data;
vscode.commands.executeCommand("extension.open", extensionName);
break;
}
Expand Down
8 changes: 6 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import {setGrepGlobPatterns} from './grep';
import {HexCalcProvider} from './HexCalcProvider';
import {WhatsNewView} from './whatsnew/whatsnewview';
import {PackageInfo} from './whatsnew/packageinfo';
import {GlobalStorage} from './globalstorage';

export function activate(context: vscode.ExtensionContext) {

// Save the extension path
PackageInfo.setExtensionPath(context.extensionPath);
// Init package info
PackageInfo.Init(context);

// Init global storage
GlobalStorage.Init(context);

// Check version and show 'What's new' if necessary.
const mjrMnrChanged = WhatsNewView.updateVersion(context);
Expand Down
31 changes: 31 additions & 0 deletions src/globalstorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as vscode from 'vscode';


export class GlobalStorage {

// The extension context.
protected static context: vscode.ExtensionContext;

/**
* Store the context here at activation of the extension.
*/
public static Init(context: vscode.ExtensionContext) {
this.context = context;
}


/**
* Get a value.
*/
public static Get<T>(key: string): T | undefined {
return this.context.globalState.get<T>(key);
}


/**
* Store a value.
*/
public static Set(key: string, value: any) {
this.context.globalState.update(key, value);
}
}
28 changes: 6 additions & 22 deletions src/whatsnew/packageinfo.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import * as vscode from 'vscode';
import * as jsonc from 'jsonc-parser';
import {readFileSync} from 'fs';


/**
* Reads the package.json of the extension.
*/
export class PackageInfo {

// The extensions path is stored here.
public static extensionPath: string;

// The publisher name (maziac) is stored here after setting the extensionPath.
public static publisher: string;

// The extension name is stored here after setting the extensionPath.
public static extensionName: string;

// The extension info is stored here after setting the extensionPath.
public static extension: vscode.Extension<any>;

Expand All @@ -25,19 +14,14 @@ export class PackageInfo {
* Sets the extension path.
* Called on extension activation.
*/
public static setExtensionPath(path: string) {
public static Init(context: vscode.ExtensionContext) {
// Store path
this.extensionPath = path;
// Unfortunately there seems no other way than reading the package.json manually to get the extension name.
const pkgJsonFile = "/package.json";
const pkgJsonPath = this.extensionPath + pkgJsonFile;
const pkgJsonData = readFileSync(pkgJsonPath, 'utf8');
const parseErrors: jsonc.ParseError[] = [];
const pkgJson = jsonc.parse(pkgJsonData, parseErrors, {allowTrailingComma: true});
this.publisher = pkgJson.publisher;
this.extensionName = pkgJson.publisher + '.' + pkgJson.name;
//this.extensionPath = path;
// Get package info from globalState
const _extension = context.globalState["_extension"]
const extensionName = _extension.id;
// Store extension info
this.extension = vscode.extensions.getExtension(this.extensionName)!;
this.extension = vscode.extensions.getExtension(extensionName)!;
}

}
Expand Down
11 changes: 6 additions & 5 deletions src/whatsnew/whatsnewview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';
import {readFileSync} from 'fs';
import {PackageInfo} from './packageinfo';
import {Version} from './version';
import {GlobalStorage} from '../globalstorage';


export class WhatsNewView {
Expand All @@ -17,13 +18,13 @@ export class WhatsNewView {
*/
public static updateVersion(context: vscode.ExtensionContext): boolean {
// Load data from extension storage
const versionId = PackageInfo.extensionPath+ '.version';
const previousVersion = context.globalState.get<string>(versionId)!;
const versionId = 'version';
const previousVersion = GlobalStorage.Get<string>(versionId)!;
const currentVersion = PackageInfo.extension.packageJSON.version;

// Update version: "major", "minor"
if(currentVersion != previousVersion)
context.globalState.update(versionId, currentVersion);
if (currentVersion != previousVersion)
GlobalStorage.Set(versionId, currentVersion);

// Compare
const isNewer = Version.isNewVersion(currentVersion, previousVersion);
Expand Down Expand Up @@ -65,7 +66,7 @@ export class WhatsNewView {
html = html.replace('${vscodeResPath}', vscodeResPath);

// Exchange extension name
html = html.replace(/\${extensionName}/g, PackageInfo.extensionName);
html = html.replace(/\${extensionName}/g, PackageInfo.extension.packageJSON.id);

// Exchange extension name
html = html.replace(/\${extensionVersion}/g, PackageInfo.extension.packageJSON.version);
Expand Down

0 comments on commit 7fc56c4

Please sign in to comment.