Public/Get-NcrementManifest.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
<#
.SYNOPSIS
Creates a new [Manifest] instance from the specified path.
 
.DESCRIPTION
This function creates a new [Manifest] instance from the specified path. If a path is not provided the function will search the current directory for a 'manifest.json' file; passing a path to a directory will also invoke the same behavior.
 
.PARAMETER Path
The path of the manifest file.
 
.PARAMETER CreateIfNotFound
Determines wether to create a new 'manifest.json' file if none exist at the specified path.
 
.INPUTS
[String]
 
.OUTPUTS
[Manifest]
 
.EXAMPLE
Get-NcrementManifest;
This example creates a new [Manifest] from the 'manifest.json' file withing the current directory.
 
Get-NcrementManifest "C:\newproject\manifest.json";
This example creates a new [Manifest] from the specified path.
 
.LINK
New-NcrementManifest
 
.LINK
Save-NcrementManifest
#>


function Get-NcrementManifest
{
    Param(
        [Alias('p', 'file')]
        [Parameter(ValueFromPipeline)]
        [string]$Path,

        [Alias('c', 'create', 'force')]
        [switch]$CreateIfNotFound
    )

    # Resolving the specified manifest path.
    if ([string]::IsNullOrEmpty($Path))
        { $Path = "$PWD\manifest.json"; }
    elseif ((Test-Path $Path -PathType Container))
        { $Path = Join-Path $Path "manifest.json"; }

    if ((-not (Test-Path $Path -PathType Leaf)) -and $CreateIfNotFound.IsPresent)
    {
        $manifest = New-NcrementManifest;
        $manifest | Save-NcrementManifest $Path;
        return $manifest;
    }
    elseif (-not (Test-Path $Path -PathType Leaf))
    {
        throw "Could not file file at '$Path'.";
    }

    # Extracting the manifest object from .json file.
    $json = Get-Content $Path | Out-String | ConvertFrom-Json;
    $manifest = $json.PSObject.Properties | Where-Object { ($_.Name -ieq "manifest") -or ($_.Name -ieq "project") -or ($_.Name -ieq "product")  } | Select-Object -ExpandProperty Value -First 1;
    if ($manifest -eq $null) { $manifest = $json; }

    # Appending missing properties.
    if ($manifest.PSObject.Properties.Match("Path").Count -eq 0)
    {
        $manifest | Add-Member -MemberType NoteProperty -Name "Path" -Value $Path;
    }

    $model = New-NcrementManifest;
    foreach ($prop in $model.PSObject.Properties)
    {
        if ($manifest.PSObject.Properties.Match($prop.Name).Count -eq 0)
        {
            $manifest | Add-Member -MemberType NoteProperty -Name $prop.Name -Value "";
        }
    }

    return $manifest;
}