View Javadoc

1   /*
2    *  LICENSE
3    *
4    * "THE BEER-WARE LICENSE" (Revision 43):
5    * "Sven Strittmatter" <weltraumschaf@googlemail.com> wrote this file.
6    * As long as you retain this notice you can do whatever you want with
7    * this stuff. If we meet some day, and you think this stuff is worth it,
8    * you can buy me a non alcohol-free beer in return.
9    *
10   * Copyright (C) 2012 "Sven Strittmatter" <weltraumschaf@googlemail.com>
11   */
12  package org.jenkinsci.plugins.darcs.cmd;
13  
14  import hudson.util.ArgumentListBuilder;
15  import org.apache.commons.lang.Validate;
16  
17  /**
18   * Builder for `darcs changes` command.
19   *
20   * Examples:
21   * <pre>
22   * `darcs changes --repo=REPDIR --xml-output [--summary] [--last=N]`
23   * `darcs changes --repo=REPDIR --count`
24   * </pre>
25   *
26   * @author Sven Strittmatter <weltraumschaf@googlemail.com>
27   */
28  public class DarcsChangesBuilder extends DarcsBaseCommandBuilder implements DarcsCommandCreator {
29  
30      /**
31       * Darcs subcommand.
32       */
33      private static final String COMMAND = "changes";
34      /**
35       * Where to pull in.
36       */
37      private String repoDir = "";
38      /**
39       * Whether to create XML output.
40       */
41      private boolean xmlOutput;
42      /**
43       * Whether to print summary.
44       */
45      private boolean summary;
46      /**
47       * Whether to count patches.
48       */
49      private boolean count;
50      /**
51       * How many last changes.
52       */
53      private int last;
54  
55      /**
56       * Initializes the {@link DarcsBaseCommandBuilder#command} with {@value #COMMAND}.
57       *
58       * @param darcsExe name of Darcs executable, e.g. "darcs" or "/usr/local/bin/darcs"
59       */
60      DarcsChangesBuilder(final String darcsExe) {
61          super(darcsExe, COMMAND);
62      }
63  
64      /**
65       * Directory of repository to pull in.
66       *
67       * @param directory path to repository
68       * @return the builder itself
69       * CHECKSTYLE:OFF
70       * @throws IllegalArgumentException if location is {@code null} or empty
71       * CHECKSTYLE:ON
72       */
73      public DarcsChangesBuilder repoDir(final String directory) {
74          Validate.notEmpty(directory);
75          repoDir = directory;
76          return this;
77      }
78  
79      /**
80       * Switch XML output on.
81       *
82       * @return the builder itself
83       */
84      public DarcsChangesBuilder xmlOutput() {
85          xmlOutput = true;
86          return this;
87      }
88  
89      /**
90       * Switch summary output on.
91       *
92       * @return the builder itself
93       */
94      public DarcsChangesBuilder summary() {
95          summary = true;
96          return this;
97      }
98  
99      /**
100      * Switch patch count on.
101      *
102      * If count is on all other options except {@link #repoDir} will be ignored.
103      *
104      * @return the builder itself
105      */
106     public DarcsChangesBuilder count() {
107         count = true;
108         return this;
109     }
110 
111     /**
112      * How many last patches to show changes for.
113      *
114      * @param amount amount of last patches
115      * @return the builder itself
116      */
117     public DarcsChangesBuilder last(final int amount) {
118         if (amount < 1) {
119             throw new IllegalArgumentException("Amount must not be less than 1!");
120         }
121 
122         last = amount;
123         return this;
124     }
125 
126     @Override
127     public DarcsCommand create() {
128         final ArgumentListBuilder arguments = createArgumentList();
129 
130         if (repoDir.length() > 0) {
131             arguments.add(String.format("--repo=%s", repoDir));
132         }
133 
134         if (count) {
135             arguments.add("--count");
136             return new DarcsCommand(arguments);
137         }
138 
139         if (xmlOutput) {
140             arguments.add("--xml-output");
141         }
142 
143         if (summary) {
144             arguments.add("--summary");
145         }
146 
147         if (last > 0) {
148             arguments.add(String.format("--last=%d", last));
149         }
150 
151         return new DarcsCommand(arguments);
152     }
153 }