|
| 1 | +using System.Collections; |
| 2 | +using System.Management.Automation; |
| 3 | +using System.Reflection; |
| 4 | +using JetBrains.Annotations; |
| 5 | +using Pog.Commands.Common; |
| 6 | + |
| 7 | +namespace Pog.Commands; |
| 8 | + |
| 9 | +// TODO: allow wildcards in PackageName and Version arguments for commands where it makes sense |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// <para type="synopsis">Import, install, enable and export a package.</para> |
| 13 | +/// <para type="description"> |
| 14 | +/// Runs all four installation stages in order. All arguments passed to this cmdlet except for <c>-InstallOnly</c> and |
| 15 | +/// <c>-NoExport</c> are forwarded to <c>Import-Pog</c>. |
| 16 | +/// </para> |
| 17 | +/// </summary> |
| 18 | +[PublicAPI] |
| 19 | +[Alias("pog")] |
| 20 | +// Cmdlet(...) params are manually copied from Import-Pog, there doesn't seem any way to dynamically copy this like with dynamicparam |
| 21 | +[Cmdlet(VerbsLifecycle.Invoke, "Pog", DefaultParameterSetName = ImportPogCommand.DefaultPS)] |
| 22 | +[OutputType(typeof(ImportedPackage))] |
| 23 | +public sealed class InvokePogCommand : PackageCommandBase, IDynamicParameters { |
| 24 | + /// <summary><para type="description"> |
| 25 | + /// Import and install the package, do not enable and export. |
| 26 | + /// </para></summary> |
| 27 | + [Parameter] public SwitchParameter Install; |
| 28 | + |
| 29 | + /// <summary><para type="description"> |
| 30 | + /// Import, install and enable the package, do not export it. |
| 31 | + /// </para></summary> |
| 32 | + [Parameter] public SwitchParameter Enable; |
| 33 | + |
| 34 | + // TODO: add an `-Imported` parameter set to allow installing+enabling+exporting an imported package |
| 35 | + |
| 36 | + private static readonly CommandInfo ImportPogInfo = new CmdletInfo("Import-Pog", typeof(ImportPogCommand)); |
| 37 | + private static readonly CommandInfo InstallPogInfo = new CmdletInfo("Install-Pog", typeof(InstallPogCommand)); |
| 38 | + private static readonly CommandInfo EnablePogInfo = new CmdletInfo("Enable-Pog", typeof(EnablePogCommand)); |
| 39 | + private static readonly CommandInfo ExportPogInfo = new CmdletInfo("Export-Pog", typeof(ExportPogCommand)); |
| 40 | + |
| 41 | + private DynamicCommandParameters? _proxiedParams; |
| 42 | + private PowerShell _ps = PowerShell.Create(); |
| 43 | + private SteppablePipeline? _pipeline; |
| 44 | + |
| 45 | + public object GetDynamicParameters() { |
| 46 | + if (_proxiedParams != null) { |
| 47 | + return _proxiedParams; |
| 48 | + } |
| 49 | + var builder = new DynamicCommandParameters.Builder(UnknownAttributeHandler: (paramName, attr) => |
| 50 | + throw new InternalError($"Cannot copy parameter '{paramName}', attribute '{attr.GetType()}'.")); |
| 51 | + return _proxiedParams = builder.CopyParameters(ImportPogInfo); |
| 52 | + } |
| 53 | + |
| 54 | + // TODO: rollback on error |
| 55 | + protected override void BeginProcessing() { |
| 56 | + base.BeginProcessing(); |
| 57 | + |
| 58 | + var importParams = _proxiedParams!.Extract(); |
| 59 | + |
| 60 | + // reuse PassThru parameter from Import-Pog for Enable-Pog |
| 61 | + var passThru = (importParams["PassThru"] as SwitchParameter?)?.IsPresent ?? false; |
| 62 | + importParams.Remove("PassThru"); |
| 63 | + |
| 64 | + // TODO: check if these aren't forwarded automatically, alternatively expand to all common parameters |
| 65 | + var logArgs = new Hashtable(); |
| 66 | + if (MyInvocation.BoundParameters.ContainsKey("Verbose")) { |
| 67 | + logArgs["Verbose"] = MyInvocation.BoundParameters["Verbose"]; |
| 68 | + } |
| 69 | + if (MyInvocation.BoundParameters.ContainsKey("Debug")) { |
| 70 | + logArgs["Debug"] = MyInvocation.BoundParameters["Debug"]; |
| 71 | + } |
| 72 | + |
| 73 | + _ps.AddCommand(ImportPogInfo).AddParameters(logArgs).AddParameter("PassThru").AddParameters(importParams); |
| 74 | + if (Install) { |
| 75 | + _ps.AddCommand(InstallPogInfo).AddParameters(logArgs).AddParameter("PassThru", passThru); |
| 76 | + } else { |
| 77 | + _ps.AddCommand(InstallPogInfo).AddParameters(logArgs).AddParameter("PassThru"); |
| 78 | + if (Enable) { |
| 79 | + _ps.AddCommand(EnablePogInfo).AddParameters(logArgs).AddParameter("PassThru", passThru); |
| 80 | + } else { |
| 81 | + _ps.AddCommand(EnablePogInfo).AddParameters(logArgs).AddParameter("PassThru"); |
| 82 | + _ps.AddCommand(ExportPogInfo).AddParameters(logArgs).AddParameter("PassThru", passThru); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + _pipeline = (SteppablePipeline) PowerShellGetSteppablePipelineMethod.Invoke(_ps, []); |
| 87 | + _pipeline.Begin(this); |
| 88 | + } |
| 89 | + |
| 90 | + protected override void ProcessRecord() { |
| 91 | + base.ProcessRecord(); |
| 92 | + |
| 93 | + // https://github.com/orgs/PowerShell/discussions/21356 |
| 94 | + var value = CurrentPipelineObjectProperty.GetValue(this); |
| 95 | + _pipeline!.Process(value); |
| 96 | + } |
| 97 | + |
| 98 | + protected override void EndProcessing() { |
| 99 | + base.EndProcessing(); |
| 100 | + _pipeline!.End(); |
| 101 | + } |
| 102 | + |
| 103 | + public override void Dispose() { |
| 104 | + _ps.Dispose(); |
| 105 | + _pipeline?.Dispose(); |
| 106 | + } |
| 107 | + |
| 108 | + private static readonly MethodInfo PowerShellGetSteppablePipelineMethod = typeof(PowerShell).GetMethod( |
| 109 | + "GetSteppablePipeline", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, [], [])!; |
| 110 | + |
| 111 | + private static readonly PropertyInfo CurrentPipelineObjectProperty = typeof(PSCmdlet).GetProperty( |
| 112 | + "CurrentPipelineObject", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 113 | +} |
0 commit comments