Skip to content

Commit

Permalink
fix multiple json issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lyu571 committed Dec 19, 2023
1 parent d71235b commit 045d8de
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
"lint": "eslint src --ext ts",
"test": "npm run compile",
"vscode:prepublish": "npm run esbuild-base -- --minify",
"esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node",
"esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/main.js --external:vscode --external:config/tips --format=cjs --platform=node",
"esbuild": "npm run esbuild-base -- --sourcemap",
"esbuild-watch": "npm run esbuild-base -- --sourcemap --watch",
"test-compile": "tsc -p ./"
Expand Down
90 changes: 56 additions & 34 deletions src/autocomplete/TerraformTipsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import * as vscode from 'vscode';
import { executeCommandByExec } from "@/utils/cpUtils";
import * as fs from "fs";
import * as path from "path";
import * as workspaceUtils from "@/utils/workspaceUtils";
import * as TelemetryWrapper from "vscode-extension-telemetry-wrapper";

const LATEST_VERSION = "latest";
const versionPattern = /^v\d+(\.\d+){2}\.json$/;
let topLevelTypes = ["output", "provider", "resource", "variable", "data"];
let topLevelRegexes = topLevelTypes.map(o => {
return {
Expand Down Expand Up @@ -53,7 +57,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
token: CancellationToken;
resourceType: string | null = null;

public provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken, context: TerraformCompletionContext): CompletionItem[] {
public async provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken, context: TerraformCompletionContext): Promise<CompletionItem[]> {
this.document = document;
this.position = position;
this.token = token;
Expand All @@ -62,7 +66,7 @@ export class TerraformTipsProvider implements CompletionItemProvider {
const lineText = document.lineAt(position.line).text;
const lineTillCurrentPosition = lineText.substring(0, position.character);

// Are we trying to type a top type?
// handle top level definition
if (this.isTopLevelType(lineTillCurrentPosition)) {
return this.getTopLevelType(lineTillCurrentPosition);
}
Expand Down Expand Up @@ -102,7 +106,9 @@ export class TerraformTipsProvider implements CompletionItemProvider {
// We're trying to type the exported field for the let
const resourceType = parts[0];
let resourceName = parts[1];
loadResource().then(tips => {
try {
// async load resource config
const tips = await loadResource();
const resources = tips.resource;
let attrs = resources[resourceType].attrs;
let result = _.map(attrs, o => {
Expand All @@ -113,19 +119,19 @@ export class TerraformTipsProvider implements CompletionItemProvider {
return c;
});
return result;
}).catch(error => {
console.error("Can not load resource from json.");
return;
});

} catch (error) {
console.error(`Can not load resource from json. error:[${error}]`);
}
}

// Which part are we completing for?
return [];
}

// Are we trying to type a parameter to a resource?
let possibleResources = this.checkTopLevelResource(lineTillCurrentPosition);
// typing a resource type
let possibleResources = await this.checkTopLevelResource(lineTillCurrentPosition);
// handle resource type
if (possibleResources.length > 0) {
return this.getHintsForStrings(possibleResources);
}
Expand All @@ -138,40 +144,43 @@ export class TerraformTipsProvider implements CompletionItemProvider {
if (endwithEqual) {
const lineBeforeEqualSign = lineTillCurrentPosition.substring(0, includeEqual).trim();
// load options
loadResource().then(tips => {
try {
// async load resource config
const tips = await loadResource();
const name = lineBeforeEqualSign;
const resources = tips.resource;
const argStrs = this.findArgByName(resources[this.resourceType].args, name);
const options = this.getOptionsFormArg(argStrs);
// clear resource type
this.resourceType = "";
return (options).length ? options : [];
}).catch(error => {
console.error("Can not load resource from json.");
return [];
});
} catch (error) {
console.error(`Can not load resource from json. error:[${error}]`);
}
}
this.resourceType = "";
return [];
}

// Check if we're in a resource definition
// handle argument
if (includeEqual < 0 && !endwithEqual) {
// we're not in options case
for (let i = position.line - 1; i >= 0; i--) {
let line = document.lineAt(i).text;
let parentType = this.getParentType(line);
if (parentType && parentType.type === "resource") {
// typing a arg in resource
// typing a argument in resource
const resourceType = this.getResourceTypeFromLine(line);
loadResource().then(tips => {
try {
// async load resource config
const tips = await loadResource();
const resources = tips.resource;
const ret = this.getItemsForArgs(resources[resourceType].args, resourceType);
return ret;
}).catch(error => {
console.error("Can not load resource from json.");
} catch (error) {
console.error(`Can not load resource from json. error:[${error}]`);
return [];
});
}
}
else if (parentType && parentType.type !== "resource") {
// We don't want to accidentally include some other containers stuff
Expand Down Expand Up @@ -281,12 +290,15 @@ export class TerraformTipsProvider implements CompletionItemProvider {
return "";
}

checkTopLevelResource(lineTillCurrentPosition: string): any[] {
async checkTopLevelResource(lineTillCurrentPosition: string): Promise<any[]> {
let parts = lineTillCurrentPosition.split(" ");
if (parts.length === 2 && parts[0] === "resource") {
let r = parts[1].replace(/"/g, '');
let regex = new RegExp("^" + r);
loadResource().then(tips => {
// handle resource
try {
// async load resource config
const tips = await loadResource();
const resources = tips.resource;
let possibleResources = _.filter(_.keys(resources), k => {
if (regex.test(k)) {
Expand All @@ -295,11 +307,10 @@ export class TerraformTipsProvider implements CompletionItemProvider {
return false;
});
return possibleResources;
}).catch(error => {
console.error("Can not load resource from json.");
} catch (error) {
console.error(`Can not load resource from json. error:[${error}]`);
return [];
});

}
}
return [];
}
Expand Down Expand Up @@ -360,12 +371,14 @@ export class TerraformTipsProvider implements CompletionItemProvider {

async function sortJsonFiles(dir: string) {
const files = fs.readdirSync(dir);
const jsonFiles = files.filter(file => path.extname(file) === '.json');
const jsonFiles = files.filter(file => path.extname(file) === '.json' && versionPattern.test(file));
// const jsonFiles: string[] = ["v1.81.50.json", "v1.81.54.json"]; // debug

// import files
const versions = await Promise.all(jsonFiles.map(async file => {
const jsonPath = path.join(dir, file);
const json = await import(jsonPath);
const jsonPath = path.join("../config/tips/", file);
// const json = await import(jsonPath);
const json = require(jsonPath);
const version = json.version as string;
return {
json,
Expand Down Expand Up @@ -396,26 +409,35 @@ function compareVersions(a, b) {
return 0;
}

// load resource config from json files based on the appropriate version
async function loadResource(): Promise<Tips> {
let tfVersion: string;
await executeCommandByExec("terraform version").then(output => {
let match = output.match(/tencentcloudstack\/tencentcloud\ (v\d+\.\d+\.\d+)/);
const cwd = workspaceUtils.getActiveEditorPath();
if (!cwd) {
TelemetryWrapper.sendError(Error("noWorkspaceSelected"));
console.error(`can not get path from active editor`);
}

await executeCommandByExec("terraform version", cwd).then(output => {
let match = RegExp(/tencentcloudstack\/tencentcloud (v\d+\.\d+\.\d+)/).exec(output);

if (match) {
tfVersion = match[1];
} else {
tfVersion = "latest";
tfVersion = LATEST_VERSION;
}
console.log(`version:v${tfVersion}`); //1.81.54
console.log(`tf provider version:[${tfVersion}],cwd:[${cwd}]`); //like: 1.81.54
}).catch(error => {
console.error(`execute terraform version failed: ${error}`);
});

const tipFiles = await sortJsonFiles("../../config/tips");
const tipsDir = path.join(__dirname, '..', 'config', 'tips');
const tipFiles = await sortJsonFiles(tipsDir);
let result: Tips | null = null;

tipFiles.some(file => {
if (compareVersions(tfVersion, file.version) >= 0) {
console.log(`loaded json version:${file.version}`);
result = file.json as Tips;
return true;
}
Expand Down
6 changes: 1 addition & 5 deletions src/client/runner/terraformRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,8 @@ export class TerraformRunner extends BaseRunner {
);
}

public async executeVersion(params: type): Promise<string> {

}

public async preImport(cwd: string, args: any, file: string): Promise<{ importArgs: string, tfFile: string }> {
const fileName = (file === undefined) ? args.resource.type + '.tf' : file;
const fileName = file ?? args.resource.type + '.tf';

const defaultContents = `resource "${args.resource.type}" "${args.resource.name}" {}`;
const resAddress = `${args.resource.type}.${args.resource.name}`;
Expand Down
7 changes: 5 additions & 2 deletions src/utils/cpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ export async function executeCommand(command: string, args: string[], options: c
});
}

export async function executeCommandByExec(command: string): Promise<string> {
export async function executeCommandByExec(command: string, cwd?: string): Promise<string> {
return new Promise((resolve, reject) => {
cp.exec(command, (error, stdout, stderr) => {
const options = {
cwd,
};
cp.exec(command, options, (error, stdout, stderr) => {
if (error) {
reject(`child_process exec failed: error:[${error}], stderr:[${stderr}]`);
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/gitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export class GitUtils {

public async submitToGit(): Promise<any> {
console.debug("[DEBUG]#### GitUtils submitToGit begin.");
const activeDocumentPath = workspaceUtils.getActiveEditorPath();
const gitRootPath = path.dirname(activeDocumentPath);
const gitRootPath = workspaceUtils.getActiveEditorPath();
if (!gitRootPath) {
vscode.window.showErrorMessage('Please open a workspace folder first!');
return;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as _ from "lodash";
import * as vscode from "vscode";
import { DialogOption, showFolderDialog } from "./uiUtils";
import * as path from "path";

export async function selectWorkspaceFolder(): Promise<string | undefined> {
let folder: vscode.WorkspaceFolder;
Expand Down Expand Up @@ -54,6 +55,6 @@ export function getActiveEditorPath(): string {
return "";
}

const activeDocumentPath = activeDocument.uri.fsPath;
const activeDocumentPath = path.dirname(activeDocument.uri.fsPath);
return activeDocumentPath;
}

0 comments on commit 045d8de

Please sign in to comment.