1+ /*******************************************************************************
2+ * Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+ *
4+ * Contributors:
5+ * Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - initial implementation
6+ ******************************************************************************/
7+
8+ package de .fraunhofer .iem .swan .assist .actions ;
9+
10+ import com .intellij .openapi .progress .ProgressIndicator ;
11+ import com .intellij .openapi .progress .ProgressManager ;
12+ import com .intellij .openapi .progress .Task ;
13+ import com .intellij .openapi .project .Project ;
14+ import com .intellij .util .messages .MessageBus ;
15+ import de .fraunhofer .iem .swan .Main ;
16+ import de .fraunhofer .iem .swan .assist .comm .SwanNotifier ;
17+ import de .fraunhofer .iem .swan .assist .data .JSONFileParser ;
18+ import de .fraunhofer .iem .swan .assist .data .MethodWrapper ;
19+ import de .fraunhofer .iem .swan .assist .util .Constants ;
20+ import org .apache .commons .io .FileUtils ;
21+ import org .jetbrains .annotations .NotNull ;
22+
23+ import java .io .File ;
24+ import java .io .IOException ;
25+ import java .time .LocalDateTime ;
26+ import java .time .format .DateTimeFormatter ;
27+ import java .util .HashMap ;
28+ import java .util .ResourceBundle ;
29+
30+ /**
31+ * Creates process to run SWAN on a separate thread.
32+ */
33+ public class RunSwanAnalysisImpl {
34+
35+ private static HashMap <String , String > parameters ;
36+ private Project project ;
37+ private long duration ;
38+
39+ /**
40+ * Initializes builder.
41+ * @param project Project on which the plugin is being used with
42+ * @param param Parameters that will be used as program arguments
43+ */
44+ RunSwanAnalysisImpl (Project project , HashMap <String , String > param ) {
45+
46+ this .project = project ;
47+ parameters = param ;
48+ }
49+
50+ /**
51+ * Sets up process to run the application and also send notification to subscribers when finished.
52+ */
53+ public void run () {
54+
55+ ResourceBundle resource = ResourceBundle .getBundle ("dialog_messages" );
56+
57+ File outputFolder = new File (parameters .get (Constants .OUTPUT_DIRECTORY ));
58+
59+ if (!outputFolder .exists ())
60+ outputFolder .mkdir ();
61+
62+ /*File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.OUTPUT_LOG));
63+ try {
64+ logFile.createNewFile();
65+ parameters.replace(Constants.OUTPUT_LOG, logFile.getPath());
66+
67+ FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
68+
69+ System.setOut(new PrintStream(fileOutputStream));
70+
71+ } catch (IOException e) {
72+ e.printStackTrace();
73+ }*/
74+
75+ ProgressManager .getInstance ().run (new Task .Backgroundable (project , resource .getString ("Messages.Title.Progress" )) {
76+ @ Override
77+ public void run (@ NotNull ProgressIndicator indicator ) {
78+
79+ long start = System .currentTimeMillis ();
80+
81+ Main .main (new String []{parameters .get (Constants .SOURCE_DIRECTORY ),
82+ parameters .get (Constants .TRAIN_DIRECTORY ),
83+ parameters .get (Constants .CONFIGURATION_FILE ),
84+ parameters .get (Constants .OUTPUT_DIRECTORY )});
85+
86+ duration = System .currentTimeMillis () - start ;
87+ }
88+
89+ @ Override
90+ public void onCancel () {
91+ super .onCancel ();
92+ }
93+
94+ @ Override
95+ public void onSuccess () {
96+ super .onSuccess ();
97+
98+ System .out .println ("FILE: " +parameters .get (Constants .OUTPUT_FILE ));
99+ //Create copy of file
100+ copyFile (new File (parameters .get (Constants .OUTPUT_FILE )));
101+
102+ JSONFileParser parser = new JSONFileParser (parameters .get (Constants .OUTPUT_FILE ));
103+ HashMap <String , MethodWrapper > exportedMethods = parser .parseJSONFileMap ();
104+
105+ HashMap <String , String > results = new HashMap <String , String >();
106+ results .put (Constants .OUTPUT_FILE , parameters .get (Constants .OUTPUT_FILE ));
107+ results .put (Constants .OUTPUT_LOG , parameters .get (Constants .OUTPUT_LOG ));
108+
109+ int m = (int ) (((duration / 1000 ) / 60 ) % 60 );
110+ int s = (int ) ((duration / 1000 ) % 60 );
111+
112+ results .put (Constants .ANALYSIS_RESULT , exportedMethods .size () + " methods found in " +m +" mins " +s + " secs" );
113+
114+ MessageBus messageBus = project .getMessageBus ();
115+ SwanNotifier publisher = messageBus .syncPublisher (SwanNotifier .END_SWAN_PROCESS_TOPIC );
116+ publisher .launchSwan (results );
117+ }
118+ });
119+ }
120+
121+ private void copyFile (File original ){
122+
123+ File copied = new File (
124+ parameters .get (Constants .OUTPUT_DIRECTORY ) + File .separator +getCurrentTimestamp ()+ "-config.json" );
125+ try {
126+ FileUtils .copyFile (original , copied );
127+ } catch (IOException e ) {
128+ e .printStackTrace ();
129+ }
130+ }
131+
132+ /**
133+ * Get the timestamp in a specified format.
134+ * @return Formatted date
135+ */
136+ private String getCurrentTimestamp () {
137+
138+ DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd-HHmmss" );
139+ return LocalDateTime .now ().format (formatter );
140+ }
141+ }
0 commit comments