Custom Scripts
DVT Custom Scripts are scripts run as tasks with access to the Visual Studio Code and JavaScript APIs, having the purpose of automating jobs in your environment.
They are created based on automatically collected scripts that fall into two categories:
Startup Scripts
These can be defined: - in the .dvt directory in the project, by creating startup.dvt.js or *.startup.dvt.js files - by pointing the
DVT_CODE_STARTUP_SCRIPT
environment variable to a script without naming constraintsThese are run automatically at startup, when the DVT.customScripts.automaticallyRunAtStartup preference is enabled.
Custom Scripts
*.dvt.js files that can be defined anywhere in the project.
Examples:
Change the value of a preference at startup
(async () => {
print("Value of 'editor.insertSpaces' before: " + vscode.workspace.getConfiguration().get("editor.insertSpaces"));
await vscode.workspace.getConfiguration().update("editor.insertSpaces", false, true);
print("Value of 'editor.insertSpaces' after: " + vscode.workspace.getConfiguration().get("editor.insertSpaces"));
})();
print(<message>)
- print messages in the terminalvscode.workspace.getConfiguration().get(<setting_id>)
- get the value of setting_id from configurationvscode.workspace.getConfiguration().update(<setting_id>, <value>, <configuration_target>)
- update the value of setting_id in the configuration_target, where:setting_id is a string corresponding to the ID of the setting
value is an object corresponding to the new value of the setting
- configuration_target can be:
true - updates Global settings
false - updates Workspace settings
undefined or null - updates to Workspace folder settings if configuration is resource specific, otherwise to Workspace settings
Format files from a directory
(async () => {
const files = await vscode.workspace.findFiles("**/<relative_directory_path>/*.sv");
for (const file of files) {
print("Formatting " + file.path + "...");
await vscode.window.showTextDocument(file);
await vscode.commands.executeCommand('editor.action.formatDocument');
await vscode.commands.executeCommand('workbench.action.files.save');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
};
})();
vscode.workspace.findFiles(<search_pattern>)
- find all the files that match the search_pattern (a glob pattern that defines the files to search for)vscode.window.showTextDocument(<file_URI>)
- open the file with the file_URIvscode.commands.executeCommand(<command_ID>, <arguments>)
- executes the command denoted by the given command_ID, to which you can optionally pass arguments
To configure the script being run for a specific task, you can define a custom task with input variables in the tasks.json file. This file can be automatically generated using the Tasks: Configure Tasks command. Select Create tasks.json file from template, then Others and a tasks.json file is generated in the .vscode directory, containing a generic task which you can further tune in order to run a custom script.
{
"version": "2.0.0",
"tasks": [
{
"label": "DVT Custom Script",
"type": "shell",
"command": "${input:run_script_with_args}",
}
],
"inputs": [
{
"type": "command",
"id": "run_script_with_args",
"command": "dvt.custom.script",
"args": [
"custom_script.js",
"arg_1",
"arg_2"
]
}
]
}
The task’s properties have the following semantic:
label
is an arbitrary name for the task, used in the user interfacetype
can be shell or processcommand
must have the ${input:<variable_id>} syntax
Also, additional configuration attributes are necessary for the input entry:
type
must be commandid
must be the same with <variableID> from the command field of the taskcommand
must be dvt.custom.scriptargs
must be a list containing the path to the script as the first element and the arguments which will be passed on to the script
Note
The script for the command can be specified using:
an absolute path
a path containing environment variables
a relative path which will be solved based on the current workspace
Example - Automatically generate the build configuration file
{
"version": "2.0.0",
"tasks": [
{
"label": "Generate default.build",
"type": "shell",
"command": "${input:DVT_Custom_Script}"
}
],
"inputs": [
{
"id": "DVT_Custom_Script",
"type": "command",
"command": "dvt.custom.script",
"args": [
".dvt/gen_default_build.dvt.js",
"ius.irun",
"dvt_filelist.f",
"subsystem_top"
]
}
]
}
(async () => {
const buildConfigPath = path.join(vscode.workspace.rootPath, <path_to_build_config>);
let buildConfigContent = "";
buildConfigContent += "+dvt_init+" + args[0] + "\n";
buildConfigContent += "-f " + args[1] + "\n";
buildConfigContent += "-top " + args[2] + "\n";
fs.writeFileSync(buildConfigPath, buildConfigContent);
})();
args
- the arguments passed on to the dvt.custom.script commandfs.writeFileSync(<file_path>, <file_content>)
- writes file_content to the file at file_path and creates it if it does not exist