Skip to content

Commit

Permalink
projectUpdater:Update it to metadata.json.Add message for major TOS u…
Browse files Browse the repository at this point in the history
…pdate

Replace saving the metadata info in .<files> with a metadata.json on
projectUpdater and also add a message and a confirmation for major
update.

Signed-off-by: Andre Riesco <[email protected]>
  • Loading branch information
andreriesco committed Dec 27, 2024
1 parent 8dbd1c0 commit 2e1053e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 37 deletions.
6 changes: 2 additions & 4 deletions scripts/createFromTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ Write-Host "Project Name -> $projectName"
Write-Host "Container Name -> $containerName"

# get the metadata
$_metadata = Get-Content "$templateFolder/../templates.json" | ConvertFrom-Json
$_templatesJson = Get-Content "$templateFolder/../templates.json" | ConvertFrom-Json
$_templateMetadata =
$_metadata.Templates |
$_templatesJson.Templates |
Where-Object { $_.folder -eq $template }

# send telemetry
Expand Down Expand Up @@ -216,8 +216,6 @@ $_metadataJson = New-Object PSObject

$_metadataJson | Add-Member -MemberType NoteProperty -Name "templateName" -Value $template
$_metadataJson | Add-Member -MemberType NoteProperty -Name "containerName" -Value $containerName

$_templatesJson = Get-Content "$templateFolder/templates.json" | ConvertFrom-Json
$_torizonOSMajor = $_templatesJson.TorizonOSMajor
$_metadataJson | Add-Member -MemberType NoteProperty -Name "torizonOSMajor" -Value $_torizonOSMajor

Expand Down
2 changes: 1 addition & 1 deletion scripts/initWorkspace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (!(Test-Path $env:HOME/.tcd/target.json)) {
}

# check if the workspace is valid
if (!(Test-Path ./.conf/.template)) {
if (!(Get-Content ./.conf/metadata.json | ConvertFrom-Json).templateName -and !(Test-Path ./.conf/.template)) {
Write-Host -ForegroundColor Red "❌ :: This folder does not have a valid Torizon project Workspace :: ❌"
Write-Host ""
exit 400
Expand Down
109 changes: 77 additions & 32 deletions scripts/projectUpdater.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,82 @@ if ([string]::IsNullOrEmpty($acceptAll)) {
}
}

# copy the new one and make the subs
$templateName = Get-Content $projectFolder/.conf/.template
$containerName = Get-Content $projectFolder/.conf/.container
# PROJECT UPDATER - The first thing is to update it
if (
-not (_checkIfFileContentIsEqual `
$Env:HOME/.apollox/scripts/projectUpdater.ps1 `
$projectFolder/.conf/projectUpdater.ps1)
) {
# in this case we need to update the project updater
# and then run it again
Copy-Item `
$Env:HOME/.apollox/scripts/projectUpdater.ps1 `
$projectFolder/.conf/projectUpdater.ps1

Write-Host `
-ForegroundColor DarkYellow `
"⚠️ project updater updated, running it again"

# run the project updater again
& $projectFolder/.conf/projectUpdater.ps1 `
$projectFolder `
$projectName `
$acceptAll `
$true

exit $LASTEXITCODE
}

# get the metadata of templates.json
$_templatesJson = Get-Content "$Env:HOME/.apollox/templates.json" | ConvertFrom-Json

# Replace the the .template and .container with metadata.json
if ((Test-Path "$projectFolder/.conf/.template") -and (Test-Path "$projectFolder/.conf/.container")) {
$templateName = Get-Content $projectFolder/.conf/.template
$containerName = Get-Content $projectFolder/.conf/.container

$_metadataJson = New-Object PSObject
$_metadataJson | Add-Member -MemberType NoteProperty -Name "templateName" -Value $templateName
$_metadataJson | Add-Member -MemberType NoteProperty -Name "containerName" -Value $containerName
# If this property doesn't exist, then we are on Torizon 6
if ($null -eq $_templatesJson.TorizonOSMajor) {
$_torizonOSMajor = "6"
} else {
$_torizonOSMajor = $_templatesJson.TorizonOSMajor
}
$_metadataJson | Add-Member -MemberType NoteProperty -Name "torizonOSMajor" -Value $_torizonOSMajor

# Save the modified JSON object to a file
Set-Content -Path "$projectFolder/.conf/metadata.json" -Value ($_metadataJson | ConvertTo-Json) -Encoding UTF8

Remove-Item -Path $projectFolder/.conf/.template -Force
Remove-Item -Path $projectFolder/.conf/.container -Force
}

# get the metadata of the project
$_metadataJson = Get-Content "$projectFolder/.conf/metadata.json" | ConvertFrom-Json
$templateName = $_metadataJson.templateName
$containerName = $_metadataJson.containerName
$_torizonOSMajor = $_metadataJson.TorizonOSMajor

# If it's not the current version, it's because the person has torizon.templatesBranch setting set
if ($_templatesJson.TorizonOSMajor -ne "7") {
Write-Host -ForegroundColor DarkYellow "The current Torizon OS version is 7. If you want to upgrade to the current version, remove the torizon.templatesBranch setting."
}

$_templatesJsonTorizonMajor = $_templatesJson.TorizonOSMajor

# Major update on the template
if ($_torizonOSMajor -ne $_templatesJsonTorizonMajor) {

Write-Host -ForegroundColor DarkRed "Your template is on Torizon OS version ${_torizonOSMajor} and you are updating it to a template in Torizon OS version ${_templatesJsonTorizonMajor}"
$_sure = Read-Host -Prompt "Are you sure you want to proceed with the update? [y/n]"

if ($_sure -ne "y") {
Write-Host -ForegroundColor DarkRed "If you want to stick to a specific Torizon OS version, set the torizon.templatesBranch on settings.json: https://developer.toradex.com/torizon/application-development/ide-extension/reference-documentation/workspace-settings#torizontemplatesbranch"
exit 0
}
}

# check first if the folder already exists
if (-not (Test-Path $projectFolder/.conf/tmp)) {
Expand All @@ -123,10 +196,8 @@ if ($null -ne $_deprecatedTemplateMetadata) {
exit 69
}

# get the metadata of templates.json
$_metadata = Get-Content "$Env:HOME/.apollox/templates.json" | ConvertFrom-Json
$_templateMetadata =
$_metadata.Templates |
$_templatesJson.Templates |
Where-Object { $_.folder -eq $templateName }

if ($_templateMetadata.status -eq "notok") {
Expand All @@ -151,32 +222,6 @@ Copy-Item `
$projectFolder/.conf/update.json


# PROJECT UPDATER:
if (
-not (_checkIfFileContentIsEqual `
$Env:HOME/.apollox/scripts/projectUpdater.ps1 `
$projectFolder/.conf/projectUpdater.ps1)
) {
# in this case we need to update the project updater
# and then run it again
Copy-Item `
$Env:HOME/.apollox/scripts/projectUpdater.ps1 `
$projectFolder/.conf/projectUpdater.ps1

Write-Host `
-ForegroundColor DarkYellow `
"⚠️ project updater updated, running it again"

# run the project updater again
& $projectFolder/.conf/projectUpdater.ps1 `
$projectFolder `
$projectName `
$acceptAll `
$true

exit $LASTEXITCODE
}

# TASKS.PS1:
Copy-Item `
$Env:HOME/.apollox/scripts/tasks.ps1 `
Expand Down

0 comments on commit 2e1053e

Please sign in to comment.