1 | /** |
2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
3 | */ |
4 | package org.sourceforge.xradar.dynamics; |
5 | |
6 | import java.io.File; |
7 | import java.util.ArrayList; |
8 | import java.util.Arrays; |
9 | import java.util.List; |
10 | import java.util.logging.Logger; |
11 | |
12 | import org.sourceforge.xradar.XRadarException; |
13 | import org.sourceforge.xradar.util.FileUtils; |
14 | |
15 | /** |
16 | * <p>Command line version of XRadar to have dynamic reports</p> |
17 | * |
18 | * @author Romain PELISSE, belaran@gmail.com |
19 | * @author Zahir Nuckchady, zahir.nu@gmail.com |
20 | * |
21 | */ |
22 | public class XRadarDynamics { |
23 | |
24 | private static final Logger logger = Logger.getLogger(XRadarDynamics.class.getSimpleName()); |
25 | /** |
26 | *Command line to use XRadar |
27 | *@param args, representing the arguments passed in the command line |
28 | * @throws XRadarException |
29 | */ |
30 | public static void runDynamics(String[] args) throws XRadarException { |
31 | List<String> arguments = Arrays.asList(args); |
32 | DynamicsOptionalParameters paramters = new DynamicsOptionalParameters(); |
33 | List <String> argsWithoutOptions = removeOptional(arguments,paramters); |
34 | checkReleaseArgs(argsWithoutOptions); |
35 | List<Input> inputs = retrieveEachReleaseData(arguments); |
36 | Dynamics dynamicsEngine = new Dynamics(); |
37 | for ( Input input : inputs) { |
38 | String masterFinalReport = FileUtils.addSeparatorIfMissing(paramters.getTarget()) + input.getMasterFinal(); |
39 | // Version specific settings |
40 | dynamicsEngine.setSequenceId(String.valueOf(input.getVersionId())); |
41 | dynamicsEngine.setDaysSinceLast(String.valueOf(input.getDaysSinceLast())); |
42 | dynamicsEngine.setQualityReportPath(input.getQualityReportPath()); |
43 | dynamicsEngine.setMasterFinal(masterFinalReport); |
44 | dynamicsEngine.setDocsHome(paramters.getTarget()); |
45 | if ( XRadarDynamics.isStaticConfigured(arguments) ) |
46 | dynamicsEngine.setStaticRoot(input.getStaticsRoot()); |
47 | // Updating file from previous version to the current version |
48 | logger.info("Calling execute merge with " + input.getPreviousReport() + " and " + input.getReportToMerge()); |
49 | dynamicsEngine.executeMerge(input.getPreviousReport(), input.getReportToMerge()); |
50 | // Post processing the previousVersion |
51 | logger.info("Calling post process with " + masterFinalReport); |
52 | dynamicsEngine.postProcess(masterFinalReport); |
53 | // Adding static images |
54 | logger.info("Adding static images to website"); |
55 | dynamicsEngine.copyImagesToWebSite(); |
56 | // Generating the dynamics 'website' |
57 | logger.info("Calling dynamics style report with " + input.getMasterFinal()); |
58 | dynamicsEngine.dynamicsStyleReport(masterFinalReport, FileUtils.addSeparatorIfMissing(paramters.getTarget()) + Dynamics.EMPTY); |
59 | } |
60 | } |
61 | |
62 | |
63 | /** |
64 | * <p>Verifies if any optional arguments are present and removes them from the list of arguments passed.</p> |
65 | * |
66 | * @param args, representing the arguments passed in the command line |
67 | */ |
68 | private static List <String> removeOptional (List<String> arguments,DynamicsOptionalParameters dynamicsParams) |
69 | { |
70 | List<String> releaseArguments = new ArrayList<String>(arguments); |
71 | for(String argument :arguments){ |
72 | if ( argument.contains("dynamic") ) { |
73 | releaseArguments.remove(argument); |
74 | } |
75 | else if ( argument.contains("--debug") ) { |
76 | dynamicsParams.setDebug(true); |
77 | releaseArguments.remove(argument); |
78 | } |
79 | else if ( argument.contains("--staticsConfigured") ) { |
80 | dynamicsParams.setStaticsConfigured(true); |
81 | releaseArguments.remove(argument); |
82 | } |
83 | else |
84 | { |
85 | boolean isConfig = argument.contains("--config"); |
86 | boolean isTarget = argument.contains("--target"); |
87 | if ( isConfig || isTarget ) { |
88 | String value = arguments.get(arguments.indexOf(argument) + 1); |
89 | if ( isConfig && fileExists(value,argument) ) |
90 | dynamicsParams.setConfig(value); |
91 | if ( isTarget ) |
92 | dynamicsParams.setTarget(FileUtils.addSeparatorIfMissing(value)); |
93 | releaseArguments.remove(argument); |
94 | releaseArguments.remove(value); |
95 | } |
96 | } |
97 | } |
98 | // Checks to see if compulsory arguments are present and intialized |
99 | if ( dynamicsParams.getTarget() == null || "".equals(dynamicsParams.getTarget()) || |
100 | dynamicsParams.getConfig() == null || "".equals(dynamicsParams.getConfig())) { |
101 | throw new IllegalArgumentException("Compulsory arguments are: --target and --config missing"); |
102 | } |
103 | return releaseArguments; |
104 | } |
105 | |
106 | /** |
107 | * <p>Verifies if all the arguments necessary are presents.</p> |
108 | * |
109 | *@param args, representing the arguments passed in the command line |
110 | *@throws IllegalArgumentException if compulsory arguments are missing |
111 | */ |
112 | private static void checkReleaseArgs(List<String> arguments) { |
113 | String [] requiredArgument = new String [] {"--qualityReportPath","--daysSinceLast","--masterFinal","--previousReport","--reportToMerge"}; |
114 | for(String argument : requiredArgument){ |
115 | // Test to see if the 1st argument is the --release command |
116 | if( ! arguments.contains(argument)){ |
117 | throw new IllegalArgumentException(argument + " is required."); |
118 | } |
119 | } |
120 | |
121 | if (! (arguments.get(0).startsWith("--release")) ){ |
122 | throw new IllegalArgumentException("It seems the --release option is missing, find this " +arguments.get(0) + " instead."); |
123 | } |
124 | } |
125 | |
126 | private static boolean isStaticConfigured(List<String> arguments) { |
127 | boolean status = false; |
128 | final String STATIC_ROOT_ARGS = "--staticsRoot"; |
129 | if ( arguments.contains(STATIC_ROOT_ARGS) ) { |
130 | // Ok arg is present, but does it have a value ? |
131 | int argValueId = arguments.indexOf(STATIC_ROOT_ARGS) + 1; |
132 | if ( argValueId > arguments.size() ) |
133 | throw new IllegalArgumentException(STATIC_ROOT_ARGS + " requires a value !"); |
134 | String value = arguments.get(argValueId); |
135 | if ( value == null || "".equals(value) || ! new File(value).exists() && ! new File(value).isDirectory() ) |
136 | throw new IllegalArgumentException(STATIC_ROOT_ARGS + " value must be a valid directory."); |
137 | } |
138 | return status; |
139 | } |
140 | |
141 | /** |
142 | *Verifies if the file exists |
143 | *@param filename and Command |
144 | */ |
145 | private static boolean fileExists (String filename, String command) { |
146 | if ( ! FileUtils.exists(filename) ) { |
147 | throw new IllegalArgumentException("File for " + command + " is missing or does not exist"); |
148 | } |
149 | return true; |
150 | } |
151 | |
152 | /** |
153 | *Verifies if the first element is the release command and initializes the release arguments |
154 | *@param args, representing the arguments passed in the command line |
155 | * @throws XRadarException |
156 | */ |
157 | private static List<Input> retrieveEachReleaseData(List<String> arguments) throws XRadarException |
158 | { |
159 | List<Input> inputs = new ArrayList<Input>(); |
160 | int firstReleaseId; |
161 | int nextReleaseId; |
162 | int idRelease = 1; |
163 | |
164 | do |
165 | { |
166 | firstReleaseId = arguments.indexOf("--release" + idRelease); |
167 | nextReleaseId = arguments.indexOf("--release" + (idRelease + 1) ) ; |
168 | if ( firstReleaseId != -1 ) |
169 | { |
170 | if ( nextReleaseId == -1 ) |
171 | nextReleaseId = arguments.size() - 1; |
172 | List<String> releaseSubList = arguments.subList(firstReleaseId + 1, nextReleaseId); |
173 | Input dynamicsReleaseParams = new Input(); |
174 | dynamicsReleaseParams.setVersionId(idRelease); |
175 | for ( String argument : releaseSubList ) |
176 | { |
177 | int idNumber = releaseSubList.indexOf(argument); |
178 | int idValue = idNumber + 1; |
179 | if ( idValue < releaseSubList.size() ) |
180 | { |
181 | if (argument.contains ("--daysSinceLast")) |
182 | dynamicsReleaseParams.setDaysSinceLast(Integer.parseInt(releaseSubList.get(idValue))); |
183 | else { |
184 | String value = releaseSubList.get(idValue); |
185 | if (argument.contains ("--previousReport") ) // File may not exists at this point, so no way to check. |
186 | dynamicsReleaseParams.setPreviousReport(value); |
187 | else if (argument.contains ("--reportToMerge") && fileExists(value, argument)) |
188 | dynamicsReleaseParams.setReportToMerge(value); |
189 | else if (argument.contains ("--qualityReportPath") && fileExists(value, argument) ) |
190 | dynamicsReleaseParams.setQualityReportPath(FileUtils.addSeparatorIfMissing(value)); |
191 | else if (argument.contains ("--staticsRoot") && fileExists(value, argument)) |
192 | dynamicsReleaseParams.setStaticsRoot(value); |
193 | else if (argument.contains ("--masterFinal") ) |
194 | dynamicsReleaseParams.setMasterFinal(value); |
195 | } |
196 | } |
197 | } |
198 | |
199 | if ( dynamicsReleaseParams.getPreviousReport() == null || |
200 | "".equals(dynamicsReleaseParams.getPreviousReport() ) ) { |
201 | if ( dynamicsReleaseParams.getVersionId() == 1 ) |
202 | dynamicsReleaseParams.setPreviousReport(Dynamics.DEFAULT_BASE_MASTER_REPORT); |
203 | else |
204 | throw new XRadarException("Previous report is required for any release but the first one"); |
205 | } |
206 | inputs.add(dynamicsReleaseParams); |
207 | idRelease++; |
208 | } |
209 | } |
210 | while ( firstReleaseId != -1 ) ; |
211 | return inputs; |
212 | } |
213 | } |