-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportPowerShellDataFile.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Management.Automation; | ||
| using System.Management.Automation.Language; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| /// <summary> | ||
| /// This class implements Import-PowerShellDataFile command. | ||
| /// </summary> | ||
| [Cmdlet(VerbsData.Import, "PowerShellDataFile", DefaultParameterSetName = "ByPath", | ||
| HelpUri = "https://go.microsoft.com/fwlink/?LinkID=623621", RemotingCapability = RemotingCapability.None)] | ||
| public class ImportPowerShellDataFileCommand : PSCmdlet | ||
| { | ||
| bool _isLiteralPath; | ||
|
|
||
| /// <summary> | ||
| /// Path specified, using globbing to resolve | ||
| /// </summary> | ||
| [Parameter(Mandatory = true, Position = 0, ParameterSetName = "ByPath")] | ||
| [ValidateNotNullOrEmpty] | ||
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] | ||
| public string[] Path { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Specifies a path to one or more locations, without globbing | ||
| /// </summary> | ||
| [Parameter(Mandatory = true, Position = 0, ParameterSetName = "ByLiteralPath", ValueFromPipelineByPropertyName = true)] | ||
| [ValidateNotNullOrEmpty] | ||
| [Alias("PSPath")] | ||
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] | ||
| public string[] LiteralPath | ||
| { | ||
| get { return _isLiteralPath ? Path : null; } | ||
| set { _isLiteralPath = true; Path = value; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// For each path, resolve it, parse it and write all hashtables | ||
| /// to the output stream | ||
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| foreach (var path in Path) | ||
| { | ||
| var resolved = PathUtils.ResolveFilePath(path, this, _isLiteralPath); | ||
| if(!string.IsNullOrEmpty(resolved) && System.IO.File.Exists(resolved)) | ||
| { | ||
| Token[] tokens; | ||
| ParseError[] errors; | ||
| var ast = Parser.ParseFile(resolved, out tokens, out errors); | ||
| if (errors.Length > 0) | ||
| { | ||
| WriteInvalidDataFileError(resolved, "CouldNotParseAsPowerShellDataFile"); | ||
| } | ||
| else | ||
| { | ||
| var data = ast.Find(a => a is HashtableAst, false); | ||
| if (data != null) | ||
| { | ||
| WriteObject(data.SafeGetValue()); | ||
| } | ||
| else | ||
| { | ||
| WriteInvalidDataFileError(resolved, "CouldNotParseAsPowerShellDataFileNoHashtableRoot"); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| WritePathNotFoundError(path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void WritePathNotFoundError(string path) | ||
| { | ||
| var errorId = "PathNotFound"; | ||
| var errorCategory = ErrorCategory.InvalidArgument; | ||
| var errorMessage = string.Format(UtilityResources.PathDoesNotExist, path); | ||
| var exception = new ArgumentException(errorMessage); | ||
| var errorRecord = new ErrorRecord(exception, errorId, errorCategory, path); | ||
| WriteError(errorRecord); | ||
| } | ||
|
|
||
| void WriteInvalidDataFileError(string resolvedPath, string errorId) | ||
| { | ||
| var errorCategory = ErrorCategory.InvalidData; | ||
| var errorMessage = string.Format(UtilityResources.CouldNotParseAsPowerShellDataFile, resolvedPath); | ||
| var exception = new InvalidOperationException(errorMessage); | ||
| var errorRecord = new ErrorRecord(exception, errorId, errorCategory, resolvedPath); | ||
| WriteError(errorRecord); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
LiteralPathwas set,_isLiteralPathwill be set to true. Then, ifPathis set,_isLiteralPathis 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.