public override CommandResult Run() { if (!Add && !Remove && !Shuffle && !OrderList) return new CommandResult(CommandStatus.Failure, "Missing one of -a -r -s -o required flag"); if (Input == null) return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("input")); if (string.IsNullOrEmpty(Delimiter)) return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("delimiter")); if (string.IsNullOrEmpty(Element) && !Shuffle && !OrderList) return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("element")); if (Shuffle && OrderList) return new CommandResult(CommandStatus.Failure, "Cannot use -s and -o together"); var elements = new List<string>(); elements.AddRange(Input.Split(new[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries)); if (Add) { if (!elements.Contains(Element)) elements.Add(Element); } else if (Remove) { if (elements.Contains(Element)) elements.Remove(Element); } else if (Shuffle) { System.Random random; if(_randomSeed != null) random = new System.Random(_randomSeed.Value); else random = new System.Random(); elements = (from element in elements orderby random.Next() select element).ToList(); } else if (OrderList) { elements = (from element in elements orderby element select element).ToList(); } else return new CommandResult(CommandStatus.Failure, "Unknown operation"); if (ReverseOrder) elements.Reverse(); return new CommandResult(CommandStatus.Success, string.Join(Delimiter, elements.ToArray())); }
public override CommandResult Run() { using (var cs = new ContextSwitcher(Context, Path)) { if (cs.Result.Status != CommandStatus.Success) return cs.Result; var languages = new List<Language>(); languages.Add(Context.CurrentItem.Language); if (AllLanguages) { languages.Clear(); languages.AddRange(Context.CurrentItem.Languages); } var versions = new List<int>(); versions.Add(Context.CurrentItem.Version.Number); if (OtherVersions) { versions.Clear(); versions.AddRange(Context.CurrentItem.Versions.GetVersionNumbers().Select(x => x.Number)); versions.Remove(Context.CurrentItem.Version.Number); if (AllLanguages) { // Other languages may have different version numbers. Need to add those extras foreach (var language in Context.CurrentItem.Languages) { var langItem = Context.CurrentItem.Database.GetItem(Context.CurrentItem.ID, language); foreach (var version in langItem.Versions.GetVersionNumbers()) { if (!versions.Contains(version.Number) && version.Number != Context.CurrentItem.Version.Number) versions.Add(version.Number); } } } } var count = 0; foreach (var language in languages) { foreach (var version in versions) { var versionItem = Context.CurrentItem.Database.GetItem(Context.CurrentItem.ID, language, new Version(version)); if (versionItem != null) { versionItem.Versions.RemoveVersion(); count++; } } } return new CommandResult(CommandStatus.Success, "Deleted {0} version{1}".FormatWith(count, count == 1 ? string.Empty : "s")); } }
/// <summary> /// Execute a command or script /// </summary> /// <param name="command">The command or script name</param> /// <param name="args">Arguments to pass to the command</param> /// <param name="directive">Execution directives to control how execution should perform</param> /// <returns>True if the command was run, otherwise returns false</returns> private CommandResult Execute(string command, string[] args, ExecutionDirective directive) { // update current date and time in environment variable Context.EnvironmentVariables["now"] = Sitecore.DateUtil.IsoNow; command = Parser.PerformSubstitution(_context, command); if (command == string.Empty) return new CommandResult(CommandStatus.Success, string.Empty); ICommand cmd = null; if (_commands.ContainsKey(command)) { cmd = (ICommand)Activator.CreateInstance(_commands[command]); } else if (_custcommands.ContainsKey(command)) { cmd = (ICommand)Activator.CreateInstance(_custcommands[command]); } else if(_commandAliases.ContainsKey(command) && _commands.ContainsKey(_commandAliases[command].CommandName)) { var alias = _commandAliases[command]; cmd = (ICommand)Activator.CreateInstance(_commands[alias.CommandName]); if(alias.Parameters != null && alias.Parameters.Length > 0) { var paramsList = new List<string>(alias.Parameters); paramsList.AddRange(args); args = paramsList.ToArray(); } } if (cmd != null) { return ExecuteCommand(cmd, args, directive); } else return ExecuteScript(command, args, directive); }