-
Notifications
You must be signed in to change notification settings - Fork 38
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
JSON created by Export-CrescendoCommand can not be read by Export-CrescendoModule #155
Comments
This used to work. You used to be able to export individual commands to separate JSON files then use The alternative is to have |
That alternative would be far neater - I'll share my reworked version later - I also spotted this
So -verbose or -confirm output the PowerShell built for the command |
Here is my new
function Export-CrescendoCommand {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[Command[]]$Command,
[Alias('TargetDirectory')]
[string]$Path = $pwd.path,
[alias('pt')]
[switch]$PassThru
)
process {
$filesAllowed = @{}
foreach($crescendoCommand in $Command) {
if (Test-Path -Path $Path -PathType Container) {
$exportPath = Join-Path -Path $Path -ChildPath "$($crescendoCommand.Verb)-$($crescendoCommand.Noun).crescendo.json"
}
elseif (Test-Path -IsValid $Path -PathType Leaf) {$exportPath = $Path}
else {throw "$Path must be a direcory or a valid file path."}
#if we have already sent something to this file add ",", newline, and the JSON for this command - but don't close the JSON yet.
if ($filesAllowed.ContainsKey($exportPath)) {
$filesAllowed[$exportPath] +=
",`n " + ($crescendoCommand.GetCrescendoConfiguration() -replace '\n',"`n ")
}
#If not, check we are allowed to output to it, and add the opening and the first command but leave the JSON open.
elseif ($PSCmdlet.ShouldProcess($exportPath)) {
$filesAllowed[$exportPath] =
"{`n" +
" `"`$schema`": `"https://aka.ms/PowerShell/Crescendo/Schemas/2021-11`",`n"+
" `"Commands`": [`n " +
($crescendoCommand.GetCrescendoConfiguration() -replace '\n',"`n " )
}
}
}
end {
foreach ($exportPath in $filesAllowed.Keys) {
#close the json that was left open when we added the command(s) and write the file
Set-Content -Confirm:$false -Path $exportPath -Value ($filesAllowed[$exportPath] + "`n ]`n}")
if ($PassThru) {Get-item $exportPath}
}
}
} |
And my new
function Import-CommandConfiguration {
[CmdletBinding()]
param (
[Parameter(Position=0,Mandatory=$true)]
[Alias('File')]
[string]$Path
)
# this dance is to support multiple configurations in a single file
# The deserializer doesn't seem to support creating [command[]]
$objects = Get-Content $Path -ErrorAction Stop | ConvertFrom-Json -depth 10
if ($objects.commands) {$commands = $objects.commands }
elseif ($objects.verb -and $objects.noun) {$commands = $objects}
else {throw "$Path does not appear to contain suitable JSON"}
$options = [System.Text.Json.JsonSerializerOptions]::new()
foreach ($c in $commands) {
$jsonText = $c | ConvertTo-Json -depth 10
$errs = $null
$configuration = [System.Text.Json.JsonSerializer]::Deserialize($jsonText, [command], $options)
if (-not (Test-Configuration -configuration $configuration -errors ([ref]$errs))) {
$errs | Foreach-Object { Write-Error -ErrorRecord $_ }
}
# emit the configuration even if there was an error
$configuration
}
} |
@theJasonHelmick what I meant on #154 was that if I could send two PRs for these two, each as a file for that function it would be a lot easier than either sending you a PSM with a ton of changes, or making a branch for each change with its own updates to original PSM1. Merging the second means git will say "two people have changed this file - you need to say which changes stay in" |
Thank you @jhoneill for the suggestion with code! I'm reviewing this and will report back after I investigate. Thank you! |
Originally, Export-CrescendoCommand was not designed to provide JSON directly to Export-CrescendoModule in the workflow. But we think this is a reasonable scenario. We are investigating the best course of action and will add this to our future plans. We also don't think the original scenario is invalid, so we don't want to create a breaking change if not necessary. |
fixed by #165 |
This doesn't seem to be fixed with the 1.1 preview when used with |
Thanks @ethanbergstrom -- We are investigating and will resolve along with add another test. |
Export-CrescendoCommand
calls theExportConfigurationFile
method of the command object.This generates json which looks like this
Export-CrescendoModule
expects to find JSON which looks like thisUnless the command definitions are in an array the
Export-CrescendoModule
generates an errorGranted anyone generating code with
Export-CrescendoCommand
can hack the JSON file, but (IMHO) that should not be requirededit crucial word was missing from the last sentence
The text was updated successfully, but these errors were encountered: