forked from kine/NVRAppDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ALAppOrder.ps1
152 lines (143 loc) · 5.78 KB
/
Get-ALAppOrder.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
<#
.SYNOPSIS
Sort apps into order in which must be compiled/installed to fullfill their dependencies
.DESCRIPTION
Sort apps into order in which must be compiled/installed to fullfill their dependencies
.EXAMPLE
PS C:\> Get-ALAppOrder -Path .\
Read all app.json from the subfolders and sort the app objects
.Parameter ContainerName
Name of the container to use to get info from .App file
.Parameter Path
Folder in which the app.json will be searched. If no app.json is found, all *.app packages will be used.
.Parameter Recurse
Will search for files recursively
.Parameter AppCollection
Array of app.json files you want to compile.
.OUTPUTS
Array of App objects having these members:
name
publisher
version
AppPath
dependencies
name
publisher
version
#>
function Get-ALAppOrder
{
[CMDLetBinding(DefaultParameterSetName="Path")]
Param(
[Parameter(ValueFromPipelineByPropertyName=$True)]
$ContainerName,
#Path to the repository
[Parameter(ParameterSetName="Path")]
$Path='.\',
[switch]$Recurse,
#Array of Files you want to use
[Parameter(ValueFromPipelineByPropertyName=$True,ParameterSetName="Collection")]
[Hashtable]$AppCollection
)
function ConvertTo-ALAppsInfo
{
Param(
$Files
)
$result = @{};
foreach ($F in $Files) {
$AppJson = Get-Content -Path $F.FullName | ConvertFrom-Json
$AppJson | Add-Member -MemberType NoteProperty -Name "AppPath" -Value $F.FullName
$result.Add($AppJson.name,$AppJson)
}
return $result
}
function Get-ALBuildOrder
{
Param(
$Apps
)
$AppsOrdered = @()
$AppsToAdd = @{}
$AppsCompiled = @{}
do {
foreach($App in $Apps.GetEnumerator()) {
if (-not $AppsCompiled.ContainsKey($App.Value.name)) {
#test if all dependencies are compiled
$DependencyOk = $true
foreach ($Dependency in $App.Value.dependencies) {
if (-not $Apps.Contains($Dependency.name)) {
$NewApp=New-Object -TypeName PSObject
$NewApp | Add-Member -MemberType NoteProperty -Name 'name' -Value $Dependency.name
$NewApp | Add-Member -MemberType NoteProperty -Name 'version' -Value $Dependency.version
$NewApp | Add-Member -MemberType NoteProperty -Name 'publisher' -Value $Dependency.publisher
$NewApp | Add-Member -MemberType NoteProperty -Name 'AppPath' -Value ""
if (-not $AppsCompiled.ContainsKey($Dependency.name)) {
$AppsCompiled.Add($Dependency.name,$NewApp)
$AppsToAdd.Add($Dependency.name,$NewApp)
$AppsOrdered += $NewApp
}
}
if (-not $AppsCompiled.ContainsKey($Dependency.name)) {
$DependencyOk = $false
}
}
if ($DependencyOk) {
$AppsOrdered += $App.Value
$AppsCompiled.Add($App.Value.name,$App.Value)
}
}
}
foreach ($App in $AppsToAdd.GetEnumerator()) {
$Apps.Add($App.Value.name,$App.Value)
}
$AppsToAdd =@{}
} while ($Apps.Count -ne $AppsCompiled.Count)
return $AppsOrdered
}
function Get-AppJsonFromApp
{
Param(
$AppFile,
$ContainerName
)
$AppDeps = @()
$AppInfo = Get-NavContainerAppInfoFile -AppPath $AppFile -ContainerName $ContainerName
$AppJson = New-Object -TypeName PSObject
$AppJson | Add-Member -MemberType NoteProperty -Name "name" -Value $AppInfo.Name
$AppJson | Add-Member -MemberType NoteProperty -Name "publisher" -Value $AppInfo.Publisher
$AppJson | Add-Member -MemberType NoteProperty -Name "version" -Value $AppInfo.Version
foreach ($AppDep in $AppInfo.Dependencies) {
$AppDepJson = New-Object -TypeName PSObject
$AppDepJson | Add-Member -MemberType NoteProperty -Name "name" -Value $AppDep.Name
$AppDepJson | Add-Member -MemberType NoteProperty -Name "publisher" -Value $AppDep.Publisher
$AppDepJson | Add-Member -MemberType NoteProperty -Name "version" -Value $AppDep.MinVersion
$AppDeps += $AppDepJson
}
$AppJson | Add-Member -MemberType NoteProperty -Name "dependencies" -Value $AppDeps
$AppJson | Add-Member -MemberType NoteProperty -Name "AppPath" -Value $AppFile
return $AppJson
}
if($AppCollection) {
$Apps = $AppCollection
}
else {
$AppConfigs = Get-ChildItem -Path $Path -Filter App.json -Recurse
if ($AppConfigs) {
$Apps = ConvertTo-ALAppsInfo -Files $AppConfigs
}
}
if(-not $Apps) {
$Apps = @{}
$AppFiles = Get-ChildItem -Path $Path -Filter *.app -Recurse:$Recurse
foreach ($AppFile in $AppFiles) {
#$App = Get-NAVAppInfo -Path $AppFile.FullName
$App = Get-AppJsonFromApp -AppFile $AppFile.FullName -ContainerName $ContainerName
if ($App.publisher -ne 'Microsoft') {
$Apps.Add($App.name,$App)
}
}
}
$AppsOrdered = Get-ALBuildOrder -Apps $Apps
Write-Output $AppsOrdered
}