Skip to content

Commit e67a273

Browse files
committed
Merge branch 'addon-versioning' of https://github.com/carsakiller/vscode-lua into carsakiller-addon-versioning
2 parents 0803101 + b48f029 commit e67a273

File tree

5 files changed

+109
-6
lines changed

5 files changed

+109
-6
lines changed

client/src/addon_manager/commands/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import refreshAddons from "./refreshAddons";
66
import openLog from "./openLog";
77
import update from "./update";
88
import uninstall from "./uninstall";
9+
import setVersion from "./setVersion";
910

1011
export const commands = {
1112
enable,
@@ -15,5 +16,6 @@ export const commands = {
1516
refreshAddons,
1617
openLog,
1718
update,
18-
uninstall
19+
uninstall,
20+
setVersion,
1921
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as vscode from "vscode";
2+
import { createChildLogger } from "../services/logging.service";
3+
import addonManager from "../services/addonManager.service";
4+
import { NotificationLevels } from "../types/webvue";
5+
import { WebVue } from "../panels/WebVue";
6+
7+
const localLogger = createChildLogger("Set Version");
8+
9+
export default async (
10+
context: vscode.ExtensionContext,
11+
message: { data: { name: string; version: string } }
12+
) => {
13+
const addon = addonManager.addons.get(message.data.name);
14+
15+
try {
16+
if (message.data.version === "Latest") {
17+
await addon.update();
18+
19+
const defaultBranch = await addon.getDefaultBranch();
20+
await addon.checkout(defaultBranch);
21+
await addon.pull();
22+
} else {
23+
await addon.checkout(message.data.version);
24+
}
25+
} catch (e) {
26+
localLogger.error(
27+
`Failed to checkout version ${message.data.version}: ${e}`
28+
);
29+
WebVue.sendNotification({
30+
level: NotificationLevels.error,
31+
message: `Failed to checkout version ${message.data.version}`,
32+
});
33+
}
34+
};

client/src/addon_manager/models/addon.ts

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,57 @@ export class Addon {
4444

4545
/** Fetch addon info from `info.json` */
4646
public async fetchInfo() {
47-
const path = vscode.Uri.joinPath(this.uri, INFO_FILENAME);
48-
const rawInfo = await filesystem.readFile(path);
47+
const infoFilePath = vscode.Uri.joinPath(this.uri, INFO_FILENAME);
48+
const modulePath = vscode.Uri.joinPath(this.uri, "module");
49+
50+
const rawInfo = await filesystem.readFile(infoFilePath);
4951
const info = JSON.parse(rawInfo) as AddonInfo;
5052

5153
this.#displayName = info.name;
5254

55+
const moduleGit = git.cwd({ path: modulePath.fsPath, root: false });
56+
57+
let currentVersion = null;
58+
let tags = [];
59+
60+
await this.getEnabled();
61+
62+
if (this.#installed) {
63+
tags = (await moduleGit.tags(["--sort=-taggerdate"])).all;
64+
65+
const currentTag = await moduleGit
66+
.raw(["describe", "--tags", "--exact-match"])
67+
.catch((err) => {
68+
localLogger.warn(err);
69+
return null;
70+
});
71+
const commitsBehindLatest = await moduleGit.raw([
72+
"rev-list",
73+
`HEAD..origin/${await this.getDefaultBranch()}`,
74+
"--count",
75+
]);
76+
77+
if (Number(commitsBehindLatest) < 1) {
78+
currentVersion = "Latest";
79+
} else if (currentTag != "") {
80+
currentVersion = currentTag;
81+
} else {
82+
currentVersion = await moduleGit
83+
.revparse(["--short", "HEAD"])
84+
.catch((err) => {
85+
localLogger.warn(err);
86+
return null;
87+
});
88+
}
89+
}
90+
5391
return {
5492
name: info.name,
5593
description: info.description,
5694
size: info.size,
5795
hasPlugin: info.hasPlugin,
96+
tags: tags,
97+
version: currentVersion,
5898
};
5999
}
60100

@@ -85,6 +125,30 @@ export class Addon {
85125
.then((message) => localLogger.debug(message));
86126
}
87127

128+
public async getDefaultBranch() {
129+
const modulePath = vscode.Uri.joinPath(this.uri, "module");
130+
131+
const result = (await git
132+
.cwd({ path: modulePath.fsPath, root: false })
133+
.remote(["show", "origin"])) as string;
134+
const match = result.match(/HEAD branch: (\w+)/);
135+
136+
return match[1];
137+
}
138+
139+
public async pull() {
140+
const modulePath = vscode.Uri.joinPath(this.uri, "module");
141+
142+
return await git.cwd({ path: modulePath.fsPath, root: false }).pull();
143+
}
144+
145+
public async checkout(obj: string) {
146+
const modulePath = vscode.Uri.joinPath(this.uri, "module");
147+
return git
148+
.cwd({ path: modulePath.fsPath, root: false })
149+
.checkout([obj]);
150+
}
151+
88152
/** Check whether this addon is enabled, given an array of enabled library paths.
89153
* @param libraryPaths An array of paths from the `Lua.workspace.library` setting.
90154
*/
@@ -253,7 +317,8 @@ export class Addon {
253317
public async toJSON() {
254318
await this.getEnabled();
255319

256-
const { name, description, size, hasPlugin } = await this.fetchInfo();
320+
const { name, description, size, hasPlugin, tags, version } =
321+
await this.fetchInfo();
257322
const enabled = this.#enabled;
258323
const installTimestamp = (await git.log()).latest?.date;
259324
const hasUpdate = this.#hasUpdate;
@@ -269,6 +334,8 @@ export class Addon {
269334
hasUpdate,
270335
processing: this.#processing,
271336
installed: this.#installed,
337+
tags,
338+
version,
272339
};
273340
}
274341

client/src/addon_manager/services/git.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { REPOSITORY_NAME, REPOSITORY_PATH } from "../config";
66

77
const localLogger = createChildLogger("Git");
88

9-
export const git = simpleGit();
9+
export const git = simpleGit({ trimmed: true });
1010

1111
export const setupGit = async (context: vscode.ExtensionContext) => {
1212
const storageURI = vscode.Uri.joinPath(

0 commit comments

Comments
 (0)