Skip to content
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

Installation improvements #293

Merged
merged 1 commit into from
Feb 10, 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
31 changes: 24 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ import { configuration } from "./extension/config";

//

const forcedDelay: number = 1000; // how long to delay install when VSCode is operational

export let installDelay = 7000; // how long to delay install when VSCode is init

export const setActive: (active?: boolean) => void = (active?: boolean) => {
console.log(active);
statusbar.text = `$(${active === false ? "file-media" : "loading~spin"}) Background`;
}

export const statusbar: StatusBarItem = (() => {
const item: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Right);

item.command = "background.config";
item.name = "Background";
item.text = "$(file-media) Background";
item.tooltip = "Open background configuration";

return item;
})();

export const activate: (context: ExtensionContext) => any = (context: ExtensionContext) => {
let workbench: string;
let product: string;
Expand Down Expand Up @@ -98,13 +118,6 @@ export const activate: (context: ExtensionContext) => any = (context: ExtensionC
const changelog: Uri = Uri.file(join(context.extensionPath, "CHANGELOG.md"));
const help: Uri = Uri.file(join(context.extensionPath, "HELP.md"));

const statusbar: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Right);

statusbar.command = "background.config";
statusbar.name = "Background";
statusbar.text = "$(file-media) Background";
statusbar.tooltip = "Open background configuration";

context.subscriptions.push(
commands.registerCommand("background.install", () => install(workbench, product, true)),
commands.registerCommand("background.uninstall", () =>
Expand All @@ -124,5 +137,9 @@ export const activate: (context: ExtensionContext) => any = (context: ExtensionC
if(configuration().get("autoInstall"))
install(workbench, product, false);

// delay before VSCode will actually refresh properly
for(let i = installDelay; i > forcedDelay; i -= 1000)
setTimeout(() => installDelay -= 1000, i);

return api;
};
17 changes: 14 additions & 3 deletions src/extension/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { reload } from "../lib/vscode";
import { copyCommand, generateChecksum } from "../lib/file";

import { clean, inject } from "./inject"
import { installDelay, setActive } from "../extension";

export const install: (workbench: PathLike, product: PathLike, force?: boolean) => void = (workbench: PathLike, product: PathLike, force: boolean = false) => {
write(workbench, product, inject(readFileSync(workbench, "utf-8")), force);
Expand All @@ -44,6 +45,8 @@ const write: (workbench: PathLike, product: PathLike, content: string, force?: b
const pJson: string = readFileSync(product, "utf-8").replace(workbenchChecksum, generateChecksum(content));

try{ // write changes
setActive(true);

let changed: boolean = force;
if(readFileSync(workbench, "utf-8") !== content){
writeFileSync(workbench, content, "utf-8");
Expand All @@ -53,7 +56,11 @@ const write: (workbench: PathLike, product: PathLike, content: string, force?: b
writeFileSync(product, pJson, "utf-8");
changed = true;
}
changed && setTimeout(reload, 1000); // artificial delay because VSCode is not updating the background for no reason

if(changed)
setTimeout(reload, installDelay); // artificial delay because VSCode is not updating the background for no reason
else
setActive(false);
}catch(error: any){
const snap: boolean = platform() === "linux" &&
/* also in */ workbench.toString().replace(/\\/g, '/').includes("/snap/") &&
Expand Down Expand Up @@ -83,17 +90,21 @@ const write: (workbench: PathLike, product: PathLike, content: string, force?: b
[productTemp, product]
]);
exec(command, { name: "VSCode Extension Host" }, (err: any) => {
if(err)
if(err){
window.showErrorMessage(
"Failed to write changes",
{
detail: `OS: ${platform()} ${release()}\nUsing command: ${command}\n\n${err.name}\n${err.message}`.trim(),
modal: true
}
);
else
setActive(false);
}else{
reload();
}
});
}else{
setActive(false);
}
});
}
Expand Down