VZ File Templates extension adds single and multi-file templates to Visual Studio Code. It allows developer to select a template in a visual way, just like in the big Visual Studio. It can work in 2 modes - new project and new project item creation.
To invoke template selection, simply right click on folder or file in vs code file explorer and choose "New File from Template" menu item.
To create new project from template, go to command palette (Ctrl+Shift+P) and select/type "VZ File Templates: New Project From Template" command. If there is a workspace opened in VS Code, new project will be created in the root folder. If workspace is not open, template selection page will show target path field and "Create directory for project" checkbox allowing to create project subfolder in that folder. Default value of destination path can be entered into "vzfiletemplates.defaultProjectsFolder" setting.
Templates are loaded from this extension resources and from user defined folders specified in "vzfiletemplates.userTemplatesFolders" setting. Sample setting can look like this one:
"vzfiletemplates.userTemplatesFolders": [
"d:\\vscode\\templates"
]
Relative paths will use current workspace root folder as the root.
It will also be possible to run additional wizard, specific to selected template in the next versions of this extension (i.e. show a page to collect asp.net view details and generate code).
New templates can also be added using another Visual Studio Code extension, 2 sample projects explaining how to do it can be found on GitHub. First one shows how to add simple templates and is available here:
https://github.com/visualzoran/vz-templates-sample-ext
Second one shows how to create multifile template that generates code and shows a wizard and is available here:
https://github.com/visualzoran/vs-template-wizardsample-ext
Each template has to be saved in a separate folder as template definition file has to be named "template.json". Basic, single file template requires 4 separate files: definition, template file, dark template icon and light template icon:
New templates can be created manually, but they can also be created using template available under "Other" category:
Here is sample "template.json" file:
{
"name" : "TypeScript class",
"description" : "New TypeScript class",
"defaultName" : "NewClass.ts",
"sortOrder" : "100",
"category" : "TypeScript",
"iconLight" : "icon-light.svg",
"iconDark" : "icon-dark.svg",
"elements" : [
{
"fileName" : "file.txt",
"targetName" : "$itemname$.ts",
"replaceParameters" : true,
"open" : true
}
],
"isProject" : false,
"command" : "",
"commandParameters" : [],
"openFiles" : [],
"settingsProcessors" : []
}
At this moment only these variables are supported:
Additional variables can be added by implementing and registering settings processors and then listing their names in "settingsProcessors" property inside template.
This extension contributes the following settings:
vzfiletemplates.userTemplatesFolders
: array of paths to folders containing user templates. Template manager scans all subfolders in these locations, so the only reason to specify more than one entry here is when templates are stored in completely separate folders (i.e. user templates and team templates)vzfiletemplates.userTemplateVariables
: object to define workspace or user dependent variables. You can define variable as {"varName>":"varValue"}
, and then - use it in your template just as $varName$
to substitute your value.vzfiletemplates.pathConversion
: how template must convert paths for substitution. For instance - you are working under Windows OS on a project, which is designed for Linux OS. If you leave the paths 'as is' it will appear in template with Win32 path separator characters \\
(because all of the paths resolved on Windows). It's not aesthetic. So this option could specify: posix
- convert all paths to Unix style; win32
- convert all paths to Windows style; leave
- leave 'as is' (default)vzfiletemplates.customVariablesConstructor
: path to javascript file, which must return object with the generated variables. It is useful, when you need the combinations of variables by some logic, or if you need to calculate some variable value depending on some conditions, or just use javascript for variable construction. Just... Use your fantasy). Could be absolute path or relative to current workspace root dir (if you need own constructor for each project). Usage examples see below..vzfiletemplates.defaultProjectsFolder
: default path for new projects created by "New Project from Template" command, when there is no open workspace in VS Code.All built in variables could be overridden with vzfiletemplates.userTemplateVariables
option in user settings or workspace settings.
For instance - your project has a name: My Awesome Project
. But for some kind of reason - the name of the workspace, you are using named as MyAwesomeProject_test_integration
or any... To be sure, that template will resolve the 'right' name of the project you could override this value by your own in settings section of workspace (file <projRoot>/.vscode/settings.json
):
{
...
"vzfiletemplates.userTemplateVariables": {
"workspacename":"My Awesome Project"
},
...
}
vzfiletemplates.customVariablesConstructor
option could provide an ability to create your own custom js file to calculate own template variables.
To do so - just specify this option in your userconfig or in workspace config json file as a path to *.js
file, which will return it:
For instance: in your <projRoot>/.vscode/settings.json
:
{
...
"vzfiletemplates.customVariablesConstructor": {
"workspacename":"./.vscode/customVars.js" // <-- path here - relative to the workspace (e.g - relative to the root of your repo (mostly:-))
},
...
}
Then in file <projRoot>/.vscode/customVars.js
:
module.exports={
//export createVariables function expecting list of variables as a parameter and create or update them inside
createVariables: function(variables) {
variables.myAwesomeVar = `My very awesome variable in project ${variables.workspacename}`;
// also here could be overriden any variablle (both built in or configurable custom var)
variables.myVariableAtUserConfig = "newValue";
variables.workspacepath = "/";
}
};
Then - you could use these variables by it's names in your templates.
[NOTE] - Be careful with long operations in custom constructor files - it may cause extension not very usable) [Other NOTE] - You could use only native JS in custom constructor file, and no dependency)))