WIP: Add support for JSON Settings Files #2134
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
Very much a work in progress, but all (still applicable) tests pass and json files are usable wherever .psd1 settings file can be used.
A refactor of settings handling with the main goal being to add support for multiple settings file formats - adding JSON support. I also found the pre-pr settings implementation quite hard to follow. Lots of contradictory comments and a single class with a lot of responsibility.
Adjusted settings to have a data model class
SettingsData. TheSettingsfile then becomes a static helper class then takes responsibility for constructing them based on need:delegating to registered parsers when appropriate to parse settings files.
Defined an
ISettingsParserinterface with 2 implementations.JsonSettingsParserandPsd1SettingsParser.Hashtable->SettingsDatadone byHashtableSettingsConverter. These parsers are registered withSettingsclass and used to parse files. The first parser to be able to parse the file "wins". The parsers define the file extension they can parse.Internal location of Settings Presets moved from
Engine\Settings->Engine\SettingsPresetssoSettingsfolder can be used for all Settings logic and parsers. Presets are still shipped in the built module'sSettingsfolder.Internal location of Command Data Files moved from
Engine\Settings->Engine\CommandDataFiles. Separated out from the Settings Presets, as presets could now be in.jsonformat - which is what previously distinguished the command data files from presets. They are shipped with the module in a new folderCommandDataFilesand a helper function added to get this folder pathSettings.GetShippedCommandDataFileDirectory(). This is consumed byAvoidOverwritingBuiltInCmdletsandUseCompatibleCmdletswhich load and use the data files.Amendments to auto discovery to search for settings files in the current working directory based on registered parsers supported formats, in order. For instance with the
JsonSettingsParserandPsd1SettingsParserregistered (in that order) - Auto-Discovery searches forPSScriptAnalyzerSettings.jsonfirst, followed byPSScriptAnalyzerSettings.psd1.Amendments to preset resolution to support different settings file formats. You could define multiple presets with different extensions. Registered parser order would decide which was used.
Added lots of comments - not to everyone's taste, but it certainly helps me.
Still to look at:
SettingsDataobject for equivalent input. Can useSettings.Create('path\to\file\with\file.extension')helper to construct the object.New-ScriptAnalyzerSettingsFilecommand which will create aPSScriptAnalyzerSettingsfile for a project. Would have options for it to be based off of a preset, bare (just the structure), or include all current rules and settings (with their default values that you can then easily tweak and play with). Could extend the parser interface to include serialisation in that format. I've seen lots of people not know settings file exist for this tool. Having a first class command to create them is more discoverable than a docs page. Down the line you could imaging this functionality being surfaced in VSCode as a 'Create Settings File` command or similar.PR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.