Repository navigation
Expand file tree
/
Copy pathgithub.ps1
More file actions
95 lines (84 loc) · 3.29 KB
/
Copy pathgithub.ps1
File metadata and controls
95 lines (84 loc) · 3.29 KB
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
param([switch]$VerboseSwitch = $false)
#$Verbose=$true -or $VerboseSwitch
$Verbose=$VerboseSwitch
# Write-Verbose "[$script] [$env:SnippetsInitialized] -not `$env:SnippetsInitialized: $(-not $env:SnippetsInitialized)" -Verbose:$Verbose
$script = $MyInvocation.MyCommand
if (-not $env:SnippetsInitialized) {
$fileInfo = New-Object System.IO.FileInfo (Get-Item $PSScriptRoot).FullName
$path = $fileInfo.Directory.FullName;
. $path/Snippets/_common.ps1;
Initialize-Snippets -Verbose:$Verbose
}
try {
if (-not $env:GITHUB -and $env:OS -eq 'Windows_NT') {
$env:GITHUB = 'C:\GitHub'
}
if (-not $env:GITHUB) {
# Search common locations instead of the entire drive
$searchPaths = @()
if($env:IsWindows -ieq 'true') {
$searchPaths += $env:USERPROFILE
if ($env:OneDrive) { $searchPaths += $env:OneDrive }
} else {
$searchPaths += $env:HOME
}
foreach ($hintPath in $searchPaths) {
Write-Verbose "[$script] searching for 'github' in [$hintPath]" -Verbose:$Verbose
$found = Get-ChildItem -Path $hintPath -Depth 1 -Directory -ErrorAction SilentlyContinue | Where-Object Name -IEQ 'github' | Select-Object -First 1
if ($found) {
$env:GITHUB = $found.FullName
break
}
}
if (-not $env:GITHUB) {
Write-Verbose "[$script] Could not find 'github' folder. Set `$env:GITHUB manually." -Verbose:$Verbose
}
}
$description="Snippets: [dev] Go to GitHub folder [$env:GITHUB]"
Write-Verbose "[$script] `$env:GITHUB set to [$env:GITHUB]" -Verbose:$Verbose
function Set-LocationGitHub {
param(
[string]$Repository = '',
[switch]$V = $false
)
Write-Verbose "[$script] `$env:GITHUB: [$($env:GITHUB)]" -Verbose:$V
Write-Verbose "[$script] `$Repository: [$Repository]" -Verbose:$V
if (-not $env:GITHUB) {
throw 'Set $env:GITHUB to your repositories directory before using hub.'
}
$target = if ($Repository) { Join-Path $env:GITHUB -ChildPath $Repository } else { $env:GITHUB }
if ($Repository -and -not (Test-Path -LiteralPath $target -PathType Container)) {
# Resolve each component using actual directory names on case-sensitive filesystems.
$target = $env:GITHUB
foreach ($part in ($Repository -split '[/\\]' | Where-Object { $_ })) {
$exact = Join-Path $target -ChildPath $part
if (Test-Path -LiteralPath $exact -PathType Container) {
$target = $exact
continue
}
$matches = @(Get-ChildItem -LiteralPath $target -Directory -Force -ErrorAction Stop |
Where-Object { $_.Name -ieq $part })
if ($matches.Count -gt 1) {
throw "Ambiguous repository directory '$part': $($matches.Name -join ', '). Use the exact casing."
}
if ($matches.Count -eq 0) {
throw "GitHub directory not found: '$exact'."
}
$target = $matches[0].FullName
}
}
if (-not (Test-Path -LiteralPath $target -PathType Container)) {
throw "GitHub directory not found: '$target'. Create it or set `$env:GITHUB to an existing directory."
}
Set-Location -LiteralPath $target -Verbose:$V -ErrorAction Stop
}
$alias = set-alias -Verbose:$Verbose -Scope Global -Description $description -Name hub -Value Set-LocationGitHub
return "Use ``hub`` to go to the GitHub folder."
}
catch {
Write-Host $Error
}
finally {
Write-Verbose '[github.ps1] Leaving...' -Verbose:$Verbose
$Verbose = $VerboseSwitch
}