8000 Moving Import-PowerShellDatafile from script to compiled cmdlet by powercode · Pull Request #2750 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Moving Import-PowerShellDatafile from script to compiled cmdlet #2750

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

Merged
merged 2 commits into from
Feb 27, 2017

Conversation

powercode
Copy link
Collaborator

Fixes #2734

@msftclas
Copy link

Hi @powercode, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

@@ -0,0 +1,122 @@
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
Copy link
Collaborator

Choose a reason for hiding this comment

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

@HemantMahawar should we have copyright preamble on the new files?

Copy link
Contributor

Choose a reason for hiding this comment

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

Given it is open-source code, I don't think it is needed. @joeyaiello Can you weigh in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removing

Copy link
Collaborator
@vors vors left a comment

Choose a reason for hiding this comment

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

Please, avoid copy-paste

@@ -47,6 +47,7 @@
"monad/src/commands/utility/GetUnique.cs": "commands/utility/GetUnique.cs",
"monad/src/commands/utility/group-object.cs": "commands/utility/group-object.cs",
"monad/src/commands/utility/ImplicitRemotingCommands.cs": "commands/utility/ImplicitRemotingCommands.cs",
"monad/src/commands/utility/ImportPowerShellDataFile.cs": "commands/utility/ImportPowerShellDataFile.cs",
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's probably not needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

}
}

private List<string> ResolveFilePaths(string[] filePaths, bool isLiteralPath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method is copied from

private List<string> ResolveFilePaths(string[] filePaths, bool isLiteralPath)

Can we avoid such copy-paste?

Copy link
Collaborator Author
@powercode powercode Dec 6, 2016

Choose a reason for hiding this comment

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

I really agree, but I actually think it would be a good Idea to provide these kind of helpers in a public API. This needs to be done by everyone who wants to handle Path and LiteralPath.

What is the right way to go here?

See #2729

@powercode powercode force-pushed the import-datafile branch 4 times, most recently from 2470581 to ba64f8f Compare December 6, 2016 21:38
public string[] LiteralPath
{
get { return Path; }
set { _isLiteralPath = true; Path = value; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we use this pattern of sharing the backing store for parameters elsewhere? If not, it seems like potentially a bad idea to start doing this.

One maybe non-issue is the parameter binder copies all parameters in the cmdlet instance before parameter binding and restores them. It seems unlikely, but possible, that a non-null value in Path could cause problems.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A quick search gives that the pattern is in

AclCommands.cs
CertificateCommands.cs
CSVCommands.cs
ExportAliasCommand.cs
MatchString.cs
Out-File.cs
SaveHelpCommand.cs
SignatureCommands.cs
StartTranscriptCmdlet.cs
UpdateHelpCommand.cs
WriteFormatDataCommand.cs
XmlCommands.cs

But the cost of changing is very small.

Copy link
Collaborator Author
@powercode powercode Jan 4, 2017

Choose a reason for hiding this comment

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

I changed it to

get { return _isLiteralPath ? Path : null; }

[Parameter(Mandatory = true,
Position = 0,
ParameterSetName = "ByPath",
ValueFromPipeline = true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't ValueFromPipeline be on LiteralPath?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Corrected.


void WriteInvalidDataFileError(string resolvedPath, string extraError)
{
var errorId = $"CouldNotParseAsPowerShellDataFile{extraError}";
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize this is just a translation of the PowerShell, but it seems overly fancy. I think it would be clearer if the caller just passed errorId, it would make searching for the error string easier too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

corrected.

Rebased version pushed.

}
else
{
var data = ast.Find(a => a is HashtableAst, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize this is just a translation of the PowerShell implementation, but a psd1 can contain much more than a simple hashtable. For example, the psd1 file contain 2 hashtables as an array, and this implementation would ignore the second one.

As implemented, the cmdlet is mostly useful for typical module manifests, but psd1 files are used for other things, e.g. localization, and sometimes use commands like ConvertFrom-StringData.

We should, as a minimum, not ignore any part of the ast.
We should also consider supporting more than a simple hashtable. Ast.SafeGetValue can certainly handle more (e.g. an array of hashtables).
We might also consider doing something more like what Import-LocalizedData does, though we'd need feedback from @LeeHolmes to understand the important differences and why this cmdlet was introduced.

Copy link
Collaborator Author
@powercode powercode Jan 4, 2017

Choose a reason for hiding this comment

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

Should we start with just the move as descripted in the issue, and open a new issue for the functional changes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 don't block the PR and open new Issue.

@lzybkr lzybkr self-assigned this Jan 4, 2017
@powercode powercode force-pushed the import-datafile branch 3 times, most recently from 6186748 to bc2b766 Compare January 4, 2017 22:37
@mirichmo
Copy link
Member
mirichmo commented Feb 9, 2017

@lzybkr and @vors Are your concerns resolved or is there additional work left here?

[Parameter(Mandatory = true, Position = 0, ParameterSetName = "ByPath")]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Path { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need a change as this code is just following a somewhat broken pattern.

If LiteralPath was set, _isLiteralPath will be set to true. Then, if Path is set, _isLiteralPath is not cleared. This isn't really a scenario I guess, but it is handled correctly in some places in our code base, but in many others it is not.

@lzybkr lzybkr merged commit e3b59e0 into PowerShell:master Feb 27, 2017
@iSazonov iSazonov removed the Review - Needed The PR is being reviewed label Mar 27, 2017
@powercode powercode deleted the import-datafile branch April 19, 2017 17:28
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.

8 participants
0