-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workspace.psm1
135 lines (128 loc) · 3.72 KB
/
Workspace.psm1
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function Get-WorkspaceConfig {
$config = & {
if ($env:WORKSPACE_CONFIG) {
$env:WORKSPACE_CONFIG
} elseif ($env:XDG_CONFIG_HOME) {
"$env:XDG_CONFIG_HOME/workspace/config"
} else {
"$([Environment]::GetFolderPath('ApplicationData'))/workspace/config"
} }
if (!(Test-Path $config)) {
throw "No config, add a workspace first"
}
$config
}
function Get-Workspaces {
Get-Content (Get-WorkspaceConfig) `
| Select-String '^## ?([^#][^ ]*)' `
| ForEach-Object { $_.Matches.Groups[1].Value }
}
function Get-WorkspacePath {
param($Workspace)
if ($null -eq $env:WORKSPACE_REPO_HOME) {
if ($env:XDG_DATA_HOME) {
$env:WORKSPACE_REPO_HOME = "$env:XDG_DATA_HOME/workspace"
} else {
$env:WORKSPACE_REPO_HOME = `
"$([Environment]::GetFolderPath('LocalApplicationData'))/workspace"
}
}
Get-Content (Get-WorkspaceConfig) `
| Select-String "^## ?($Workspace)( (.*))?$" `
| ForEach-Object { if ($_.Matches.Groups[3].Value) {
(($_.Matches.Groups[3].Value | Select-String -AllMatches `
"([^$]*)(\`$(\{([^}]+)\}|[A-Za-z0-9_]+))?").Matches `
| ForEach-Object {
if ($_.Groups[2].Value) {
[System.Environment]::GetEnvironmentVariable((& {
if ($_.Groups[4].Value) {
$_.Groups[4].Value
}
else {
$_.Groups[3].Value
} }))
}
elseif ($_.Groups[1].Length -gt 0) {
$_.Groups[1].Value
}
}) -join ""
}
else {
$_.Matches.Groups[1].Value
} } `
| ForEach-Object {
if (!(Split-Path $_ -IsAbsolute)) {
Join-Path $env:WORKSPACE_REPO_HOME $_
}
else { $_ }
}
}
function Get-WorkspaceScript {
param($Workspace, $Action)
$currentWorkspace = $Workspace;
$currentAction = $Action;
$currentShell = "powershell";
(Get-Content (Get-WorkspaceConfig) | ForEach-Object {
switch -Regex ($_) {
"^## ?([^#][^ ]*)( (.*))?$" {
if ($Matches[1].Length -gt 0) { $currentWorkspace = $Matches.1 }
else { $currentWorkspace = $Workspace }
$currentAction = "clone"
$currentShell = "powershell"
}
"^### ?([^#].*|)$" { $currentAction = $Matches.1 }
"^#### ?([^#].*|)$" {
if ($Matches[1].Length -gt 0) { $currentShell = $Matches.1 }
else { $currentShell = "powershell" }
}
default {
if ($currentWorkspace -eq $Workspace `
-and $currentAction -eq $Action `
-and $currentShell -eq "powershell")
{ $_ }
}
}
}) -join "`n"
}
function Enter-Workspace {
param(
[Parameter(Position = 0)]
[ValidateScript( { $_ -in (Get-Workspaces) })]
[ArgumentCompleter(
{
param($cmd, $param, $wordToComplete)
[array] $validValues = (Get-Workspaces)
$validValues -like "$wordToComplete*"
}
)]
$Workspace
)
if (!$Workspace) {
if (Get-Command -Name fzf -ErrorAction SilentlyContinue) {
$NextWorkspace = (Get-Workspaces | fzf)
if ($LastExitCode -ne 0) {
return
}
$Workspace = $NextWorkspace
}
else {
throw "fzf not found. Install it to use the fuzzy finder functionality."
}
}
$WorkspacePath = (Get-WorkspacePath $Workspace)
if (!$(Test-Path $WorkspacePath)) {
New-Item -Type Directory $WorkspacePath 1>$null
Set-Location $WorkspacePath
Invoke-Expression (Get-WorkspaceScript $Workspace clone)
}
else {
Set-Location $WorkspacePath
}
if (Get-WorkspaceScript $Workspace cd) {
Invoke-Expression (Get-WorkspaceScript $Workspace cd)
}
}
Export-ModuleMember Get-Workspaces
Export-ModuleMember Get-WorkspacePath
Export-ModuleMember Get-WorkspaceScript
Export-ModuleMember Enter-Workspace