Skip to content

Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlet - #12845

Closed
Shri Ram K S (Shriram0908) wants to merge 7 commits into
PowerShell:masterfrom
Shriram0908:ConvertTo-Clixml-#3898
Closed

Add ConvertTo-Clixml and ConvertFrom-Clixml cmdlet#12845
Shri Ram K S (Shriram0908) wants to merge 7 commits into
PowerShell:masterfrom
Shriram0908:ConvertTo-Clixml-#3898

Conversation

@Shriram0908

@Shriram0908 Shri Ram K S (Shriram0908) commented May 30, 2020

Copy link
Copy Markdown
Contributor

PR Summary

ConvertTo-Clixml and ConvertFrom-CliXml to work with clixml objects. Fixes #3898

PR Context

The new cmdlet can be used to work with clixml without needed to save it file.
The cmdlets behave similar to ConvertTo-Csv and ConvertFrom-Csv

PR Checklist

@ghost ghost assigned Ilya (iSazonov) May 30, 2020
@Shriram0908

Copy link
Copy Markdown
Contributor Author

Hi Ilya (@iSazonov), Could you review the PR and provide your feedback.

Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs Outdated
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs Outdated
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs Outdated
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs Outdated
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Duplicate code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ilya (@iSazonov) , I tired to use the same function but I was not sure how to do it so I have created a overloaded the method. This is to make sure that Import-Clixml and ConvertFrom-Clixml use the same helper function.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It is static. So it could be moved to a temp class or a base class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

class ImportXmlHelper does't not derive from any other class.
I tried to convert sting into memory stream so that we dont need this function itself. Let me know if we can implementation this

internal ImportXmlHelper(PSCmdlet cmdlet, string cliObject)
        {
            _cmdlet = cmdlet;
            _memoryStream = new MemoryStream();
            StreamWriter memeoryWriter = new StreamWriter(_memoryStream);
            memeoryWriter.Write(cliObject);
            memeoryWriter.Flush();
            _memoryStream.Position = 0;
            CreateStream();
        }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It depends on whether we want to process "#< CLIXML".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comands like below produce output with "#< CLIXML"

Start-Process -FilePath $PSHOME\pwsh -RedirectStandardOutput "temp.xml" -Args "-noprofile -nologo -outputformat xml -command get-command import-clixml" -Wait

I have not sure where else this behavior exists.
image

Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs Outdated
@Shriram0908

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) I have made the requested changes. Please review and provide your feedback.

Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/XMLCommand.Tests.ps1 Outdated
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/clixml.tests.ps1 Outdated
@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jun 4, 2020
@vexx32

Copy link
Copy Markdown
Collaborator

I have one main question regarding the code here (there are minor things, but I think the main thing should be addressed first) -- if I'm not mistaken, a fair amount of parameters and the serialization methods are pretty similar to the existing Export/Import-Csv cmdlets. Have we looked at whether some of the existing code (and/or the new code) can be moved into a base class that both cmdlets can implement to reduce code duplication here? Or are we expecting to do that after this PR?

@Shriram0908

Copy link
Copy Markdown
Contributor Author

Rain Sallow (/u/ta11ow) (@vexx32), I tried not make too many changes to existing code. I made modifications only to ImportXmlHelper class (which is used by Import-Clixml) to work with ConvertFrom-Clixml.

I am following this approach to reduce the PR size. we can have two separate PR or one single PR. I am okay with it either way.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me, apart from a couple smaller comments. 🙂

Comment on lines 586 to 611

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's unusual for a Dispose() method to throw, but iirc it can happen; I'd prefer to see this with a try/finally so that _disposed is always set correctly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems _importXmlHelper.Dispose() can not throw.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Dongbo Wang (@daxian-dbw) seeing PSObject[] as a parameter type reminds of the weird issue we had with Write-Output where it was enumerating input arrays and wrapping every single item in PSObject during parameter binding. Is that desired here, or should we go with PSObject to avoid it like I think we did with Write-Output?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We use the same pattern in ConvertFrom-Csv.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rain Sallow (/u/ta11ow) (@vexx32) I changed to PSObject and tried it code execution seems to follow the same path. No difference either way. Maybe using PSObject can improve readability.
I used PSObject[] seeing ConvertFrom-Csv.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah Ilya (@iSazonov) I'm sure a lot of commands still do. I'm not sure if it's the right way to go though.

Shri Ram K S (@Shriram0908) if it works both ways... it's probably fine to leave as is. I would prefer to have someone on the PS team weigh in first though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rain Sallow (/u/ta11ow) (@vexx32) Sure okay. let me know anything else needs to be done.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
_stringBuilder.AppendLine( InputObject[0].ToString() );
_stringBuilder.AppendLine(InputObject[0].ToString());

Comment on lines 164 to 167

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
$content = Get-Command export* -Type Cmdlet | Select-Object -First 3 | ConvertTo-Clixml
$results = ConvertFrom-CliXml $content
$results.Count | Should -Be 3
$results[0].PSTypeNames[0] | Should -BeExactly "Deserialized.System.Management.Automation.CmdletInfo"
$content = Get-Command export* -Type Cmdlet | Select-Object -First 3 | ConvertTo-Clixml
$results = ConvertFrom-CliXml $content
$results.Count | Should -Be 3
$results[0].PSTypeNames[0] | Should -BeExactly "Deserialized.System.Management.Automation.CmdletInfo"

