Functions/GenXdev.Coding.PowerShell.Modules/Assert-GenXdevDependencyUsage.ps1

# Don't remove this line [dontrefactor]

################################################################################
<#
.SYNOPSIS
Validates dependency usage across GenXdev modules to ensure proper module
hierarchy is maintained.

.DESCRIPTION
This function analyzes GenXdev modules to ensure they follow the correct
dependency hierarchy. It checks that modules only reference dependencies
that are listed in their RequiredModules manifest, and prevents circular
dependencies by validating that modules don't reference modules that come
later in the dependency chain.

.LICENSE
Copyright (C) 2026 René Vaessen / GenXdev

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

.PARAMETER ModuleName
Filter to apply to module names. Must match GenXdev naming pattern. Defaults
to checking all GenXdev modules.

.PARAMETER FromScripts
Search in script files instead of module files.

.EXAMPLE
Assert-GenXdevDependencyUsage -ModuleName "GenXdev.Coding"

.EXAMPLE
checkgenxdevdependencies "GenXdev*" -FromScripts
#>

function Assert-GenXdevDependencyUsage {

    [CmdletBinding()]
    [Alias('checkgenxdevdependencies')]

    param(
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            Position = 1,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'Filter to apply to module names'
        )]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern('^(GenXdev|GenXde[v]\*|GenXdev(\.[\w\*\[\]\?]*)+)+$')]
        [SupportsWildcards()]
        [string[]] $ModuleName = @('GenXdev*'),
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Search in script files instead of module files'
        )]
        [switch] $FromScripts
        ###############################################################################
    )

    begin {

        # retrieve ordered list of all genxdev modules by dependency hierarchy
        $dependencies = @(GenXdev\Get-GenXDevNewModulesInOrderOfDependency |
                Microsoft.PowerShell.Core\ForEach-Object ModuleName) +
        @('GenXdev.Local')
    }

    process {

        # temporarily disabled functionality - early return
        return;

        # invoke processing on each matching genxdev module
        GenXdev\Invoke-OnEachGenXdevModule `
            -ModuleName:$ModuleName `
            -FromScripts:$FromScripts `
            -OnlyPublished `
            -NoLocal `
            -ScriptBlock {

            param($module)

            # extract module name from current module object
            $moduleName = $module.Name;

            # construct path to module manifest file
            $moduleManifestPath = GenXdev\Expand-Path (
                ".\$moduleName.psd1")

            # load module manifest data for dependency analysis
            $moduleManifest = Microsoft.PowerShell.Utility\Import-PowerShellDataFile `
                -LiteralPath $moduleManifestPath

            # find current module's position in dependency hierarchy
            $index = $dependencies.IndexOf($moduleName)

            # validate module exists in dependency list
            if ($index -lt 0) {

                Microsoft.PowerShell.Utility\Write-Error (
                    "Module $moduleName not found in dependencies list")
                return
            }

            # check for invalid references to modules later in dependency chain
            for ($i = $index + 1; $i -lt $dependencies.Count; $i++) {

                # get dependency name from current position
                $dependency = $dependencies[$i]

                # extract module name if dependency is object with modulename property
                if ($null -ne $dependency.ModuleName) {

                    $dependency = $dependency.ModuleName
                }

                # log dependency checking activity
                Microsoft.PowerShell.Utility\Write-Verbose (
                    "Checking if $moduleName references $dependency")

                # search for references to this dependency in module files
                $references = GenXdev\Find-Item `
                    -PassThru `
                    -SearchMask '.\*.ps1' `
                    -Pattern "$([System.Text.RegularExpressions.Regex]::Escape($dependency))\\" `
                    -ErrorAction 'SilentlyContinue' |
                    Microsoft.PowerShell.Core\ForEach-Object FullName

                # process any found references
                if ($references) {

                    # skip test files as they may reference other modules for testing
                    if ($references -like '*.Tests.ps1') {

                        continue
                    }

                    # analyze each reference file for dependency violations
                    $references |
                        Microsoft.PowerShell.Core\ForEach-Object {

                            # read file content to check for allowed reference patterns
                            [string] $content = [IO.File]::ReadAllText($_)

                            # skip files with install-module commands or allowed local references
                            if ($content.Contains("Install-Module $dependency") -or
                                $content.Contains('GenXdev\KeyValueStores') -or
                                $content.Contains("`"`$($MyInvocation.MyCommand.Module.ModuleBase)\..\..\GenXdev.Local\")) {

                                return
                            }

                            # report dependency violation
                            Microsoft.PowerShell.Utility\Write-Error (
                                "Module $moduleName references $dependency in file $_")
                        }
                }
            }

            # check for missing dependencies in module manifest
            for ($i = 0; $i -lt $index; $i++) {

                # get dependency name from current position
                $dependency = $dependencies[$i]

                # extract module name if dependency is object with modulename property
                if ($null -ne $dependency.ModuleName) {

                    $dependency = $dependency.ModuleName
                }

                # log dependency checking activity
                Microsoft.PowerShell.Utility\Write-Verbose (
                    "Checking if $moduleName references $dependency")

                # search for references to this dependency in module files
                $references = GenXdev\Find-Item `
                    -PassThru `
                    -SearchMask '.\*.ps1' `
                    -Pattern "$([System.Text.RegularExpressions.Regex]::Escape($dependency))\\" `
                    -ErrorAction 'SilentlyContinue' |
                    Microsoft.PowerShell.Core\ForEach-Object FullName

                # process any found references
                if ($references) {

                    # skip test files as they may reference other modules for testing
                    if ($references -like '*.Tests.ps1') {

                        continue
                    }

                    # analyze each reference file for missing manifest entries
                    $references |
                        Microsoft.PowerShell.Core\ForEach-Object {

                            # check if dependency is properly declared in module manifest
                            $hasDependency = ($dependency -eq $moduleName) -or
                            (@($moduleManifest.RequiredModules.ModuleName |
                                    Microsoft.PowerShell.Core\Where-Object {
                                        $_ -like $dependency }).Count -gt 0);

                                # validate dependency declaration
                                if (-not $hasDependency) {

                                    # allow install-module references without manifest declaration
                                    if ([IO.File]::ReadAllText($_).Contains("Install-Module $dependency")) {

                                        Microsoft.PowerShell.Utility\Write-Verbose (
                                            ("Module $moduleName references $dependency in file, " +
                                            "but has Install-Module $dependency in file. File: $_"))
                                        return
                                    }

                                    # report missing dependency in module manifest
                                    Microsoft.PowerShell.Utility\Write-Error (
                                        ("Module $moduleName references $dependency in file, " +
                                        "but has module $dependency not listed in " +
                                        "RequiredModules of $moduleManifestPath. File: $_"))
                                }
                            }
                }
            }
        }
    }

    end {
    }
}
################################################################################