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

Update version prompt if l2 binary below v1.5.1 #11

Merged
merged 11 commits into from
Jul 26, 2023
2,024 changes: 153 additions & 1,871 deletions package-lock.json

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions src/checkL2Version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { exec } from "child_process";
import * as vscode from "vscode";
import { getShowLama2Term } from "./utils";

const MIN_VERSION_TO_CHECK = "1.5.2";
const LAMA2_TERM_NAME = "AutoLama2";
const UPDATE_MSG = "Support for environment variables.";

export function checkL2Version() {
try {
getL2Version((l2Version) => {
// Check if version is below MIN_VERSION_TO_CHECK
if (compareL2Versions(l2Version, MIN_VERSION_TO_CHECK) < 0) {
showUpdateWarning();
}
});
} catch (e: any) {
console.log("Problem while checking for version -> ", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should surface this as a warning - I think.

Especially - what happens when there is no binary in the path?

I think the user ought to know that something is wrong, and that action is needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah added

}
}

function getL2Version(callback: (version: string) => void) {
exec("l2 --version", (error, stdout, stderr) => {
const versionOutput = stdout.trim();
const versionMatch = versionOutput.match(/v(\d+\.\d+\.\d+)/);
if (versionMatch && versionMatch.length === 2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why length === 2? We can use capture group to get a triple for (major, minor, patch) as well directly, I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can try to use the official semver regex. See here for details. Check the actual regex in action at regex101

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn never knew so many kinds are there

const l2Version = versionMatch[1];
callback(l2Version);
}
});
}

function compareL2Versions(currentVersion: string, latestVersion: string) {
Copy link
Contributor

@shrsv shrsv Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using a library suggested in this SO Answer. Our semver definition is simple now, but there are more changes possible. Best to simply use standardized library for it.

Should be as simple as:

semver.gte('3.4.8', '3.4.7') //true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're using this library, then ignore the previous comment about regex*.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah using this

const curVersionParts = currentVersion.split(".");
const lstVersionParts = latestVersion.split(".");
for (let i = 0; i < 3; i++) {
const curNum = Number(curVersionParts[i]);
const lstNum = Number(lstVersionParts[i]);
if (curNum > lstNum) {
return 1;
}
if (lstNum > curNum) {
return -1;
}
if (!isNaN(curNum) && isNaN(lstNum)) {
return 1;
}
if (isNaN(curNum) && !isNaN(lstNum)) {
return -1;
}
}
return 0;
}

function showUpdateWarning() {
const updateAction: vscode.MessageItem = { title: "Update" };
vscode.window
.showWarningMessage(
`Your Lama2 version is outdated. \nPlease update to version ${MIN_VERSION_TO_CHECK} or above for the best experience.\nUpdate: ${UPDATE_MSG}`,
updateAction
)
.then((selectedAction) => {
if (selectedAction === updateAction) {
runL2UpdateCommand();
}
});
}

function runL2UpdateCommand() {
const terminal: any = getShowLama2Term(LAMA2_TERM_NAME);
terminal.sendText("l2 -u");
terminal.show();
}
29 changes: 2 additions & 27 deletions src/executeCurrentFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let fs = require('fs')
var path = require('path');
var json2html = require('json2html')
import splitLama2Output from './parseOut'
import { getShowLama2Term } from './utils';

class ExecuteCurrentFile {
LAMA2_TERM_NAME = "AutoLama2"
Expand All @@ -20,32 +21,6 @@ class ExecuteCurrentFile {
this.context = ctx
}


getActiveTerminals() {
return vscode.window.terminals;
}

findTerminalsByName(name: string) {
let terminals = this.getActiveTerminals()
let found = terminals.find(element => element.name == name)
return found
}

findOrCreateTerminal(name: string) {
let terminal = this.findTerminalsByName(name)
if (terminal == null) {
return vscode.window.createTerminal(name)
} else {
return terminal
}
}

getShowLam2Term(name: string) {
let terminal = this.findOrCreateTerminal(name)
terminal.show()
return terminal
}

generateRandomName(length: any) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
Expand Down Expand Up @@ -247,7 +222,7 @@ class ExecuteCurrentFile {
}

execFile() {
let terminal = this.getShowLam2Term(this.LAMA2_TERM_NAME)
let terminal = getShowLama2Term(this.LAMA2_TERM_NAME)
let { cmd, rflag, rfile } = this.getLama2Command()
this.outPath = rfile
this.flagPath = rflag
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { replaceTextAfterEnvSelected, suggestENVs } from "./suggestEnvironmentVa
import { genLama2Examples } from "./genLama2Examples";
import { execCurL2File } from "./executeCurrentFile";
import { prettifyL2File } from "./prettifyL2File";
import { checkL2Version } from "./checkL2Version";

export function activate(context: vscode.ExtensionContext) {
console.log('>>> Congratulations, your extension "Lama2" is now active!');
Expand Down Expand Up @@ -34,6 +35,9 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(generateCodeSnippetDisposable);
console.log(">>> generateCodeSnippetDisposable is now active!");

// Automatically check L2 version on extension activation
checkL2Version();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if version is too old, what happens to the variable suggestion? Is it totally dead in older versions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No in the older version what we were doing is searching the le.env file in front end and suggesting it. So it won't be a problem.

Copy link
Contributor

@shrsv shrsv Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I am saying is something else.

Say, the extension frontend updates, but the l2 version is still old, what happens?

Remember - VSCode automatically updates extensions quite quickly (I think)


let suggestEnvVariables = suggestENVs();
context.subscriptions.push(
suggestEnvVariables,
Expand Down
28 changes: 2 additions & 26 deletions src/generateCodeSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,13 @@ import ChokiExtension from "./watchFile";
let fs = require("fs");
import { window } from "vscode";
import LanguagesData from "./languages";
import { getShowLama2Term } from "./utils";

class GenerateCodeSnippet {
LAMA2_TERM_NAME = "AutoLama2";
outPath: string = "";
flagPath: string = "";

getActiveTerminals() {
return vscode.window.terminals;
}

findTerminalsByName(name: string) {
let terminals = this.getActiveTerminals();
let found = terminals.find((element) => element.name == name);
return found;
}

findOrCreateTerminal(name: string) {
let terminal = this.findTerminalsByName(name);
if (terminal == null) {
return vscode.window.createTerminal(name);
} else {
return terminal;
}
}

getShowLam2Term(name: string) {
let terminal = this.findOrCreateTerminal(name);
terminal.show();
return terminal;
}

generateRandomName(length: any) {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Expand Down Expand Up @@ -102,7 +78,7 @@ class GenerateCodeSnippet {
}

execFile(lang: string, cli: string) {
let terminal = this.getShowLam2Term(this.LAMA2_TERM_NAME);
let terminal = getShowLama2Term(this.LAMA2_TERM_NAME);
let { cmd, rflag, rfile } = this.getLama2Command(lang, cli);
this.outPath = rfile;
this.flagPath = rflag;
Expand Down
33 changes: 33 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from "vscode";

// Define a custom interface for the terminal object (optional, based on your specific use case)
interface CustomTerminal extends vscode.Terminal {
name: string;
}

function getActiveTerminals(): CustomTerminal[] {
// Convert the readonly array to a mutable array by creating a copy
return vscode.window.terminals.slice();
}

function findTerminalsByName(name: string): CustomTerminal | undefined {
let terminals = getActiveTerminals();
let found = terminals.find((element) => element.name === name);
return found;
}

function findOrCreateTerminal(name: string): CustomTerminal {
let terminal = findTerminalsByName(name);
if (!terminal) {
terminal = vscode.window.createTerminal(name);
}
return terminal;
}

export function getShowLama2Term(name: string): CustomTerminal {
let terminal = findOrCreateTerminal(name);
// Clear terminal and send Ctrl+C before showing
terminal.sendText("\x03");
terminal.show();
return terminal;
}
Loading