Skip to content

Commit

Permalink
Fixes CLI discovery of installed modules #1779 (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Mar 22, 2024
1 parent d184d5d commit f7688ce
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"SARIF",
"SAST",
"SBOM",
"STRINGARRAY",
"subselector",
"unencrypted",
"Worktree",
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ What's changed since pre-release v3.0.0-B0153:
[#1771](https://github.com/microsoft/PSRule/pull/1771)
- Bump Microsoft.CodeAnalysis.Common to v4.9.2.
[#1773](https://github.com/microsoft/PSRule/pull/1773)
- Bug fixes:
- Fixed discovery of installed modules in CLI by @BernieWhite.
[#1779](https://github.com/microsoft/PSRule/issues/1779)

## v3.0.0-B0153 (pre-release)

Expand Down
4 changes: 4 additions & 0 deletions src/PSRule.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.CommandLine;
using System.CommandLine.Parsing;
using System.Management.Automation;

namespace PSRule.Tool;

Expand All @@ -13,6 +14,9 @@ static class Program
/// </summary>
static async Task<int> Main(string[] args)
{
var ps = ModuleIntrinsics.GetPSModulePath(ModuleIntrinsics.PSModulePathScope.User);
System.Environment.SetEnvironmentVariable("PSModulePath", ps, EnvironmentVariableTarget.Process);

return await ClientBuilder.New().InvokeAsync(args);
}
}
12 changes: 6 additions & 6 deletions src/PSRule.Tool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"profiles": {
"ps-rule analyze": {
"commandName": "Project",
"commandLineArgs": "analyze -m PSRule.Rules.Azure",
"workingDirectory": "../../"
},
"ps-rule restore": {
"commandName": "Project",
"commandLineArgs": "restore",
Expand All @@ -14,6 +9,11 @@
"commandName": "Project",
"commandLineArgs": "module add abc --version 1.0.0",
"workingDirectory": "../../"
},
"ps-rule run": {
"commandName": "Project",
"commandLineArgs": "run -m PSRule.Rules.Azure",
"workingDirectory": "../../"
}
}
}
}
4 changes: 2 additions & 2 deletions src/PSRule.Types/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class Environment
private static CultureInfo _CurrentCulture = Thread.CurrentThread.CurrentCulture;

/// <summary>
/// A delgate to allow callback get current working path.
/// A delegate to allow callback get current working path.
/// </summary>
public delegate string WorkingPathResolver();

Expand Down Expand Up @@ -329,7 +329,7 @@ private static bool TryParseBool(string? variable, out bool? value)
/// Determine if the <seealso cref="char"/> is a path separator character.
/// </summary>
/// <param name="c">The character to check.</param>
/// <returns>Returns <c>true</c> if the charater is a path separator. Otherwise <c>false</c> is returned.</returns>
/// <returns>Returns <c>true</c> if the character is a path separator. Otherwise <c>false</c> is returned.</returns>
[DebuggerStepThrough]
private static bool IsPathSeparator(char c)
{
Expand Down
17 changes: 12 additions & 5 deletions src/PSRule/Pipeline/SourcePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,18 @@ public void Module(PSModuleInfo[] module)
public void ModuleByName(string name, string version = null)
{
var basePath = FindModule(name, version) ?? throw new PipelineBuilderException(PSRuleResources.ModuleNotFound);
var info = LoadManifest(basePath) ?? throw new PipelineBuilderException(PSRuleResources.ModuleNotFound);
var info = LoadManifest(basePath, name) ?? throw new PipelineBuilderException(PSRuleResources.ModuleNotFound);

VerboseScanModule(info.Name);
var files = GetFiles(basePath, basePath, excludeDefaultRulePath: false, restrictScriptSource: _RestrictScriptSource, info.Name);
var files = GetFiles(
path: basePath,
helpPath: basePath,
excludeDefaultRulePath: false,
restrictScriptSource: _RestrictScriptSource,
moduleName: info.Name,
workspacePath: _WorkspacePath
);

if (files == null || files.Length == 0)
return;

Expand Down Expand Up @@ -296,7 +304,7 @@ private bool TryInstalledModule(string name, string version, out string path)

var sorted = SortModulePath(unsorted);
if (sorted.Length > 0)
path = sorted[0];
path = Environment.GetRootedBasePath(sorted[0]);

return sorted.Length > 0;
}
Expand All @@ -308,9 +316,8 @@ private static string[] SortModulePath(IEnumerable<string> values)
return results;
}

