Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Add support for indirect template + customizations #853

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Actions/CheckForUpdates/CheckForUpdates.HelperFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function IsDirectALGo {

function GetSrcFolder {
Param(
[hashtable] $repoSettings,
[string] $repoType,
[string] $templateUrl,
[string] $templateFolder,
[string] $srcPath
Expand All @@ -291,7 +291,7 @@ function GetSrcFolder {
return ''
}
if (IsDirectALGo -templateUrl $templateUrl) {
switch ($repoSettings.type) {
switch ($repoType) {
"PTE" {
$typePath = "Per Tenant Extension"
}
Expand Down
91 changes: 82 additions & 9 deletions Actions/CheckForUpdates/CheckForUpdates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
# ContainerHelper is used for determining project folders and dependencies
DownloadAndImportBcContainerHelper

$anchors = @{
"_BuildALGoProject.yaml" = @{
"BuildALGoProject" = @(
@{ "Step" = 'Read settings'; "Before" = $false }
@{ "Step" = 'Read secrets'; "Before" = $false }
@{ "Step" = 'Build'; "Before" = $true }
@{ "Step" = 'Build'; "Before" = $false }
@{ "Step" = 'Cleanup'; "Before" = $true }
)
}
}

if ($update -eq 'Y') {
if (-not $token) {
throw "A personal access token with permissions to modify Workflows is needed. You must add a secret called GhTokenWorkflow containing a personal access token. You can Generate a new token from https://github.com/settings/tokens. Make sure that the workflow scope is checked."
Expand Down Expand Up @@ -64,19 +76,48 @@
$downloadLatest = $true
}

$realTemplateFolder = $null
$templateFolder = DownloadTemplateRepository -headers $headers -templateUrl $templateUrl -templateSha ([ref]$templateSha) -downloadLatest $downloadLatest
Write-Host "Template Folder: $templateFolder"

$templateBranch = $templateUrl.Split('@')[1]
$templateOwner = $templateUrl.Split('/')[3]

$indirectTemplateRepoSettings = @{}
$indirectTemplateProjectSettings = @{}

$isDirectALGo = IsDirectALGo -templateUrl $templateUrl
if (-not $isDirectALGo) {
$ALGoSettingsFile = Join-Path $templateFolder "*/$repoSettingsFile"
if (Test-Path -Path $ALGoSettingsFile -PathType Leaf) {
$templateRepoSettings = Get-Content $ALGoSettingsFile -Encoding UTF8 | ConvertFrom-Json | ConvertTo-HashTable -Recurse
$myRepoSettingsFile = Join-Path $templateFolder "*/$RepoSettingsFile"
if (Test-Path -Path $myRepoSettingsFile -PathType Leaf) {
$templateRepoSettings = Get-Content $myRepoSettingsFile -Encoding UTF8 | ConvertFrom-Json | ConvertTo-HashTable -Recurse
if ($templateRepoSettings.Keys -contains "templateUrl" -and $templateRepoSettings.templateUrl -ne $templateUrl) {
throw "The specified template repository is not a template repository, but instead another AL-Go repository. This is not supported."
# The template repository is a url to another AL-Go repository (an indirect template repository)
# TemplateUrl and TemplateSha from .github/AL-Go-Settings.json in the indirect template reposotiry points to the "real" template repository
# Copy files and folders from the indirect template repository, but grab the unmodified file from the "real" template repository if it exists and apply customizations
Write-Host "Indirect AL-Go template repository detected, downloading the 'real' template repository"
$realTemplateUrl = $templateRepoSettings.templateUrl
if ($templateRepoSettings.Keys -contains "templateSha") {
$realTemplateSha = $templateRepoSettings.templateSha
}
else {
$realTemplateSha = ""
}
# Download the "real" template repository - use downloadLatest if no TemplateSha is specified in the indirect template repository
$realTemplateFolder = DownloadTemplateRepository -headers $headers -templateUrl $realTemplateUrl -templateSha ([ref]$realTemplateSha) -downloadLatest ($realTemplateSha -eq '')
Write-Host "Real Template Folder: $realTemplateFolder"

# Set TemplateBranch and TemplateOwner
# Keep TemplateUrl and TemplateSha pointing to the indirect template repository
$templateBranch = $realTemplateUrl.Split('@')[1]
$templateOwner = $realTemplateUrl.Split('/')[3]

$indirectTemplateRepoSettings = $templateRepoSettings
$myALGoSettingsFile = Join-Path $templateFolder "*/$ALGoSettingsFile"
if (Test-Path $myALGoSettingsFile -PathType Leaf) {
Write-Host "Read project settings from indirect template repository"
$indirectTemplateProjectSettings = Get-Content $myALGoSettingsFile -Encoding UTF8 | ConvertFrom-Json | ConvertTo-HashTable -Recurse
}
}
}
}
Expand Down Expand Up @@ -128,10 +169,21 @@
$srcPath = $checkfile.srcPath
$dstPath = $checkfile.dstPath
$dstFolder = Join-Path $baseFolder $dstPath
$srcFolder = GetSrcFolder -repoSettings $repoSettings -templateUrl $templateUrl -templateFolder $templateFolder -srcPath $srcPath
$srcFolder = GetSrcFolder -repoType $repoSettings.type -templateUrl $templateUrl -templateFolder $templateFolder -srcPath $srcPath
$realSrcFolder = $null
if ($realTemplateFolder) {
$realSrcFolder = GetSrcFolder -repoType $repoSettings.type -templateUrl $realTemplateUrl -templateFolder $realTemplateFolder -srcPath $srcPath
}
if ($srcFolder) {
Push-Location -Path $srcFolder
try {
if ($srcPath -eq '.AL-Go' -and $type -eq "script" -and $realSrcFolder) {
Write-Host "Update Project Settings"
# Copy individual settings from the indirect template repository .AL-Go/settings.json (if the setting doesn't exist in the project folder)
$projectSettingsFile = Join-Path $dstFolder "settings.json"
UpdateSettingsFile -settingsFile $projectSettingsFile -updateSettings @{} -additionalSettings $indirectTemplateProjectSettings
$updateFiles += @{ "DstFile" = Join-Path $dstPath "settings.json"; "content" = (Get-Content -Path $projectSettingsFile -Encoding UTF8 -Raw) }
}
# Loop through all files in the template repository matching the pattern
Get-ChildItem -Path $srcFolder -Filter $checkfile.pattern | ForEach-Object {
# Read the template file and modify it based on the settings
Expand All @@ -140,24 +192,41 @@
Write-Host "- $filename"
$dstFile = Join-Path $dstFolder $fileName
$srcFile = $_.FullName
$realSrcFile = $srcFile
$isFileDirectALGo = $isDirectALGo
Write-Host "SrcFolder: $srcFolder"
if ($realSrcFolder) {
# if SrcFile is an indirect template repository, we need to find the file in the "real" template repository
$fname = Join-Path $realSrcFolder (Resolve-Path $srcFile -Relative)
if (Test-Path -Path $fname -PathType Leaf) {
Write-Host "File is available in the 'real' template repository"
$realSrcFile = $fname
$isFileDirectALGo = IsDirectALGo -templateUrl $realTemplateUrl
}
}
if ($type -eq "workflow") {
# for workflow files, we might need to modify the file based on the settings
$srcContent = GetWorkflowContentWithChangesFromSettings -srcFile $srcFile -repoSettings $repoSettings -depth $depth
$srcContent = GetWorkflowContentWithChangesFromSettings -srcFile $realSrcFile -repoSettings $repoSettings -depth $depth
}
else {
# For non-workflow files, just read the file content
$srcContent = Get-ContentLF -Path $srcFile
$srcContent = Get-ContentLF -Path $realSrcFile
}

# Replace static placeholders
$srcContent = $srcContent.Replace('{TEMPLATEURL}', $templateUrl)

if ($isDirectALGo) {
if ($isFileDirectALGo) {
# If we are using direct AL-Go repo, we need to change the owner to the remplateOwner, the repo names to AL-Go and AL-Go/Actions and the branch to templateBranch
ReplaceOwnerRepoAndBranch -srcContent ([ref]$srcContent) -templateOwner $templateOwner -templateBranch $templateBranch
}

if ($type -eq 'workflow' -and $realSrcFile -ne $srcFile) {
# Apply customizations from indirect template repository
Write-Host "Apply customizations from indirect template repository: $srcFile"
[Yaml]::ApplyCustomizations([ref] $srcContent, $srcFile, $anchors)
}

Fixed Show fixed Hide fixed
$dstFileExists = Test-Path -Path $dstFile -PathType Leaf
if ($unusedALGoSystemFiles -contains $fileName) {
# file is not used by ALGo, remove it if it exists
Expand All @@ -167,6 +236,10 @@
}
}
elseif ($dstFileExists) {
if ($type -eq 'workflow') {
Write-Host "Apply customizations from my repository: $dstFile"
[Yaml]::ApplyCustomizations([ref] $srcContent,$dstFile, $anchors)
}
# file exists, compare and add to $updateFiles if different
$dstContent = Get-ContentLF -Path $dstFile
if ($dstContent -cne $srcContent) {
Expand Down Expand Up @@ -204,7 +277,7 @@

invoke-git status

UpdateSettingsFile -settingsFile (Join-Path ".github" "AL-Go-Settings.json") -updateSettings @{ "templateUrl" = $templateUrl; "templateSha" = $templateSha }
UpdateSettingsFile -settingsFile (Join-Path ".github" "AL-Go-Settings.json") -updateSettings @{ "templateUrl" = $templateUrl; "templateSha" = $templateSha } -additionalSettings $indirectTemplateRepoSettings

# Update the files
# Calculate the release notes, while updating
Expand Down