public CostCommand(ITariffSource source) { Spec = CommandSpec.Named( "cost", new[] { "POWER_USAGE_KWH", "GAS_USAGE_KWH" }, ctx => { // model 0's explicitly as no fuel supply for that type var p = ctx.GetRequiredDecimal("POWER_USAGE_KWH"); var powerKwh = p == 0 ? new Kwh <Power>?() : new Kwh <Power>(p); var g = ctx.GetRequiredDecimal("GAS_USAGE_KWH"); var gasKwh = g == 0 ? new Kwh <Gas>?() : new Kwh <Gas>(g); // calculate the costs and output foreach (var line in FuelCalculator .CostsPerTariff(source.GetAll(), powerKwh, gasKwh) .Select(cost => $"{cost.Tariff.Name} {cost.Total.PostTax:F2}")) { ctx.Output.WriteLine(line); } return(Commands.Ok()); }); }
public CommandResult Exec(CommandSpec spec) { var result = new Command(spec, _executor).Execute(); result.EnsureSuccessful(); return(result); }
private void OnCommandClick(CommandSpec spec) { var cmdSpec = (CommandItemInfoSpec)spec; var macroInfo = cmdSpec.Info; var trigger = macroInfo.Triggers.HasFlag(Triggers_e.ToggleButton) ? Triggers_e.ToggleButton : Triggers_e.Button; var targetDoc = m_App.Documents.Active; if (m_MacroRunner.TryRunMacroCommand(trigger, macroInfo, targetDoc, m_WorkDir)) { if (macroInfo.Triggers.HasFlag(Triggers_e.ToggleButton)) { if (macroInfo.ResolveButtonStateCodeOnce || macroInfo.ToggleButtonStateCodeType == ToggleButtonStateCode_e.None) { if (m_CachedToggleStates.TryGetValue(macroInfo, out bool isChecked)) { isChecked = !isChecked; m_CachedToggleStates[macroInfo] = isChecked; } else { Debug.Assert(false, "State should have been resolved by SOLIDWORKS before this call"); } } } } }
internal CommandItemEnableState_e RaiseCommandEnable(CommandSpec spec) { var state = new CommandState(); state.ResolveState(spec.SupportedWorkspace, m_App); CommandStateResolve?.Invoke(spec, state); if (state.Enabled) { if (state.Checked) { return(CommandItemEnableState_e.SelectEnable); } else { return(CommandItemEnableState_e.DeselectEnable); } } else { if (state.Checked) { return(CommandItemEnableState_e.SelectDisable); } else { return(CommandItemEnableState_e.DeselectDisable); } } }
private ICommand GetTargetCommand() { var globalProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { // This property disables default item globbing to improve performance // This should be safe because we are not evaluating items, only properties { Constants.EnableDefaultItems, "false" }, { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } if (!string.IsNullOrWhiteSpace(Runtime)) { globalProperties.Add("RuntimeIdentifier", Runtime); } var project = new ProjectInstance(Project, globalProperties, null); string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { ThrowUnableToRunError(project); } string runArguments = project.GetPropertyValue("RunArguments"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); if (Args.Any()) { runArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(Args); } CommandSpec commandSpec = new CommandSpec(runProgram, runArguments); var command = CommandFactoryUsingResolver.Create(commandSpec) .WorkingDirectory(runWorkingDirectory); var rootVariableName = Environment.Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)"; if (Environment.GetEnvironmentVariable(rootVariableName) == null) { command.EnvironmentVariable(rootVariableName, Path.GetDirectoryName(new Muxer().MuxerPath)); } return(command); }
private void OnCommandClick(CommandSpec spec) { if (m_Handlers.TryGetValue(spec, out Tuple <Delegate, Enum> handler)) { handler.Item1.DynamicInvoke(handler.Item2); } else { System.Diagnostics.Debug.Assert(false, "Handler is not registered"); } }
Option <TResult> ICommandPatternSystem.Execute <TSpec, TResult>(CommandSpec <TSpec, TResult> command, bool save) { var result = ExecuteCommand((ICommandSpec)command, save); if (result.IsSome) { return((TResult)result.Value); } else { return(none <TResult>(result.Message)); } }
private void OnCommandStateResolve(CommandSpec spec, CommandState state) { var cmdSpec = (CommandItemInfoSpec)spec; state.Enabled = cmdSpec.Info.Scope.IsInScope(m_App); if (state.Enabled) { if (cmdSpec.Info.Triggers.HasFlag(Triggers_e.ToggleButton)) { state.Checked = TryResolveState(cmdSpec.Info); } } }
public UsageCommand(ITariffSource source) { Spec = CommandSpec.Named( "usage", new[] { "TARIFF_NAME", "FUEL_TYPE", "TARGET_MONTHLY_SPEND" }, ctx => { var tariffName = ctx.GetRequiredString("TARIFF_NAME"); var fuelType = ctx.GetRequiredEnum <FuelType>("FUEL_TYPE"); var monthlyBudget = TaxedValue.FromPostTaxValue( ctx.GetRequiredDecimal("TARGET_MONTHLY_SPEND"), TaxHelper.RemoveTax); if (!source.TryGet(tariffName, out var tariff)) { return(Commands.Error($"The specified tariff doesn't exist '{tariffName}'.")); } // c# has no proper pattern matching, so I've run out of functional luck here, apologies for the switch statement... switch (fuelType) { case FuelType.Gas: if (!tariff.GasRate.HasValue) { return(Commands.Error($"Tariff '{tariff.Name}' does not include {FuelType.Gas}.")); } ctx.Output.WriteLine( FuelCalculator.AnnualGasUsage(tariff, monthlyBudget).GetValueOrDefault().Value); // just output 0 if tariff doesn't include fuel type break; case FuelType.Power: if (!tariff.PowerRate.HasValue) { return(Commands.Error($"Tariff '{tariff.Name}' does not include {FuelType.Power}.")); } ctx.Output.WriteLine( FuelCalculator.AnnualPowerUsage(tariff, monthlyBudget).GetValueOrDefault().Value); break; default: return(Commands.Error($"Unsupported fuel type : {fuelType}")); } return(Commands.Ok()); }); }
/// <summary> /// Returns a CommandSpec object given a filter name. /// </summary> private CommandSpec GetCmdSpec(string filterName) { CommandSpec result = null; foreach (CommandSpec cmdSpec in PipeEng.CmdSpecs) { if (filterName.ToUpper() == cmdSpec.Name.ToUpper()) { result = cmdSpec; break; } } return(result); }
private ICommand GetRunCommand() { var globalProperties = new Dictionary <string, string> { { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); string runProgram = projectInstance.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { string outputType = projectInstance.GetPropertyValue("OutputType"); throw new GracefulException( string.Format( LocalizableStrings.RunCommandExceptionUnableToRun, "dotnet run", "OutputType", outputType)); } string runArguments = projectInstance.GetPropertyValue("RunArguments"); string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
private ICommand GetTargetCommand() { var globalProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { // This property disables default item globbing to improve performance // This should be safe because we are not evaluating items, only properties { Constants.EnableDefaultItems, "false" }, { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } if (!string.IsNullOrWhiteSpace(Runtime)) { globalProperties.Add("RuntimeIdentifier", Runtime); } var project = new ProjectInstance(Project, globalProperties, null); string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { ThrowUnableToRunError(project); } string runArguments = project.GetPropertyValue("RunArguments"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); if (Args.Any()) { runArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(Args); } CommandSpec commandSpec = new CommandSpec(runProgram, runArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
private ICommand GetRunCommand() { Dictionary <string, string> globalProperties = new Dictionary <string, string>() { { LocalizableStrings.RunCommandMSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add(LocalizableStrings.RunCommandConfiguration, Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add(LocalizableStrings.RunCommandTargetFramework, Framework); } ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); string runProgram = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandProjectInstance); if (string.IsNullOrEmpty(runProgram)) { string outputType = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandOutputType); throw new GracefulException(string.Join(Environment.NewLine, LocalizableStrings.RunCommandExceptionUnableToRun1, LocalizableStrings.RunCommandExceptionUnableToRun2, $"{LocalizableStrings.RunCommandExceptionUnableToRun3} '{outputType}'.")); } string runArguments = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandRunArguments); string runWorkingDirectory = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandRunWorkingDirectory); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
private ICommand GetRunCommand() { Dictionary <string, string> globalProperties = new Dictionary <string, string>() { { "MSBuildExtensionsPath", AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); string runProgram = projectInstance.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { string outputType = projectInstance.GetPropertyValue("OutputType"); throw new GracefulException(string.Join(Environment.NewLine, "Unable to run your project.", "Please ensure you have a runnable project type and ensure 'dotnet run' supports this project.", $"The current OutputType is '{outputType}'.")); } string runArguments = projectInstance.GetPropertyValue("RunArguments"); string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
protected virtual void FilterButton_OnActivated(object sender, System.EventArgs e) { TextIter InsertIter = PipeTextView.Buffer.GetIterAtMark( PipeTextView.Buffer.InsertMark); string filterName = ((ToolButton)sender).Label; PipeTextView.Buffer.Insert(ref InsertIter, filterName + " "); // Update the status bar: CommandSpec CmdSpec = GetCmdSpec(filterName); StatusBarTextLabel.Text = CmdSpec.Name + " " + CmdSpec.Prompt; // Set focus to the pipe: PipeTextView.GrabFocus(); }
public ICommand ToCommand() { var commandSpec = new CommandSpec(FileName, EscapeArgs(), CommandResolutionStrategy.Path); ICommand ret = Command.Create(commandSpec); if (WorkingDirectory != null) { ret = ret.WorkingDirectory(WorkingDirectory); } // It's necessary to set the environment variables here instead of passing them to the CommandSpec constructor, // because if they are passed to the CommandSpec constructor, they won't override existing environment variables, // which can cause the wrong MSBuildSDKsPath to be used foreach (var kvp in Environment) { ret.EnvironmentVariable(kvp.Key, kvp.Value); } return(ret); }
public override int Execute() { CommandSpec commandspec = _localToolsCommandResolver.ResolveStrict(new CommandResolverArguments() { // since LocalToolsCommandResolver is a resolver, and all resolver input have dotnet- CommandName = $"dotnet-{_toolCommandName}", CommandArguments = _forwardArgument }); if (commandspec == null) { throw new GracefulException( new string[] { string.Format(LocalizableStrings.CannotFindCommandName, _toolCommandName) }, isUserError: false); } var result = CommandFactoryUsingResolver.Create(commandspec).Execute(); return(result.ExitCode); }
private ICommand GetRunCommand() { var globalProperties = new Dictionary <string, string> { { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } Project project = new Project(Project, globalProperties, null); string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { ThrowUnableToRunError(project); } string runArguments = project.GetPropertyValue("RunArguments"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
private void UpdateCmdSyntax() { // Update command syntax on status bar: TextIter ti = PipeTextView.Buffer.GetIterAtMark(PipeTextView.Buffer.InsertMark); TextIter begIter = PipeTextView.Buffer.GetIterAtLine(ti.Line); TextIter endIter = PipeTextView.Buffer.GetIterAtLine(ti.Line); endIter.ForwardLine(); string text = PipeTextView.Buffer.GetText(begIter, endIter, false).Replace(System.Environment.NewLine, string.Empty); string filterName = ParseFilterName(text); CommandSpec cmdSpec = GetCmdSpec(filterName); if (cmdSpec != null) { StatusBarTextLabel.Text = cmdSpec.Name + " " + cmdSpec.Prompt; } else { StatusBarTextLabel.Text = string.Empty; } }
public Command Create(CommandSpec spec) { return(new Command(spec, _executor)); }
internal CommandInfo(CommandSpec spec, SwCommandGroup parent, int commandId) { Spec = spec; Group = parent; CommandId = commandId; }
internal CommandInfo(SwCommandGroup grp, CommandSpec spec) { Grp = grp; Spec = spec; }
internal void RaiseCommandClick(CommandSpec spec) { CommandClick?.Invoke(spec); }
internal InvalidMenuToolbarOptionsException(CommandSpec cmd) : base($"Neither toolbar nor menu option is specified for {cmd.Title} ({cmd.UserId}) command. Use") { }