private Source.ModuleInfo LoadManifest(string basePath)
private Source.ModuleInfo LoadManifest(string basePath, string name)
{
var name = Path.GetFileName(Path.GetDirectoryName(basePath));
var path = Path.Combine(basePath, GetManifestName(name));
if (!File.Exists(path))
return null;
Expand Down
26 changes: 14 additions & 12 deletions src/PSRule/Runtime/ObjectPath/PathExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace PSRule.Runtime.ObjectPath;
internal sealed class PathExpressionBuilder
{
private const int DEFAULT_RECURSE_MAX_DEPTH = 100;
private static readonly object[] DEFAULT_EMPTY_ARRAY = new object[] { };
private static readonly object[] DEFAULT_EMPTY_ARRAY = [];

private readonly int _RecurseMaxDepth;

Expand Down Expand Up @@ -357,15 +357,15 @@ private static bool Return(IPathExpressionContext context, object input, out IEn
value = eo;
}
else
value = new object[] { input };
value = [input];

return true;
}

private static PathExpressionFn Literal(object arg)
{
var isEnumerable = arg is object[];
var result = isEnumerable ? arg as object[] : new object[] { arg };
var result = isEnumerable ? arg as object[] : [arg];
return (IPathExpressionContext context, object input, out IEnumerable<object> value, out bool enumerable) =>
{
value = result;
Expand Down Expand Up @@ -428,7 +428,7 @@ private static IEnumerable<object> GetAllField(object o)
if (baseObject == null)
yield break;

// Handle dictionaries and hashtables
// Handle dictionaries and hash tables
if (baseObject is IDictionary dictionary)
{
foreach (var value in dictionary.Values)
Expand All @@ -447,7 +447,7 @@ private static IEnumerable<object> GetAllField(object o)
yield return property.Value;
}
// Handle DynamicObjects
else if (o is DynamicObject dynamicObject)
else if (o is DynamicObject)
{

}
Expand All @@ -473,10 +473,11 @@ private static bool TryGetField(object o, string fieldName, bool caseSensitive,
{
value = null;
var baseObject = ExpressionHelpers.GetBaseObject(o);
if (baseObject == null || (baseObject is JValue jValue && jValue.Type == JTokenType.Null))
if (baseObject == null || (baseObject is JValue jValue && jValue.Type == JTokenType.Null) ||
baseObject is string || baseObject is int || baseObject is long || baseObject is float)
return false;

// Handle dictionaries and hashtables
// Handle dictionaries and hash tables
if (baseObject is IDictionary dictionary)
{
return TryDictionary(dictionary, fieldName, caseSensitive, out value);
Expand All @@ -496,6 +497,7 @@ private static bool TryGetField(object o, string fieldName, bool caseSensitive,
{
return TryPropertyValue(dynamicObject, fieldName, caseSensitive, out value);
}

// Handle all other CLR types
var baseType = baseObject.GetType();
return TryPropertyValue(o, fieldName, baseType, caseSensitive, out value) ||
Expand All @@ -507,7 +509,7 @@ private static bool TryGetIndex(object o, int index, out object value)
{
value = null;
var baseObject = ExpressionHelpers.GetBaseObject(o);
if (baseObject == null)
if (baseObject == null || baseObject is string || baseObject is int || baseObject is long || baseObject is float)
return false;

// Handle array indexes
Expand Down Expand Up @@ -672,7 +674,7 @@ private static bool TryIndexerProperty(object targetObject, object index, Type b
{
var converter = GetConverter(parameters[0].ParameterType);
var p1 = converter(index);
value = property.GetValue(targetObject, new object[] { p1 });
value = property.GetValue(targetObject, [p1]);
return true;
}
catch
Expand All @@ -686,10 +688,10 @@ private static bool TryIndexerProperty(object targetObject, object index, Type b

private static Converter<object, object> GetConverter(Type targetType)
{
var convertAtribute = targetType.GetCustomAttribute<TypeConverterAttribute>();
if (convertAtribute != null)
var convertAttribute = targetType.GetCustomAttribute<TypeConverterAttribute>();
if (convertAttribute != null)
{
var converterType = Type.GetType(convertAtribute.ConverterTypeName);
var converterType = Type.GetType(convertAttribute.ConverterTypeName);
if (converterType.IsSubclassOf(typeof(TypeConverter)))
{
var converter = (TypeConverter)Activator.CreateInstance(converterType);
Expand Down

0 comments on commit f7688ce

Please sign in to comment.