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

Publish-PSResource: Pack and Push Feature #1682

Merged

Conversation

jshigetomi
Copy link
Contributor

@jshigetomi jshigetomi commented Jul 31, 2024

PR Summary

Fixes: #1635

Decomposed Publish-PSResource to Pack and Push model with an addition of Compress-PSResource command.

Compress-PSResource requires a path to a PS module/script and a destination path to save the resulting nupkg.

Added -Nupkg parameter to Publish-PSResource that pushes the .nupkg file to a specified repository.

Publish-PSResource retains old functionality

PR Context

This change is to meet an SFI2 requirement to ensure signed packages. This PR enables publishing signed packages through Publish-PSResource by first packing (Compress-PSResource) then signing (User's Choice) then pushing(Publish-PSResource -Nupkg .

PR Checklist

@jshigetomi jshigetomi changed the title Publish-PSResource: Pack and Push eature Publish-PSResource: Pack and Push Feature Jul 31, 2024
@jshigetomi jshigetomi marked this pull request as draft July 31, 2024 19:33
@jshigetomi jshigetomi marked this pull request as ready for review August 2, 2024 06:44
Copy link
Member

@alerickson alerickson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good, just made some small suggestions here and there.

test/PublishPSResourceTests/PublishPSResource.Tests.ps1 Outdated Show resolved Hide resolved
test/PublishPSResourceTests/CompressPSResource.Tests.ps1 Outdated Show resolved Hide resolved
src/code/CompressPSResource.cs Outdated Show resolved Hide resolved
src/code/CompressPSResource.cs Outdated Show resolved Hide resolved
src/code/CompressPSResource.cs Outdated Show resolved Hide resolved
src/code/PSResourceHelper.cs Outdated Show resolved Hide resolved
src/code/PSResourceHelper.cs Outdated Show resolved Hide resolved
}
finally
{
if(_callerCmdlet == CallerCmdlet.CompressPSResource)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we always want to delete outputDir when we're done with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CompressPSResource path deletes it here. But the PublishPSResource path still needs it for push to preserve the previous functionality. The PushResource method finally block handles that.

{
if(_isNupkgPathSpecified)
{
outputNupkgDir = pathToNupkgToPublish;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also consider having ouputNupkgDir as a parameter that's passed in to PushResource()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing outputNupkgDir makes it difficult to maintain old PublishResource Behavior. All the outputNupkgDir logic needs to be done in Publish and not in helper and that means in Compress as well.

src/code/PSResourceHelper.cs Outdated Show resolved Hide resolved
@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@adityapatwardhan
Copy link
Member

Closing and re-opening to re-trigger CI

@adityapatwardhan
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jshigetomi
Copy link
Contributor Author

Fixed a test that was strangely passing on local machine but failing on git.

The problem was with

Get-ChildItem $script:repositoryPath2 | Should -Be $expectedPath
changed to
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -567,6 +567,8 @@ Describe "Test Publish-PSResource" -tags 'CI' {

$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath

Remove-Item -Path $scriptPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be moved into the "After All" or "After Each" (whichever is appropriate), just to isolate the test functionality itself and have clean up done outside of test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't delete the test.ps1 file it creates. It only happen for this test.
Maybe it can be like the first test where it removes it in the test?

    It "Publish module with required module not installed on the local machine using -SkipModuleManifestValidate" {
        # Skip the module manifest validation test, which fails from the missing manifest required module.
        $testModulePath = Join-Path -Path $TestDrive -ChildPath ModuleWithMissingRequiredModule
        Publish-PSResource -Path $testModulePath -Repository $testRepository2 -Confirm:$false -SkipDependenciesCheck -SkipModuleManifestValidate

        $expectedPath = Join-Path -Path $script:repositoryPath2  -ChildPath 'ModuleWithMissingRequiredModule.1.0.0.nupkg'
        $publishedModuleFound = Test-Path -Path $expectedPath
        $publishedModuleFound | Should -BeTrue

        if ($publishedModuleFound) {
            Remove-Item $expectedPath -Force -ErrorAction SilentlyContinue
        }
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry I was just looking at the line kind of out of context. Really the entire line should be removed because $scriptPath will be deleted during clean up in the "After Each" block already:

$pkgsToDelete = Join-Path -Path "$script:repositoryPath" -ChildPath "*"


private bool PackNupkg(string outputDir, string outputNupkgDir, string nuspecFile, out ErrorRecord error)
{
_cmdlet.WriteDebug($"In {_callerCmdlet}::PackNupkg()");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These debug messages should specify "PublishHelper" instead of "_callerCmdlet" to stay consistent with how we're using the debug/verbose messages elsewhere in the repo

Copy link
Contributor Author

@jshigetomi jshigetomi Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Should I also change the errors as well like...

                    _cmdlet.WriteError(new ErrorRecord(
                        new ArgumentException($"The resource repository '{Repository}' is not a registered. Please run 'Register-PSResourceRepository' in order to publish to this repository."),
                        "RepositoryNotFound",
                        ErrorCategory.ObjectNotFound,
                        this._cmdlet));

                    return;

should this._cmdlet be instead this? that refers to the PublishHelper?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just leave it as this without the .cmdlet (eg:

_cmdletPassedIn.WriteError(new ErrorRecord(
). And looking at that example, could you change _cmdlet to _cmdletPassedIn? Staying consistent with the naming convention just helps us in the future if there's changes we need to make to the entire code base. It's easier to search and replace one term instead of looking up the different permutations

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@adityapatwardhan
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@adityapatwardhan
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@adityapatwardhan
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@alerickson
Copy link
Member

/azp run PowerShell.PSResourceGet

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@adityapatwardhan adityapatwardhan merged commit e9e8ecb into PowerShell:master Aug 23, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for Publishing "Packed" Modules to PSGallery
3 participants