Skip to content

Commit

Permalink
Tsp 896 typescript changes to support node definitions changes (#77)
Browse files Browse the repository at this point in the history
typescript changes to support node definitions changes

---------

Co-authored-by: esarver <[email protected]>
  • Loading branch information
jharajeev55 and esarver authored Dec 4, 2024
1 parent 016ec0f commit 6586c96
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 107 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
uses: actions/checkout@v4
- name: Install dependencies
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
npm install
- name: Run eslint
run: 'npx eslint --rule "{ prettier/prettier: off }" src'
Expand All @@ -66,7 +66,7 @@ jobs:
uses: actions/checkout@v4
- name: Install Tools
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
npm ci --devDependencies
- name: Generate NPM BOM
run: npx @cyclonedx/cyclonedx-npm --output-format JSON --package-lock-only --output-reproducible --output-file npm.cdx.json
Expand All @@ -93,7 +93,7 @@ jobs:
uses: actions/checkout@v4
- name: Install dependencies
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
npm install
- name: Run Tests with Coverage
run: npx nyc --nycrc-path=.nycrc.json npm run test-ci
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
uses: actions/checkout@v4
- name: Install dependencies
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
npm install
- name: Build
run: npm run compile
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
Security -- in case of vulnerabilities.
-->

## [1.1.1]

### Added
- support dynamically creating enums for configured nodes

## [1.1.0]

### Added
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "Tektronix",
"displayName": "Keithley TSP Toolkit",
"description": "VSCode extension for Keithley Instruments' Test Script Processor",
"version": "1.1.0",
"version": "1.1.1",
"icon": "./resources/TSP_Toolkit_128x128.png",
"galleryBanner": {
"color": "#EEEEEE",
Expand Down Expand Up @@ -477,7 +477,7 @@
"typescript": "5.5.4"
},
"dependencies": {
"@tektronix/keithley_instrument_libraries": "0.18.2",
"@tektronix/keithley_instrument_libraries": "0.18.3",
"@tektronix/web-help-documents": "0.18.0",
"@types/cheerio": "0.22.35",
"cheerio": "1.0.0",
Expand Down
127 changes: 60 additions & 67 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ import {
IoType,
KicProcessMgr,
} from "./resourceManager"
import {
getClassName,
getNodeDetails,
updateNodeDetails,
} from "./tspConfigJsonParser"
import { getNodeDetails } from "./tspConfigJsonParser"
import {
configure_initial_workspace_configurations,
processWorkspaceFolders,
RELATIVE_TSP_CONFIG_FILE_PATH,
updateConfiguration,
} from "./workspaceManager"
import { Log, SourceLocation } from "./logging"
Expand Down Expand Up @@ -366,88 +361,86 @@ export async function onDidChangeTspConfigFile(uri: vscode.Uri) {
* @param textDocument text document path
*/
async function onDidSaveTextDocument(textDocument: vscode.TextDocument) {
// Perform actions when a text document is saved
const workspace_path = vscode.workspace.getWorkspaceFolder(textDocument.uri)
const filePath = textDocument.uri.fsPath
if (filePath.endsWith("config.tsp.json") && fs.existsSync(filePath)) {
const nodeDetails = getNodeDetails(filePath)

if (
filePath.endsWith("config.tsp.json") &&
fs.existsSync(filePath) &&
workspace_path
) {
const new_library_settings: string[] = []
let nodeStr = ""
new_library_settings.push(join(COMMAND_SETS, "tsp-lua-5.0"))

const lua_definitions_folder_path = join(
COMMAND_SETS,
"nodes_definitions",
)
if (fs.existsSync(lua_definitions_folder_path)) {
fs.rmSync(lua_definitions_folder_path, {
recursive: true,
force: true,
})
}
fs.mkdirSync(lua_definitions_folder_path, { recursive: true })
const nodeDetails = getNodeDetails(filePath)

const supported_models = fs
.readdirSync(COMMAND_SETS)
.filter((folder) =>
fs.statSync(`${COMMAND_SETS}/${folder}`).isDirectory(),
)

for (const [model, nodes] of Object.entries(nodeDetails)) {
// supported model
const supported_models = fs
.readdirSync(COMMAND_SETS)
.filter((folder) =>
fs.statSync(`${COMMAND_SETS}/${folder}`).isDirectory(),
)
if (!supported_models.includes(model.toUpperCase())) {
void vscode.window.showInformationMessage(
`${model} model is not supported`,
)
if (workspace_path)
await updateConfiguration(
"Lua.workspace.library",
[join(COMMAND_SETS, "tsp-lua-5.0")],
vscode.ConfigurationTarget.WorkspaceFolder,
workspace_path,
false,
)
return
}
const lib_base_path = join(COMMAND_SETS, model.toUpperCase())

const lib_base_path = join(COMMAND_SETS, model.toUpperCase())
new_library_settings.push(join(lib_base_path, "Helper"))

if (nodes.some((str) => str.includes("self"))) {
new_library_settings.push(join(lib_base_path, "AllTspCommands"))
}
if (nodes.some((str) => str.includes("node"))) {
new_library_settings.push(
join(lib_base_path, "tspLinkSupportedCommands"),
)
}
const className = getClassName(
join(
lib_base_path,
"tspLinkSupportedCommands",
"nodeTable.lua",
),
)
nodes.forEach((node: string) => {
if (node.includes(".")) {
// slot configuration can be handled here
} else {
if (node.includes("node")) {
const node_num = parseInt(
node.match(/\d+/)?.[0] || "",
10,
)
nodeStr =
nodeStr + `node[${node_num}] = ${className}\n`
}

nodes.forEach((node) => {
if (node.includes("node")) {
const node_num = parseInt(node.match(/\d+/)?.[0] || "", 10)
const node_cmd_file_path = join(
lib_base_path,
"tspLinkSupportedCommands",
"definitions.txt",
)
const node_cmd_file_content = fs
.readFileSync(node_cmd_file_path, "utf8")
.replace(/\$node_number\$/g, node_num.toString())
const new_node_cmd_file_path = join(
lua_definitions_folder_path,
`${model}_node${node_num}.lua`,
)
fs.writeFileSync(
new_node_cmd_file_path,
node_cmd_file_content,
)
}
})
}

// open workspace nodeTable.lua file and update node details in it
// check if lua_definitions_folder_path is not empty

if (workspace_path != undefined) {
updateNodeDetails(
join(
workspace_path.uri.fsPath,
RELATIVE_TSP_CONFIG_FILE_PATH,
"nodeTable.tsp",
),
nodeStr,
)
new_library_settings.push(join(COMMAND_SETS, "tsp-lua-5.0")) // including Lua 5.0 definitions folder path
await updateConfiguration(
"Lua.workspace.library",
new_library_settings,
vscode.ConfigurationTarget.WorkspaceFolder,
workspace_path,
true,
)
if (fs.readdirSync(lua_definitions_folder_path).length !== 0) {
new_library_settings.push(lua_definitions_folder_path)
}
await updateConfiguration(
"Lua.workspace.library",
new_library_settings,
vscode.ConfigurationTarget.WorkspaceFolder,
workspace_path,
true,
)
}
}

Expand Down
16 changes: 0 additions & 16 deletions src/tspConfigJsonParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from "fs"
import { nodesTableHeader } from "./workspaceManager"

interface Slot {
[slotNumber: string]: string
Expand Down Expand Up @@ -63,21 +62,6 @@ export function getNodeDetails(filePath: string): Record<string, string[]> {
return output
}

/**
* Update node details in provide lua file
* @param file_path file path
* @param node_details node details
*/
export function updateNodeDetails(
file_path: string,
node_details: string,
): void {
// Read the file content
// Write the updated content back to the file
fs.writeFileSync(file_path, nodesTableHeader + node_details, "utf-8")
console.log("File updated successfully.")
}

export function getClassName(file_path: string): string {
// Read the file content
const content = fs.readFileSync(file_path, "utf-8")
Expand Down
23 changes: 5 additions & 18 deletions src/workspaceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ let supported_models: string[] = fs
// Remove "tsp-lua-5.0" from supported_models if it exists, because its am lua 5.0 library not a model
supported_models = supported_models.filter((model) => model !== "tsp-lua-5.0")

// Remove "nodes_definitions" from supported_models if it exists, because this folder is getting created at runtime to manager node definitions
supported_models = supported_models.filter(
(model) => model !== "nodes_definitions",
)

export const RELATIVE_TSP_CONFIG_FILE_PATH = path.join(".vscode", "tspConfig")

const tspSchemaContent = `{
Expand Down Expand Up @@ -69,13 +74,6 @@ const tspConfigJsonContent = `{
"self": ""
}`

export const nodesTableHeader = `
-- !!! DO NOT EDIT !!!
-- Auto-generated script
-- To enable language features for specific model, edit the config.tsp.json file in this folder.
`

/**
* Create default ".vscode/tspConfig" folder in root level directory of workspace
* if doesn't exist.
Expand Down Expand Up @@ -146,17 +144,6 @@ function createTspFileFolder(folderPath: string) {
tspSchema,
Buffer.from(tspSchemaContent),
)
const nodeTable = vscode.Uri.file(
path.join(
folderPath,
RELATIVE_TSP_CONFIG_FILE_PATH,
"nodeTable.tsp",
),
)
await vscode.workspace.fs.writeFile(
nodeTable,
Buffer.from(nodesTableHeader),
)
},
)
}
Expand Down

0 comments on commit 6586c96

Please sign in to comment.