Public/New-M365DeprecationAnalyzerSettings.ps1

function New-M365DeprecationAnalyzerSettings {
    <#
    .SYNOPSIS
        Creates a PSScriptAnalyzer settings file that shows deprecated M365 commands directly in VS Code.
 
    .DESCRIPTION
        Writes a PSScriptAnalyzerSettings.psd1 that loads the M365DeprecationScanner rules together
        with the default PSScriptAnalyzer rules. Point the VS Code setting
        "powershell.scriptAnalysis.settingsPath" at this file and deprecated commands are underlined
        while you type.
 
        The file contains the absolute path of the installed module. Re-run this command after
        updating the module to a new version folder.
 
    .PARAMETER Path
        Where to write the settings file. Default: .\PSScriptAnalyzerSettings.psd1
 
    .PARAMETER ExcludeDefaultRules
        Only run the deprecation rules, not the default PSScriptAnalyzer rules.
 
    .PARAMETER Force
        Overwrite an existing file.
 
    .EXAMPLE
        New-M365DeprecationAnalyzerSettings
 
        Creates .\PSScriptAnalyzerSettings.psd1 in the current folder.
 
    .EXAMPLE
        New-M365DeprecationAnalyzerSettings -Path "$HOME\PSScriptAnalyzerSettings.psd1" -Force
 
        Creates (or replaces) one settings file in your profile folder that you can use for all projects.
 
    .OUTPUTS
        System.IO.FileInfo
 
    .LINK
        https://github.com/matejpaunovski/M365DeprecationScanner/blob/main/docs/vscode.md
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'Named after the PSScriptAnalyzerSettings.psd1 file it creates.')]
    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([System.IO.FileInfo])]
    param(
        [ValidateNotNullOrEmpty()]
        [string]$Path = '.\PSScriptAnalyzerSettings.psd1',

        [switch]$ExcludeDefaultRules,

        [switch]$Force
    )

    $target = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
    Assert-WritableReportTarget -Path $target -Force:$Force

    # EscapeSingleQuotedStringContent also handles typographic quotes, which PowerShell treats as quotes too.
    $rulePath = [System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent((Get-M365DeprecationAnalyzerRulePath))
    $includeDefault = if ($ExcludeDefaultRules) { '$false' } else { '$true' }

    $content = @"
# PSScriptAnalyzer settings generated by M365DeprecationScanner.
# VS Code: set "powershell.scriptAnalysis.settingsPath" to this file.
# Docs: https://github.com/matejpaunovski/M365DeprecationScanner/blob/main/docs/vscode.md
@{
    CustomRulePath = @('$rulePath')
    IncludeDefaultRules = $includeDefault
}
"@


    if ($PSCmdlet.ShouldProcess($target, 'Create PSScriptAnalyzer settings file')) {
        $directory = Split-Path -Path $target -Parent
        if ($directory -and -not (Test-Path -LiteralPath $directory)) {
            $null = [System.IO.Directory]::CreateDirectory($directory)
        }
        [System.IO.File]::WriteAllText($target, $content, (New-Object System.Text.UTF8Encoding($false)))
        Get-Item -LiteralPath $target
    }
}