-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponentspush.ps1
72 lines (57 loc) · 3.34 KB
/
componentspush.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
Param(
[string]$libraryId = "",
[string]$apiKey = "",
[string]$endpoint = "https://components.sitecorecloud.io/api",
[string]$rootFolder = ".\src\components"
)
$headers = @{
"x-api-key" = $apiKey
}
function Invoke-PostVersion ($libraryId, $collectionId, $componentId, $version) {
Write-Host " Version: " $version.Name "(" $version.status ")"
$versionsUrl = "$endpoint/libraries/$libraryId/collections/$collectionId/components/$componentId/versions"
Invoke-WebRequest -Uri $versionsUrl -Method Post -Body ($version | ConvertTo-Json -depth 100) -Headers $headers -ContentType "application/json"
}
# Components
$collections = Get-Content "$rootFolder\collections.json" | ConvertFrom-Json
foreach ($collection in $collections) {
Write-Host "Collection: " $collection.
Invoke-WebRequest -Uri "$endpoint/libraries/$libraryId/collections" -Method Post -Body ($collection | ConvertTo-Json -depth 100) -Headers $headers -ContentType "application/json"
foreach ($component in $collection.components) {
Write-Host " Component: " $component.Name
$componentUrl = "$endpoint/libraries/$libraryId/collections/$($collection.id)/components"
Invoke-WebRequest -Uri $componentUrl -Method Post -Body ($component | ConvertTo-Json -depth 100) -Headers $headers -ContentType "application/json"
$versions = Get-Content "$rootFolder\components\$($component.id).json" | ConvertFrom-Json
# We must create the versions in a specific order for the components to be published and available in Pages: saved, staged, published, draft
$savedVersions = $versions | Where-Object -Property status -EQ 'saved'
$stagedVersions = $versions | Where-Object -Property status -EQ 'staged'
$publishedVersions = $versions | Where-Object -Property status -EQ 'published'
$draftVersions = $versions | Where-Object -Property status -EQ 'draft'
foreach ($version in $savedVersions) {
Invoke-PostVersion $libraryId $collection.id $component.id $version
}
foreach ($version in $stagedVersions) {
Invoke-PostVersion $libraryId $collection.id $component.id $version
}
foreach ($version in $publishedVersions) {
Invoke-PostVersion $libraryId $collection.id $component.id $version
}
foreach ($version in $draftVersions) {
Invoke-PostVersion $libraryId $collection.id $component.id $version
}
}
}
# Styles
Write-Host "Styles"
$targetStyles = (Invoke-WebRequest -Uri "$endpoint/libraries/$libraryId/stylesheets" -Method Get -Headers $headers -UseBasicParsing -ContentType "application/json").Content | ConvertFrom-Json
$stylesUrl = "$endpoint/libraries/$libraryId/stylesheets"
$styles = Get-Content "$rootFolder\styles.json" | ConvertFrom-Json
$styles.revision = $targetStyles.revision + 1
Invoke-WebRequest -Uri $stylesUrl -Method Put -Body ($styles | ConvertTo-Json -depth 100) -Headers $headers -ContentType "application/json"
# Datasources
$datasourcesUrl = "$endpoint/libraries/$libraryId/datasources"
$datasources = Get-Content "$rootFolder\datasources.json" | ConvertFrom-Json
foreach ($datasource in $datasources) {
Write-Host "Datasource: " $datasource.Name
Invoke-WebRequest -Uri $datasourcesUrl -Method Post -Body ($datasource | ConvertTo-Json -depth 100) -Headers $headers -ContentType "application/json"
}