-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathNavigation.cs
More file actions
4224 lines (3719 loc) · 147 KB
/
Navigation.cs
File metadata and controls
4224 lines (3719 loc) · 147 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Provider;
using Dbg = System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
#region CoreCommandBase
/// <summary>
/// The base command for the core commands.
/// </summary>
public abstract class CoreCommandBase : PSCmdlet, IDynamicParameters
{
#region Tracer
/// <summary>
/// An instance of the PSTraceSource class used for trace output
/// using "NavigationCommands" as the category.
/// </summary>
[Dbg.TraceSource("NavigationCommands", "The namespace navigation tracer")]
internal static readonly Dbg.PSTraceSource tracer = Dbg.PSTraceSource.GetTracer("NavigationCommands", "The namespace navigation tracer");
#endregion Tracer
#region Protected members
/// <summary>
/// The context for the command that is passed to the core command providers.
/// </summary>
internal virtual CmdletProviderContext CmdletProviderContext
{
get
{
CmdletProviderContext coreCommandContext = new(this);
coreCommandContext.Force = Force;
Collection<string> includeFilter =
SessionStateUtilities.ConvertArrayToCollection<string>(Include);
Collection<string> excludeFilter =
SessionStateUtilities.ConvertArrayToCollection<string>(Exclude);
coreCommandContext.SetFilters(includeFilter, excludeFilter, Filter);
coreCommandContext.SuppressWildcardExpansion = SuppressWildcardExpansion;
coreCommandContext.DynamicParameters = RetrievedDynamicParameters;
stopContextCollection.Add(coreCommandContext);
return coreCommandContext;
}
}
internal virtual SwitchParameter SuppressWildcardExpansion
{
get => _suppressWildcardExpansion;
set => _suppressWildcardExpansion = value;
}
private bool _suppressWildcardExpansion;
/// <summary>
/// A virtual method for retrieving the dynamic parameters for a cmdlet. Derived cmdlets
/// that require dynamic parameters should override this method and return the
/// dynamic parameter object.
/// </summary>
/// <param name="context">
/// The context under which the command is running.
/// </param>
/// <returns>
/// An object representing the dynamic parameters for the cmdlet or null if there
/// are none.
/// </returns>
internal virtual object GetDynamicParameters(CmdletProviderContext context) => null;
/// <summary>
/// Called by the base implementation that checks the SupportShouldProcess provider
/// capability. This virtual method gives the
/// derived cmdlet a chance query the CmdletProvider capabilities to determine
/// if the provider supports ShouldProcess.
/// </summary>
/// <value></value>
protected virtual bool ProviderSupportsShouldProcess => true;
/// <summary>
/// A helper for derived classes to call to determine if the paths specified
/// are for a provider that supports ShouldProcess.
/// </summary>
/// <param name="paths">
/// The paths to check to see if the providers support ShouldProcess.
/// </param>
/// <returns>
/// If the paths are to different providers, and any don't support
/// ShouldProcess, then the return value is false. If they all
/// support ShouldProcess then the return value is true.
/// </returns>
protected bool DoesProviderSupportShouldProcess(string[] paths)
{
// If no paths are specified, then default to true as the paths
// may be getting piped in.
bool result = true;
if (paths != null)
{
foreach (string path in paths)
{
ProviderInfo provider = null;
PSDriveInfo drive = null;
// I don't really care about the returned path, just the provider name
SessionState.Path.GetUnresolvedProviderPathFromPSPath(
path,
this.CmdletProviderContext,
out provider,
out drive);
// Check the provider's capabilities
if (!CmdletProviderManagementIntrinsics.CheckProviderCapabilities(
ProviderCapabilities.ShouldProcess,
provider))
{
result = false;
break;
}
}
}
return result;
}
/// <summary>
/// The dynamic parameters which have already been retrieved from the provider
/// and bound by the command processor.
/// </summary>
protected internal object RetrievedDynamicParameters => _dynamicParameters;
/// <summary>
/// The dynamic parameters for the command. They are retrieved using the
/// GetDynamicParameters virtual method.
/// </summary>
private object _dynamicParameters;
#endregion Protected members
#region Public members
/// <summary>
/// Stops the processing of the provider by using the
/// CmdletProviderContext to tunnel the stop message to
/// the provider instance.
/// </summary>
protected override void StopProcessing()
{
foreach (CmdletProviderContext stopContext in stopContextCollection)
{
stopContext.StopProcessing();
}
}
internal Collection<CmdletProviderContext> stopContextCollection =
new();
/// <summary>
/// Gets or sets the filter property.
/// </summary>
/// <remarks>
/// This is meant to be overridden by derived classes if
/// they support the Filter parameter. This property is on
/// the base class to simplify the creation of the CmdletProviderContext.
/// </remarks>
public virtual string Filter { get; set; }
/// <summary>
/// Gets or sets the include property.
/// </summary>
/// <remarks>
/// This is meant to be overridden by derived classes if
/// they support the Include parameter. This property is on
/// the base class to simplify the creation of the CmdletProviderContext.
/// </remarks>
public virtual string[] Include
{
get;
set;
} = Array.Empty<string>();
/// <summary>
/// Gets or sets the exclude property.
/// </summary>
/// <remarks>
/// This is meant to be overridden by derived classes if
/// they support the Exclude parameter. This property is on
/// the base class to simplify the creation of the CmdletProviderContext.
/// </remarks>
public virtual string[] Exclude
{
get;
set;
} = Array.Empty<string>();
/// <summary>
/// Gets or sets the force property.
/// </summary>
/// <remarks>
/// Gives the provider guidance on how vigorous it should be about performing
/// the operation. If true, the provider should do everything possible to perform
/// the operation. If false, the provider should attempt the operation but allow
/// even simple errors to terminate the operation.
/// For example, if the user tries to copy a file to a path that already exists and
/// the destination is read-only, if force is true, the provider should copy over
/// the existing read-only file. If force is false, the provider should write an error.
///
/// This is meant to be overridden by derived classes if
/// they support the Force parameter. This property is on
/// the base class to simplify the creation of the CmdletProviderContext.
/// </remarks>
public virtual SwitchParameter Force
{
get => _force;
set => _force = value;
}
private bool _force;
/// <summary>
/// Retrieves the dynamic parameters for the command from
/// the provider.
/// </summary>
public object GetDynamicParameters()
{
// Don't stream errors or Write* to the pipeline.
CmdletProviderContext context = CmdletProviderContext;
context.PassThru = false;
try
{
_dynamicParameters = GetDynamicParameters(context);
}
catch (ItemNotFoundException)
{
_dynamicParameters = null;
}
catch (ProviderNotFoundException)
{
_dynamicParameters = null;
}
catch (DriveNotFoundException)
{
_dynamicParameters = null;
}
return _dynamicParameters;
}
/// <summary>
/// Determines if the cmdlet and CmdletProvider supports ShouldProcess.
/// </summary>
public bool SupportsShouldProcess => ProviderSupportsShouldProcess;
#endregion Public members
}
#endregion CoreCommandBase
#region CoreCommandWithCredentialsBase
/// <summary>
/// The base class for core commands to extend when they require credentials
/// to be passed as parameters.
/// </summary>
public class CoreCommandWithCredentialsBase : CoreCommandBase
{
#region Parameters
/// <summary>
/// Gets or sets the credential parameter.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[Credential]
public PSCredential Credential { get; set; }
#endregion Parameters
#region parameter data
#endregion parameter data
#region Protected members
/// <summary>
/// The context for the command that is passed to the core command providers.
/// </summary>
internal override CmdletProviderContext CmdletProviderContext
{
get
{
CmdletProviderContext coreCommandContext = new(this, Credential);
coreCommandContext.Force = Force;
Collection<string> includeFilter =
SessionStateUtilities.ConvertArrayToCollection<string>(Include);
Collection<string> excludeFilter =
SessionStateUtilities.ConvertArrayToCollection<string>(Exclude);
coreCommandContext.SetFilters(includeFilter, excludeFilter, Filter);
coreCommandContext.SuppressWildcardExpansion = SuppressWildcardExpansion;
coreCommandContext.DynamicParameters = RetrievedDynamicParameters;
stopContextCollection.Add(coreCommandContext);
return coreCommandContext;
}
}
#endregion Protected members
}
#endregion CoreCommandWithCredentialsBase
#region GetLocationCommand
/// <summary>
/// The get-location command class.
/// This command does things like list the contents of a container, get
/// an item at a given path, get the current working directory, etc.
/// </summary>
/// <remarks>
/// </remarks>
[Cmdlet(VerbsCommon.Get, "Location", DefaultParameterSetName = LocationParameterSet, SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096495")]
[OutputType(typeof(PathInfo), ParameterSetName = new string[] { LocationParameterSet })]
[OutputType(typeof(PathInfoStack), ParameterSetName = new string[] { StackParameterSet })]
public class GetLocationCommand : DriveMatchingCoreCommandBase
{
private const string LocationParameterSet = "Location";
private const string StackParameterSet = "Stack";
#region Command parameters
#region Location parameter set parameters
/// <summary>
/// Gets or sets the provider from which to get the current location.
/// </summary>
[Parameter(ParameterSetName = LocationParameterSet, ValueFromPipelineByPropertyName = true)]
public string[] PSProvider
{
get => _provider;
set => _provider = value ?? Array.Empty<string>();
}
/// <summary>
/// Gets or sets the drive from which to get the current location.
/// </summary>
[Parameter(ParameterSetName = LocationParameterSet, ValueFromPipelineByPropertyName = true)]
public string[] PSDrive { get; set; }
#endregion Location parameter set parameters
#region Stack parameter set parameters
/// <summary>
/// Gets or sets the Stack switch parameter which is used
/// to disambiguate parameter sets.
/// </summary>
/// <value></value>
[Parameter(ParameterSetName = StackParameterSet)]
public SwitchParameter Stack
{
get => _stackSwitch;
set => _stackSwitch = value;
}
private bool _stackSwitch;
/// <summary>
/// Gets or sets the stack ID for the location stack that will
/// be retrieved.
/// </summary>
[Parameter(ParameterSetName = StackParameterSet, ValueFromPipelineByPropertyName = true)]
public string[] StackName
{
get => _stackNames;
set => _stackNames = value;
}
#endregion Stack parameter set parameters
#endregion Command parameters
#region command data
#region Location parameter set data
/// <summary>
/// The name of the provider from which to return the current location.
/// </summary>
private string[] _provider = Array.Empty<string>();
#endregion Location parameter set data
#region Stack parameter set data
/// <summary>
/// The name of the location stack from which to return the stack.
/// </summary>
private string[] _stackNames;
#endregion Stack parameter set data
#endregion command data
#region command code
/// <summary>
/// The main execution method for the get-location command. Depending on
/// the parameter set that is specified, the command can do many things.
/// -locationSet gets the current working directory as a Monad path
/// -stackSet gets the directory stack of directories that have been
/// pushed by the push-location command.
/// </summary>
protected override void ProcessRecord()
{
// It is OK to use a switch for string comparison here because we
// want a case sensitive comparison in the current culture.
switch (ParameterSetName)
{
case LocationParameterSet:
PathInfo result = null;
if (PSDrive != null && PSDrive.Length > 0)
{
foreach (string drive in PSDrive)
{
List<PSDriveInfo> foundDrives = null;
try
{
foundDrives = GetMatchingDrives(drive, PSProvider, null);
}
catch (DriveNotFoundException e)
{
ErrorRecord errorRecord =
new(
e,
"GetLocationNoMatchingDrive",
ErrorCategory.ObjectNotFound,
drive);
WriteError(errorRecord);
continue;
}
catch (ProviderNotFoundException e)
{
ErrorRecord errorRecord =
new(
e,
"GetLocationNoMatchingProvider",
ErrorCategory.ObjectNotFound,
PSProvider);
WriteError(errorRecord);
continue;
}
catch (ArgumentException argException)
{
ErrorRecord errorRecord =
new(
argException,
"GetLocationNoMatchingDrive",
ErrorCategory.ObjectNotFound,
drive);
WriteError(errorRecord);
continue;
}
// Get the current location for a specific drive and provider
foreach (PSDriveInfo workingDrive in foundDrives)
{
try
{
string path =
LocationGlobber.GetDriveQualifiedPath(
workingDrive.CurrentLocation,
workingDrive);
result = new PathInfo(workingDrive, workingDrive.Provider, path, SessionState);
WriteObject(result);
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
}
}
}
// If the drive wasn't specified but the provider was
else if ((PSDrive == null || PSDrive.Length == 0) &&
(PSProvider != null && PSProvider.Length > 0))
{
foreach (string providerName in PSProvider)
{
bool providerContainsWildcard = WildcardPattern.ContainsWildcardCharacters(providerName);
if (!providerContainsWildcard)
{
// Since the Provider was specified and doesn't contain
// wildcard characters, make sure it exists.
try
{
SessionState.Provider.GetOne(providerName);
}
catch (ProviderNotFoundException e)
{
ErrorRecord errorRecord =
new(
e,
"GetLocationNoMatchingProvider",
ErrorCategory.ObjectNotFound,
providerName);
WriteError(errorRecord);
continue;
}
}
// Match the providers
foreach (ProviderInfo providerInfo in SessionState.Provider.GetAll())
{
if (providerInfo.IsMatch(providerName))
{
try
{
WriteObject(SessionState.Path.CurrentProviderLocation(providerInfo.FullName));
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
catch (DriveNotFoundException driveNotFound)
{
if (providerContainsWildcard)
{
// NTRAID#Windows Out Of Band Releases-923607-2005/11/02-JeffJon
// This exception is ignored, because it just means we didn't find
// an active drive for the provider.
continue;
}
else
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
}
}
}
}
}
}
else
{
// Get the current working directory using the core command API.
WriteObject(SessionState.Path.CurrentLocation);
}
break;
case StackParameterSet:
if (_stackNames != null)
{
foreach (string stackName in _stackNames)
{
try
{
// Get the directory stack. This is similar to the "dirs" command
WriteObject(SessionState.Path.LocationStack(stackName), false);
}
catch (PSArgumentException argException)
{
WriteError(
new ErrorRecord(
argException.ErrorRecord,
argException));
continue;
}
}
}
else
{
try
{
WriteObject(SessionState.Path.LocationStack(null), false);
}
catch (PSArgumentException argException)
{
WriteError(
new ErrorRecord(
argException.ErrorRecord,
argException));
}
}
break;
default:
Dbg.Diagnostics.Assert(false, string.Create(System.Globalization.CultureInfo.InvariantCulture, $"One of the predefined parameter sets should have been specified, instead we got: {ParameterSetName}"));
break;
}
}
#endregion command code
}
#endregion GetLocationCommand
#region SetLocationCommand
/// <summary>
/// The core command for setting/changing location.
/// This is the equivalent of cd command.
/// </summary>
[Cmdlet(VerbsCommon.Set, "Location", DefaultParameterSetName = PathParameterSet, SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097049")]
[OutputType(typeof(PathInfo), typeof(PathInfoStack))]
public class SetLocationCommand : CoreCommandBase
{
#region Command parameters
private const string PathParameterSet = "Path";
private const string LiteralPathParameterSet = "LiteralPath";
private const string StackParameterSet = "Stack";
/// <summary>
/// Gets or sets the path property.
/// </summary>
[Parameter(Position = 0, ParameterSetName = PathParameterSet,
ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Path
{
get => _path;
set => _path = value;
}
/// <summary>
/// Gets or sets the path property, when bound from the pipeline.
/// </summary>
[Parameter(ParameterSetName = LiteralPathParameterSet,
Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Alias("PSPath", "LP")]
public string LiteralPath
{
get => _path;
set
{
_path = value;
base.SuppressWildcardExpansion = true;
}
}
/// <summary>
/// Gets or sets the parameter -passThru which states output from
/// the command should be placed in the pipeline.
/// </summary>
[Parameter]
public SwitchParameter PassThru
{
get => _passThrough;
set => _passThrough = value;
}
/// <summary>
/// Gets or sets the StackName parameter which determines which location stack
/// to use for the push. If the parameter is missing or empty the default
/// location stack is used.
/// </summary>
[Parameter(ParameterSetName = StackParameterSet, ValueFromPipelineByPropertyName = true)]
public string StackName { get; set; }
#endregion Command parameters
#region Command data
/// <summary>
/// The filter used when doing a dir.
/// </summary>
private string _path = string.Empty;
/// <summary>
/// Determines if output should be passed through for
/// set-location.
/// </summary>
private bool _passThrough;
#endregion Command data
#region Command code
/// <summary>
/// The functional part of the code that does the changing of the current
/// working directory.
/// </summary>
protected override void ProcessRecord()
{
object result = null;
switch (ParameterSetName)
{
case PathParameterSet:
case LiteralPathParameterSet:
try
{
// Change the current working directory
if (string.IsNullOrEmpty(Path))
{
// If user just typed 'cd', go to FileSystem provider home directory
Path = SessionState.Internal.GetSingleProvider(Commands.FileSystemProvider.ProviderName).Home;
}
result = SessionState.Path.SetLocation(Path, CmdletProviderContext, ParameterSetName == LiteralPathParameterSet);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
}
catch (PSArgumentException argException)
{
WriteError(
new ErrorRecord(
argException.ErrorRecord,
argException));
}
break;
case StackParameterSet:
try
{
// Change the default location stack
result = SessionState.Path.SetDefaultLocationStack(StackName);
}
catch (ItemNotFoundException itemNotFound)
{
WriteError(
new ErrorRecord(
itemNotFound.ErrorRecord,
itemNotFound));
}
break;
default:
Dbg.Diagnostics.Assert(
false,
"One of the specified parameter sets should have been called");
break;
}
if (_passThrough && result != null)
{
WriteObject(result);
}
}
#endregion Command code
}
#endregion SetLocationCommand
#region PushLocationCommand
/// <summary>
/// The core command for setting/changing location and pushing it onto a location stack.
/// This is the equivalent of the pushd command.
/// </summary>
[Cmdlet(VerbsCommon.Push, "Location", DefaultParameterSetName = PathParameterSet, SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097105")]
public class PushLocationCommand : CoreCommandBase
{
#region Command parameters
private const string PathParameterSet = "Path";
private const string LiteralPathParameterSet = "LiteralPath";
/// <summary>
/// Gets or sets the path property.
/// </summary>
[Parameter(Position = 0, ParameterSetName = PathParameterSet,
ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Path
{
get => _path;
set => _path = value;
}
/// <summary>
/// Gets or sets the literal path parameter to the command.
/// </summary>
[Parameter(ParameterSetName = LiteralPathParameterSet,
ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Alias("PSPath", "LP")]
public string LiteralPath
{
get => _path;
set
{
base.SuppressWildcardExpansion = true;
_path = value;
}
}
/// <summary>
/// Gets or sets the parameter -passThru which states output from
/// the command should be placed in the pipeline.
/// </summary>
[Parameter]
public SwitchParameter PassThru
{
get => _passThrough;
set => _passThrough = value;
}
/// <summary>
/// Gets or sets the StackName parameter which determines which location stack
/// to use for the push. If the parameter is missing or empty the default
/// location stack is used.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public string StackName
{
get => _stackName;
set => _stackName = value;
}
#endregion Command parameters
#region Command data
/// <summary>
/// The filter used when doing a dir.
/// </summary>
private string _path = string.Empty;
/// <summary>
/// Determines if output should be passed through for
/// push-location.
/// </summary>
private bool _passThrough;
/// <summary>
/// The ID of the stack to use for the pop.
/// </summary>
private string _stackName;
#endregion Command data
#region Command code
/// <summary>
/// The functional part of the code that does the changing of the current
/// working directory and pushes the container onto the stack.
/// </summary>
protected override void ProcessRecord()
{
// Push the current working directory onto the
// working directory stack
SessionState.Path.PushCurrentLocation(_stackName);
if (Path != null)
{
try
{
// Now change the directory to the one specified
// in the command
PathInfo result = SessionState.Path.SetLocation(Path, CmdletProviderContext);
if (PassThru)
{
WriteObject(result);
}
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
return;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
return;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
return;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
return;
}
catch (PSArgumentException argException)
{
WriteError(
new ErrorRecord(
argException.ErrorRecord,
argException));
return;
}
}
}
#endregion Command code
}
#endregion PushLocationCommand
#region PopLocationCommand
/// <summary>
/// The core command for pop-location. This is the equivalent of the popd command.
/// It pops a container from the stack and sets the current location to that container.
/// </summary>
[Cmdlet(VerbsCommon.Pop, "Location", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096907")]
public class PopLocationCommand : CoreCommandBase
{
#region Command parameters
/// <summary>
/// Gets or sets the parameter -passThru which states output from
/// the command should be placed in the pipeline.
/// </summary>
[Parameter]
public SwitchParameter PassThru
{
get => _passThrough;
set => _passThrough = value;
}
/// <summary>
/// Gets or sets the StackName parameter which determines which location stack
/// to use for the pop. If the parameter is missing or empty the default
/// location stack is used.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
public string StackName
{
get => _stackName;
set => _stackName = value;
}
#endregion Command parameters
#region Command data