1414import edu .stevens .swe .research .java .cli .analyzer .TaskManager ;
1515import edu .stevens .swe .research .java .cli .analyzer .TaskResult ;
1616import edu .stevens .swe .research .java .cli .analyzer .ResultFormatter ;
17+ import edu .stevens .swe .research .java .cli .analyzer .LogData ;
1718
18- @ Command (name = "analyzer" , mixinStandardHelpOptions = true , version = "Analyzer CLI 1.1.2 " ,
19+ @ Command (name = "analyzer" , mixinStandardHelpOptions = true , version = "Analyzer CLI 1.3.1 " ,
1920 description = "Analyzes Java source code based on specified tasks." )
2021public class Main implements Callable <Integer > {
2122
@@ -47,16 +48,25 @@ public class Main implements Callable<Integer> {
4748 private File pluginPath ;
4849
4950 private TaskManager taskManager ;
51+ private LogData logData ;
52+ private volatile boolean normalExit = false ; // Flag to track normal exit
5053
5154 public Main () {
5255 // TaskManager will be initialized in call() after ProjectCtx is created
5356 }
5457
5558 @ Override
5659 public Integer call () throws Exception {
60+ // Extract project name from directory
61+ String projectName = projectDir .getName ();
62+
63+ // Initialize logging
64+ logData = new LogData (projectName , taskName );
65+
5766 System .out .println ("Analyzer CLI starting..." );
5867 System .out .println ("Task: " + taskName );
5968 System .out .println ("Project Directory: " + projectDir .getAbsolutePath ());
69+ System .out .println ("Project Name: " + projectName );
6070 System .out .println ("Language: " + language );
6171 System .out .println ("Threads: " + (threads == 0 ? "Default (CPU Cores)" : threads ));
6272 if (configFile != null ) {
@@ -78,42 +88,88 @@ public Integer call() throws Exception {
7888 }
7989 System .out .println ("Output Directory: " + outputDir .getAbsolutePath ());
8090
81- // 1. Create ProjectCtx
82- ProjectCtx projectCtx = new ProjectCtx (projectDir .toPath (), language );
83- // Set the output directory in ProjectCtx
84- projectCtx .setOutputDirectory (outputDir .toPath ());
85- // TODO: Populate ProjectCtx further from configFile if provided
86- // For example, load source roots, classpath, specific task configs etc.
91+ // Add shutdown hook to handle interruption
92+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
93+ if (logData != null && !normalExit ) {
94+ try {
95+ logData .finish ("INTERRUPTED" , "Process was interrupted by user" );
96+ logData .writeToFile (outputDir .toPath (), projectName );
97+ } catch (Exception e ) {
98+ System .err .println ("Error writing log during shutdown: " + e .getMessage ());
99+ }
100+ }
101+ }));
102+
103+ try {
104+ // 1. Create ProjectCtx
105+ ProjectCtx projectCtx = new ProjectCtx (projectDir .toPath (), language );
106+ // Set the output directory in ProjectCtx
107+ projectCtx .setOutputDirectory (outputDir .toPath ());
108+ // Add log data to project context for tasks to access
109+ projectCtx .setLogData (logData );
110+ // TODO: Populate ProjectCtx further from configFile if provided
111+ // For example, load source roots, classpath, specific task configs etc.
112+
113+ // 2. Initialize TaskManager
114+ this .taskManager = new TaskManager (projectCtx , threads );
115+
116+ // 3. Get the specified task
117+ // AnalyzerTask task = taskManager.getTask(taskName); // This is done in TaskManager.executeTask
118+
119+ // 4. Execute the task
120+ System .out .println ("Executing task via TaskManager: " + taskName );
121+ TaskResult result = taskManager .executeTask (taskName );
122+
123+ if (result == null ) {
124+ System .err .println ("Task execution failed or returned null result for task: " + taskName );
125+ logData .finish ("FAILED" , "Task execution returned null result" );
126+ // Create a minimal result to avoid NullPointerException with formatter
127+ result = new TaskResult (projectCtx .getProjectPath ().toString (), taskName + " [Execution Failed]" );
128+ } else {
129+ logData .finish ("COMPLETED" );
130+ }
131+
132+ // 5. Format and output results
133+ ResultFormatter formatter = new ResultFormatter (outputFormat );
134+ try (OutputStream os = (outputFile != null ) ? new FileOutputStream (outputFile ) : System .out ) {
135+ formatter .format (result , os ); // Use the actual result from taskManager
136+ } catch (Exception e ) {
137+ System .err .println ("Error formatting or writing results: " + e .getMessage ());
138+ e .printStackTrace ();
139+ logData .finish ("FAILED" , "Error formatting results: " + e .getMessage ());
140+ return 1 ;
141+ }
142+
143+ // Write log file
144+ try {
145+ logData .writeToFile (outputDir .toPath (), projectName );
146+ } catch (Exception e ) {
147+ System .err .println ("Error writing analysis log: " + e .getMessage ());
148+ e .printStackTrace ();
149+ // Don't fail the entire process for log writing issues
150+ }
151+
152+ System .out .println ("Analysis finished." );
153+ normalExit = true ; // Mark as normal exit
154+ return 0 ;
87155
88- // 2. Initialize TaskManager
89- this .taskManager = new TaskManager (projectCtx , threads );
90-
91- // 3. Get the specified task
92- // AnalyzerTask task = taskManager.getTask(taskName); // This is done in TaskManager.executeTask
93-
94- // 4. Execute the task
95- System .out .println ("Executing task via TaskManager: " + taskName );
96- TaskResult result = taskManager .executeTask (taskName );
97-
98- if (result == null ) {
99- System .err .println ("Task execution failed or returned null result for task: " + taskName );
100- // Create a minimal result to avoid NullPointerException with formatter
101- result = new TaskResult (projectCtx .getProjectPath ().toString (), taskName + " [Execution Failed]" );
102- }
103-
104- // 5. Format and output results
105- ResultFormatter formatter = new ResultFormatter (outputFormat );
106- try (OutputStream os = (outputFile != null ) ? new FileOutputStream (outputFile ) : System .out ) {
107- formatter .format (result , os ); // Use the actual result from taskManager
108156 } catch (Exception e ) {
109- System .err .println ("Error formatting or writing results : " + e .getMessage ());
157+ System .err .println ("Error during analysis : " + e .getMessage ());
110158 e .printStackTrace ();
159+ if (logData != null ) {
160+ try {
161+ logData .finish ("FAILED" , "Unexpected error: " + e .getMessage ());
162+ logData .writeToFile (outputDir .toPath (), projectName );
163+ } catch (Exception logException ) {
164+ System .err .println ("Error writing error log: " + logException .getMessage ());
165+ }
166+ }
111167 return 1 ;
168+ } finally {
169+ if (taskManager != null ) {
170+ taskManager .shutdown (); // Shutdown the executor service
171+ }
112172 }
113-
114- System .out .println ("Analysis finished." );
115- taskManager .shutdown (); // Shutdown the executor service
116- return 0 ;
117173 }
118174
119175 public static void main (String [] args ) {
0 commit comments