aboutsummaryrefslogtreecommitdiff
path: root/.run/src/bin/windows-folder-hide.ps1
blob: ff532024164fb841614a6afaebfc18ddaeec2a98 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
$skipDirs = @('.git', '.temp', 'target', 'node_modules', '.pnpm')
$selfPath = (Get-Item -LiteralPath $MyInvocation.MyCommand.Path).Directory.FullName

function Test-InSkipDir {
    param(
        [object]$Item
    )
    $path = if ($Item -is [string]) {
        $Item
    } elseif ($Item.PSPath) {
        $Item.PSPath -replace '^.*::', ''
    } else {
        $Item.FullName
    }

    $parts = $path.Split([System.IO.Path]::DirectorySeparatorChar)
    for ($i = 0; $i -lt $parts.Length - 1; $i++) {
        if ($parts[$i] -in $skipDirs) {
            return $true
        }
    }
    return $false
}

function Invoke-UnhideRecursive {
    param([string]$Path)
    Get-ChildItem -LiteralPath $Path -Force | ForEach-Object {
        if ($_.PSIsContainer) {
            if ($_.Name -in $skipDirs) {
                if ($_.Attributes -band [System.IO.FileAttributes]::Hidden) {
                    Write-Host "    -> unhiding skip directory (self only): `"$($_.FullName)`""
                    $_.Attributes = $_.Attributes -bxor [System.IO.FileAttributes]::Hidden
                }
                return
            }
            Invoke-UnhideRecursive $_.FullName
        } else {
            if ($_.Attributes -band [System.IO.FileAttributes]::Hidden) {
                Write-Host "    -> unhiding: `"$($_.FullName)`""
                $_.Attributes = $_.Attributes -bxor [System.IO.FileAttributes]::Hidden
            }
        }
    }
}

function Test-GitPathSkippable {
    param([string]$GitPath)
    $parts = $GitPath.Split(@('/', '\'))
    for ($i = 0; $i -lt $parts.Length - 1; $i++) {
        if ($parts[$i] -in $skipDirs) {
            return $true
        }
    }
    return $false
}

Write-Host "Step 1: Unhiding all files and directories (skipping $($skipDirs -join ', '))..."

Invoke-UnhideRecursive -Path (Get-Location).Path

Write-Host "Step 2: Hiding git-ignored items..."

git ls-files --others --ignored --exclude-standard | Where-Object {
    -not (Test-GitPathSkippable $_)
} | ForEach-Object {
    $itemPath = $_
    Write-Host "... checking: `"$itemPath`""
    $item = Get-Item $_ -Force -ErrorAction SilentlyContinue
    if (-not $item) { return }

    if ($item.FullName -eq $selfPath) { return }

    if (Test-InSkipDir $item) {
        Write-Host "    -> skipping (inside skip directory)"
        return
    }

    if ($item.PSIsContainer) {
        if (-not ($item.Attributes -band [System.IO.FileAttributes]::Hidden)) {
            Write-Host "    -> hiding directory (non-recursive)"
            $item.Attributes = $item.Attributes -bor [System.IO.FileAttributes]::Hidden
        }
    } else {
        if (-not ($item.Attributes -band [System.IO.FileAttributes]::Hidden)) {
            Write-Host "    -> hiding"
            $item.Attributes = $item.Attributes -bor [System.IO.FileAttributes]::Hidden
        }
    }
}

Write-Host "Step 3: Hiding dot-prefixed items..."
Get-ChildItem -Path . -Force -Directory | Where-Object { $_.Name -match '^\.' } | ForEach-Object {
    Write-Host "... checking: `"$($_.FullName)`""
    if (Test-InSkipDir $_) {
        Write-Host "    -> skipping (inside skip directory)"
        return
    }
    if (-not ($_.Attributes -band [System.IO.FileAttributes]::Hidden)) {
        Write-Host "    -> hiding directory"
        $_.Attributes = $_.Attributes -bor [System.IO.FileAttributes]::Hidden
    }
}

Get-ChildItem -Path . -Force -File | Where-Object { $_.Name -match '^\.' } | ForEach-Object {
    if ($_.FullName -eq $selfPath) { return }
    Write-Host "... checking: `"$($_.FullName)`""
    if (Test-InSkipDir $_) {
        Write-Host "    -> skipping (inside skip directory)"
        return
    }
    if (-not ($_.Attributes -band [System.IO.FileAttributes]::Hidden)) {
        Write-Host "    -> hiding file"
        $_.Attributes = $_.Attributes -bor [System.IO.FileAttributes]::Hidden
    }
}