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

Support creating a single file with Export-CrescendoCommand #165

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
RootModule = 'Microsoft.PowerShell.Crescendo.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'
ModuleVersion = '1.1.0'

# ID used to uniquely identify this module
GUID = '2dd09744-1ced-4636-a8ce-09a0bf0e566a'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,30 +621,88 @@ function New-CrescendoCommand {
}

function Export-CrescendoCommand {
[CmdletBinding(SupportsShouldProcess=$true)]
[CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="MultipleFile")]
param (
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[Command[]]$command,
[Parameter()][string]$targetDirectory = "."
[Parameter(ParameterSetName="MultipleFile")][string]$targetDirectory = ".",
[Parameter(ParameterSetName="SingleFile", Mandatory=$true)][string]$fileName = "",
[Parameter(ParameterSetName="SingleFile")][switch]$Force
)

BEGIN
{
if ( $PSCmdlet.ParameterSetName -eq "SingleFile") {
$commandConfigurations = @()
$outputFile = Get-Item -Path $filename -ErrorAction Ignore

if ( @($outputFile).Count -gt 1) {
throw ("'$fileName' must resolve to a single file")
}

# output file does not exist
if ( ! $outputFile ) {
$outputFile = $fileName
}
else {
# check to see if the path is a directory
if ( $outputFile.PSIsContainer ) {
throw ("'$fileName' is a directory, it must resolve to a single file")
}
if ( $Force ) {
$outputFile.Delete()
}
else {
throw ("File '$fileName' already exists. Use -Force to overwrite")
}
}
}
}

PROCESS
{
foreach($crescendoCommand in $command) {
if($PSCmdlet.ShouldProcess($crescendoCommand)) {
$fileName = "{0}-{1}.crescendo.json" -f $crescendoCommand.Verb, $crescendoCommand.Noun
$exportPath = Join-Path $targetDirectory $fileName
$crescendoCommand.ExportConfigurationFile($exportPath)

if($PSCmdlet.ShouldProcess($crescendoCommand.FunctionName)) {
if ($PSCmdlet.ParameterSetName -eq "MultipleFile") {
$fileName = "{0}-{1}.crescendo.json" -f $crescendoCommand.Verb, $crescendoCommand.Noun
$exportPath = Join-Path $targetDirectory $fileName
$crescendoCommand.ExportConfigurationFile($exportPath)
}
else {
$commandConfigurations += $crescendoCommand
}
}
}
}

END
{
# there's nothing to do for this parameter set.
if ($PSCmdlet.ParameterSetName -eq "MultipleFile") {
return
}

# now save all the command configurations to a single file.
$multiConfiguration = [System.Collections.Specialized.OrderedDictionary]::new()
$multiConfiguration.Add('$schema', 'https://aka.ms/PowerShell/Crescendo/Schemas/2022-06')
$multiConfiguration.Add('commands', $commandConfigurations)
$sOptions = [System.Text.Json.JsonSerializerOptions]::new()
$sOptions.WriteIndented = $true
$sOptions.MaxDepth = 10
$sOptions.IgnoreNullValues = $true
$text = [System.Text.Json.JsonSerializer]::Serialize($multiConfiguration, $sOptions)
if ($PSCmdlet.ShouldProcess($outputFile)) {
Out-File -LiteralPath $outputFile -InputObject $text
}
}
}

function Import-CommandConfiguration
{
[CmdletBinding()]
param ([Parameter(Position=0,Mandatory=$true)][string]$file)
param (
[Parameter(Position=0,Mandatory=$true)][string]$file
)
$options = [System.Text.Json.JsonSerializerOptions]::new()
# this dance is to support multiple configurations in a single file
# The deserializer doesn't seem to support creating [command[]]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Describe "Export-CrescendoCommand tests" {
BeforeAll {
$assetDir = Join-Path -Path $PSScriptRoot -ChildPath "../Samples"
$configurationSet = Get-ChildItem -Path $assetDir -Filter "Docker*" |
ForEach-Object { $_.FullName } |
Sort-Object
$commands = $configurationSet | ForEach-Object { Import-CommandConfiguration $_ }
$commandNames = $commands.FunctionName | Sort-Object
$expectedFilenames = $commandNames.foreach({"${_}.crescendo.json"})
$singleCommand = $commands[0]
}

Context "MultipleFile parameter set" {
BeforeAll {
Export-CrescendoCommand -Command $commands -targetDirectory TestDrive:
$expectedCount = $expectedFilenames.Count
$testCases = $expectedFilenames | Foreach-Object {
@{ file = $_ }
}
}

It "Correct count of files should be present" {
(Get-ChildItem TestDrive:).Count | Should -Be $expectedCount
}

It "individual file '<file>' exists" -testCases $testCases {
param ( $file )
$filePath = Join-Path -Path TestDrive: -ChildPath $file
$filePath | Should -Exist
}
}

Context "SingleFile parameter set" {
BeforeAll {
$comboCrescendoFile = "TestDrive:combo.crescendo.json"
Export-CrescendoCommand -Command $commands -filename $comboCrescendoFile
$comboJson = Get-Content $comboCrescendoFile | ConvertFrom-Json
$comboCommandNames = $comboJson.Commands.Foreach({"{0}-{1}" -f $_.Verb, $_.Noun})
$testCases = $comboCommandNames.Foreach({@{ file = $_ }})

$singleCrescendoFile = "TestDrive:single.crescendo.json"
Export-CrescendoCommand -Command $singleCommand -filename $singleCrescendoFile
$singleJson = Get-Content $singleCrescendoFile | ConvertFrom-Json
$singleCommandName = $singleCommand.FunctionName
}

It "file 'TestDrive:combo.crescendo.json' should exist" {
Get-Item $comboCrescendoFile | Should -Exist
}

It "The file should have the correct count of commands" {
$comboJson.Commands.Count | Should -Be $expectedCount
}

It "The command '<file>' is found in the configuration" -TestCases $testCases {
param ( $file )
$file | Should -BeIn $comboCommandNames
}

It "Single file configuration should exist" {
Get-Item $singleCrescendoFile | Should -Exist
}

It "The single file configuration should have the correct count of commands" {
$singleJson.Commands.Count | Should -Be 1
}

It "The single file configuration should have the correct command" {
$cmdName = $singleJson.Commands[0].Verb + "-" + $singleJson.Commands[0].Noun
$cmdName | Should -Be $singleCommandName
}

It "If the file exists, an error should be thrown" {
{ Export-CrescendoCommand -Command $commands -filename $comboCrescendoFile } |
Should -Throw "File '$comboCrescendoFile' already exists"
}

It "Force should overwrite the file without error" {
Export-CrescendoCommand -Command $commands[0,1] -filename $comboCrescendoFile -Force
$comboCrescendoFile | Should -Exist
$json = Get-Content $comboCrescendoFile | ConvertFrom-Json
$json.Commands.Count | Should -Be 2
}

It "WhatIf should not create a file" {
$whatifFile = "TestDrive:whatif.crescendo.json"
$cmd = New-CrescendoCommand -Noun Get -Verb Thing -OriginalName notexist
Export-CrescendoCommand -Command $cmd -filename $whatifFile -WhatIf
$whatifFile | Should -Not -Exist
}

}


}