Skip to content

Commit ab7124e

Browse files
committed
Added semantic versioning to track upgrades
1 parent 5682854 commit ab7124e

2 files changed

Lines changed: 110 additions & 3 deletions

File tree

src/control.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
'use strict';
77

8-
import { Disposable, ExtensionContext } from 'vscode';
8+
import { commands, Disposable, ExtensionContext } from 'vscode';
99
import { Config, configuration } from './util/config';
1010
import { Logger } from './util/logger';
1111
import { StatusBarControl } from './util/statusBar';
1212
import { NavTreeView } from './views/navTreeView';
1313
import { GCodeUnitsController } from './gcodeUnits';
1414
import { StatsView } from './views/statsView';
15-
import { PIcon } from './util/constants';
15+
import { constants, Contexts, GlobalState, PIcon, VSBuiltInCommands } from './util/constants';
1616
import { Commands } from './util/commands';
17+
import { LocalStorageService } from './util/localStorageService';
18+
import { Version } from './util/version';
1719

1820
export class Control {
1921
private static _config: Config | undefined;
@@ -23,6 +25,7 @@ export class Control {
2325
// Controllers
2426
private static _statusBarControl: StatusBarControl;
2527
private static _unitsController: GCodeUnitsController | undefined;
28+
private static _storageManager: LocalStorageService;
2629

2730
// Views
2831
private static _statsView: StatsView | undefined;
@@ -32,6 +35,9 @@ export class Control {
3235
this._context = context;
3336
this._config = config;
3437

38+
// Load GlobalState Storage Manager
39+
this._storageManager = new LocalStorageService(context);
40+
3541
// Load StatusBars
3642
context.subscriptions.push((this._statusBarControl = new StatusBarControl(this._context)));
3743

@@ -88,7 +94,7 @@ export class Control {
8894

8995
// Load Support Heart to Statusbar
9096
this._statusBarControl.updateStatusBar(
91-
PIcon.HEART,
97+
PIcon.Heart,
9298
'support',
9399
'Support G-Code Syntax ❤',
94100
undefined,
@@ -108,6 +114,28 @@ export class Control {
108114
return secs * 1000 + nanosecs / 1000000;
109115
}
110116

117+
static checkVersion() {
118+
const localVer = new Version(constants.extension.version);
119+
120+
const prevVer = this._storageManager.getValue<Version>(GlobalState.PreviousVersion, 'global') ?? localVer;
121+
122+
const newVer = localVer.compareWith(prevVer.getVersion()) === 1 ? true : false;
123+
124+
if (newVer) {
125+
// Extension has been updated
126+
} else {
127+
return;
128+
}
129+
}
130+
131+
static showWhatsNew() {
132+
// Show Whats New Message
133+
}
134+
135+
static setContext(key: Contexts | string, value: any) {
136+
return commands.executeCommand(VSBuiltInCommands.SetContext, key, value);
137+
}
138+
111139
static get context() {
112140
return this._context;
113141
}

src/util/version.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* ---------------------------------------------------------------------------------------------
2+
* Copyright (c) Applied Eng & Design All rights reserved.
3+
* Licensed under the MIT License. See License.md in the project root for license information.
4+
* -------------------------------------------------------------------------------------------- */
5+
'use strict';
6+
7+
export interface IVersion {
8+
major: number;
9+
minor: number;
10+
patch: number;
11+
}
12+
13+
export type VersionCheck = -1 | 0 | 1;
14+
15+
export class Version {
16+
private _version: IVersion;
17+
18+
constructor(ver: IVersion | string) {
19+
if (typeof ver === 'string') {
20+
this._version = this.parseVer(ver);
21+
} else {
22+
this._version = ver;
23+
}
24+
}
25+
26+
private parseVer(stringVer: string): IVersion {
27+
const [major, minor, patch] = stringVer.split('.');
28+
return {
29+
major: parseInt(major, 10),
30+
minor: parseInt(minor, 10),
31+
patch: patch == null ? 0 : parseInt(patch, 10),
32+
};
33+
}
34+
35+
getVersion(): IVersion {
36+
return this._version;
37+
}
38+
39+
getVersionAsString(): string {
40+
return `${this._version.major}.${this._version.minor}.${this._version.patch}`;
41+
}
42+
43+
compareWith(ver: IVersion | string): VersionCheck {
44+
// Compare Versions
45+
if (typeof ver === 'string') {
46+
ver = this.parseVer(ver);
47+
}
48+
49+
// Check Major Version
50+
if (this._version.major > ver.major) {
51+
return 1;
52+
}
53+
54+
if (this._version.major < ver.major) {
55+
return -1;
56+
}
57+
58+
// Check Minor Version
59+
if (this._version.minor > ver.minor) {
60+
return 1;
61+
}
62+
63+
if (this._version.minor < ver.minor) {
64+
return -1;
65+
}
66+
67+
// Check Patch Version
68+
if (this._version.patch > ver.patch) {
69+
return 1;
70+
}
71+
72+
if (this._version.patch < ver.patch) {
73+
return -1;
74+
}
75+
76+
// Versions are Equal
77+
return 0;
78+
}
79+
}

0 commit comments

Comments
 (0)