GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Refactor module code related to 'Get-Module -ListAvailable' by daxian-dbw · Pull Request #7145 · PowerShell/PowerShell

Refactor module code related to 'Get-Module -ListAvailable' #7145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Dongbo Wang (daxian-dbw) merged 4 commits into PowerShell:master from daxian-dbw:moduleRefactor
Jul 2, 2018

Uh oh!

There was an error while loading. Please reload this page.

Merged

Refactor module code related to 'Get-Module -ListAvailable' #7145

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
ff97ae2
[Feature] Refactor module code related to 'Get-Module -ListAvailable'
daxian-dbw Jun 21, 2018
1ca7e12
[Feature] Add tests for 'Get-Module -List -All'
daxian-dbw Jun 29, 2018
406dfc2
[Feature] Address feedback about the 'string[]' parameter
daxian-dbw Jun 29, 2018
4211401
Update a comment based on feedback
daxian-dbw Jun 29, 2018
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 1 addition & 1 deletion 2 src/System.Management.Automation/engine/CommandDiscovery.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ private static CommandInfo TryModuleAutoDiscovery(string commandName,
// Get the available module files, preferring modules from $PSHOME so that user modules don't
// override system modules during auto-loading
if (etwEnabled) CommandDiscoveryEventSource.Log.SearchingForModuleFilesStart();
var defaultAvailableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(true, true, context);
var defaultAvailableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(isForAutoDiscovery: true, context);
if (etwEnabled) CommandDiscoveryEventSource.Log.SearchingForModuleFilesStop();

foreach (string modulePath in defaultAvailableModuleFiles)
Expand Down
2 changes: 1 addition & 1 deletion 2 src/System.Management.Automation/engine/InitialSessionState.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,7 @@ private string[] GetModulesForUnResolvedCommands(IEnumerable<string> unresolvedC
foreach (var unresolvedCommand in commandsToResolve)
{
// Use the analysis cache to find the first module containing the unresolved command.
foreach (string modulePath in ModuleUtils.GetDefaultAvailableModuleFiles(true, true, context))
foreach (string modulePath in ModuleUtils.GetDefaultAvailableModuleFiles(isForAutoDiscovery: true, context))
Comment thread Outdated

@rjmholt Rob Holt (rjmholt) Jun 22, 2018

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hooray for this and the one above!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
{
string expandedModulePath = IO.Path.GetFullPath(modulePath);
var exportedCommands = AnalysisCache.GetExportedCommands(expandedModulePath, false, context);
Expand Down
160 changes: 55 additions & 105 deletions 160 src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -869,37 +869,19 @@ internal List<PSModuleInfo> GetModule(string[] names, bool all, bool refresh)
moduleNames.Add(n);
}
}
modulesToReturn.AddRange(GetModuleForRootedPaths(modulePaths.ToArray(), all, refresh));
modulesToReturn.AddRange(GetModuleForRootedPaths(modulePaths, all, refresh));
}

// If no names were passed to this function, then this API will return list of all available modules
if (names == null || moduleNames.Count > 0)
{
modulesToReturn.AddRange(GetModuleForNonRootedPaths(moduleNames.ToArray(), all, refresh));
modulesToReturn.AddRange(GetModuleForNames(moduleNames, all, refresh));
}

return modulesToReturn;
}

private IEnumerable<PSModuleInfo> GetModuleForNonRootedPaths(string[] names, bool all, bool refresh)
{
const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(names, wildcardOptions);

Dictionary<String, List<PSModuleInfo>> availableModules = GetAvailableLocallyModulesCore(names, all, refresh);
foreach (var entry in availableModules)
{
foreach (PSModuleInfo module in entry.Value)
{
if (SessionStateUtilities.MatchesAnyWildcardPattern(module.Name, patternList, true))
{
yield return module;
}
}
}
}

private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths, bool all, bool refresh)
private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePaths, bool all, bool refresh)
{
// This is to filter out duplicate modules
var modules = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Expand Down Expand Up @@ -950,7 +932,7 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths,

var availableModuleFiles = all
? ModuleUtils.GetAllAvailableModuleFiles(resolvedModulePath)
: ModuleUtils.GetModuleVersionsFromAbsolutePath(resolvedModulePath);
: ModuleUtils.GetModuleFilesFromAbsolutePath(resolvedModulePath);

bool foundModule = false;
foreach (string file in availableModuleFiles)
Expand Down Expand Up @@ -998,38 +980,31 @@ private ErrorRecord CreateModuleNotFoundError(string modulePath)
return er;
}

