functions/Add-DBOBuild.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

function Add-DBOBuild {
    <#
    .SYNOPSIS
        Creates a new build in existing DBOps package
     
    .DESCRIPTION
        Creates a new build in existing DBOps package from specified set of scripts.
     
    .PARAMETER ScriptPath
        A collection of script files to add to the build. Accepts Get-Item/Get-ChildItem objects and wildcards.
        Will recursively add all of the subfolders inside folders. See examples if you want only custom files to be added.
        During deployment, scripts will be following this deployment order:
         - Item order provided in the ScriptPath parameter
           - Files inside each child folder (both folders and files in alphabetical order)
             - Files inside the root folder (in alphabetical order)
              
        Aliases: SourcePath
     
    .PARAMETER Path
        Path to the existing DBOpsPackage.
        Aliases: Name, FileName, Package
     
    .PARAMETER Build
        A string that would be representing a build number of this particular build.
        Optional - can be genarated automatically.
        Can only contain characters that will be valid on the filesystem.
 
    .PARAMETER Type
        Adds only files that were not added to the package yet. The following options are available:
        * New: add new files based on their source path (can be relative)
        * Modified: adds files only if they have been modified since they had last been added to the package
        * Unique: adds unique files to the build based on their hash values. Compares hashes accross the whole package
        * All: add all files regardless of their previous involvement
         
        More than one value can be specified at the same time.
         
        Default value: All
     
    .PARAMETER Confirm
        Prompts to confirm certain actions
 
    .PARAMETER WhatIf
        Shows what would happen if the command would execute, but does not actually perform the command
 
    .EXAMPLE
        # Add new build 2.0 to the existing package using files from .\Scripts\2.0
        Add-DBOBuild -Path MyPackage.zip -ScriptPath .\Scripts\2.0 -Build 2.0
 
    .EXAMPLE
        # Add new build 2.1 to the existing package using modified files from .\Scripts\2.0
        Get-ChildItem .\Scripts\2.0 | Add-DBOBuild -Path MyPackage.zip -Build 2.1 -UniqueOnly
 
    .EXAMPLE
        # Add new build 3.0 to the existing package checking if there were any new files in the Scripts folder
        Add-DBOBuild -Path MyPackage.zip -ScriptPath .\Scripts\* -Build 3.0 -NewOnly
 
    .NOTES
        See 'Get-Help New-DBOPackage' for additional info about packages.
#>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        [Parameter(Mandatory = $true,
            Position = 1)]
        [Alias('FileName', 'Name', 'Package')]
        [string]$Path,
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            Position = 2)]
        [Alias('SourcePath')]
        [object[]]$ScriptPath,
        [string]$Build,
        [ValidateSet('New', 'Modified', 'Unique', 'All')]
        [string[]]$Type = 'All'
    )
    
    begin {
        if (!$Build) {
            $Build = Get-NewBuildNumber
        }
        $scriptCollection = @()
    }
    process {
        foreach ($scriptItem in $ScriptPath) {
            Write-PSFMessage -Level Verbose -Message "Processing path item $scriptItem"
            $scriptCollection += Get-ChildScriptItem $scriptItem
        }
    }
    end {
        Write-PSFMessage -Level Verbose -Message "Loading package information from $Path"
        if ($package = Get-DBOPackage -Path $Path) {
            #Prepare the scripts that's going to be added to the build
            $scriptsToAdd = @()
            foreach ($childScript in $scriptCollection) {
                # Include file by default
                $includeFile = $Type -contains 'All'
                if ($Type -contains 'New') {
                    #Check if the script path was already added in one of the previous builds
                    if (!$package.SourcePathExists($childScript.SourcePath)) {
                        $includeFile = $true
                        Write-PSFMessage -Level Verbose -Message "File $($childScript.SourcePath) was not found among the package source files, adding to the list."
                    }
                }
                if ($Type -contains 'Modified') {
                    #Check if the file was modified in the previous build
                    if ($package.ScriptModified($childScript.FullName, $childScript.SourcePath)) {
                        $includeFile = $true
                        Write-PSFMessage -Level Verbose -Message "Hash of the file $($childScript.FullName) was modified since last deployment, adding to the list."
                    }
                }
                if ($Type -contains 'Unique') {
                    #Check if the script hash was already added in one of the previous builds
                    if (!$package.ScriptExists($childScript.FullName)) {
                        $includeFile = $true
                        Write-PSFMessage -Level Verbose -Message "Hash of the file $($childScript.FullName) was not found among the package scripts, adding to the list.."
                    }
                }
                if ($includeFile) {
                    $scriptsToAdd += $childScript
                }
                else {
                    Write-PSFMessage -Level Verbose -Message "File $($childScript.FullName) was not added to the current build due to -Type restrictions: $($Type -join ',')"
                }
            }

            if ($scriptsToAdd) {

                #Create new build object
                $currentBuild = $package.NewBuild($Build)

                foreach ($buildScript in $scriptsToAdd) {
                    $s = $currentBuild.NewScript($buildScript)
                    Write-PSFMessage -Level Verbose -Message "Adding file '$($buildScript.FullName)' to $currentBuild as $($s.GetPackagePath())"
                }

                if ($pscmdlet.ShouldProcess($package, "Writing new build $currentBuild into the original package")) {
                    $currentBuild.Alter()
                }
            }
            else {
                Write-Warning "No scripts have been selected, the original file is unchanged."
            }
            $package
        }
    }
}