Skip to main content

Automatic metadata formating

Header

Level: Beginner

Keywords: project, metadata, formatting, readable code

The result: automatic formatting of metadata in your project

This chapter describes how to configure the local implementation environment to maintain the same metadata formatting throughout the project automatically.

The recommended development environment (a tool for writing metadata) is Visual Studio Code with installed Format Files extension (by jbockle). Maintaining correctly formatted metadata throughout the whole project will only work if all project members use the same formatting settings (ideally if they use VSC and Format Files extension). Also, it is necessary to open root folders (e.g. packages repository root folder) containing .vscode folder with settings in your Visual Code Studio working environment.

Motivation​

  • not formatted code is difficult to read, navigate and search
  • if you do this manually, chances are, that each implementator will use different formatting
  • automatic code styling can potentially reduce bugs
  • this simple setup will do all the job for you

How to configure automatic metadata formatting?​

It is highly recommended to set automatic formatting of metadata files at the beginning of the project. This settings is in settings.json located under the .vscode folder.

.vscode/settings.json - configuration example
{
"editor.rulers": [
100
],
"editor.detectIndentation": false,
"editor.wordWrapColumn": 100,
"editor.formatOnSave": true,
"editor.tabSize" : 2
}

This configuration ensures that after saving the file, it will be automatically formated (before pushing to git).

warning

Be aware of formatting JS (JavaScript) files! Implementators who work with Scripting API are used to special kind of formatted code. Functionality described in this chpater could ruin JS code. Before you proceed to format JS files, consult this with JS implementators. To avoid formatting JS files, add this code to settings.json file:

.vscode/settings.json - avoid formatting JS files
{
...

"[javascript]": {
"editor.formatOnSave": false
}
}
tip

If this file does not already exist, create a settings.json file in the .vscode folder and commit it. This settings file must be in each project and metadata packages (configuration) folder.

gitlab/customers
└───my_new_customer
└───my_new_project # My project
│ └───.vscode
│ │ └───settings.json
│ └───environments
│ └───metadata
│ ...
└───metadata-packages
└───portal # My package
│ └───.vscode
│ │ └───settings.json
│ └───gateway
│ └───gateway
│ └───lids-as
│ ...
└───requests # My package
└───.vscode
└───settings.json
...
warning

If you configure this formatting in the middle of the project, the change will apply only after saving specific file and will produce commits with changes only in formatting. Be aware, that this will affect your project git history. It is recommended to do one time formatting of all files in the project, see the following guide.

How to apply formatting to all project files at once?​

It is also possible to format all configuration files one time. Recommended way is to use the Format Files extension (by jbockle) in Visual Code Studio.

  1. Download Format Files extension.
  2. Open your project workspace. It is important that you have all and whole your project package repositories here.
  3. Press SHIFT + CTRL + P and run Start Format Files: From Glob.
  4. Set Format Files matching glob pattern to: **/*.json (for JSON files) or **/*.js (for JavaScript files). This will format all your files (with the given extension) in all subfolders.
  5. Confirm (Yes).
  6. Confirm (Yes - Use 'formatFiles' settings in vscode).

This process will basically open each file and save it, which will trigger the formatting process. This can take quite a long time, depending on the amount of your configuration files. After the process is finished, you just need to commit and push all the changes to Git.