functions/Install-Dependencies.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
function RIT-Install-Dependencies {
    Param (
        [Parameter(ValueFromPipelineByPropertyName = $True)]
        $ContainerName = '',
        [Parameter(ValueFromPipelineByPropertyName = $True)]
        $DependenciesYAML = '',
        [Parameter(ValueFromPipelineByPropertyName = $True)]
        $DownloadApps = $true
    )

    write-host $('
    ____ __ _
   / __ \___ ____ ___ ____ ____/ /__ ____ _____(_)__ _____
  / / / / _ \/ __ \/ _ \/ __ \/ __ / _ \/ __ \/ ___/ / _ \/ ___/
 / /_/ / __/ /_/ / __/ / / / /_/ / __/ / / / /__/ / __(__ )
/_____/\___/ .___/\___/_/ /_/\__,_/\___/_/ /_/\___/_/\___/____/
          /_/ '
) -ForegroundColor Yellow




    function DownloadApps {
        Param (
            [Parameter(ValueFromPipelineByPropertyName = $True)]
            $ContainerName = '',
            [Parameter(ValueFromPipelineByPropertyName = $True)]
            $DependenciesYAML = ''
        )

        az login

        $AllApps = @()
        $Feed = "";
        $package = "";
        $version = "";

        foreach ($line in [System.IO.File]::ReadLines("$DependenciesYAML")) {
            $words = $line.Split(" ");



            foreach ($word in $words) {

                if ($Feed -eq "next") {       
                    $Feed = $word;
                }

                if ($word -eq "Feed:") {
                    $Feed = "next";
                }

                if ($package -eq "next") {       
                    $package = $word;
                }

                if ($word -eq "Package:") {
                    $package = "next";
                }


                if ($version -eq "next") {       
                    $version = $word;
                    $AllApps += [PSCustomObject]@{
                        Feed    = $Feed
                        Package = $package
                        Version = $version
                    }

                }

                if ($word -eq "Version:") {
                    $version = "next";
            
                }

           


            }

       
        }

        foreach ($app in $AllApps) { 

            $versionVar = $app.Version
            $versionVar = $versionVar.Replace("'", "")
            $last_index = $versionVar.LastIndexOf('.')
    
            $cleanVersion = $versionVar.Remove($last_index, 1).Insert($last_index, '-')

            $feed = $app.Feed
            $feed = $feed.Replace("'", "")

            $package = $app.Package;
            $package = $package.Replace("'", "")

            az artifacts universal download --organization "https://dev.azure.com/relateit/" --feed "$feed" --name "$package" --version "$cleanVersion" --path "C:\ProgramData\BcContainerHelper\Extensions\$ContainerName\my"

        }
    }

    if($DownloadApps)
    {
        DownloadApps -ContainerName $ContainerName -DependenciesYAML $DependenciesYAML
    }

    Invoke-ScriptInBcContainer -scriptblock {
        Param($ContainerName)
        function Install-AppDependencies {
            [CmdletBinding()]
            param(            
                [string] $Path,
                [Parameter(ValueFromPipelineByPropertyName = $True)]
                $ContainerName = ''
            )
    
            function AddToDependencyTree() {
                param(
                    [PSObject] $App,
                    [PSObject[]] $DependencyArray,
                    [PSObject[]] $AppCollection,
                    [Int] $Order = 1,
                    [Parameter(ValueFromPipelineByPropertyName = $True)]
                    $containerName = '' 
                )   

                foreach ($Dependency in $App.Dependencies) {
                    $DependencyArray = AddToDependencyTree `
                        -App ($AppCollection | where AppId -eq $Dependency.AppId) `
                        -DependencyArray $DependencyArray `
                        -AppCollection $AppCollection `
                        -Order ($Order - 1)
                }

                if (-not($DependencyArray | where AppId -eq $App.AppId)) {
                    $DependencyArray += $App
                    try {
                        ($DependencyArray | where AppId -eq $App.AppId).ProcessOrder = $Order
                    }
                    catch { }
                }
                else {
                    if (($DependencyArray | where AppId -eq $App.AppId).ProcessOrder -gt $Order) {
                        ($DependencyArray | where AppId -eq $App.AppId).ProcessOrder = $Order
                    } 
                }

                $DependencyArray
            }

            #Script execution
            #. (Join-Path $PSScriptRoot "GetDependencies_TestApps.ps1")

            if ($Path -eq "") {
                $Path = "C:\ProgramData\BcContainerHelper\Extensions\$containerName\my"
            }
            $AllAppFiles = Get-ChildItem -Path $Path -Filter "*.app"

            $AllApps = @()
            foreach ($AppFile in $AllAppFiles) {
                $App = Get-NAVAppInfo -Path $AppFile.FullName
                $AllApps += [PSCustomObject]@{
                    AppId        = $App.AppId
                    Version      = $App.Version
                    Name         = $App.Name
                    Publisher    = $App.Publisher
                    ProcessOrder = 0                            
                    Dependencies = $App.Dependencies
                    Path         = $AppFile.FullName
                }
            }
    
            $FinalResult = @()

            $AllApps | foreach {    
                $FinalResult = AddToDependencyTree -App $_ -DependencyArray $FinalResult -AppCollection $AllApps -Order $AllApps.Count -ContainerName $ContainerName

            }

            Write-Output $FinalResult | sort ProcessOrder | ft

            foreach ($f in $FinalResult) {
                if (-not ([string]::IsNullOrEmpty($f.Path))) {
                    Publish-NAVApp -ServerInstance BC -Path $f.Path -SkipVerification -ErrorAction Ignore
                    Sync-NavApp -ServerInstance BC -Name $f.Name -Version $f.Version -ErrorAction Ignore
                    Install-NAVApp -ServerInstance BC -Name $f.Name -ErrorAction Ignore 
                }
            }

            Restart-NAVServerInstance -ServerInstance BC -force

        }


        Install-AppDependencies -ContainerName $ContainerName

    } -containerName $ContainerName -argumentList $ContainerName


}

Export-ModuleMember -Function RIT-Install-Dependencies