4141 */
4242package org .netbeans .modules .php .wordpress .commands ;
4343
44+ import java .io .File ;
45+ import java .io .FileInputStream ;
46+ import java .io .FileOutputStream ;
47+ import java .io .IOException ;
48+ import java .io .InputStreamReader ;
49+ import java .io .OutputStreamWriter ;
50+ import java .io .PrintWriter ;
4451import java .util .ArrayList ;
4552import java .util .Arrays ;
4653import java .util .Collections ;
@@ -93,6 +100,7 @@ public final class WordPressCli {
93100
94101 // XXX default?
95102 private final List <String > DEFAULT_PARAMS = Collections .emptyList ();
103+ private static final List <FrameworkCommand > commandsCache = new ArrayList <FrameworkCommand >();
96104
97105 private WordPressCli (String wpCliPath ) {
98106 this .wpCliPath = wpCliPath ;
@@ -162,18 +170,17 @@ public String getVersion() {
162170 /**
163171 * Get help.
164172 *
165- * @param phpModule
166173 * @param commands
167174 * @return help for command
168175 */
169- public String getHelp (PhpModule phpModule , List <String > commands ) {
176+ public String getHelp (List <String > commands ) {
170177 List <String > params = new ArrayList <String >(commands .size () + 1 );
171178 params .addAll (commands );
172179 params .add (HELP_PARAM );
173180
174181 HelpLineProcessor helpLineProcessor = new HelpLineProcessor ();
175182
176- Future <Integer > result = getExecutable ( phpModule )
183+ Future <Integer > result = createExecutable ( )
177184 .additionalParameters (params )
178185 .run (getSilentDescriptor (), getOutputProcessorFactory (helpLineProcessor ));
179186 try {
@@ -191,27 +198,74 @@ public String getHelp(PhpModule phpModule, List<String> commands) {
191198 /**
192199 * Get commands from help. Also get subcommands, so, take a little time.
193200 *
194- * @param phpModule
201+ * @param isForce clear cache
195202 * @return commands
196203 */
197204 @ NbBundle .Messages ("WordPressCli.commands.empty=Please check whether config file and DB settings exist." )
198- public List <FrameworkCommand > getCommands (PhpModule phpModule ) {
199- List <FrameworkCommand > commands = new ArrayList <FrameworkCommand >();
200- getCommands (phpModule , Collections .<String >emptyList (), commands );
201- if (commands .isEmpty ()) {
205+ public List <FrameworkCommand > getCommands (boolean isForce ) {
206+ if (!isForce && !commandsCache .isEmpty ()) {
207+ return commandsCache ;
208+ }
209+ commandsCache .clear ();
210+ if (!isForce ) {
211+ // exists xml?
212+ String commandList = WordPressOptions .getInstance ().getWpCliCommandList ();
213+ if (!StringUtils .isEmpty (commandList )) {
214+ try {
215+ File temp = File .createTempFile ("nb-wpcli-tmp" , ".xml" ); // NOI18N
216+ try {
217+ FileOutputStream outputStream = new FileOutputStream (temp );
218+ PrintWriter pw = new PrintWriter (new OutputStreamWriter (outputStream , "UTF-8" )); // NOI18N
219+ try {
220+ pw .println (commandList );
221+ } finally {
222+ pw .close ();
223+ }
224+
225+ // parse
226+ FileInputStream fileInputStream = new FileInputStream (temp );
227+ InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream , "UTF-8" ); // NOI18N
228+ WordPressCliCommandsXmlParser .parse (inputStreamReader , commandsCache );
229+ } finally {
230+ temp .deleteOnExit ();
231+ }
232+ } catch (IOException ex ) {
233+ Exceptions .printStackTrace (ex );
234+ }
235+ if (!commandsCache .isEmpty ()) {
236+ return commandsCache ;
237+ }
238+ }
239+ }
240+
241+ // update
242+ updateCommands ();
243+
244+ return commandsCache ;
245+ }
246+
247+ public void updateCommands () {
248+ getCommands (Collections .<String >emptyList (), commandsCache );
249+ if (commandsCache .isEmpty ()) {
202250 NotifyDescriptor .Message message = new NotifyDescriptor .Message (Bundle .WordPressCli_commands_empty (), NotifyDescriptor .WARNING_MESSAGE );
203251 DialogDisplayer .getDefault ().notify (message );
252+ } else {
253+ WordPressCliCommandListXmlBuilder builder = new WordPressCliCommandListXmlBuilder ();
254+ builder .build (commandsCache );
255+ String commadlist = builder .asText ();
256+ if (!StringUtils .isEmpty (commadlist )) {
257+ WordPressOptions .getInstance ().setWpCliCommandList (commadlist );
258+ }
204259 }
205- return commands ;
206260 }
207261
208262 // XXX get help later?
209- private void getCommands (PhpModule phpModule , List <String > subcommands , List <FrameworkCommand > commands ) {
263+ private void getCommands (List <String > subcommands , List <FrameworkCommand > commands ) {
210264 ArrayList <String > params = new ArrayList <String >(subcommands .size () + 1 );
211265 params .add (HELP_COMMAND );
212266 params .addAll (subcommands );
213267 HelpLineProcessor helpLineProcessor = new HelpLineProcessor ();
214- Future <Integer > result = getExecutable ( phpModule )
268+ Future <Integer > result = createExecutable ( )
215269 .additionalParameters (params )
216270 .run (getSilentDescriptor (), getOutputProcessorFactory (helpLineProcessor ));
217271 try {
@@ -249,11 +303,11 @@ private void getCommands(PhpModule phpModule, List<String> subcommands, List<Fra
249303 ArrayList <String > nextSubcommands = new ArrayList <String >(subcommands .size () + 1 );
250304 nextSubcommands .addAll (subcommands );
251305 nextSubcommands .add (subcommand );
252- String help = getHelp (phpModule , nextSubcommands );
306+ String help = getHelp (nextSubcommands );
253307 commands .add (new WordPressCliCommand (nextSubcommands .toArray (new String []{}), description , help )); // NOI18N
254308
255309 // recursive
256- getCommands (phpModule , nextSubcommands , commands );
310+ getCommands (nextSubcommands , commands );
257311 }
258312
259313 if (line .toLowerCase ().startsWith ("subcommands" )) { // NOI18N
0 commit comments