5454import java .util .List ;
5555import java .util .concurrent .ExecutionException ;
5656import java .util .concurrent .Future ;
57+ import static java .util .logging .Level .FINE ;
58+ import static java .util .logging .Level .INFO ;
5759import java .util .logging .Logger ;
60+ import static java .util .logging .Level .WARNING ;
61+ import java .util .regex .Pattern ;
5862import org .netbeans .api .extexecution .ExecutionDescriptor ;
5963import org .netbeans .api .extexecution .base .input .InputProcessor ;
6064import org .netbeans .api .extexecution .base .input .InputProcessors ;
6165import org .netbeans .api .extexecution .base .input .LineProcessor ;
66+ import org .netbeans .api .progress .ProgressHandle ;
6267import org .netbeans .modules .php .api .executable .InvalidPhpExecutableException ;
6368import org .netbeans .modules .php .api .executable .PhpExecutable ;
6469import org .netbeans .modules .php .api .executable .PhpExecutableValidator ;
@@ -109,18 +114,24 @@ public final class WordPressCli {
109114
110115 // XXX default?
111116 private final List <String > DEFAULT_PARAMS = Collections .emptyList ();
112- private static final List <FrameworkCommand > commandsCache = new ArrayList <>();
117+ private static final List <FrameworkCommand > COMMANDS_CACHE = new ArrayList <>();
118+
119+ private static final Pattern WHITESPACES_PATTERN = Pattern .compile (" +" ); // NOI18N
113120
114121 private WordPressCli (String wpCliPath ) {
115122 this .wpCliPath = wpCliPath ;
116123 }
117124
125+ public static WordPressCli getDefault (boolean warn ) throws InvalidPhpExecutableException {
126+ String wpCliPath = WordPressOptions .getInstance ().getWpCliPath ();
127+ return newInstance (wpCliPath , warn );
128+ }
129+
118130 @ NbBundle .Messages ({
119131 "# {0} - error message" ,
120132 "WordPressCli.invalid.wpcli.script=<html>wp-cli is not valid.<br>({0})"
121133 })
122- public static WordPressCli getDefault (boolean warn ) throws InvalidPhpExecutableException {
123- String wpCliPath = WordPressOptions .getInstance ().getWpCliPath ();
134+ public static WordPressCli newInstance (String wpCliPath , boolean warn ) throws InvalidPhpExecutableException {
124135 String error = validate (wpCliPath );
125136 if (error == null ) {
126137 return new WordPressCli (wpCliPath );
@@ -229,7 +240,7 @@ public String getVersion() {
229240 } catch (InterruptedException ex ) {
230241 Thread .currentThread ().interrupt ();
231242 } catch (ExecutionException ex ) {
232- Exceptions . printStackTrace ( ex );
243+ LOGGER . log ( WARNING , null , ex );
233244 }
234245 return helpLineProcessor .getHelp ();
235246 }
@@ -318,10 +329,10 @@ public String getHelp(List<String> commands) {
318329 */
319330 @ NbBundle .Messages ("WordPressCli.commands.empty=Please check whether config file and DB settings exist." )
320331 public List <FrameworkCommand > getCommands (boolean isForce ) {
321- if (!isForce && !commandsCache .isEmpty ()) {
322- return commandsCache ;
332+ if (!isForce && !COMMANDS_CACHE .isEmpty ()) {
333+ return COMMANDS_CACHE ;
323334 }
324- commandsCache .clear ();
335+ COMMANDS_CACHE .clear ();
325336 if (!isForce ) {
326337 // exists xml?
327338 String commandList = WordPressOptions .getInstance ().getWpCliCommandList ();
@@ -330,53 +341,61 @@ public List<FrameworkCommand> getCommands(boolean isForce) {
330341 File temp = File .createTempFile ("nb-wpcli-tmp" , ".xml" ); // NOI18N
331342 try {
332343 FileOutputStream outputStream = new FileOutputStream (temp );
333- PrintWriter pw = new PrintWriter (new OutputStreamWriter (outputStream , "UTF-8" )); // NOI18N
334- try {
344+ try (PrintWriter pw = new PrintWriter (new OutputStreamWriter (outputStream , "UTF-8" ))) { // NOI18N
335345 pw .println (commandList );
336- } finally {
337- pw .close ();
338346 }
339347
340348 // parse
341349 FileInputStream fileInputStream = new FileInputStream (temp );
342350 InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream , "UTF-8" ); // NOI18N
343- WordPressCliCommandsXmlParser .parse (inputStreamReader , commandsCache );
351+ WordPressCliCommandsXmlParser .parse (inputStreamReader , COMMANDS_CACHE );
344352 } finally {
345353 temp .deleteOnExit ();
346354 }
347355 } catch (IOException ex ) {
348- Exceptions . printStackTrace ( ex );
356+ LOGGER . log ( WARNING , null , ex );
349357 }
350- if (!commandsCache .isEmpty ()) {
351- return commandsCache ;
358+ if (!COMMANDS_CACHE .isEmpty ()) {
359+ return COMMANDS_CACHE ;
352360 }
353361 }
354362 }
355363
356364 // update
357365 updateCommands ();
358366
359- return commandsCache ;
367+ return COMMANDS_CACHE ;
360368 }
361369
370+ @ NbBundle .Messages ("WordPressCli.update.command.progress=Updating wp-cli commands..." )
362371 public void updateCommands () {
363- commandsCache .clear ();
364- getCommands (Collections .<String >emptyList (), commandsCache );
365- if (commandsCache .isEmpty ()) {
366- NotifyDescriptor .Message message = new NotifyDescriptor .Message (Bundle .WordPressCli_commands_empty (), NotifyDescriptor .WARNING_MESSAGE );
367- DialogDisplayer .getDefault ().notify (message );
368- } else {
369- WordPressCliCommandListXmlBuilder builder = new WordPressCliCommandListXmlBuilder ();
370- builder .build (commandsCache );
371- String commadlist = builder .asText ();
372- if (!StringUtils .isEmpty (commadlist )) {
373- WordPressOptions .getInstance ().setWpCliCommandList (commadlist );
372+ ProgressHandle handle = ProgressHandle .createHandle (Bundle .WordPressCli_update_command_progress ());
373+ try {
374+ handle .start ();
375+ long startTime = System .currentTimeMillis ();
376+ COMMANDS_CACHE .clear ();
377+ getCommands (Collections .<String >emptyList (), COMMANDS_CACHE , handle );
378+ if (COMMANDS_CACHE .isEmpty ()) {
379+ NotifyDescriptor .Message message = new NotifyDescriptor .Message (Bundle .WordPressCli_commands_empty (), NotifyDescriptor .WARNING_MESSAGE );
380+ DialogDisplayer .getDefault ().notify (message );
381+ } else {
382+ WordPressCliCommandListXmlBuilder builder = new WordPressCliCommandListXmlBuilder ();
383+ builder .build (COMMANDS_CACHE );
384+ String commadlist = builder .asText ();
385+ if (!StringUtils .isEmpty (commadlist )) {
386+ WordPressOptions .getInstance ().setWpCliCommandList (commadlist );
387+ }
374388 }
389+ long endTime = System .currentTimeMillis ();
390+ LOGGER .log (INFO , "Update Commands: took {0}ms" , endTime - startTime ); // NOI18N
391+ LOGGER .log (INFO , "{0} wp-cli commands." , COMMANDS_CACHE .size ()); // NOI18N
392+ } finally {
393+ handle .finish ();
375394 }
376395 }
377396
378397 // XXX get help later?
379- private void getCommands (List <String > subcommands , List <FrameworkCommand > commands ) {
398+ private void getCommands (List <String > subcommands , List <FrameworkCommand > commands , ProgressHandle handle ) {
380399 ArrayList <String > params = new ArrayList <>(subcommands .size () + 1 );
381400 params .add (HELP_COMMAND );
382401 params .addAll (subcommands );
@@ -389,14 +408,18 @@ private void getCommands(List<String> subcommands, List<FrameworkCommand> comman
389408 result .get ();
390409 }
391410 } catch (InterruptedException ex ) {
392- Exceptions . printStackTrace ( ex );
411+ Thread . currentThread (). interrupt ( );
393412 } catch (ExecutionException ex ) {
394- Exceptions . printStackTrace ( ex );
413+ LOGGER . log ( WARNING , null , ex );
395414 }
396415 List <String > lines = helpLineProcessor .asLines ();
397416
398417 boolean isSubcommands = false ;
399418 boolean isFirstEmpty = false ;
419+ LOGGER .log (FINE , "{0} WP Command" , StringUtils .implode (subcommands , " " )); // NOI18N
420+ if (handle != null ) {
421+ handle .progress (StringUtils .implode (subcommands , " " )); // NOI18N
422+ }
400423 for (String line : lines ) {
401424 if (isSubcommands ) {
402425 if (StringUtils .isEmpty (line )) {
@@ -406,8 +429,13 @@ private void getCommands(List<String> subcommands, List<FrameworkCommand> comman
406429 isFirstEmpty = true ;
407430 continue ;
408431 }
432+ // XXX just ignore
433+ // in the case of long description
434+ if (line .startsWith (" " )) { // NOI18N
435+ continue ;
436+ }
409437 line = line .trim ();
410- line = line .replaceAll (" +" , " " ); // NOI18N
438+ line = WHITESPACES_PATTERN . matcher ( line ) .replaceAll (" " ); // NOI18N
411439 int indexOf = line .indexOf (" " ); // NOI18N
412440 if (indexOf == -1 ) {
413441 continue ;
@@ -422,8 +450,8 @@ private void getCommands(List<String> subcommands, List<FrameworkCommand> comman
422450 String help = getHelp (nextSubcommands );
423451 commands .add (new WordPressCliCommand (nextSubcommands .toArray (new String []{}), description , help )); // NOI18N
424452
425- // recursive
426- getCommands (nextSubcommands , commands );
453+ // get commands recursively
454+ getCommands (nextSubcommands , commands , handle );
427455 }
428456
429457 if (line .toLowerCase ().startsWith ("subcommands" )) { // NOI18N
@@ -537,12 +565,7 @@ private List<String> getAllParameters(List<String> parameters) {
537565 * @return InputProcessorFactory
538566 */
539567 private ExecutionDescriptor .InputProcessorFactory2 getOutputProcessorFactory (final LineProcessor lineProcessor ) {
540- return new ExecutionDescriptor .InputProcessorFactory2 () {
541- @ Override
542- public InputProcessor newInputProcessor (InputProcessor defaultProcessor ) {
543- return InputProcessors .ansiStripping (InputProcessors .bridge (lineProcessor ));
544- }
545- };
568+ return (InputProcessor defaultProcessor ) -> InputProcessors .ansiStripping (InputProcessors .bridge (lineProcessor ));
546569 }
547570
548571 @ NbBundle .Messages ({
0 commit comments