Functions/ProjectManagement.ps1

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
318
319
320
321
# ===========================================================================
# Get-Projects.ps1 --------------------------------------------------------
# ===========================================================================

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-ConfigurationFile {
    
    <#
    .DESCRIPTION
        Returns the path of a single module project configuration file.
     
    .PARAMETER Type
 
    .OUTPUTS
        System.String. Path of project configuration file.
    #>


    [CmdletBinding(PositionalBinding)]
    
    [OutputType([Void])]

    Param (
        [Parameter(Position=1, HelpMessage="Existing project type.")]
        [System.String] $Type="project"
    )

    Process {

        if ($Type -eq "project") {
            return $SciProfile.ProjectFile 
        } else {
            return Join-Path -Path $SciProfile.ConfigDir -ChildPath "project-$($Type).json"
        }
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function New-ConfigurationFile {

    <#
    .DESCRIPTION
        Returns an object containing projects of all types found in modules project directory.
 
    .OUTPUTS
        System.String. Path of project configuration file.
    #>


    [CmdletBinding(PositionalBinding)]
    
    [OutputType([PSCustomObject])]

    Param ()

    Process {
        $project_file = Get-ConfigurationFile
        if (Test-Path -Path $project_file) {
            Remove-Item -Path $project_file -Force
        }

        $project_list = @()
        if ($SciProfile.ProjectAlias){       
            $SciProfile.ProjectAlias -split " "  | ForEach-Object {
                $project_content = Get-Content -Path $(Get-ConfigurationFile -Type $_) | ConvertFrom-Json 
                $project_list += $project_content | Select-Object -Property $SciProfile.Format
            }
        }

        $project_json = $(ConvertTo-Json $project_list)
        Out-File -FilePath $project_file -InputObject $project_json
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Set-ProfileProjectList {
    
    [CmdletBinding(PositionalBinding)]
    
    [OutputType([Void])]

    Param ()

    Process{

        Set-PocsLibrary -Name $SciProfile.ConfigLib -VirtualEnv $SciProfile.VirtualEnv


        if ($SciProfile.ProjectAlias){
            $project_list = $SciProfile.ProjectAlias -split " "
            
            $project_list | ForEach-Object {
                $project_file = Get-ConfigurationFile -Type $_

                If (Test-Path -Path $project_file){
                    Remove-Item -Path $project_file -Force
                }

                Start-Process -Path "papis" -Args "export", "--format", "json", "--out", $project_file, "type:$_", "--all" -WindowStyle "Hidden"
            }

            do {
                $wait = $False
                $project_list | ForEach-Object {
                    if (-not (Test-Path -Path $(Get-ConfigurationFile -Type $_))) {
                        $wait = $True
                    }
                }
                Start-Sleep -Seconds 0.5
            } while ($wait)
        }

        Restore-PocsLibrary
        Restore-VirtualEnv

        New-ConfigurationFile
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-ProjectList {

    <#
    .DESCRIPTION
        Returns all entries of each user defined project type.
     
    .PARAMETER Type
 
    .PARAMETER Unformatted
 
    .OUTPUTS
        PSCustomObject. All project entries related to specified type.
    #>

    
    [CmdletBinding()]

    [OutputType([PSCustomObject])]

    Param (
        [Parameter(Position=1, HelpMessage="Existing project type.")]
        [System.String] $Type="project",

        [Parameter(HelpMessage="Does not return information as readable table.")]
        [Switch] $Unformatted
    )

    Process {
        $project_file = Get-ConfigurationFile -Type $Type
        if ($Type -eq "project" -and -not $(Test-Path -Path $project_file)) {
            New-ConfigurationFile
        }

        $project_list = Get-Content  $project_file | ConvertFrom-Json

        if ($Unformatted) {
            return $project_list
        }
        return Format-Project -Type $Type -Projects $project_list
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Format-Project {
    
    <#
    .DESCRIPTION
        Returns all entries of each user defined project type as formatted table.
     
    .PARAMETER Type
 
    .PARAMETER Projects
     
    .OUTPUTS
        Format. All project entries as formatted table.
    #>


    [CmdletBinding(PositionalBinding)]
    
    [OutputType([PSCustomObject])]

    Param (
        [Parameter(Position=1, HelpMessage="Existing project type.")]
        [System.String] $Type="project",

        [Parameter(Position=2, HelpMessage="Project list with all entries of specified project type")]
        [PSCustomObject] $Projects
    )

    Process {

        return $Projects | Format-Table -Property $SciProfile.Format

    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Test-Project {

    <#
    .DESCRIPTION
        Check whether a list of projects contains a entry with specified identifier.
     
    .PARAMETER Name
 
    .PARAMETER Projects
 
    .OUTPUTS
        PSCustomObject. Project which contains the specified identifier.
    #>


    [CmdletBinding(PositionalBinding)]

    [OutputType([PSCustomObject])]

    Param (

        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name,

        [Parameter(Position=2, HelpMessage="Project list with all entries of specified project type")]
        [PSCustomObject] $Projects
    )
    
    Process {

        $project = $Projects | Where-Object {$_.Name -eq $Name -or $_.Alias -eq $Name}
        if ($project) {
            return $project
        }
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-ProjectType {

    <#
    .DESCRIPTION
        Get project type of specified identifier.
     
    .PARAMETER Name
 
    .OUTPUTS
        System.String. Project type of specified identifier
    #>


    [CmdletBinding(PositionalBinding)]
    
    [OutputType([PSCustomObject])]

    Param(
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    $project_list = Get-ProjectList -Unformatted
    $project = Test-Project -Name $Name -Projects $project_list

    if ($project) {
        return $project | Select-Object -ExpandProperty "Type"
    }
    else {
        Write-FormattedError -Message "No entry with user specification '$($Name)' was found in project type '$($Type)'." -Module $SciProfile.Name
        return
    }

}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Select-Project {

    <#
    .DESCRIPTION
        Returns a project entry as dictionary for a given.
     
    .PARAMETER Name
 
    .PARAMETER Property
 
    .PARAMETER Project
 
    .OUTPUTS
        PSCustomObject. All project entries as formatted table.
    #>


    [CmdletBinding(PositionalBinding)]
    
    [OutputType([PSCustomObject])]

    Param (
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name,

        [Parameter(Position=2, HelpMessage="Property of project entry, which shall be returned.")]
        [System.String] $Property,

        [Parameter(Position=1, HelpMessage="Existing project type.")]
        [System.String] $Type="project"
    )

    Process{ 

        $project_list = Get-ProjectList -Type $Type -Unformatted
        $project = Test-Project -Name $Name -Projects $project_list

        if ($project.($Property)) {
            return  $project | Select-Object -ExpandProperty $Property
        }
        else {
            Write-FormattedError -Message "No property '$($Property)' for user specification '$($Name)' was found in project type '$($Type)'." -Module $SciProfile.Name
            return
        }
    }
}