Skip to content

Commit

Permalink
GetPackageId (#3309)
Browse files Browse the repository at this point in the history
Add new function to get package id from publisher, name and id

---------

Co-authored-by: freddydk <freddydk@users.noreply.github.com>
Co-authored-by: Maria Zhelezova <43066499+mazhelez@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 26, 2024
1 parent 40f7c4a commit 8a8f866
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 21 deletions.
1 change: 1 addition & 0 deletions BC.NuGetHelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (-not (([System.Management.Automation.PSTypeName]"NuGetFeed").Type)) {
. (Join-Path $PSScriptRoot "NuGet\New-BcNuGetPackage.ps1")
. (Join-Path $PSScriptRoot "NuGet\Find-BcNuGetPackage.ps1")
. (Join-Path $PSScriptRoot "NuGet\Get-BcNuGetPackage.ps1")
. (Join-Path $PSScriptRoot "NuGet\Get-BcNuGetPackageId.ps1")
. (Join-Path $PSScriptRoot "NuGet\Push-BcNuGetPackage.ps1")
. (Join-Path $PSScriptRoot "NuGet\Publish-BcNuGetPackageToContainer.ps1")
. (Join-Path $PSScriptRoot "NuGet\Download-BcNuGetPackageToFolder.ps1")
Binary file modified BC.NuGetHelper.psd1
Binary file not shown.
3 changes: 2 additions & 1 deletion BcContainerHelper.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ FunctionsToExport = 'Add-FontsToBcContainer', 'Add-GitToAlProjectFolder',
'Get-BcEnvironmentOperations', 'Get-BcEnvironmentPublishedApps',
'Get-BcEnvironments', 'Get-BcEnvironmentScheduledUpgrade',
'Get-BcEnvironmentUpdateWindow', 'Get-BcEnvironmentUsedStorage',
'Get-BcNotificationRecipients', 'Find-BcNuGetPackage', 'Get-BcNuGetPackage',
'Get-BcNotificationRecipients', 'Find-BcNuGetPackage',
'Get-BcNuGetPackage', 'Get-BcNuGetPackageId',
'Get-BestBcContainerImageName', 'Get-BestGenericImageName',
'Get-CloudBcContainerEventLog',
'Get-CloudBcContainerServerConfiguration',
Expand Down
2 changes: 1 addition & 1 deletion NuGet/Download-BcNuGetPackageToFolder.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Function Download-BcNuGetPackageToFolder {
Remove-Item -Path $package -Recurse -Force
continue
}
if (Test-Path (Join-Path $package $installedCountry) -PathType Container) {
if ($installedCountry -and (Test-Path (Join-Path $package $installedCountry) -PathType Container)) {
# NuGet packages of Runtime packages might exist in different versions for different countries
# The runtime package might contain C# invoke calls with different methodis for different countries
# if the installedCountry doesn't have a special version, then the w1 version is used (= empty string)
Expand Down
62 changes: 62 additions & 0 deletions NuGet/Get-BcNuGetPackageId.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<#
.Synopsis
PROOF OF CONCEPT PREVIEW: Get Business Central NuGet Package Id from Publisher, Name and Id
.Description
Get Business Central NuGet Package Id from Publisher, Name and Id
.OUTPUTS
string
Package Id
.PARAMETER packageIdTemplate
Template for package id with placeholders for publisher, name, id and version
.PARAMETER publisher
App Publisher (will be normalized)
.PARAMETER name
App name (will be normalized)
.PARAMETER id
App Id (must be a GUID if present)
.PARAMETER tag
Tag to add to the package id
.PARAMETER version
App Version
.EXAMPLE
Get-BcNuGetPackageId -publisher 'Freddy Kristiansen' -name 'Bing Maps PTE' -id '165d73c1-39a4-4fb6-85a5-925edc1684fb'
#>
function Get-BcNuGetPackageId {
Param(
[Parameter(Mandatory=$false)]
[string] $packageIdTemplate = '{publisher}.{name}.{tag}.{id}',
[Parameter(Mandatory=$true)]
[string] $publisher,
[Parameter(Mandatory=$true)]
[string] $name,
[Parameter(Mandatory=$false)]
[string] $id = '',
[Parameter(Mandatory=$false)]
[string] $tag = '',
[Parameter(Mandatory=$false)]
[string] $version = ''
)

if ($id) {
try { $id = ([GUID]::Parse($id)).Guid } catch { throw "App id must be a valid GUID: $id" }
}
$nname = [nuGetFeed]::Normalize($name)
$npublisher = [nuGetFeed]::Normalize($publisher)
if ($nname -eq '') { throw "App name is invalid: '$name'" }
if ($npublisher -eq '') { throw "App publisher is invalid: '$publisher'" }

$packageIdTemplate = $packageIdTemplate.replace('{id}',$id).replace('{publisher}',$npublisher).replace('{tag}',$tag).replace('{version}',$version).replace('..','.').TrimEnd('.')
# Max. Length of NuGet Package Id is 100 - we shorten the name part of the id if it is too long
$packageId = $packageIdTemplate.replace('{name}',$nname)
if ($packageId.Length -ge 100) {
if ($nname.Length -gt ($packageId.Length - 99)) {
$nname = $nname.Substring(0, $nname.Length - ($packageId.Length - 99))
}
else {
throw "Package id is too long: $packageId, unable to shorten it"
}
$packageId = $packageIdTemplate.replace('{name}',$nname)
}
return $packageId
}
Export-ModuleMember -Function Get-BcNuGetPackageId
22 changes: 3 additions & 19 deletions NuGet/New-BcNuGetPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ Function New-BcNuGetPackage {
$stream.Write($bytes,0,$bytes.Length)
}

function CalcPackageId([string] $packageIdTemplate, [string] $publisher, [string] $name, [string] $id, [string] $version) {
$name = [nuGetFeed]::Normalize($name)
$publisher = [nuGetFeed]::Normalize($publisher)
$packageId = $packageIdTemplate.replace('{id}',$id).replace('{name}',$name).replace('{publisher}',$publisher).replace('{version}',$version)
if ($packageId.Length -ge 100) {
if ($name.Length -gt ($packageId.Length - 99)) {
$name = $name.Substring(0, $name.Length - ($packageId.Length - 99))
}
else {
throw "Package id is too long: $packageId, unable to shorten it"
}
$packageId = $packageIdTemplate.replace('{id}',$id).replace('{name}',$name).replace('{publisher}',$publisher).replace('{version}',$version)
}
return $packageId
}

Write-Host "Create NuGet package"
Write-Host "AppFile:"
Write-Host $appFile
Expand Down Expand Up @@ -134,7 +118,7 @@ Function New-BcNuGetPackage {
}
}
$appJson = Get-AppJsonFromAppFile -appFile $appFile
$packageId = CalcPackageId -packageIdTemplate $packageId -publisher $appJson.publisher -name $appJson.name -id $appJson.id -version $appJson.version.replace('.','-')
$packageId = Get-BcNuGetPackageId -packageIdTemplate $packageId -publisher $appJson.publisher -name $appJson.name -id $appJson.id -version $appJson.version.replace('.','-')
if ($null -eq $packageVersion) {
$packageVersion = [System.Version]$appJson.version
}
Expand Down Expand Up @@ -195,7 +179,7 @@ Function New-BcNuGetPackage {
} else {
$dependencyId = $_.appId
}
$id = CalcPackageId -packageIdTemplate $dependencyIdTemplate -publisher $_.publisher -name $_.name -id $dependencyId -version $_.version.replace('.','-')
$id = Get-BcNuGetPackageId -packageIdTemplate $dependencyIdTemplate -publisher $_.publisher -name $_.name -id $dependencyId -version $_.version.replace('.','-')
$XmlObjectWriter.WriteStartElement("dependency")
$XmlObjectWriter.WriteAttributeString("id", $id)
$XmlObjectWriter.WriteAttributeString("version", $_.version)
Expand All @@ -216,7 +200,7 @@ Function New-BcNuGetPackage {
}
if ($isIndirectPackage.IsPresent) {
$XmlObjectWriter.WriteStartElement("dependency")
$id = CalcPackageId -packageIdTemplate $runtimeDependencyId -publisher $appJson.publisher -name $appJson.name -id $appJson.id -version $appJson.version.replace('.','-')
$id = Get-BcNuGetPackageId -packageIdTemplate $runtimeDependencyId -publisher $appJson.publisher -name $appJson.name -id $appJson.id -version $appJson.version.replace('.','-')
$XmlObjectWriter.WriteAttributeString("id", $id)
$XmlObjectWriter.WriteAttributeString("version", '1.0.0.0')
$XmlObjectWriter.WriteEndElement()
Expand Down
2 changes: 2 additions & 0 deletions ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
6.0.5
Add new function Get-BcNuGetPackageId to get a NuGet Package Id based on publisher, name, id and version
Issue 3301 Run-AlValidation fails running AppsourceCop "Could not load type 'System.Object' from assembly 'System.Private.CoreLib'
Give better error message if GitHub CLI, GIT or dotnet is not installed
Issue 3301 Run-AlValidation fails running AppsourceCop "Could not load type 'System.Object' from assembly 'System.Private.CoreLib' (Regression in 6.0.4)
Issue 3313 Strange error in Copy-BcEnvironment
Expand Down

0 comments on commit 8a8f866

Please sign in to comment.