-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSProjectStatus.psm1
317 lines (268 loc) · 10.3 KB
/
PSProjectStatus.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#region load string data
# used for culture debugging
# write-host "Importing with culture $(Get-Culture)" -ForeGroundColor yellow
if ((Get-Culture).Name -match '\w+') {
#write-host "Using culture $(Get-Culture)" -ForegroundColor yellow
Import-LocalizedData -BindingVariable strings
}
else {
#force using En-US if no culture found, which might happen on non-Windows systems.
#write-host "Loading $PSScriptRoot/en-us/PSWorkItem.psd1" -ForegroundColor yellow
Import-LocalizedData -BindingVariable strings -FileName psprojectstatus.psd1 -BaseDirectory $PSScriptRoot/en-us
}
#endregion
#region dot source functions
Get-ChildItem $PSScriptRoot\functions\*.ps1 -Recurse |
ForEach-Object {
. $_.FullName
}
#endregion
#region class definitions
enum PSProjectStatus {
Development
Updating
Stable
AlphaTesting
BetaTesting
ReleaseCandidate
Patching
UnitTesting
AcceptanceTesting
Other
Archive
}
Enum gitMode {
fetch
push
}
Class PSProjectRemote {
[string]$Name
[string]$Url
[gitMode]$Mode
PSProjectRemote ($Name, $url, $mode) {
$this.Name = $Name
$this.url = $url
$this.mode = $mode
}
#allow an empty remote setting
PSProjectRemote() {
$this.Name = ''
$this.url = ''
}
}
Class PSProject {
[string]$Name = (Split-Path (Get-Location).path -Leaf)
[string]$Path = (Convert-Path (Get-Location).path)
[DateTime]$LastUpdate = (Get-Date)
[string[]]$Tasks = @()
[PSProjectStatus]$Status = 'Development'
[Version]$ProjectVersion = (Test-ModuleManifest -Path ".\$(Split-Path $pwd -Leaf).psd1" -Verbose:$False -ErrorAction SilentlyContinue).version
[string]$GitBranch = ''
#using .NET classes to ensure compatibility with non-Windows platforms
[string]$UpdateUser = "$([System.Environment]::UserDomainName)\$([System.Environment]::Username)"
[string]$Computername = [System.Environment]::MachineName
[PSProjectRemote[]]$RemoteRepository = @()
[string]$Comment = ''
[string[]]$Tags = @()
[void]Save() {
$json = Join-Path -Path $this.Path -ChildPath psproject.json
#convert the ProjectVersion to a string in the JSON file
#convert the LastUpdate to a formatted date string
$this | Select-Object -Property @{Name = '$schema'; Expression = { 'https://raw.githubusercontent.com/jdhitsolutions/PSProjectStatus/main/psproject.schema.json' } },
Name, Path,
@{Name = 'LastUpdate'; Expression = { '{0:o}' -f $_.LastUpdate } },
@{Name = 'Status'; Expression = { $_.status.toString() } },
@{Name = 'ProjectVersion'; Expression = { $_.ProjectVersion.toString() } },
UpdateUser, Computername, RemoteRepository, Tasks, GitBranch, Tags, Comment |
ConvertTo-Json | Out-File -FilePath $json -Encoding utf8
}
[void]RefreshProjectVersion() {
$this.ProjectVersion = (Test-ModuleManifest ".\$(Split-Path $pwd -Leaf).psd1" -ErrorAction SilentlyContinue).version
}
[void]RefreshUser() {
$this.UpdateUser = "$([System.Environment]::UserDomainName)\$([System.Environment]::Username)"
}
[void]RefreshComputer() {
$this.Computername = [System.Environment]::MachineName
}
[void]RefreshRemoteRepository() {
if (Test-Path .git) {
$remotes = git remote -v
if ($remotes) {
$repos = @()
foreach ($remote in $remotes) {
$split = $remote.split()
$RemoteName = $split[0]
$Url = $split[1]
$Mode = $split[2].replace('(', '').Replace(')', '')
$repos += [PSProjectRemote]::new($RemoteName, $url, $mode)
} #foreach
$this.RemoteRepository = $repos
} #if remotes found
}
}
[void]RefreshAll() {
$this.RefreshProjectVersion()
$this.RefreshUser()
$this.RefreshComputer()
$this.RefreshRemoteRepository()
$this.Save()
}
}
<#
Consider expanding the schema to add a structured task object,
with commands to add, set, complete, and remove.
This would be a major breaking change
[DateTime]$Created
[DateTime]$DueDate
[String]$AssignedTo
[Int32]$Progress
[Boolean]$Completed
#>
Class PSProjectTask {
[string]$ProjectName
[string]$Path
[string]$TaskDescription
[version]$ProjectVersion
[int]$TaskID
PSProjectTask ($TaskDescription, $Path, $ProjectName, $ProjectVersion) {
$this.ProjectName = $ProjectName
$this.Path = $Path
$this.TaskDescription = $TaskDescription
$this.ProjectVersion = $ProjectVersion
}
}
#endregion
#region add a VSCode/PowerShell ISE extension to the project
if ($host.name -eq 'visual studio code host') {
Function Update-PSProjectStatus {
[cmdletbinding()]
Param ($context)
$title = "$([char]27)[4;3;38;5;228mStatus Options$([char]27)[0m"
$menu = @"
$title
[1] Development [6] ReleaseCandidate
[2] Updating [7] Patching
[3] Stable [8] UnitTesting
[4] AlphaTesting [9] AcceptanceTesting
[5] BetaTesting [10] Other
[11] Archive
"@
Do {
Clear-Host
Write-Host $menu
[int]$r = Read-Host 'Select a project status. Enter no value to cancel'
if ($r -eq 0) {
#cancel
return
}
if ($r -lt 1 -OR $r -gt 10) {
$PSEditor.Window.ShowWarningMessage('You entered an invalid value. Enter nothing or a value between 1 and 10.')
}
} until ($r -ge 1 -AND $r -le 10)
$PSEditor.Window.SetStatusBarMessage('Updating PSProject status', 3000)
switch ($r) {
1 { $status = 'Development' }
2 { $status = 'Updating' }
3 { $status = 'Stable' }
4 { $status = 'AlphaTesting' }
5 { $status = 'BetaTesting' }
6 { $status = 'ReleaseCandidate' }
7 { $status = 'Patching' }
8 { $status = 'UnitTesting' }
9 { $status = 'AcceptanceTesting' }
10 { $status = 'Other' }
11 { $status = 'Archive' }
}
if ($status) {
#update the project if a status is specified
$splat = @{
LastUpdate = (Get-Date)
Status = $status
}
if (Test-Path ".\$(Split-Path $pwd -Leaf).psd1" ) {
$splat.Add('ProjectVersion', (Test-ModuleManifest ".\$(Split-Path $pwd -Leaf).psd1").version)
}
$s = Set-PSProjectStatus @splat | Select-Object VersionInfo | Out-String
#parse out ANSI escape sequences
$detail = $s -replace "$([char]27)\[[\d;]*m", ''
#show a summary message
$PSEditor.Window.ShowInformationMessage($detail)
}
}#end function
Register-EditorCommand -Function 'Update-PSProjectStatus' -name 'UpdatePSProjectStatus' -DisplayName 'Update PSProject Status'
} #VSCode
if ($host.name -match 'ISE') {
#The ISE specific version of the update function
Function Update-PSProjectStatus {
[cmdletbinding()]
Param ()
$title = "Status Options`n --------------"
$menu = @"
$title
[1] Development [6] ReleaseCandidate
[2] Updating [7] Patching
[3] Stable [8] UnitTesting
[4] AlphaTesting [9] AcceptanceTesting
[5] BetaTesting [10] Other
[11] Archive
"@
Write-Host $menu
[int]$r = Read-Host 'Select a project status. Enter no value to cancel'
if ($r -lt 1 -OR $r -gt 10) {
return 'You entered an invalid value. '
}
switch ($r) {
1 { $status = 'Development' }
2 { $status = 'Updating' }
3 { $status = 'Stable' }
4 { $status = 'AlphaTesting' }
5 { $status = 'BetaTesting' }
6 { $status = 'ReleaseCandidate' }
7 { $status = 'Patching' }
8 { $status = 'UnitTesting' }
9 { $status = 'AcceptanceTesting' }
10 { $status = 'Other' }
11 { $status = 'Archive' }
}
if ($status) {
#update the project if a status is specified
$splat = @{
LastUpdate = (Get-Date)
Status = $status
}
if (Test-Path ".\$(Split-Path $pwd -Leaf).psd1" ) {
$splat.Add('ProjectVersion', (Test-ModuleManifest ".\$(Split-Path $pwd -Leaf).psd1").version)
}
Set-PSProjectStatus @splat | Select-Object -Property VersionInfo
}
}#end function
if ($psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.DisplayName -notContains 'Update PSProjectStatus') {
#add the action to the Add-Ons menu
[void]($psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Update PSProjectStatus', { Update-PSProjectStatus }, $Null))
}
} #ISE
#endregion
#region definitions and exports
#path to the JSON schema file
$jsonSchema = 'https://raw.githubusercontent.com/jdhitsolutions/PSProjectStatus/main/psproject.schema.json'
# for testing
# $jsonSchema = "file:///c:/scripts/psprojectstatus/psproject.schema.json"
#a hash table to store ANSI escape sequences for different commands used in verbose output with the
#private _verbose helper function
$PSProjectANSI = @{
'Get-PSProjectGitStatus' = '[1;38;5;51m'
'Get-PSProjectReport' = '[1;38;5;111m'
'Get-PSProjectStatus' = '[1;96m'
'Get-PSProjectTask' = '[1;38;5;10m'
'New-PSProjectStatus' = '[1;38;5;208m'
'New-PSProjectTask' = '[1;38;5;159m'
'Remove-PSProjectTask' = '[1;38;5;195m'
'Set-PSProjectStatus' = '[1;38;5;214m'
Default = '[1;38;5;51m'
}
Set-Variable -Name PSProjectANSI -Description "a hash table to store ANSI escape sequences for different commands used in verbose output. You can modify settings using ANSI sequences or `$PSStyle"
#Export the module version to a global variable that will be used in Verbose messages
New-Variable -Name PSProjectStatusModule -Value '0.14.0' -Description 'The PSProjectStatus module version used in verbose messaging.'
Export-ModuleMember -Variable PSProjectStatusModule, PSProjectANSI -Alias 'Update-PSProjectStatus', 'gitstat', 'gpstat', 'npstat', 'spstat'
#endregion