-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle-byDatatype.ps1
87 lines (78 loc) · 2.14 KB
/
bundle-byDatatype.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
param(
# Tells the script to ignore any content-less sources
[switch]$SkipEmpty,
# Compresses the JSON output
[switch]$Compress
)
if (Test-Path ./indexes/datatypes.json) {
$datatypes = Get-Content ./indexes/datatypes.json -Encoding utf8NoBOM | ConvertFrom-Json
} else {
throw 'Index of datatypes not found! Use `npm run index:datatypes` to generate the index, then rerun this script.'
}
function Group-DatatypeContent {
param (
# Datatype to bundle
[Parameter(Mandatory)]
[string]
$Datatype,
# Families to bundle
[Parameter(Mandatory)]
[string]
$Family
)
process {
$bundle = [System.Collections.Generic.List[pscustomobject]]::new()
$dataPath = if ($Family -eq 'common') {
"./data/common/$Datatype/*"
} elseif ($Datatype -eq 'source') {
"./data/$Family/*/*"
} else {
"./data/$Family/*/$Datatype/*"
}
Get-ChildItem $dataPath -File | ForEach-Object {
$bundle.Add(($_ | Get-Content -Encoding utf8NoBOM | ConvertFrom-Json))
}
if ($SkipEmpty -and -not $bundle.Count) {
$null
} else {
[pscustomobject]@{
$Datatype = $bundle
}
}
}
}
Write-Host "Deleting existing bundle...`n"
if (Test-Path ./bundles/byDatatype) {
Get-ChildItem ./bundles/byDatatype -Recurse -File | Remove-Item
}
function Get-DatatypeContentGroup {
param (
[string]
$Datatype,
[string]
$Family
)
$bundle = Group-DatatypeContent -Datatype $Datatype -Family $Family
if ($bundle) {
if (-not (Test-Path "./bundles/byDatatype/$Family")) {
$null = mkdir "./bundles/byDatatype/$Family"
Write-Host "`nCreated directory ./bundles/byDatatype/$Family"
}
$bundle
| ConvertTo-Json -Depth 99 -Compress:$Compress
| Out-File "./bundles/byDatatype/$Family/$Datatype.json" -Encoding utf8NoBOM
Write-Host "Bundled $Family ""$Datatype"" data."
} else {
Write-Host "Skipped empty bundle of $Family ""$Datatype"" data"
}
}
$datatypes | ForEach-Object {
if ($_ -in @('license', 'sourceGroup')) {
Get-DatatypeContentGroup -Datatype $_ -Family 'common'
} else {
foreach ($family in @('core', 'homebrew')) {
Get-DatatypeContentGroup -Datatype $_ -Family $family
}
}
}
Write-Host "`nBundled everything into ./bundles/byDatatype"