Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedrichWeinmann committed Jan 3, 2025
1 parent 5876757 commit e2044bb
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 158 deletions.
24 changes: 2 additions & 22 deletions PSFramework.NuGet/functions/Get/Publish-PSFModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
foreach ($sourceModule in $Path) {
# Update Metadata per Parameter
$moduleData = Copy-Module -Path $sourceModule -Destination $workingDirectory -Cmdlet $PSCmdlet -Continue
Update-ModuleInformation -Module $moduleData -Tags $Tags -LicenseUri $LicenseUri -IconUri $IconUri -ProjectUri $ProjectUri -ReleaseNotes $ReleaseNotes -Prerelease $Prerelease -Cmdlet $PSCmdlet -Continue
Update-PSFModuleManifest -Path $moduleData.ManifestPath -Tags $Tags -LicenseUri $LicenseUri -IconUri $IconUri -ProjectUri $ProjectUri -ReleaseNotes $ReleaseNotes -Prerelease $Prerelease -Cmdlet $PSCmdlet -Continue

# Case 1: Publish to Destination Path
if ($DestinationPath) {
Expand Down Expand Up @@ -108,24 +108,4 @@
Remove-PSFTempItem -ModuleName PSFramework.NuGet -Name Publish.*
}
}
}
<#
- Path
- DestinationPath (-/V3)
- Repository
- Type
- Credential
- ApiKey
- SkipAutopmaticTags (V2/-) - Disregard
- Force (V2/-) - Disregard
- SkipDependenciesCheck (-/V3) - Partial (only for V3, as V2 does not support)
- SkipModuleManifestValidate (-/V3) - Always. Cheat to make V2 work out.
# Will be implemented outside of the Get Commands
- Tags (V2/-)
- LicenseUri (V2/-)
- IconUri (V2/-)
- ProjectUri (V2/-)
#>
}
167 changes: 167 additions & 0 deletions PSFramework.NuGet/functions/Get/Update-PSFModuleManifest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
function Update-PSFModuleManifest {
[CmdletBinding()]
param (
$Path,

[string[]]
$Tags,

[string]
$LicenseUri,

[string]
$IconUri,

[string]
$ProjectUri,

[string]
$ReleaseNotes,

[string]
$Prerelease,

[switch]
$PassThru,

$Cmdlet = $PSCmdlet,

[switch]
$Continue
)
begin {
#region Utility Functions
function Update-ManifestProperty {
[OutputType([System.Management.Automation.Language.Ast])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[System.Management.Automation.Language.Ast]
$Ast,

[Parameter(Mandatory = $true)]
[string]
$Property,

[Parameter(Mandatory = $true)]
$Value,

[Parameter(Mandatory = $true)]
[ValidateSet('String', 'StringArray', 'HashtableArray')]
[string]
$Type
)

$mainHash = $Ast.FindAll({
$args[0] -is [System.Management.Automation.Language.HashtableAst] -and
$args[0].KeyValuePairs.Item1.Value -contains 'RootModule' -and
$args[0].KeyValuePairs.Item1.Value -Contains 'ModuleVersion'
}, $true)

$entry = $mainhash.KeyValuePairs | Where-Object { $_.Item1.Value -eq $Property }
$stringValue = switch ($Type) {
'String' { $Value | ConvertTo-Psd1 }
'StringArray' { , @(, @($Value)) | ConvertTo-Psd1 }
'HashtableArray' { , @(, @($Value)) | ConvertTo-Psd1 }
}
$format = '{0}'
#region Case: Key Already Exists
if ($entry) {
$start = $entry.Item2.Extent.StartOffset
$end = $entry.Item2.Extent.EndOffset
}
#endregion Case: Key Already Exists

#region Case: Key Does not exist
else {
$line = $Ast.Extent.Text -split "`n" | Where-Object { $_ -match "#\s+$Property = " }
# Entry already exists but is commented out
if ($line) {
$format = "$Property = {0}"
$index = $Ast.Extent.Text.IndexOf($line)
$start = $index + $line.Length - $line.TrimStart().Length
$end = $index + $line.Length
}
# Entry does not exist already
else {
$indent = ($Ast.Extent.Text -split "`n" | Where-Object { $_ -match "^\s+ModuleVersion" }) -replace '^(\s*).+$', '$1'
$format = "$($indent)$($Property) = {0}`n"
$start = $mainHash.Extent.EndOffset - 1
$end = $mainHash.Extent.EndOffset - 1
}
}
#endregion Case: Key Does not exist

$newText = $Ast.Extent.Text.SubString(0,$start) + ($format -f $stringValue) + $Ast.Extent.SubString($end)
[System.Management.Automation.Language.Parser]::ParseInput($newText, [ref]$null, [ref]$null)
}
#endregion Utility Functions
}
process {
# If Nothing to do, do nothing
if (-not ($Tags -or $LicenseUri -or $IconUri -or $ProjectUri -or $ReleaseNotes -or $Prerelease)) { return }

$killIt = $ErrorActionPreference -eq 'Stop'

$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)

$mainHash = $ast.FindAll({
$args[0] -is [System.Management.Automation.Language.HashtableAst] -and
$args[0].KeyValuePairs.Item1.Value -contains 'RootModule' -and
$args[0].KeyValuePairs.Item1.Value -Contains 'ModuleVersion'
}, $true)

if (-not $mainHash) {
Stop-PSFFunction -String 'Update-PSFModuleManifest.Error.BadManifest' -StringValues (Get-Item -Path $Path).BaseName, $Path -Cmdlet $Cmdlet -EnableException $killIt -Continue:$Continue
return
}

#region PrivateData Content
$privateData = [ordered]@{
PSData = [ordered]@{ }
}
$replacements = @{ }

$privateDataAst = $mainHash.KeyValuePairs | Where-Object { $_.Item1.Value -eq 'PrivateData' } | ForEach-Object { $_.Item2.PipelineElements[0].Expression }

if ($privateDataAst) {
foreach ($pair in $privateDataAst.KeyValuePairs) {
if ($pair.Item1.Value -ne 'PSData') {
$id = "%PSF_$(Get-Random)%"
$privateData[$pair.Item1.Value] = $id
$replacements[$id] = $pair.Item2.Extent.Text
continue
}

foreach ($subPair in $pair.Item2.PipelineElements[0].Expression.KeyValuePairs) {
$id = "%PSF_$(Get-Random)%"
$privateData.PSData[$subPair.Item1.Value] = $id
$replacements[$id] = $subPair.Item2.Extent.Text
}
}
}

if ($Tags) { $privateData.PSData['Tags'] = $Tags }
if ($LicenseUri) { $privateData.PSData['LicenseUri'] = $LicenseUri }
if ($IconUri) { $privateData.PSData['IconUri'] = $IconUri }
if ($ProjectUri) { $privateData.PSData['ProjectUri'] = $ProjectUri }
if ($ReleaseNotes) { $privateData.PSData['ReleaseNotes'] = $ReleaseNotes }
if ($Prerelease) { $privateData.PSData['Prerelease'] = $Prerelease }

$privateDataString = $privateData | ConvertTo-Psd1 -Depth 5
foreach ($pair in $replacements.GetEnumerator()) {
$privateDataString = $privateDataString -replace "'$($pair.Key)'", $pair.Value
}

if (-not $privateDataAst) {
$newManifest = $ast.Extent.Text.Insert(($mainHash.Extent.EndOffset - 1), "PrivateData = $privateDataString`n")
}
else {
$newManifest = $ast.Extent.Text.SubString(0, $privateDataAst.Extent.StartOffset) + $privateDataString + $ast.Extent.Text.SubString($privateDataAst.Extent.EndOffset)
}
#endregion PrivateData Content

if ($PassThru) { $newManifest }
else { $newManifest | Set-Content -Path $Path }
}
}
97 changes: 84 additions & 13 deletions PSFramework.NuGet/functions/Resource/Publish-PSFResourceModule.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,91 @@
function Publish-PSFResourceModule
{
function Publish-PSFResourceModule {
[CmdletBinding()]
Param (

[PsfValidateScript('PSFramework.Validate.SafeName', ErrorString = 'PSFramework.Validate.SafeName')]
[string]
$Name,

[string]
$Version = '1.0.0',

[Parameter(Mandatory = $true)]
[PsfPath]
$Path,

[Parameter(Mandatory = $true, ParameterSetName = 'ToRepository')]
[PsfValidateSet(TabCompletion = 'PSFramework.NuGet.Repository')]
[PsfArgumentCompleter('PSFramework.NuGet.Repository')]
[string[]]
$Repository,

[Parameter(ParameterSetName = 'ToRepository')]
[ValidateSet('All', 'V2', 'V3')]
[string]
$Type = 'All',

[Parameter(ParameterSetName = 'ToRepository')]
[PSCredential]
$Credential,

[Parameter(ParameterSetName = 'ToRepository')]
[string]
$ApiKey,

[Parameter(ParameterSetName = 'ToRepository')]
[switch]
$SkipDependenciesCheck,

[Parameter(Mandatory = $true, ParameterSetName = 'ToPath')]
[PsfDirectory]
$DestinationPath,

[object[]]
$RequiredModules,

[string]
$Description = '<Dummy Description>',

[string]
$Author,

[string[]]
$Tags,

[string]
$LicenseUri,

[string]
$IconUri,

[string]
$ProjectUri,

[string]
$ReleaseNotes,

[string]
$Prerelease
)

begin
{

}
process
{
#TODO: Implement
begin {
$killIt = $ErrorActionPreference -eq 'Stop'
$stagingDirectory = New-PSFTempDirectory -ModuleName 'PSFramework.NuGet' -Name Publish.ResourceModule -DirectoryName $Name
$publishParam = $PSBoundParameters | ConvertTo-PSFHashtable -ReferenceCommand Publish-PSFModule -Exclude Path, ErrorAction
}
end
{

process {
try {
New-DummyModule -Path $stagingDirectory -Name $Name -Version $Version -RequiredModules $RequiredModules -Description $Description -Author $Author -Cmdlet $PSCmdlet
$resources = New-Item -Path $stagingDirectory -Name Resources -ItemType Directory -Force
$Path | Copy-Item -Destination $resources.FullName -Recurse -Force -Confirm:$false -WhatIf:$false

Publish-PSFModule @publishParam -Path $stagingDirectory -ErrorAction Stop
}
catch {
Stop-PSFFunction -String 'Publish-PSFResourceModule.Error' -StringValues $Name -EnableException $killIt -ErrorRecord $_ -Cmdlet $PSCmdlet
return
}
finally {
Remove-PSFTempItem -ModuleName 'PSFramework.NuGet' -Name Publish.ResourceModule
}
}
}
16 changes: 8 additions & 8 deletions PSFramework.NuGet/functions/Resource/Save-PSFResourceModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,30 @@
try {
$saveParam = $PSBoundParameters | ConvertTo-PSFHashtable -ReferenceCommand Save-PSFModule -Exclude Path, ErrorAction
Invoke-PSFProtectedCommand -ActionString 'Save-PSFResourceModule.Downloading' -ActionStringValues ($Name -join ', ') -ScriptBlock {
Save-PSFModule @saveParam -Path $tempDirectory -ErrorAction Stop -WhatIf:$false -Confirm:$false
$null = Save-PSFModule @saveParam -Path $tempDirectory -ErrorAction Stop -WhatIf:$false -Confirm:$false
} -PSCmdlet $PSCmdlet -EnableException $killIt -WhatIf:$false -Confirm:$false
if (Test-PSFFunctionInterrupt) { return }

foreach ($pathEntry in $Path) {
foreach ($module in Get-ChildItem -Path $tempDirectory) {
foreach ($version in Get-ChildItem -LiteralPath $module.FullName) {
$dataPath = Join-Path -Path $version.FullName -ChildPath 'Resources'
foreach ($versionFolder in Get-ChildItem -LiteralPath $module.FullName) {
$dataPath = Join-Path -Path $versionFolder.FullName -ChildPath 'Resources'
if (-not (Test-Path -Path $dataPath)) {
Write-PSFMessage -String 'Save-PSFResourceModule.Skipping.InvalidResource' -StringValues $module.Name, $version.Name
Write-PSFMessage -String 'Save-PSFResourceModule.Skipping.InvalidResource' -StringValues $module.Name, $versionFolder.Name
continue
}
if (-not $PSCmdlet.ShouldProcess("$($module.Name) ($($version.Name))", "Deploy to $pathEntry")) {
if (-not $PSCmdlet.ShouldProcess("$($module.Name) ($($versionFolder.Name))", "Deploy to $pathEntry")) {
continue
}

foreach ($item in Get-ChildItem -LiteralPath $version.FullName) {
foreach ($item in Get-ChildItem -LiteralPath $dataPath) {
$targetPath = Join-Path -Path $pathEntry -ChildPath $item.Name
if (-not $Force -and (Test-path -Path $targetPath)) {
Write-PSFMessage -String 'Save-PSFResourceModule.Skipping.AlreadyExists' -StringValues $module.Name, $version.Name, $item.Name, $pathEntry
Write-PSFMessage -String 'Save-PSFResourceModule.Skipping.AlreadyExists' -StringValues $module.Name, $versionFolder.Name, $item.Name, $pathEntry
continue
}

Invoke-PSFProtectedCommand -ActionString 'Save-PSFResourceModule.Deploying' -ActionStringValues $module.Name, $version.Name, $item.Name, $pathEntry -ScriptBlock {
Invoke-PSFProtectedCommand -ActionString 'Save-PSFResourceModule.Deploying' -ActionStringValues $module.Name, $versionFolder.Name, $item.Name, $pathEntry -ScriptBlock {
Move-Item -LiteralPath $item.FullName -Destination $pathEntry -Force -ErrorAction Stop -Confirm:$false -WhatIf:$false
} -Target $item.Name -PSCmdlet $PSCmdlet -EnableException $killIt -Continue -Confirm:$false -WhatIf:$false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}

$destinationPath = Join-Path -Path $Destination -ChildPath $moduleName
try { Copy-Item -Path "$($sourceDirectoryPath.Trim('\/'))\*" Destination $destinationPath -Recurse -Force -ErrorAction Stop }
try { Copy-Item -Path "$($sourceDirectoryPath.Trim('\/'))\*" -Destination $destinationPath -Recurse -Force -ErrorAction Stop }
catch {
Stop-PSFFunction -String 'Copy-Module.Error.StagingFolderCopy' -StringValues $Path -Target $Path @stopCommon -ErrorRecord $_
return
Expand All @@ -67,8 +67,9 @@

$hashtableAst = $ast.EndBlock.Statements[0].PipelineElements[0].Expression
[PSCustomObject]@{
Name = $moduleNamee
Name = $moduleName
Path = $destinationPath
ManifestPath = Join-Path -Path $destinationPath -ChildPath "$moduleName.psd1"
SourcePath = $sourceDirectoryPath
Author = @($hashtableAst.KeyValuePairs).Where{ $_.Item1.Value -eq 'Author' }.Item2.PipelineElements.Expression.Value
Version = @($hashtableAst.KeyValuePairs).Where{ $_.Item1.Value -eq 'ModuleVersion' }.Item2.PipelineElements.Expression.Value
Expand Down
Loading

0 comments on commit e2044bb

Please sign in to comment.