Browse Source

initial windows support

Daniel Sheffield 1 month ago
parent
commit
798ce1e7ba
2 changed files with 129 additions and 0 deletions
  1. 86 0
      dot.ps1
  2. 43 0
      park.ps1

+ 86 - 0
dot.ps1

@@ -0,0 +1,86 @@
+#!/usr/bin/env pwsh
+param (
+    [Parameter(Mandatory=$true)][string]$Action,
+    [Parameter(Mandatory=$true)][string]$Package,
+    [string]$Target
+)
+
+$ErrorActionPreference = "Stop"
+
+
+$stow = "Write-Output" # For debugging
+#$stow = "./park.ps1"
+
+# Default to current directory if not set
+$DOTFILES_DIR = $env:DOTFILES_DIR
+if (-not $DOTFILES_DIR) {
+    $DOTFILES_DIR = (Get-Location).Path
+}
+
+function ActionToArg {
+    param ([string]$action)
+    switch ($action) {
+        "remove" { return "--delete" }
+        "update" { return "--restow" }
+        "apply"  { return "--stow" }
+        default  { return $null }
+    }
+}
+
+function TargetToDest {
+    param ([string]$target)
+    switch ($target) {
+        "user"   { return $env:HOME }
+        "system" { return "/" }
+        default  { return $null }
+    }
+}
+
+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) {
+    Write-Host "No such action: $Action"
+    exit 1
+}
+
+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
+    }
+    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
+    }
+}
+else {
+    $any = $false
+    foreach ($t in "user", "system") {
+        $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
+        }
+    }
+
+    if (-not $any) {
+        $dest = TargetToDest "user"
+        Invoke-Stow -StowDir $DOTFILES_DIR -TargetDir $dest -StowAction $actionArg -StowPackage $Package
+    }
+}
+

+ 43 - 0
park.ps1

@@ -0,0 +1,43 @@
+#!/bin/env pwsh
+# Copyright (c) Daniel Sheffield 2025
+# All rights reserved
+#
+# Park is inspired by GNU Stow.
+#
+# It can't stow your files properly,
+# but it will park them.
+#
+# Hopefully that is good enough.
+#
+param(
+    [Parameter(Mandatory=$true)][string]$Source,
+    [Parameter(Mandatory=$true)][string]$Target
+)
+
+function New-Symlink($sourcePath, $targetPath) {
+    $item = Get-Item $sourcePath
+
+    if ($item.PSIsContainer) {
+        # Directory symlink
+        New-Item -ItemType SymbolicLink -Path $targetPath -Target $sourcePath | Out-Null
+        Write-Host "Linked dir: $targetPath -> $sourcePath"
+    }
+    else {
+        # File symlink
+        New-Item -ItemType SymbolicLink -Path $targetPath -Target $sourcePath | Out-Null
+        Write-Host "Linked file: $targetPath -> $sourcePath"
+    }
+}
+
+# Ensure $Target exists
+if (-not (Test-Path $Target)) {
+    New-Item -ItemType Directory -Path $Target | Out-Null
+}
+
+# Link each item from source into target
+Get-ChildItem -Path $Source -Force | ForEach-Object {
+    $src = $_.FullName
+    $dest = Join-Path $Target $_.Name
+    New-Symlink -sourcePath $src -targetPath $dest
+}
+