internal void GetNames(string[] path, ReturnContainers returnContainers, bool recurse, ProviderRuntime runtime) { // the include/exclude filters apply to the results, not to the globbing process. Make this sure runtime.IgnoreFiltersForGlobbing = true; // compile here, not in every recursive iteration var filter = new IncludeExcludeFilter(runtime.Include, runtime.Exclude, false); // do the globbing manually, because the behavior depends on it... foreach (var p in path) { CmdletProvider provider; var doGlob = Globber.ShouldGlob(p, runtime); // even if we don't actually glob, the next method will return the resolved path & provider var resolved = Globber.GetGlobbedProviderPaths(p, runtime, out provider); var contProvider = CmdletProvider.As <ContainerCmdletProvider>(provider); foreach (var curPath in resolved) { if (!doGlob && filter.CanBeIgnored && !recurse) { contProvider.GetChildNames(curPath, returnContainers, runtime); continue; } if ((recurse || !doGlob) && Item.IsContainer(contProvider, curPath, runtime)) { ManuallyGetChildNames(contProvider, curPath, "", returnContainers, recurse, filter, runtime); continue; } var cn = Path.ParseChildName(contProvider, curPath, runtime); if (filter.Accepts(cn)) { runtime.WriteObject(cn); } } } }
// actual work with callid the providers internal void Get(string[] paths, bool recurse, ProviderRuntime runtime) { // the include/exclude filters apply to the results, not to the globbing process. Make this sure runtime.IgnoreFiltersForGlobbing = true; // globbing is here a little more complicated, so we do it "manually" (without GlobAndInvoke) foreach (var curPath in paths) { var path = curPath; // if the path won't be globbed or filtered, we will directly list it's child var listChildsWithoutRecursion = !Globber.ShouldGlob(path, runtime) && !runtime.HasFilters(); // the Path might be a mixture of a path and an include filter bool clearIncludeFilter; path = SplitFilterFromPath(path, recurse, runtime, out clearIncludeFilter); // now perform the actual globbing CmdletProvider provider; var globbed = Globber.GetGlobbedProviderPaths(path, runtime, out provider); var containerProvider = CmdletProvider.As <ContainerCmdletProvider>(provider); var filter = new IncludeExcludeFilter(runtime.Include, runtime.Exclude, false); foreach (var globPath in globbed) { try { // if we need to actively filter that stuff, we have to handle the recursion manually if (!filter.CanBeIgnored) { ManuallyGetChildItems(containerProvider, globPath, recurse, filter, runtime); return; } // otherwise just get the child items / the item directly if (recurse || listChildsWithoutRecursion) { GetItemOrChildItems(containerProvider, globPath, recurse, runtime); return; } // no recursion and globbing was performed: get the item, not the child items containerProvider.GetItem(globPath, runtime); } catch (Exception e) { HandleCmdletProviderInvocationException(e); } } // clean up the include filter of the runtime for the next item, if we split a filter from the path if (clearIncludeFilter) { runtime.Include.Clear(); } } }
string SplitFilterFromPath(string curPath, bool recurse, ProviderRuntime runtime, out bool clearIncludeFilter) { // When using Get-ChildItems, one could specify something like // "Get-ChildItem .\Source\*.cs -recurse" which should get all *.cs files recursively from .\Source. // This only works if no "-Include" was specified. So basically, it's a short form for // "Get-ChildItem .\Source -recurse -include *.cs", but of course the first is easier to write :) clearIncludeFilter = false; // first check if we deal with a LiteralPath, a container, or include is set. If so, this won't work // also if we don't need to recurse, the globber can simply do the work if (!recurse || !Globber.ShouldGlob(curPath, runtime) || (runtime.Include != null && runtime.Include.Count > 0) || Item.IsContainer(curPath, runtime)) { return(curPath); } clearIncludeFilter = true; var childName = Path.ParseChildName(curPath, runtime); runtime.Include.Add(childName); return(curPath.Substring(0, curPath.Length - childName.Length)); }