forked from jdhitsolutions/PSWorkItem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSWorkItem.psm1
172 lines (148 loc) · 5.15 KB
/
PSWorkItem.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
# used for culture debugging
# write-host "Importing with culture $(Get-Culture)"
if ((Get-Culture).Name -match "\w+") {
Import-LocalizedData -BindingVariable strings
}
else {
#force using En-US if no culture found, which might happen on non-Windows systems.
Import-LocalizedData -BindingVariable strings -FileName PSWorkItem.psd1 -BaseDirectory $PSScriptRoot\en-us
}
#Adding a failsafe check for Windows PowerShell
if ($IsMacOS -or ($PSEdition -eq "Desktop")) {
Write-Warning $Strings.Unsupported
#bail out
Return
}
Get-ChildItem $PSScriptRoot\functions\*.ps1 -Recurse |
ForEach-Object {
. $_.FullName
}
#testing localization
#Write-Host $strings.Testing -fore cyan
#region assembly loading
#there may be version conflicts
#required versions
[version]$NStackVersion = "1.0.7"
[version]$TerminalGuiVersion = "1.14.0"
$dlls = "$PSScriptRoot\assemblies\NStack.dll","$PSScriptRoot\assemblies\Terminal.Gui.dll"
foreach ($dll in $dlls) {
$name = Split-Path -path $dll -leaf
#write-host "Loading $dll" -fore yellow
Try {
Add-Type -Path $dll -ErrorAction Stop
}
Catch {
$msg = ($strings.WarnAssemblyLoaded -f $Name)
Write-Warning $msg
#$verMessage = "Detected version $($PSStyle.Foreground.Red){0}$($PSStyle.Reset) which is less than the expected version of $($PSStyle.Foreground.Green){1}$($PSStyle.Reset). $($PSStyle.Italic)There may be unexpected behavior$($PSStyle.Reset). You may need to start a new PowerShell session and load this module first."
Switch ($Name) {
"NStack.dll" {
#get currently loaded version
$ver = [System.Reflection.Assembly]::GetAssembly([NStack.uString]).GetName().version
if ($ver -lt $NStackVersion) {
$Detail = $strings.WarnDetected -f $ver,$NStackVersion,$PSStyle.Foreground.Red,$PSStyle.Foreground.Green,$PSStyle.Italic,$PSStyle.Reset
}
}
"Terminal.Gui.dll" {
$ver = [System.Reflection.Assembly]::GetAssembly([Terminal.Gui.Application]).GetName().version
if ($ver -lt $TerminalGuiVersion) {
$Detail = $strings.WarnDetected -f $ver,$TerminalGuiVersion,$PSStyle.Foreground.Red,$PSStyle.Foreground.Green,$PSStyle.Italic,$PSStyle.Reset
}
}
} #switch
if ($Detail) {
Write-Warning $Detail
}
}
}
#endregion
#region class definitions
<#
classes for PSWorkItem and PSWorkItemArchive
#>
#define base PSWorkItem class
class PSWorkItemBase {
[int]$ID
[String]$Name
[String]$Category
[String]$Description
[DateTime]$TaskCreated = (Get-Date)
[DateTime]$TaskModified = (Get-Date)
[boolean]$Completed
[String]$Path
#this will be last resort GUID to ensure uniqueness
hidden[guid]$TaskID = (New-Guid).Guid
}
class PSWorkItem:PSWorkItemBase {
[DateTime]$DueDate = (Get-Date).AddDays(30)
[int]$Progress = 0
PSWorkItem ([String]$Name, [String]$Category) {
$this.Name = $Name
$this.Category = $Category
}
PSWorkItem() {
$this
}
}
Class PSWorkItemArchive:PSWorkItemBase {
[DateTime]$DueDate
[int]$Progress
}
class PSWorkItemCategory {
[String]$Category
[String]$Description
#constructor
PSWorkItemCategory([String]$Category, [String]$Description) {
$this.Category = $Category
$this.Description = $Description
}
}
class PSWorkItemDatabase {
[String]$Path
[DateTime]$Created
[DateTime]$LastModified
[int32]$Size
[int32]$TaskCount
[int32]$CategoryCount
[int32]$ArchiveCount
[String]$Encoding
[int32]$PageCount
[int32]$PageSize
[string]$SQLiteVersion
[string]$CreatedBy
}
#endregion
#region type extensions
Update-TypeData -TypeName PSWorkItemCategory -MemberType ScriptProperty -MemberName ANSIString -Value { $PSWorkItemCategory[$this.Category] -replace "`e",
"``e" } -force
#endregion
#region settings and configuration
<#
Default categories when creating a new database file.
This will be a module-scoped variable, not exposed to the user
#>
$script:PSWorkItemDefaultCategories = 'Work', 'Personal', 'Project', 'Other'
#a global hashtable used for formatting PSWorkItems
$global:PSWorkItemCategory = @{
'Work' = $PSStyle.Foreground.Cyan
'Personal' = $PSStyle.Foreground.Green
}
#import and use the preference file if found
$PreferencePath = Join-Path -Path $HOME -ChildPath '.psworkitempref.json'
If (Test-Path $PreferencePath) {
$importPref = Get-Content $PreferencePath | ConvertFrom-Json
$global:PSWorkItemPath = $importPref.Path
$importPref.categories.foreach({ $PSWorkItemCategory[$_.category] = $_.ansi })
if ($importPref.DefaultDays) {
$global:PSWorkItemDefaultDays = $importPref.DefaultDays
}
If ($importPref.DefaultCategory) {
$global:PSDefaultParameterValues["New-PSWorkItem:Category"] = $importPref.DefaultCategory
}
}
else {
#make this variable global instead of exporting so that I don't have to use Export-ModuleMember 7/28/2022 JDH
$global:PSWorkItemPath = Join-Path -Path $HOME -ChildPath 'PSWorkItem.db'
$global:PSWorkItemDefaultDays = 30
}
#endregion