Skip to content

Commit

Permalink
Add dynamic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNieto committed Mar 24, 2024
1 parent 0b4a51a commit 3f31b04
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/code/ArchitectureCompleter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Thomas Nieto - All Rights Reserved
// You may use, distribute and modify this code under the
// terms of the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Language;

namespace AnyPackage.Provider.DotNet
{
public sealed class ArchitectureCompleter : IArgumentCompleter
{
private static string[] architectures = new string[]
{
"android-arm64",
"ios-arm64",
"linux-arm",
"linux-arm64",
"linux-bionic-arm64",
"linux-musl-arm64",
"linux-musl-x64",
"linux-x64",
"osx-arm64",
"osx-x64",
"win-arm64",
"win-x64",
"win-x86"
};

public IEnumerable<CompletionResult> CompleteArgument(string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
var wildcard = new WildcardPattern(wordToComplete + "*", WildcardOptions.IgnoreCase);

foreach (var arch in architectures)
{
if (wildcard.IsMatch(arch))
{
yield return new CompletionResult(arch, arch, CompletionResultType.ParameterValue, arch);
}
}
}
}
}
20 changes: 20 additions & 0 deletions src/code/InstallPackageDynamicParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Thomas Nieto - All Rights Reserved
// You may use, distribute and modify this code under the
// terms of the MIT license.

using System.Management.Automation;

namespace AnyPackage.Provider.DotNet
{
public sealed class InstallPackageDynamicParameters
{
[Parameter]
[ArgumentCompleter(typeof(ArchitectureCompleter))]
[ValidateNotNullOrEmpty]
public string Architecture { get; set; } = string.Empty;

[Parameter]
[ValidateNotNullOrEmpty]
public string Framework { get; set; } = string.Empty;
}
}
40 changes: 40 additions & 0 deletions src/code/ToolProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ namespace AnyPackage.Provider.DotNet
[PackageProvider(".NET Tool")]
public class ToolProvider : PackageProvider, IFindPackage, IGetPackage, IInstallPackage, IUpdatePackage, IUninstallPackage
{
protected override object? GetDynamicParameters(string commandName)
{
switch (commandName)
{
case "Install-Package":
return new InstallPackageDynamicParameters();

case "Update-Package":
return new UpdatePackageDynamicParameters();

default:
return null;
}
}

public void FindPackage(PackageRequest request)
{
if (request.Name == "*")
Expand Down Expand Up @@ -127,6 +142,21 @@ public void InstallPackage(PackageRequest request)
args += " --prerelease";
}

if (request.DynamicParameters is not null)
{
var dynamicParameters = request.DynamicParameters as InstallPackageDynamicParameters;

if (dynamicParameters is not null && dynamicParameters.Architecture != "")
{
args += $" --arch {dynamicParameters.Architecture}";
}

if (dynamicParameters is not null && dynamicParameters.Framework != "")
{
args += $" --framework {dynamicParameters.Framework}";
}
}

InvokeDotNet(request, args);
}

Expand All @@ -151,6 +181,16 @@ public void UpdatePackage(PackageRequest request)
args += " --prerelease";
}

if (request.DynamicParameters is not null)
{
var dynamicParameters = request.DynamicParameters as UpdatePackageDynamicParameters;

if (dynamicParameters is not null && dynamicParameters.Framework != "")
{
args += $" --framework {dynamicParameters.Framework}";
}
}

InvokeDotNet(request, args);
}

Expand Down
15 changes: 15 additions & 0 deletions src/code/UpdatePackageDynamicParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Thomas Nieto - All Rights Reserved
// You may use, distribute and modify this code under the
// terms of the MIT license.

using System.Management.Automation;

namespace AnyPackage.Provider.DotNet
{
public sealed class UpdatePackageDynamicParameters
{
[Parameter]
[ValidateNotNullOrEmpty]
public string Framework { get; set; } = string.Empty;
}
}

0 comments on commit 3f31b04

Please sign in to comment.