forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportPowerShellDataFile.cs
More file actions
95 lines (89 loc) · 3.81 KB
/
Copy pathImportPowerShellDataFile.cs
File metadata and controls
95 lines (89 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
}
}
}