Private/VSCode/Copy-VSCodeFile.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
function Copy-VSCodeFile { [CmdletBinding()] param ( # The destination to the repo [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0 )] [string] $RepoPath, # The source file to copy [Parameter( Mandatory = $true, Position = 1 )] [ValidateNotNullOrEmpty()] [string] $VSCodeFile ) # Try to be clever and strip out the .vscode directory if it has been passed. if ($RepoPath -match "\.vscode$") { $RepoPath = $RepoPath -replace '.vscode', '' } # Make sure our repo path is valid try { $RepoDetails = Get-Item $RepoPath if (!$RepoDetails.PSIsContainer) { Write-Error "$RepoPath does not appear to be a valid repo." } } catch { throw $_.Exception.Message } # Make sure the file path is valid $VSCodeFilePath = Join-Path $PSScriptRoot $VSCodeFile if (!(Test-Path $VSCodeFilePath)) { throw "$VSCodeFilePath does not exist" } $VSCodePath = Join-Path $RepoPath -ChildPath '.vscode' if (!(Test-Path $VSCodePath)) { try { Write-Verbose "Setting up new .vscode directory at $VSCodePath" New-Item $VSCodePath -ItemType Directory | Out-Null Write-Verbose "Copying settings to $VSCodePath" Copy-Item $VSCodeFilePath -Destination $VSCodePath } catch { throw "Failed to set-up .vscode directory.`n$($_.Exception.Message)" } } else { Write-Verbose "Copying file to $VSCodePath" # Overwrite them if they exist... Copy-Item $VSCodeFilePath -Destination $VSCodePath -Force } } |