66package com .magento .idea .magento2uct .execution ;
77
88import com .intellij .codeInspection .ProblemDescriptor ;
9+ import com .intellij .execution .process .ProcessAdapter ;
10+ import com .intellij .execution .process .ProcessEvent ;
911import com .intellij .execution .process .ProcessHandler ;
12+ import com .intellij .json .psi .JsonFile ;
1013import com .intellij .openapi .application .ApplicationManager ;
14+ import com .intellij .openapi .editor .Document ;
1115import com .intellij .openapi .project .Project ;
1216import com .intellij .openapi .vfs .VfsUtil ;
1317import com .intellij .openapi .vfs .VirtualFile ;
1418import com .intellij .psi .PsiDirectory ;
19+ import com .intellij .psi .PsiDocumentManager ;
1520import com .intellij .psi .PsiFile ;
1621import com .intellij .psi .PsiManager ;
1722import com .jetbrains .php .lang .psi .PhpFile ;
23+ import com .magento .idea .magento2uct .execution .output .ReportBuilder ;
24+ import com .magento .idea .magento2uct .execution .output .Summary ;
25+ import com .magento .idea .magento2uct .execution .output .UctReportOutputUtil ;
1826import com .magento .idea .magento2uct .execution .process .OutputWrapper ;
1927import com .magento .idea .magento2uct .execution .scanner .ModuleFilesScanner ;
2028import com .magento .idea .magento2uct .execution .scanner .ModuleScanner ;
@@ -51,6 +59,15 @@ public GenerateUctReportCommand(
5159 this .output = output ;
5260 this .process = process ;
5361 settingsService = UctSettingsService .getInstance (project );
62+
63+ this .process .addProcessListener (new ProcessAdapter () {
64+
65+ @ Override
66+ public void processTerminated (final @ NotNull ProcessEvent event ) {
67+ super .processTerminated (event );
68+ output .write ("\n Process finished with exit code " + event .getExitCode () + "\n " );
69+ }
70+ });
5471 }
5572
5673 /**
@@ -74,9 +91,15 @@ public void execute() {
7491 rootDirectory ,
7592 new ExcludeMagentoBundledFilter ()
7693 );
94+ final Summary summary = new Summary (
95+ settingsService .getCurrentVersion (),
96+ settingsService .getTargetVersion ()
97+ );
98+ final ReportBuilder reportBuilder = new ReportBuilder (project );
7799
78100 ApplicationManager .getApplication ().executeOnPooledThread (() -> {
79101 ApplicationManager .getApplication ().runReadAction (() -> {
102+ summary .trackProcessStarted ();
80103 for (final ComponentData componentData : scanner ) {
81104 if (process .isProcessTerminated ()) {
82105 return ;
@@ -87,6 +110,7 @@ public void execute() {
87110 if (!(psiFile instanceof PhpFile )) {
88111 continue ;
89112 }
113+ final String filename = psiFile .getVirtualFile ().getPath ();
90114 final UctProblemsHolder fileProblemsHolder = inspectionManager .run (psiFile );
91115
92116 if (fileProblemsHolder == null ) {
@@ -98,7 +122,7 @@ public void execute() {
98122 outputUtil .printModuleName (componentData .getName ());
99123 isModuleHeaderPrinted = true ;
100124 }
101- outputUtil .printProblemFile (psiFile . getVirtualFile (). getPath () );
125+ outputUtil .printProblemFile (filename );
102126 }
103127
104128 for (final ProblemDescriptor descriptor
@@ -107,15 +131,47 @@ public void execute() {
107131 descriptor
108132 );
109133 if (code != null ) {
134+ final SupportedIssue issue = SupportedIssue .getByCode (code );
135+
136+ if (issue != null ) {
137+ final String errorMessage = descriptor
138+ .getDescriptionTemplate ()
139+ .substring (6 )
140+ .trim ();
141+ summary .addToSummary (issue .getLevel ());
142+ reportBuilder .addIssue (
143+ descriptor .getLineNumber () + 1 ,
144+ filename ,
145+ errorMessage ,
146+ issue
147+ );
148+ }
110149 outputUtil .printIssue (descriptor , code );
111150 }
112151 }
113152 }
114153 }
115- if (scanner .getModuleCount () == 0 ) {
116- output .print (output .wrapInfo ("Couldn't find modules to analyse" ).concat ("\n " ));
154+ summary .trackProcessFinished ();
155+ summary .setProcessedModules (scanner .getModuleCount ());
156+ outputUtil .printSummary (summary );
157+ reportBuilder .addSummary (summary );
158+ });
159+
160+ ApplicationManager .getApplication ().invokeLaterOnWriteThread (() -> {
161+ final JsonFile report = reportBuilder .build ();
162+
163+ if (report != null ) {
164+ final PsiDocumentManager psiDocumentManager = PsiDocumentManager .getInstance (
165+ project
166+ );
167+ final Document document = psiDocumentManager .getDocument (report );
168+
169+ if (document != null ) {
170+ psiDocumentManager .commitDocument (document );
171+ }
172+ outputUtil .printReportFile (report .getVirtualFile ().getPath ());
173+ process .destroyProcess ();
117174 }
118- process .destroyProcess ();
119175 });
120176 });
121177 }
@@ -139,65 +195,4 @@ public void execute() {
139195
140196 return PsiManager .getInstance (project ).findDirectory (targetDirVirtualFile );
141197 }
142-
143- private static final class UctReportOutputUtil {
144-
145- private static final String ISSUE_FORMAT = " * {SEVERITY}[{code}] Line {line}: {message}" ;
146- private final OutputWrapper stdout ;
147-
148- /**
149- * UCT report styled output util.
150- *
151- * @param output OutputWrapper
152- */
153- public UctReportOutputUtil (final @ NotNull OutputWrapper output ) {
154- stdout = output ;
155- }
156-
157- /**
158- * Print module name header.
159- *
160- * @param moduleName String
161- */
162- public void printModuleName (final @ NotNull String moduleName ) {
163- final String moduleNameLine = "Module Name: " .concat (moduleName );
164- stdout .print ("\n \n " + stdout .wrapInfo (moduleNameLine ).concat ("\n " ));
165- stdout .print (stdout .wrapInfo ("-" .repeat (moduleNameLine .length ())).concat ("\n " ));
166- }
167-
168- /**
169- * Print problem file header.
170- *
171- * @param filePath String
172- */
173- public void printProblemFile (final @ NotNull String filePath ) {
174- final String file = "File: " .concat (filePath );
175- stdout .print ("\n " .concat (stdout .wrapInfo (file )).concat ("\n " ));
176- stdout .print (stdout .wrapInfo ("-" .repeat (file .length ())).concat ("\n \n " ));
177- }
178-
179- /**
180- * Print issue message.
181- *
182- * @param descriptor ProblemDescriptor
183- * @param code int
184- */
185- public void printIssue (final @ NotNull ProblemDescriptor descriptor , final int code ) {
186- final String errorMessage = descriptor .getDescriptionTemplate ().substring (6 ).trim ();
187- final SupportedIssue issue = SupportedIssue .getByCode (code );
188-
189- if (issue == null ) {
190- return ;
191- }
192-
193- final String output = ISSUE_FORMAT
194- .replace ("{SEVERITY}" , issue .getLevel ().getFormattedLabel ())
195- .replace ("{code}" , Integer .toString (code ))
196- .replace ("{line}" , Integer .toString (descriptor .getLineNumber () + 1 ))
197- .replace ("{message}" , errorMessage )
198- .concat ("\n " );
199-
200- stdout .print (output );
201- }
202- }
203198}
0 commit comments