Selaa lähdekoodia

fix iteration root

Daniel Sheffield 2 viikkoa sitten
vanhempi
sitoutus
72b49a7215
2 muutettua tiedostoa jossa 54 lisäystä ja 36 poistoa
  1. 33 31
      dot.ps1
  2. 21 5
      park.ps1

+ 33 - 31
dot.ps1

@@ -8,19 +8,14 @@ param (
 $ErrorActionPreference = "Stop"
 
 if ($env:DEBUG) {
-    $stow = "./park.ps1"
     $stowArgs = @{
         Simulate = $true
         Verbose  = $true
     }
 } else {
-    $stow = "./park.ps1"
     $stowArgs = @{}
 }
 
-$stow = "Write-Output" # For debugging
-#$stow = "./park.ps1"
-
 # Default to current directory if not set
 $DOTFILES_DIR = $env:DOTFILES_DIR
 if (-not $DOTFILES_DIR) {
@@ -30,34 +25,25 @@ if (-not $DOTFILES_DIR) {
 function ActionToArg {
     param ([string]$action)
     switch ($action) {
-        "remove" { return "--delete" }
-        "update" { return "--restow" }
-        "apply"  { return "--stow" }
-        default  { return $null }
+        "remove" { return @{ delete = $true }}
+        "update" { return @{ restow = $true }}
+        "apply"  { return @{ stow = $true }}
+        default  { return @{} }
     }
 }
 
 function TargetToDest {
     param ([string]$target)
     switch ($target) {
-        "user"   { return $env:HOME }
-        "system" { return "/" }
-        default  { return $null }
+        "user"   { return @{ target = $env:HOME }}
+        "system" { return @{ target = "/" }} # TODO: should be C:/ or something
+        default  { return @{ }}
     }
 }
 
-function Invoke-Stow {
-    param (
-        [string]$StowDir,
-        [string]$TargetDir,
-        [string]$StowAction,
-        [string]$StowPackage
-    )
-    & $stow -d $StowDir -t $TargetDir $StowAction $StowPackage
-}
-
 $actionArg = ActionToArg $Action
-if (-not $actionArg) {
+$stowArgs += $actionArg
+if ($actionArg.Count -eq 0) {
     Write-Host "No such action: $Action"
     exit 1
 }
@@ -65,16 +51,24 @@ if (-not $actionArg) {
 if ($Target) {
     $packageTargetPath = Join-Path -Path "$DOTFILES_DIR/$Package" -ChildPath $Target
     if (Test-Path $packageTargetPath -PathType Container) {
-        $dest = TargetToDest $Target
-        Invoke-Stow -StowDir "$DOTFILES_DIR/$Package" -TargetDir $dest -StowAction $actionArg -StowPackage $Target
+        $stowArgs += @{
+            dir = "$DOTFILES_DIR/$Package"
+            package = "$Target"
+        }
+        $stowArgs += TargetToDest $Target
+        ./park.ps1 @stowArgs
     }
     elseif ($Target -ne "user") {
         Write-Host "Target '$Target' does not exist for package '$Package'"
         exit 1
     }
     else {
-        $dest = TargetToDest "user"
-        Invoke-Stow -StowDir $DOTFILES_DIR -TargetDir $dest -StowAction $actionArg -StowPackage $Package
+        $stowArgs += @{
+            dir = "$DOTFILES_DIR"
+            package = "$Package"
+        }
+        $stowArgs += TargetToDest "user"
+        ./park.ps1 @stowArgs
     }
 }
 else {
@@ -83,14 +77,22 @@ else {
         $path = "$DOTFILES_DIR/$Package/$t"
         if (Test-Path $path -PathType Container) {
             $any = $true
-            $dest = TargetToDest $t
-            Invoke-Stow -StowDir "$DOTFILES_DIR/$Package" -TargetDir $dest -StowAction $actionArg -StowPackage $t
+            $stowArgs += @{
+                dir = "$DOTFILES_DIR/$Package"
+                package = "$t"
+            }
+            $stowArgs += TargetToDest $t
+            ./park.ps1 @stowArgs
         }
     }
 
     if (-not $any) {
-        $dest = TargetToDest "user"
-        Invoke-Stow -StowDir $DOTFILES_DIR -TargetDir $dest -StowAction $actionArg -StowPackage $Package
+        $stowArgs += @{
+            dir = "$DOTFILES_DIR"
+            package = "$Package"
+        }
+        $stowArgs += TargetToDest "user"
+        ./park.ps1 @stowArgs
     }
 }
 

+ 21 - 5
park.ps1

@@ -22,10 +22,11 @@
 param(
     [Parameter(Mandatory = $true)][string]$Dir,
     [Parameter(Mandatory = $true)][string]$Package,
-    [string]$Target,
+    [Parameter(Mandatory = $true)][string]$Target,
     [string]$Ignore,
     [string]$Defer,
     [string]$Override,
+    [switch]$Stow,
     [switch]$Restow,
     [switch]$Delete,
     [switch]$Simulate,
@@ -35,7 +36,13 @@ param(
 
 $SourcePath = (Resolve-Path $Dir).Path
 $SourceRoot = [System.IO.Path]::GetFullPath($SourcePath)
+$SourceRoot = Join-Path $SourceRoot $Package
 $TargetRoot = [System.IO.Path]::GetFullPath($Target)
+Write-Host "Parameters:"
+foreach ($param in $MyInvocation.MyCommand.Parameters.Keys) {
+    $value = Get-Variable -Name $param -ValueOnly -ErrorAction SilentlyContinue
+    Write-Host "$param = $value"
+}
 
 function Is-Under($child, $parent) {
     $child = [System.IO.Path]::GetFullPath($child)
@@ -59,13 +66,22 @@ if ($Delete -or $Restow) {
 }
 
 if ($Stow -or $Restow) {
-    Get-ChildItem -Path $SourceRoot -Recurse -File -Force | ForEach-Object {
+    Get-ChildItem -Path $SourceRoot -Recurse -Force | Where-Object {
+        -not $_.PSIsContainer -or $_.Attributes -match 'ReparsePoint'
+    } | ForEach-Object {
         $relativePath = $_.FullName.Substring($SourceRoot.Length).TrimStart('\', '/')
         $targetFile = Join-Path $TargetRoot $relativePath
         $targetDir = Split-Path $targetFile
-    
-        if (-not (Test-Path $targetDir)) {
-            Write-Verbose "Creating dir: $targetDir"
+        if (Test-Path $targetDir) {
+            if (-not (Get-Item $targetDir).PSIsContainer) {
+                Write-Verbose "Path '$targetDir' exists but is not a directory."
+                if (-not $Simulate) {
+                    throw "Path '$targetDir' exists but is not a directory."
+                }
+            } else {
+                Write-Verbose "Creating dir: $targetDir"
+            }
+        } else {
             if (-not $Simulate) {
                 New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
             }