private Dictionary<String, List<PSModuleInfo>> GetAvailableLocallyModulesCore(string[] names, bool all, bool refresh)
private IEnumerable<PSModuleInfo> GetModuleForNames(List<string> names, bool all, bool refresh)
{
var modules = new Dictionary<string, List<PSModuleInfo>>(StringComparer.OrdinalIgnoreCase);
var modulePaths = ModuleIntrinsics.GetModulePath(false, Context);

IEnumerable<PSModuleInfo> allModules = null;
HashSet<string> modulePathSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
bool cleanupModuleAnalysisAppDomain = Context.TakeResponsibilityForModuleAnalysisAppDomain();

try
{
foreach (string path in ModuleIntrinsics.GetModulePath(false, Context))
{
try
{
var availableModules = all
? GetAllAvailableModules(path, refresh)
: GetDefaultAvailableModules(names, path, refresh);
string uniquePath = path.TrimEnd(Utils.Separators.Directory);

// Add the path in $env:PSModulePath as the keys of the dictionary
// If the paths are repeated, ignore the repetitions
string uniquePath = path.TrimEnd(Utils.Separators.Directory);
if (!modules.ContainsKey(uniquePath))
{
modules.Add(uniquePath, availableModules.OrderBy(m => m.Name).ToList());
}
}
catch (IOException)
// Ignore repeated module path.
if (!modulePathSet.Add(uniquePath)) { continue; }

try
{
continue; // ignore directories that can't be accessed
IEnumerable<PSModuleInfo> modulesFound = GetModulesFromOneModulePath(
names, uniquePath, all, refresh).OrderBy(m => m.Name);
allModules = allModules == null ? modulesFound : allModules.Concat(modulesFound);
}
catch (UnauthorizedAccessException)
catch (Exception e) when (e is IOException || e is UnauthorizedAccessException)
{
continue; // ignore directories that can't be accessed
// ignore directories that can't be accessed
continue;
}
}
}
Expand All @@ -1041,64 +1016,43 @@ private Dictionary<String, List<PSModuleInfo>> GetAvailableLocallyModulesCore(st
}
}

return modules;
// Make sure we always return a non-null collection.
return allModules ?? Utils.EmptyArray<PSModuleInfo>();
}

/// <summary>
/// Get a list of all modules
/// which can be imported just by specifying a non rooted file name of the module
/// (Import-Module foo\bar.psm1; but not Import-Module .\foo\bar.psm1)
/// Get modules based on the given names and module files.
/// </summary>
private IEnumerable<PSModuleInfo> GetAllAvailableModules(string directory, bool refresh)
private IEnumerable<PSModuleInfo> GetModulesFromOneModulePath(List<string> names, string modulePath, bool all, bool refresh)
{
var availableModuleFiles = ModuleUtils.GetAllAvailableModuleFiles(directory);

foreach (string file in availableModuleFiles)
const WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
IEnumerable<WildcardPattern> namePatterns = null;
if (names != null && names.Count > 0)
{
PSModuleInfo module = CreateModuleInfoForGetModule(file, refresh);
if (module != null)
{
yield return module;
}
namePatterns = SessionStateUtilities.CreateWildcardsFromStrings(names, options);
}
}

/// <summary>
/// Get a list of the available modules
/// which can be imported just by specifying a non rooted directory name of the module
/// (Import-Module foo\bar; but not Import-Module .\foo\bar or Import-Module .\foo\bar.psm1)
/// </summary>
private List<PSModuleInfo> GetDefaultAvailableModules(string[] name, string directory, bool refresh)
{
List<PSModuleInfo> availableModules = new List<PSModuleInfo>();

const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(name, wildcardOptions);
IEnumerable<string> moduleFiles = all
? ModuleUtils.GetAllAvailableModuleFiles(modulePath)
: ModuleUtils.GetDefaultAvailableModuleFiles(modulePath);

var availableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(directory);

foreach (string file in availableModuleFiles)
foreach (string file in moduleFiles)
{
string actualModuleName = System.IO.Path.GetFileNameWithoutExtension(file);
if (SessionStateUtilities.MatchesAnyWildcardPattern(actualModuleName, patternList, true))
if (namePatterns == null ||
SessionStateUtilities.MatchesAnyWildcardPattern(
Path.GetFileNameWithoutExtension(file), namePatterns, defaultValue: true))
{
PSModuleInfo module = CreateModuleInfoForGetModule(file, refresh);
if (module == null) { continue; }

if (module != null)
if (all || !ModuleUtils.IsModuleInVersionSubdirectory(file, out Version directoryVersion) || directoryVersion == module.Version)
{
Version directoryVersion;
if (!ModuleUtils.IsModuleInVersionSubdirectory(file, out directoryVersion)
|| directoryVersion == module.Version)
{
availableModules.Add(module);
}
yield return module;
}
}
}

ClearAnalysisCaches();

return availableModules;
}

