4545import java .util .Arrays ;
4646import java .util .Collections ;
4747import java .util .List ;
48+ import java .util .concurrent .ExecutionException ;
4849import java .util .concurrent .Future ;
4950import java .util .logging .Level ;
5051import java .util .logging .Logger ;
5152import org .netbeans .api .extexecution .ExecutionDescriptor ;
53+ import org .netbeans .api .extexecution .base .input .InputProcessor ;
54+ import org .netbeans .api .extexecution .base .input .InputProcessors ;
55+ import org .netbeans .api .extexecution .base .input .LineProcessor ;
5256import org .netbeans .modules .php .api .executable .InvalidPhpExecutableException ;
5357import org .netbeans .modules .php .api .executable .PhpExecutable ;
5458import org .netbeans .modules .php .api .executable .PhpExecutableValidator ;
5761import org .netbeans .modules .php .phpcsfixer .options .PhpCsFixerOptionsPanelController ;
5862import org .openide .filesystems .FileObject ;
5963import org .openide .filesystems .FileUtil ;
64+ import org .openide .util .Exceptions ;
6065import org .openide .util .NbBundle ;
6166import org .openide .windows .InputOutput ;
6267
@@ -72,17 +77,22 @@ public final class PhpCsFixer {
7277 public static final String DOWNLOAD_URL = "http://get.sensiolabs.org/php-cs-fixer.phar" ; // NOI18N
7378 private final String phpcsfixerPath ;
7479 private boolean isDryRun ;
80+ private boolean useSilentDescriptor ;
7581 // commands
7682 private static final String FIX_COMMAND = "fix" ; // NOI18N
7783 private static final String SELF_UPDATE_COMMAND = "self-update" ; // NOI18N
7884 //parameters
85+ private static final String VERSION_PARAM = "--version" ; // NOI18N
7986 public static final String DRY_RUN_PARAM = "--dry-run" ; // NOI18N
87+ public static final String VERBOSE_PARAM = "--verbose" ; // NOI18N
88+ public static final String DIFF_PARAM = "--diff" ; // NOI18N
8089 // 1.x
8190 public static final String CONFIG_PARAM = "--config=%s" ; // NOI18N
8291 public static final String LEVEL_PARAM = "--level=%s" ; // NOI18N
8392 public static final String FIXERS_PARAM = "--fixers=%s" ; // NOI18N
8493 // 2.x
8594 public static final String RULES_PARAM = "--rules=%s" ; // NOI18N
95+ public static final String DIFF_FORMAT_UDIFF_PARAM = "--diff-format=udiff" ; // NOI18N
8696 private static final List <String > DEFAULT_PARAMS = Arrays .asList (
8797 "--ansi" , // NOI18N
8898 "--no-interaction" ); // NOI18N
@@ -99,19 +109,32 @@ public final class PhpCsFixer {
99109 .inputOutput (InputOutput .NULL );
100110 private static final Logger LOGGER = Logger .getLogger (PhpCsFixer .class .getName ());
101111
102- public PhpCsFixer (String phpcsfixerPath ) {
112+ private PhpCsFixer (String phpcsfixerPath ) {
113+ this (phpcsfixerPath , false );
114+ }
115+
116+ private PhpCsFixer (String phpcsfixerPath , boolean useSilentDescriptor ) {
103117 this .phpcsfixerPath = phpcsfixerPath ;
104- isDryRun = false ;
118+ this .useSilentDescriptor = useSilentDescriptor ;
119+ this .isDryRun = false ;
105120 }
106121
107122 public static PhpCsFixer getDefault () throws InvalidPhpExecutableException {
108123 String phpcsfixerPath = PhpCsFixerOptions .getInstance ().getPhpCsFixerPath ();
109- String warning = validate (phpcsfixerPath );
124+ return newInstance (phpcsfixerPath );
125+ }
126+
127+ public static PhpCsFixer newInstance (String scriptPath ) throws InvalidPhpExecutableException {
128+ return newInstance (scriptPath , false );
129+ }
130+
131+ public static PhpCsFixer newInstance (String scriptPath , boolean useSilentDescriptor ) throws InvalidPhpExecutableException {
132+ String warning = validate (scriptPath );
110133 if (warning != null ) {
111134 LOGGER .log (Level .WARNING , "PHP CS Fixer path is not set." ); // NOI18N
112135 throw new InvalidPhpExecutableException (warning );
113136 }
114- return new PhpCsFixer (phpcsfixerPath );
137+ return new PhpCsFixer (scriptPath , useSilentDescriptor );
115138 }
116139
117140 @ NbBundle .Messages ("PhpCsFixer.script.label=PHP CS Fixer" )
@@ -139,6 +162,28 @@ public Future<Integer> selfUpdate(PhpModule phpModule) {
139162 return runCommand (phpModule , SELF_UPDATE_COMMAND , Bundle .PhpCsFixer_run (SELF_UPDATE_COMMAND ));
140163 }
141164
165+ /**
166+ * Get version.
167+ *
168+ * @return version
169+ */
170+ public String getVersion () {
171+ DefaultLineProcessor lineProcessor = new DefaultLineProcessor ();
172+ Future <Integer > result = getPhpExecutable (null , "version" )
173+ .additionalParameters (Arrays .asList (VERSION_PARAM ))
174+ .run (NO_OUTPUT_EXECUTION_DESCRIPTOR , getOutputProcessorFactory (lineProcessor ));
175+ try {
176+ if (result != null ) {
177+ result .get ();
178+ }
179+ } catch (InterruptedException ex ) {
180+ Thread .currentThread ().interrupt ();
181+ } catch (ExecutionException ex ) {
182+ Exceptions .printStackTrace (ex );
183+ }
184+ return lineProcessor .getResult ();
185+ }
186+
142187 private Future <Integer > runCommand (PhpModule phpModule , String command , String title ) {
143188 return runCommand (phpModule , command , title , Collections .<String >emptyList ());
144189 }
@@ -177,7 +222,9 @@ private PhpExecutable getPhpExecutable(PhpModule phpModule, String title) {
177222
178223 private ExecutionDescriptor getDescriptor (PhpModule phpModule ) {
179224 ExecutionDescriptor descriptor ;
180- if (PhpCsFixerOptions .getInstance ().showOutputWindow () || isDryRun ) {
225+ if (useSilentDescriptor ) {
226+ descriptor = NO_OUTPUT_EXECUTION_DESCRIPTOR ;
227+ } else if (PhpCsFixerOptions .getInstance ().showOutputWindow () || isDryRun ) {
181228 descriptor = DEFAULT_EXECUTION_DESCRIPTOR ;
182229 } else {
183230 descriptor = NO_OUTPUT_EXECUTION_DESCRIPTOR ;
@@ -194,4 +241,45 @@ private ExecutionDescriptor getDescriptor(PhpModule phpModule) {
194241 }
195242 return descriptor ;
196243 }
244+
245+ /**
246+ * Get InputProcessorFactory.
247+ *
248+ * @param lineProcessor
249+ * @return InputProcessorFactory
250+ */
251+ private ExecutionDescriptor .InputProcessorFactory2 getOutputProcessorFactory (final LineProcessor lineProcessor ) {
252+ return (InputProcessor defaultProcessor ) -> InputProcessors .ansiStripping (InputProcessors .bridge (lineProcessor ));
253+ }
254+
255+ private static class DefaultLineProcessor implements LineProcessor {
256+
257+ private final StringBuilder result = new StringBuilder ();
258+ private final List <String > list = new ArrayList <>();
259+
260+ @ Override
261+ public void processLine (String line ) {
262+ list .add (line );
263+ if (!list .isEmpty ()) {
264+ result .append ("\n " ); // NOI18N
265+ }
266+ result .append (line );
267+ }
268+
269+ @ Override
270+ public void reset () {
271+ }
272+
273+ @ Override
274+ public void close () {
275+ }
276+
277+ public String getResult () {
278+ return result .toString ();
279+ }
280+
281+ public List <String > asLines () {
282+ return list ;
283+ }
284+ }
197285}
0 commit comments