Ver código fonte

support simulate and verbose options

Daniel Sheffield 1 mês atrás
pai
commit
dde4117356
1 arquivos alterados com 22 adições e 8 exclusões
  1. 22 8
      park.ps1

+ 22 - 8
park.ps1

@@ -9,6 +9,7 @@
 #
 # Hopefully that is good enough.
 #
+
 param(
     [Parameter(Mandatory = $true)]
     [string]$Source,
@@ -22,10 +23,10 @@ param(
     [string]$Override,
     [switch]$Restow,
     [switch]$Delete,
-    [switch]$Verbose,
+    [int]$Verbose = 1
     [switch]$Simulate,
     [switch]$NoFolding,
-    [switch]$Adopt
+    [switch]$Adopt,
 )
 
 $SourcePath = (Resolve-Path $Source).Path
@@ -38,30 +39,43 @@ function Is-Under($child, $parent) {
     return $child.StartsWith($parent, [System.StringComparison]::OrdinalIgnoreCase)
 }
 
+function Write-Log($msg, $level = 1) {
+    if ($Verbose -ge $level) {
+        Write-Host $msg
+    }
+}
+
 if ($Delete) {
     Get-ChildItem -Path $TargetRoot -Recurse -Force | ForEach-Object {
         if ($_.LinkType -eq 'SymbolicLink') {
             $linkTarget = (Get-Item $_.FullName -Force).Target
             if ($linkTarget -and (Is-Under $linkTarget $SourceRoot)) {
-                Remove-Item $_.FullName
-                Write-Host "Unlinked: $($_.FullName)"
+                Write-Log "Unlinking: $($_.FullName) -> $linkTarget"
+                if (-not $Simulate) {
+                    Remove-Item $_.FullName
+                }
             }
         }
     }
     return
 }
 
-# Stow (create symlinks)
+# Create symlinks
 Get-ChildItem -Path $SourceRoot -Recurse -File -Force | ForEach-Object {
     $relativePath = $_.FullName.Substring($SourceRoot.Length).TrimStart('\', '/')
     $targetFile = Join-Path $TargetRoot $relativePath
     $targetDir = Split-Path $targetFile
 
     if (-not (Test-Path $targetDir)) {
-        New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
+        Write-Log "Creating dir: $targetDir" 2
+        if (-not $Simulate) {
+            New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
+        }
     }
 
-    New-Item -ItemType SymbolicLink -Path $targetFile -Target $_.FullName | Out-Null
-    Write-Host "Linked: $targetFile -> $($_.FullName)"
+    Write-Log "Linking: $targetFile -> $($_.FullName)"
+    if (-not $Simulate) {
+        New-Item -ItemType SymbolicLink -Path $targetFile -Target $_.FullName | Out-Null
+    }
 }