Skip to content

Add rough addon versioning #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/src/addon_manager/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import refreshAddons from "./refreshAddons";
import openLog from "./openLog";
import update from "./update";
import uninstall from "./uninstall";
import setVersion from "./setVersion";

export const commands = {
enable,
Expand All @@ -15,5 +16,6 @@ export const commands = {
refreshAddons,
openLog,
update,
uninstall
uninstall,
setVersion,
};
34 changes: 34 additions & 0 deletions client/src/addon_manager/commands/setVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as vscode from "vscode";
import { createChildLogger } from "../services/logging.service";
import addonManager from "../services/addonManager.service";
import { NotificationLevels } from "../types/webvue";
import { WebVue } from "../panels/WebVue";

const localLogger = createChildLogger("Set Version");

export default async (
context: vscode.ExtensionContext,
message: { data: { name: string; version: string } }
) => {
const addon = addonManager.addons.get(message.data.name);

try {
if (message.data.version === "Latest") {
await addon.update();

const defaultBranch = await addon.getDefaultBranch();
await addon.checkout(defaultBranch);
await addon.pull();
} else {
await addon.checkout(message.data.version);
}
} catch (e) {
localLogger.error(
`Failed to checkout version ${message.data.version}: ${e}`
);
WebVue.sendNotification({
level: NotificationLevels.error,
message: `Failed to checkout version ${message.data.version}`,
});
}
};
73 changes: 70 additions & 3 deletions client/src/addon_manager/models/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,57 @@ export class Addon {

/** Fetch addon info from `info.json` */
public async fetchInfo() {
const path = vscode.Uri.joinPath(this.uri, INFO_FILENAME);
const rawInfo = await filesystem.readFile(path);
const infoFilePath = vscode.Uri.joinPath(this.uri, INFO_FILENAME);
const modulePath = vscode.Uri.joinPath(this.uri, "module");

const rawInfo = await filesystem.readFile(infoFilePath);
const info = JSON.parse(rawInfo) as AddonInfo;

this.#displayName = info.name;

const moduleGit = git.cwd({ path: modulePath.fsPath, root: false });

let currentVersion = null;
let tags = [];

await this.getEnabled();

if (this.#installed) {
tags = (await moduleGit.tags(["--sort=-taggerdate"])).all;

const currentTag = await moduleGit
.raw(["describe", "--tags", "--exact-match"])
.catch((err) => {
localLogger.warn(err);
return null;
});
const commitsBehindLatest = await moduleGit.raw([
"rev-list",
`HEAD..origin/${await this.getDefaultBranch()}`,
"--count",
]);

if (Number(commitsBehindLatest) < 1) {
currentVersion = "Latest";
} else if (currentTag != "") {
currentVersion = currentTag;
} else {
currentVersion = await moduleGit
.revparse(["--short", "HEAD"])
.catch((err) => {
localLogger.warn(err);
return null;
});
}
}

return {
name: info.name,
description: info.description,
size: info.size,
hasPlugin: info.hasPlugin,
tags: tags,
version: currentVersion,
};
}

Expand Down Expand Up @@ -85,6 +125,30 @@ export class Addon {
.then((message) => localLogger.debug(message));
}

public async getDefaultBranch() {
const modulePath = vscode.Uri.joinPath(this.uri, "module");

const result = (await git
.cwd({ path: modulePath.fsPath, root: false })
.remote(["show", "origin"])) as string;
const match = result.match(/HEAD branch: (\w+)/);

return match[1];
}

public async pull() {
const modulePath = vscode.Uri.joinPath(this.uri, "module");

return await git.cwd({ path: modulePath.fsPath, root: false }).pull();
}

public async checkout(obj: string) {
const modulePath = vscode.Uri.joinPath(this.uri, "module");
return git
.cwd({ path: modulePath.fsPath, root: false })
.checkout([obj]);
}

/** Check whether this addon is enabled, given an array of enabled library paths.
* @param libraryPaths An array of paths from the `Lua.workspace.library` setting.
*/
Expand Down Expand Up @@ -254,7 +318,8 @@ export class Addon {
public async toJSON() {
await this.getEnabled();

const { name, description, size, hasPlugin } = await this.fetchInfo();
const { name, description, size, hasPlugin, tags, version } =
await this.fetchInfo();
const enabled = this.#enabled;
const installTimestamp = (await git.log()).latest.date;
const hasUpdate = this.#hasUpdate;
Expand All @@ -270,6 +335,8 @@ export class Addon {
hasUpdate,
processing: this.#processing,
installed: this.#installed,
tags,
version,
};
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/addon_manager/services/git.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { REPOSITORY_NAME, REPOSITORY_PATH } from "../config";

const localLogger = createChildLogger("Git");

export const git = simpleGit();
export const git = simpleGit({ trimmed: true });

export const setupGit = async (context: vscode.ExtensionContext) => {
const storageURI = vscode.Uri.joinPath(
Expand Down
2 changes: 1 addition & 1 deletion client/webvue
2 changes: 1 addition & 1 deletion server
Submodule server updated 139 files
Loading