internal Collection <PSObject> GetChildItems(string path, bool recurse, ProviderRuntime providerRuntime) { if (string.IsNullOrEmpty(path)) { path = CurrentLocation.Path; } CmdletProvider provider = GetProviderByPath(path); if ((path != null) && (ItemExists(provider, path, providerRuntime))) { if (IsItemContainer(provider, path, providerRuntime)) { ContainerCmdletProvider containerProvider = provider as ContainerCmdletProvider; if (containerProvider != null) { containerProvider.GetChildItems(path, recurse, providerRuntime); } } else { ItemCmdletProvider itemProvider = provider as ItemCmdletProvider; if (itemProvider != null) { itemProvider.GetItem(path, providerRuntime); } } } return(providerRuntime.RetreiveAllProviderData()); }
private bool ItemExists(CmdletProvider provider, string path, ProviderRuntime providerRuntime) { ItemCmdletProvider itemProvider = provider as ItemCmdletProvider; if (itemProvider == null) { return(false); } return(itemProvider.ItemExists(path, providerRuntime)); }
/// <summary> /// Gets the dynamic parameters for the set-item cmdlet. /// </summary> /// <param name="path"> /// The path to the item if it was specified on the command line. /// </param> /// <param name="providerInstance"> /// The instance of the provider to use. /// </param> /// <param name="value"> /// The value to be set. /// </param> /// <param name="context"> /// The context which the core command is running. /// </param> /// <returns> /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// </returns> /// <exception cref="NotSupportedException"> /// If the <paramref name="providerInstance"/> does not support this operation. /// </exception> /// <exception cref="PipelineStoppedException"> /// If the pipeline is being stopped while executing the command. /// </exception> /// <exception cref="ProviderInvocationException"> /// If the provider threw an exception. /// </exception> private object SetItemDynamicParameters( CmdletProvider providerInstance, string path, object value, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); object result = null; try { result = itemCmdletProvider.SetItemDynamicParameters(path, value, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "SetItemDynamicParametersProviderException", SessionStateStrings.SetItemDynamicParametersProviderException, itemCmdletProvider.ProviderInfo, path, e); } return(result); }
/// <summary> /// Clears the item at the specified path. /// </summary> /// <param name="providerInstance"> /// The provider instance to use. /// </param> /// <param name="path"> /// The path to the item if it was specified on the command line. /// </param> /// <param name="context"> /// The context which the core command is running. /// </param> /// <exception cref="NotSupportedException"> /// If the <paramref name="providerInstance"/> does not support this operation. /// </exception> /// <exception cref="PipelineStoppedException"> /// If the pipeline is being stopped while executing the command. /// </exception> /// <exception cref="ProviderInvocationException"> /// If the provider threw an exception. /// </exception> private void ClearItemPrivate( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); try { itemCmdletProvider.ClearItem(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "ClearItemProviderException", SessionStateStrings.ClearItemProviderException, itemCmdletProvider.ProviderInfo, path, e); } }
internal PathInfo SetLocation(Path path, ProviderRuntime providerRuntime) { // TODO: deal with paths starting with ".\" if (path == null) { throw new NullReferenceException("Path can't be null"); } if (path == "~") { // Older Mono versions (sadly the one that's currently still // available) have a bug where GetFolderPath returns an empty // string for most SpecialFolder values, but only on // non-Windows. // See: https://bugzilla.xamarin.com/show_bug.cgi?id=2873 path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); // HACK: Use $Env:HOME until Mono 2.10 dies out. if (path == "") { path = Environment.GetEnvironmentVariable("HOME"); } } PSDriveInfo nextDrive = CurrentDrive; path = path.NormalizeSlashes(); string driveName = null; if (path.TryGetDriveName(out driveName)) { nextDrive = GetDrive(driveName); } if (nextDrive == null) { nextDrive = CurrentDrive; } Path newLocation = PathNavigation.CalculateFullPath(nextDrive.CurrentLocation, path); // I'm not a fan of this block of code. // The goal here is to throw an exception if trying to "CD" into an invalid location // // Not sure why the providerInstances are returned as a collection. Feels like given a // path we should have one provider we're talking to. if (_providerInstances.ContainsKey(nextDrive.Provider.Name)) { bool pathExists = false; IEnumerable <ItemCmdletProvider> cmdletProviders = _providerInstances[nextDrive.Provider.Name].Where(x => x is ItemCmdletProvider).Cast <ItemCmdletProvider>(); ItemCmdletProvider currentProvider = null; foreach (var provider in cmdletProviders) { if (provider.ItemExists(newLocation, providerRuntime)) { pathExists = true; currentProvider = provider; break; } } if (!pathExists) { throw new Exception(string.Format("Cannot find path '{0}' because it does not exist.", newLocation)); } else { if (currentProvider is FileSystemProvider) { System.Environment.CurrentDirectory = newLocation; } } } else { throw new NotImplementedException("Unsure how to set location with provider:" + nextDrive.Provider.Name); } nextDrive.CurrentLocation = newLocation; CurrentDrive = nextDrive; _providersCurrentDrive[CurrentDrive.Provider] = CurrentDrive; return(CurrentLocation); }
internal PathInfo SetLocation(Path path, ProviderRuntime providerRuntime) { // TODO: deal with paths starting with ".\" if (path == null) { throw new NullReferenceException("Path can't be null"); } PSDriveInfo nextDrive = CurrentDrive; path = path.NormalizeSlashes().ResolveTilde(); string driveName = null; if (path.TryGetDriveName(out driveName)) { try { nextDrive = _executionContext.SessionState.Drive.Get(driveName); } catch (MethodInvocationException) //didn't find a drive (maybe it's "\" on Windows) { nextDrive = CurrentDrive; } } Path newLocation = PathNavigation.CalculateFullPath(nextDrive.CurrentLocation, path); // I'm not a fan of this block of code. // The goal here is to throw an exception if trying to "CD" into an invalid location // // Not sure why the providerInstances are returned as a collection. Feels like given a // path we should have one provider we're talking to. if (_providerInstances.ContainsKey(nextDrive.Provider.Name)) { bool pathExists = false; IEnumerable <ItemCmdletProvider> cmdletProviders = _providerInstances[nextDrive.Provider.Name].Where(x => x is ItemCmdletProvider).Cast <ItemCmdletProvider>(); ItemCmdletProvider currentProvider = null; foreach (var provider in cmdletProviders) { if (provider.ItemExists(newLocation, providerRuntime)) { pathExists = true; currentProvider = provider; break; } } if (!pathExists) { throw new Exception(string.Format("Cannot find path '{0}' because it does not exist.", newLocation)); } else { if (currentProvider is FileSystemProvider) { System.Environment.CurrentDirectory = newLocation; } } } else { throw new NotImplementedException("Unsure how to set location with provider:" + nextDrive.Provider.Name); } nextDrive.CurrentLocation = newLocation; CurrentDrive = nextDrive; _providersCurrentDrive[CurrentDrive.Provider] = CurrentDrive; return(CurrentLocation); }