forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-Data.cs
More file actions
100 lines (91 loc) · 3.69 KB
/
Copy pathUpdate-Data.cs
File metadata and controls
100 lines (91 loc) · 3.69 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
96
97
98
99
100
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
using System.Collections.ObjectModel;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This is the base class for update-typedata and update-formatdata
/// </summary>
public class UpdateData : PSCmdlet
{
/// <summary>
/// File parameter set name
/// </summary>
protected const string FileParameterSet = "FileSet";
/// <summary>
/// Files to append to the existing set
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true,
ParameterSetName = FileParameterSet)]
[Alias("PSPath", "Path")]
[ValidateNotNull]
public string[] AppendPath { set; get; } = Utils.EmptyArray<string>();
/// <summary>
/// Files to prepend to the existing set
/// </summary>
[Parameter(ParameterSetName = FileParameterSet)]
[ValidateNotNull]
public string[] PrependPath { set; get; } = Utils.EmptyArray<string>();
private static void ReportWrongExtension(string file, string errorId, PSCmdlet cmdlet)
{
ErrorRecord errorRecord = new ErrorRecord(
PSTraceSource.NewInvalidOperationException(UpdateDataStrings.UpdateData_WrongExtension, file, "ps1xml"),
errorId,
ErrorCategory.InvalidArgument,
null);
cmdlet.WriteError(errorRecord);
}
private static void ReportWrongProviderType(string providerId, string errorId, PSCmdlet cmdlet)
{
ErrorRecord errorRecord = new ErrorRecord(
PSTraceSource.NewInvalidOperationException(UpdateDataStrings.UpdateData_WrongProviderError, providerId),
errorId,
ErrorCategory.InvalidArgument,
null);
cmdlet.WriteError(errorRecord);
}
/// <summary>
///
/// </summary>
/// <param name="files"></param>
/// <param name="errorId"></param>
/// <param name="cmdlet"></param>
/// <returns></returns>
internal static Collection<string> Glob(string[] files, string errorId, PSCmdlet cmdlet)
{
Collection<string> retValue = new Collection<string>();
foreach (string file in files)
{
Collection<string> providerPaths;
ProviderInfo provider = null;
try
{
providerPaths = cmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath(file, out provider);
}
catch (SessionStateException e)
{
cmdlet.WriteError(new ErrorRecord(e, errorId, ErrorCategory.InvalidOperation, file));
continue;
}
if (!provider.NameEquals(cmdlet.Context.ProviderNames.FileSystem))
{
ReportWrongProviderType(provider.FullName, errorId, cmdlet);
continue;
}
foreach (string providerPath in providerPaths)
{
if (!providerPath.EndsWith(".ps1xml", StringComparison.OrdinalIgnoreCase))
{
ReportWrongExtension(providerPath, "WrongExtension", cmdlet);
continue;
}
retValue.Add(providerPath);
}
}
return retValue;
}
}
}