コード例 #1
0
ファイル: DeleteVersions.cs プロジェクト: KerwinMa/revolver
    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"));
      }
    }
コード例 #2
0
ファイル: ListManipulator.cs プロジェクト: KerwinMa/revolver
    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()));
    }