Comment on lines 170 to 177

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
It "ConvertFrom-CliXml with Rehydration should work" {
$property1 = 256
$property2 = "abcdef"
$isHiddenTestType = [IsHiddenTestType]::New($property1,$property2)
$content = $isHiddenTestType | ConvertTo-Clixml
$results = ConvertFrom-CliXml $content
$results.Property1 | Should -Be $property1
$results.Property2 | Should -BeExactly $property2
It "ConvertFrom-CliXml with Rehydration should work" {
$property1 = 256
$property2 = "abcdef"
$isHiddenTestType = [IsHiddenTestType]::New($property1,$property2)
$content = $isHiddenTestType | ConvertTo-Clixml
$results = ConvertFrom-CliXml $content
$results.Property1 | Should -Be $property1
$results.Property2 | Should -BeExactly $property2

Comment on lines 193 to 201

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
It "ConvertFrom-CliXml StopProcessing should succeed" {
$content = 1,2,3 | ConvertTo-Clixml
$ps = [PowerShell]::Create()
$ps.AddCommand("Get-Process")
$ps.AddCommand("ConvertFrom-CliXml")
$ps.AddParameter("InputObject", $content)
$ps.BeginInvoke()
$ps.Stop()
$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"
It "ConvertFrom-CliXml StopProcessing should succeed" {
$content = 1,2,3 | ConvertTo-Clixml
$ps = [PowerShell]::Create()
$ps.AddCommand("Get-Process")
$ps.AddCommand("ConvertFrom-CliXml")
$ps.AddParameter("InputObject", $content)
$ps.BeginInvoke()
$ps.Stop()
$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"

Comment on lines 246 to 251

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
It "ConvertFrom-CliXml -First and -Skip work together for collections" {
$content = @{ a = 1; b = 2; c = 3; d = 4 } | ConvertTo-Clixml
# order not guaranteed, even with [ordered] so we have to be smart here and compare against the full result
$out1 = ConvertFrom-CliXml -InputObject $content # this results in a hashtable
$out2 = ConvertFrom-CliXml -InputObject $content -First 2 -Skip 1 # this results in a dictionary entry
$out2.Count | Should -Be 2
It "ConvertFrom-CliXml -First and -Skip work together for collections" {
$content = @{ a = 1; b = 2; c = 3; d = 4 } | ConvertTo-Clixml
# order not guaranteed, even with [ordered] so we have to be smart here and compare against the full result
$out1 = ConvertFrom-CliXml -InputObject $content # this results in a hashtable
$out2 = ConvertFrom-CliXml -InputObject $content -First 2 -Skip 1 # this results in a dictionary entry
$out2.Count | Should -Be 2

Comment on lines 204 to 207

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Indentation.

Comment on lines 231 to 233

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Indentation.

Comment on lines 238 to 241

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Indentation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ilya (@iSazonov) I just realized that the indentation issues is due to mix of tab and space. Which one should I use space or tab or is it fine as long as I use it consistently ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Spaces is generally the norm in the repo. The only exceptions I usually see are really old files.

@Shriram0908

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Rain Sallow (/u/ta11ow) (@vexx32)
What is the next step. Shall I file a new document request.

@vexx32

Copy link
Copy Markdown
Collaborator

Yeah, please file a documentation issue in the docs repo and put the link in the PR description. 😊

@Shriram0908

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Rain Sallow (/u/ta11ow) (@vexx32) , Please let me know what to do next.

@iSazonov

Copy link
Copy Markdown
Collaborator

Waiting MSFT team approve.

@ghost ghost added the Review - Needed The PR is being reviewed label Jul 4, 2020
@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@bravo-kernel

Copy link
Copy Markdown

Shri Ram K S (@Shriram0908) thank you for this one, hope it gets merged ❤️

@Shriram0908

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Rain Sallow (/u/ta11ow) (@vexx32) Travis Plunk (@TravisEz13) Steve Lee (@SteveL-MSFT)

Its been quite some time. Please let me know if I could help.

@iSazonov

Copy link
Copy Markdown
Collaborator

I can not merge without additional reviews.

@ghost ghost removed the Review - Needed The PR is being reviewed label Jan 20, 2021
@ghost ghost added the Review - Needed The PR is being reviewed label Jan 28, 2021
@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment.

@ghost ghost closed this Sep 8, 2021
@Shriram0908

Copy link
Copy Markdown
Contributor Author

How to re-open this PR ?

@ghost ghost removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept Stale labels Nov 17, 2021
@Shriram0908

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Thank you for opening the PR. Seems like Link in internals.md and ADOPTERS.md are failing. Let me know if there is something that I can do.

@iSazonov

Copy link
Copy Markdown
Collaborator

Shri Ram K S (@Shriram0908) I think you need to rebase.

@Shriram0908

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Once I rebased the pushed to repo, It worked. Thanks.

@ghost ghost added the Review - Needed The PR is being reviewed label Nov 25, 2021
@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@pull-request-quantifier-deprecated

Copy link
Copy Markdown

This PR has 401 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Large
Size       : +316 -85
Percentile : 80.03%

Total files changed: 6

Change summary by file extension:
.cs : +111 -2
.psd1 : +3 -2
.ps1 : +202 -81

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@SteveL-MSFT

Copy link
Copy Markdown
Member

It appears that Travis Plunk (@TravisEz13)'s feedback hasn't been addressed yet

@ghost ghost removed the Review - Needed The PR is being reviewed label Mar 13, 2023

@TravisEz13 Travis Plunk (TravisEz13) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please address the comments from #12845 (comment).

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Mar 13, 2023
@ghost ghost added the Stale label Mar 28, 2023
@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Large Stale Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept WG-Cmdlets general cmdlet issues WG-Security security related areas such as JEA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export-CliXml shouldn't require writing to a file

7 participants