|
1 | 1 | package Designite; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | -import java.io.FileNotFoundException; |
5 | | -import java.io.IOException; |
6 | | -import Designite.SourceModel.*; |
| 3 | +import Designite.ArgumentParser.ArgumentParser; |
| 4 | +import Designite.ArgumentParser.CLIArgumentParser; |
| 5 | +import Designite.ArgumentParser.InputArgs; |
| 6 | +import Designite.ArgumentParser.RegularArgumentParser; |
| 7 | +import Designite.SourceModel.SM_Project; |
7 | 8 | import Designite.utils.Constants; |
8 | | -import Designite.utils.Logger; |
| 9 | +import Designite.utils.DJLogger; |
9 | 10 |
|
| 11 | +import java.io.FileNotFoundException; |
10 | 12 | import java.io.PrintWriter; |
11 | | -import java.nio.file.Files; |
12 | | -import java.nio.file.LinkOption; |
13 | | -import java.nio.file.Path; |
14 | 13 | import java.text.SimpleDateFormat; |
15 | 14 | import java.util.Calendar; |
16 | | -import org.apache.commons.cli.*; |
17 | 15 |
|
18 | 16 | /** |
19 | | - * |
20 | 17 | * This is the start of the project |
21 | 18 | */ |
22 | 19 | public class Designite { |
23 | | - public static void main(String[] args) throws IOException { |
24 | | - InputArgs argsObj = parseArguments(args); |
25 | | - SM_Project project = new SM_Project(argsObj); |
26 | | - Logger.logFile = getlogFileName(argsObj); |
27 | | - //TODO: log the version number |
28 | | - project.parse(); |
29 | | - project.resolve(); |
30 | | - project.computeMetrics(); |
31 | | - project.detectCodeSmells(); |
32 | | - if (Constants.DEBUG) |
33 | | - writeDebugLog(argsObj, project); |
34 | | - Logger.log("Done."); |
35 | | - } |
36 | | - |
37 | | - private static void writeDebugLog(InputArgs argsObj, SM_Project project) { |
38 | | - PrintWriter writer = getDebugLogStream(argsObj); |
39 | | - project.printDebugLog(writer); |
40 | | - if (writer != null) |
41 | | - writer.close(); |
42 | | - } |
43 | | - |
44 | | - private static InputArgs parseArguments(String[] args) { |
45 | | - Options argOptions = new Options(); |
46 | | - |
47 | | - Option input = new Option("i", "Input", true, "Input source folder path"); |
48 | | - input.setRequired(true); |
49 | | - argOptions.addOption(input); |
50 | | - |
51 | | - Option output = new Option("o", "Output", true, "Path to the output folder"); |
52 | | - output.setRequired(true); |
53 | | - argOptions.addOption(output); |
54 | | - |
55 | | - CommandLineParser parser = new DefaultParser(); |
56 | | - HelpFormatter formatter = new HelpFormatter(); |
57 | | - CommandLine cmd = null; |
58 | | - |
59 | | - try { |
60 | | - cmd = parser.parse(argOptions, args); |
61 | | - } catch (ParseException e) { |
62 | | - System.out.println(e.getMessage()); |
63 | | - formatter.printHelp("Designite", argOptions); |
64 | | - Logger.log("Quitting.."); |
65 | | - System.exit(1); |
66 | | - } |
67 | | - if(cmd==null) |
68 | | - { |
69 | | - System.out.println("Couldn't parse the command line arguments."); |
70 | | - formatter.printHelp("Designite", argOptions); |
71 | | - Logger.log("Quitting.."); |
72 | | - System.exit(2); |
| 20 | + public static void main(String[] args) throws FileNotFoundException { |
| 21 | + ArgumentParser argumentParser = (Constants.DEBUG) ? new RegularArgumentParser() : new CLIArgumentParser(); |
| 22 | + InputArgs argsObj = argumentParser.parseArguments(args); |
| 23 | + DJLogger.getInstance().setOutputDirectory(argsObj.getOutputFolder()); |
| 24 | + SM_Project project = new SM_Project(argsObj); |
| 25 | + project.parse(); |
| 26 | + project.resolve(); |
| 27 | + project.computeMetrics(); |
| 28 | + project.detectCodeSmells(); |
| 29 | + writeDebugLog(argsObj, project); |
| 30 | + DJLogger.log("Done."); |
| 31 | + } |
| 32 | + |
| 33 | + private static void writeDebugLog(InputArgs argsObj, SM_Project project) { |
| 34 | + if (Constants.DEBUG) { |
| 35 | + PrintWriter writer = getDebugLogStream(argsObj); |
| 36 | + project.printDebugLog(writer); |
| 37 | + if (writer != null) writer.close(); |
73 | 38 | } |
74 | | - |
75 | | - String inputFolderPath = cmd.getOptionValue("Input"); |
76 | | - String outputFolderPath = cmd.getOptionValue("Output"); |
77 | | - |
78 | | - InputArgs inputArgs= null; |
79 | | - try |
80 | | - { |
81 | | - inputArgs = new InputArgs(inputFolderPath, outputFolderPath); |
| 39 | + } |
| 40 | + |
| 41 | + private static PrintWriter getDebugLogStream(InputArgs argsObj) { |
| 42 | + PrintWriter writer = null; |
| 43 | + if (!argsObj.getOutputFolder().equals("")) { |
| 44 | + String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(Calendar.getInstance().getTime()); |
| 45 | + String filename = argsObj.getOutputFolder() + "DesigniteDebugLog" + timeStamp + ".txt"; |
| 46 | + try { |
| 47 | + writer = new PrintWriter(filename); |
| 48 | + } catch (FileNotFoundException ex) { |
| 49 | + DJLogger.log(ex.getMessage()); |
| 50 | + } |
82 | 51 | } |
83 | | - catch(IllegalArgumentException ex) |
84 | | - { |
85 | | - Logger.log(ex.getMessage()); |
86 | | - Logger.log("Quitting.."); |
87 | | - System.exit(3); |
88 | | - } |
89 | | - return inputArgs; |
90 | | - } |
91 | | - |
92 | | - private static String getlogFileName(InputArgs argsObj) { |
93 | | - String file = null; |
94 | | - String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(Calendar.getInstance().getTime()); |
95 | | - file = argsObj.getOutputFolder() + File.separator + "DesigniteLog" + timeStamp + ".txt"; |
96 | | - ensureOutputFolderExists(argsObj.getOutputFolder()); |
97 | | - return file; |
98 | | - } |
99 | | - |
100 | | - private static void ensureOutputFolderExists(String outputFolder) { |
101 | | - if (outputFolder == null) |
102 | | - return; |
103 | | - File folder = new File(outputFolder); |
104 | | - |
105 | | - if (folder.exists() && folder.isDirectory()) |
106 | | - return; |
107 | | - |
108 | | - try |
109 | | - { |
110 | | - boolean isCreated = folder.mkdirs(); |
111 | | - if (!isCreated) |
112 | | - { |
113 | | - System.out.println("Couldn't create output folder."); |
114 | | - } |
115 | | - } |
116 | | - catch (Exception ex) |
117 | | - { |
118 | | - System.out.println("Couldn't create output folder. " + ex.getMessage()); |
119 | | - } |
120 | | - } |
121 | | - private static PrintWriter getDebugLogStream(InputArgs argsObj) { |
122 | | - PrintWriter writer = null; |
123 | | - if (!argsObj.getOutputFolder().equals("")) { |
124 | | - String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(Calendar.getInstance().getTime()); |
125 | | - String filename = argsObj.getOutputFolder() + "DesigniteDebugLog" + timeStamp + ".txt"; |
126 | | - try { |
127 | | - writer = new PrintWriter(filename); |
128 | | - } catch (FileNotFoundException ex) { |
129 | | - Logger.log(ex.getMessage()); |
130 | | - } |
131 | | - } |
132 | | - return writer; |
133 | | - } |
| 52 | + return writer; |
| 53 | + } |
134 | 54 | } |
0 commit comments