/// <summary>
Expand Down Expand Up @@ -1191,24 +1145,24 @@ private PSModuleInfo CreateModuleInfoForGetModule(string file, bool refresh)
moduleInfo = LoadModuleManifest(
scriptInfo,
flags /* - don't write errors, don't load elements */,
null,
null,
null,
null);
minimumVersion: null,
Comment thread Outdated

@rjmholt Rob Holt (rjmholt) Jun 22, 2018

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah much nicer!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
maximumVersion: null,
requiredVersion: null,
requiredModuleGuid: null);
}
else
{
// It's not a module manifest, process the individual module
ImportModuleOptions options = new ImportModuleOptions();
bool found = false;

moduleInfo = LoadModule(file, null, String.Empty, null, ref options, flags, out found);
moduleInfo = LoadModule(file, moduleBase: null, prefix: String.Empty, ss: null, ref options, flags, out found);
}

// return fake PSModuleInfo if can't read the file for any reason
if (moduleInfo == null)
{
moduleInfo = new PSModuleInfo(file, null, null);
moduleInfo = new PSModuleInfo(file, context: null, sessionState: null);
moduleInfo.HadErrorsLoading = true; // Prevent analysis cache from caching a bad module.
}

Expand Down Expand Up @@ -2677,9 +2631,13 @@ internal PSModuleInfo LoadModuleManifest(
}
}

var needToAnalyzeScriptModules = usedWildcard;
// We have to further analyze the module if any wildcard characters are used.
bool needToAnalyzeScriptModules = usedWildcard;

if (!needToAnalyzeScriptModules)
// We can skip further analysis if 'FunctionsToExport', 'CmdletsToExport' and 'AliasesToExport'
// are all declared and no wildcard character is used for them. But if any of 'FunctionsToExport',
// 'CmdletsToExport' or 'AliasesToExport' were not given, we must check to see if more analysis is needed.
if (!needToAnalyzeScriptModules && (!sawExportedCmdlets || !sawExportedFunctions || !sawExportedAliases))
{
foreach (var nestedModule in nestedModules)
{
Expand Down Expand Up @@ -3173,9 +3131,9 @@ internal PSModuleInfo LoadModuleManifest(
newManifestInfo.DeclaredCmdletExports = manifestInfo.DeclaredCmdletExports;
}

if (manifestInfo._detectedCmdletExports != null)
if (manifestInfo.DetectedCmdletExports != null)
{
foreach (string detectedExport in manifestInfo._detectedCmdletExports)
foreach (string detectedExport in manifestInfo.DetectedCmdletExports)
{
newManifestInfo.AddDetectedCmdletExport(detectedExport);
}
Expand All @@ -3187,9 +3145,9 @@ internal PSModuleInfo LoadModuleManifest(
newManifestInfo.DeclaredFunctionExports = manifestInfo.DeclaredFunctionExports;
}

if (manifestInfo._detectedFunctionExports != null)
if (manifestInfo.DetectedFunctionExports != null)
{
foreach (string detectedExport in manifestInfo._detectedFunctionExports)
foreach (string detectedExport in manifestInfo.DetectedFunctionExports)
{
newManifestInfo.AddDetectedFunctionExport(detectedExport);
}
Expand All @@ -3200,9 +3158,9 @@ internal PSModuleInfo LoadModuleManifest(
newManifestInfo.DeclaredAliasExports = manifestInfo.DeclaredAliasExports;
}

if (manifestInfo._detectedAliasExports != null)
if (manifestInfo.DetectedAliasExports != null)
{
foreach (var pair in manifestInfo._detectedAliasExports)
foreach (var pair in manifestInfo.DetectedAliasExports)
{
newManifestInfo.AddDetectedAliasExport(pair.Key, pair.Value);
}
Expand Down Expand Up @@ -3269,15 +3227,7 @@ internal PSModuleInfo LoadModuleManifest(
}
else
{
Collection<string> v = new Collection<string>();
if (manifestInfo.DeclaredFunctionExports != null)
{
foreach (var f in manifestInfo.DeclaredFunctionExports)
{
v.Add(f);
}
}
UpdateCommandCollection(v, exportedFunctions);
UpdateCommandCollection(manifestInfo.DeclaredFunctionExports, exportedFunctions);
}
}

Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.