Netscoot.Core/Public/Move-DotnetFile.ps1
|
function Move-DotnetFile { <# .SYNOPSIS Move a single managed .NET file and reconcile references, routing by extension to the right specialist. The front door for file moves in the .NET family. .DESCRIPTION Dispatches a managed .NET file to the right specialist by extension (see Output for the routing). Native (.vcxproj), PowerShell (.ps1/.psd1) and Unity assets are deliberately not handled here - use Move-NativeProject / Move-PowerShellScript / Move-PowerShellModule / Move-UnityAsset. -WhatIf/-Confirm/-Verbose propagate to the specialist, and -Force and -RepositoryRoot/-NoBuild are forwarded where the specialist accepts them. .PARAMETER Path The .NET file to move. Accepts a path string or a Get-ChildItem/Get-Item item from the pipeline, and rejects other object types. .PARAMETER Destination New path (file or folder), following `git mv` rules, passed through to the specialist. For a project file, Destination is the project's new folder, and the whole folder moves. .PARAMETER RepositoryRoot Repository root the specialist scans for references. Defaults to the enclosing git repository root. Not used for a solution file. .PARAMETER NoBuild Skip the verifying 'dotnet build' (forwarded to Move-DotnetProject). .PARAMETER Force When git is not installed, move with a plain PowerShell `Move-Item` without asking first. Without -Force it asks before falling back. The plain move does not preserve git history. .PARAMETER NoJournal Skip recording this move in the undo journal for this call (forwarded to the specialist), even when journaling is enabled. .OUTPUTS The result object from the .NET specialist it routes to, by file extension. .EXAMPLE # A project file routes to Move-DotnetProject Move-DotnetFile -Path ./src/Tarragon/Tarragon.csproj -Destination ./libs/Tarragon # A solution routes to Move-Solution (rebases stored project paths) Move-DotnetFile -Path ./Demo.slnx -Destination ./build/Demo.slnx # A shared import routes to Move-MSBuildImport (fixes <Import> in consumers) Move-DotnetFile -Path ./Shared.props -Destination ./build/Shared.props #> # SupportsShouldProcess so -WhatIf/-Confirm bind and propagate to the specialist; this # dispatcher only routes (the specialist calls ShouldProcess), so suppress the rule here. [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '', Justification = 'Delegates to a specialist cmdlet that calls ShouldProcess')] [CmdletBinding(SupportsShouldProcess)] [OutputType('Netscoot.MoveResult', 'Netscoot.SolutionMoveResult', 'Netscoot.ImportMoveResult')] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline)] [Netscoot.PathInputTransform()] [ValidateNotNullOrEmpty()] [string]$Path, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$Destination, [string]$RepositoryRoot, [switch]$NoBuild, [switch]$Force, [switch]$NoJournal ) process { $full = Resolve-FullPath $Path if (-not (Test-Path -LiteralPath $full -PathType Leaf)) { $PSCmdlet.WriteError([System.Management.Automation.ErrorRecord]::new( [System.IO.FileNotFoundException]::new("File not found: $Path"), 'FileNotFound', [System.Management.Automation.ErrorCategory]::ObjectNotFound, $Path)) return } $ext = ([System.IO.Path]::GetExtension($full)).ToLowerInvariant() switch -regex ($ext) { '\.(cs|fs|vb)proj$' { # Specialist takes -Project (not -Path). Drop the dispatcher's Path, add Project=$full. $fwd = New-ForwardArgs $PSBoundParameters -Drop 'Path' -Add @{ Project = $full } Write-Verbose "Routing $ext -> Move-DotnetProject" Move-DotnetProject @fwd } '\.slnx?$' { # Move-Solution does not take RepositoryRoot/NoBuild. $fwd = New-ForwardArgs $PSBoundParameters -Drop 'RepositoryRoot', 'NoBuild' -Add @{ Path = $full } Write-Verbose "Routing $ext -> Move-Solution" Move-Solution @fwd } '\.(props|targets)$' { # Move-MSBuildImport does not take NoBuild. $fwd = New-ForwardArgs $PSBoundParameters -Drop 'NoBuild' -Add @{ Path = $full } Write-Verbose "Routing $ext -> Move-MSBuildImport" Move-MSBuildImport @fwd } default { $PSCmdlet.WriteError([System.Management.Automation.ErrorRecord]::new( [System.NotSupportedException]::new("Not a managed .NET file: $Path. Use Move-NativeProject (.vcxproj), Move-PowerShellScript (.ps1), Move-PowerShellModule (.psd1), or Move-UnityAsset."), 'NotADotnetFile', [System.Management.Automation.ErrorCategory]::InvalidArgument, $Path)) } } } } |