visualzoran / vz-file-templates

File template manager for Visual Studio Code
MIT License
19 stars 10 forks source link

VZ File Templates for Visual Studio Code

Features

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.

Creating new project item from template

To invoke template selection, simply right click on folder or file in vs code file explorer and choose "New File from Template" menu item.

Template selection

Creating new project from template

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

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

Template definition

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:

Creating new template from template

Template file

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" : []
}

Template variables

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.

Extension Settings

This extension contributes the following settings:

Tips and tricks with template variables

Variables overriding

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"
    },
    ...
}

Own variables constructor

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)))

Contributors

Release Notes

1.0.4

1.0.3

1.0.2

1.0.1

1.0.0

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

0.0.1