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
38 changes: 16 additions & 22 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/semi": "warn",
"curly": "warn",
"eqeqeq": "warn",
"no-throw-literal": "warn",
"semi": "off"
},
"ignorePatterns": [
"out",
"dist",
"**/*.d.ts"
]
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/semi": "warn",
"curly": "warn",
"eqeqeq": "warn",
"no-throw-literal": "warn",
"semi": "off"
},
"ignorePatterns": ["out", "dist", "**/*.d.ts"]
}
57 changes: 34 additions & 23 deletions http/package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
{
"name": "l2",
"displayName": "l2",
"description": "Utilties and support for working with Lama2",
"version": "0.0.1",
"engines": {
"vscode": "^1.65.0"
},
"categories": [
"Programming Languages"
"name": "l2",
"displayName": "l2",
"description": "Utilties and support for working with Lama2",
"version": "0.0.1",
"engines": {
"vscode": "^1.65.0"
},
"categories": [
"Programming Languages"
],
"contributes": {
"languages": [
{
"id": "l2",
"aliases": [
"l2",
"lama2"
],
"extensions": [
".l2",
".lama",
".lama2"
],
"configuration": "./language-configuration.json"
}
],
"contributes": {
"languages": [{
"id": "l2",
"aliases": ["l2", "lama2"],
"extensions": [".l2", ".lama", ".lama2"],
"configuration": "./language-configuration.json"
}],
"grammars": [{
"language": "lama2",
"scopeName": "source.http",
"path": "./syntaxes/http.tmLanguage.json"
}]
}
}
"grammars": [
{
"language": "lama2",
"scopeName": "source.http",
"path": "./syntaxes/http.tmLanguage.json"
}
]
}
}
22 changes: 18 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"test": "node ./out/test/runTest.js",
"format": "prettier --write ."
},
"devDependencies": {
"@types/glob": "^7.2.0",
Expand All @@ -100,12 +101,14 @@
"eslint": "^8.9.0",
"glob": "^7.2.0",
"mocha": "^9.2.1",
"prettier": "2.6.2",
"typescript": "^4.5.5"
},
"dependencies": {
"ansi-to-html": "^0.7.2",
"chokidar": "^3.5.3",
"json2html": "^0.0.8",
"semver": "^7.5.4",
"simple-git": "^3.17.0"
}
}
9 changes: 9 additions & 0 deletions prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
trailingComma: "es5",
tabWidth: 2,
semi: false,
singleQuote: false,
arrowParens: "always",
useTabs: false,
printWidth: 120, // Add line length here (e.g., 80)
};
59 changes: 16 additions & 43 deletions src/checkL2Version.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,35 @@
import { exec } from "child_process";
import { exec, execSync } from "child_process";
import * as semver from "semver";
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() {
export async function getL2VersionAndUpdatePrompt(minVersionToCheck: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Which function call within this requires async? I don't see any await

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed this, it wasn't needed

try {
getL2Version((l2Version) => {
// Check if version is below MIN_VERSION_TO_CHECK
if (compareL2Versions(l2Version, MIN_VERSION_TO_CHECK) < 0) {
showUpdateWarning();
const l2Version = execSync("l2 --version", { encoding: 'utf-8' }).trim();
// Use the semver library to validate and normalize the version string
const normalizedVersion = semver.valid(l2Version);
if (normalizedVersion) {

if (semver.lt(l2Version, minVersionToCheck)) {
showUpdateWarning(minVersionToCheck);
}
});
return normalizedVersion;
} else {
throw new Error("Invalid version format: " + l2Version);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we report to the user - that the version is unknown and probably the extension will not work in full?

We can have a button "Download" to direct them to website.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added this

}
} 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) {
const l2Version = versionMatch[1];
callback(l2Version);
}
});
}

function compareL2Versions(currentVersion: string, latestVersion: string) {
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() {
function showUpdateWarning(minVersionToCheck: string) {
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}`,
`Your Lama2 version is outdated. \nPlease update to version ${minVersionToCheck} or above for the best experience.\nUpdate: ${UPDATE_MSG}`,
updateAction
)
.then((selectedAction) => {
Expand Down
20 changes: 15 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as vscode from "vscode";
import * as semver from "semver";

import { genCodeSnip } from "./generateCodeSnippet";
import { getRemoteUrl } from "./getRemoteUrl";
import { replaceTextAfterEnvSelected, suggestENVs } from "./suggestEnvironmentVars";
import { getDotENVS, replaceTextAfterEnvSelected, suggestENVSFromDotEnv, suggestENVs } from "./suggestEnvironmentVars";
import { genLama2Examples } from "./genLama2Examples";
import { execCurL2File } from "./executeCurrentFile";
import { prettifyL2File } from "./prettifyL2File";
import { checkL2Version } from "./checkL2Version";
import { getL2VersionAndUpdatePrompt } from "./checkL2Version";

export function activate(context: vscode.ExtensionContext) {
const MIN_VERSION_TO_CHECK = "1.5.1";

export async function activate(context: vscode.ExtensionContext) {
console.log('>>> Congratulations, your extension "Lama2" is now active!');

// Level1 command pallette
Expand Down Expand Up @@ -36,9 +39,16 @@ export function activate(context: vscode.ExtensionContext) {
console.log(">>> generateCodeSnippetDisposable is now active!");

// Automatically check L2 version on extension activation
checkL2Version();
let suggestEnvVariables: any;
let l2Version = await getL2VersionAndUpdatePrompt(MIN_VERSION_TO_CHECK);
if (l2Version === null || l2Version === undefined || semver.lt(l2Version, MIN_VERSION_TO_CHECK)) {
getDotENVS();
suggestEnvVariables = suggestENVSFromDotEnv();
} else {
suggestEnvVariables = suggestENVs();
}


let suggestEnvVariables = suggestENVs();
context.subscriptions.push(
suggestEnvVariables,
vscode.commands.registerCommand("envoptions", () => {
Expand Down
Loading