Skip to content

Commit 0fd7a1a

Browse files
committed
Add wp-cli option to NewConfigurationPanel #16
1 parent 538f1d2 commit 0fd7a1a

7 files changed

Lines changed: 107 additions & 10 deletions

File tree

src/org/netbeans/modules/php/wordpress/WordPressPhpModuleExtender.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@
5252
import java.net.URL;
5353
import java.net.URLConnection;
5454
import java.util.ArrayList;
55+
import java.util.Collections;
5556
import java.util.HashMap;
5657
import java.util.HashSet;
5758
import java.util.List;
5859
import java.util.Locale;
5960
import java.util.Map;
6061
import java.util.Set;
62+
import java.util.concurrent.ExecutionException;
63+
import java.util.concurrent.Future;
6164
import java.util.logging.Level;
6265
import java.util.logging.Logger;
6366
import java.util.regex.Matcher;
@@ -66,10 +69,13 @@
6669
import javax.net.ssl.HttpsURLConnection;
6770
import javax.swing.JComponent;
6871
import javax.swing.event.ChangeListener;
72+
import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
6973
import org.netbeans.modules.php.api.phpmodule.PhpModule;
7074
import org.netbeans.modules.php.api.util.FileUtils;
7175
import org.netbeans.modules.php.api.util.FileUtils.ZipEntryFilter;
76+
import org.netbeans.modules.php.api.util.StringUtils;
7277
import org.netbeans.modules.php.spi.framework.PhpModuleExtender;
78+
import org.netbeans.modules.php.wordpress.commands.WordPressCli;
7379
import org.netbeans.modules.php.wordpress.preferences.WordPressPreferences;
7480
import org.netbeans.modules.php.wordpress.ui.options.WordPressOptions;
7581
import org.netbeans.modules.php.wordpress.ui.wizards.NewProjectConfigurationPanel;
@@ -187,7 +193,6 @@ public Set<FileObject> extend(PhpModule pm) throws ExtendingException {
187193
panel.setAllEnabled(panel.getWpConfigPanel(), false);
188194

189195
FileObject sourceDirectory = pm.getSourceDirectory();
190-
Set<FileObject> files = new HashSet<FileObject>();
191196
if (panel.useUrl()) {
192197
// url
193198
String urlPath = panel.getUrlLabel();
@@ -197,26 +202,55 @@ public Set<FileObject> extend(PhpModule pm) throws ExtendingException {
197202
} catch (MalformedURLException ex) {
198203
Exceptions.printStackTrace(ex);
199204
} catch (IOException ex) {
200-
Exceptions.printStackTrace(ex);
205+
throw new ExtendingException(ex.getLocalizedMessage());
201206
}
202207
} else if (panel.useLocalFile()) {
203208
// local file
204209
String path = panel.getLocalFileLabel();
205210
try {
206211
FileUtils.unzip(path, FileUtil.toFile(sourceDirectory), new ZipEntryFilterImpl());
207212
} catch (IOException ex) {
208-
Exceptions.printStackTrace(ex);
213+
throw new ExtendingException(ex.getLocalizedMessage());
214+
}
215+
216+
} else if (panel.useWpCli()) {
217+
try {
218+
// params
219+
ArrayList<String> params = new ArrayList<String>(2);
220+
WordPressOptions options = WordPressOptions.getInstance();
221+
String locale = options.getWpCliDownloadLocale();
222+
if (!StringUtils.isEmpty(locale)) {
223+
params.add(String.format(WordPressCli.LOCALE_PARAM, locale));
224+
}
225+
String version = options.getWpCliDownloadVersion();
226+
if (!StringUtils.isEmpty(version)) {
227+
params.add(String.format(WordPressCli.VERSION_PARAM, version));
228+
}
229+
230+
// run
231+
WordPressCli wpCli = WordPressCli.getDefault(false);
232+
Future<Integer> result = wpCli.download(pm, params);
233+
if (result != null) {
234+
result.get();
235+
}
236+
} catch (InvalidPhpExecutableException ex) {
237+
throw new ExtendingException(ex.getLocalizedMessage());
238+
} catch (InterruptedException ex) {
239+
Thread.currentThread().interrupt();
240+
} catch (ExecutionException ex) {
241+
throw new ExtendingException(ex.getLocalizedMessage());
209242
}
210243
} else {
211244
// do nothing
212-
return files;
245+
return Collections.emptySet();
213246
}
214247

215248
// set format
216249
if (panel.useFormatOption()) {
217250
WordPressPreferences.setWordPressFormat(pm);
218251
}
219252

253+
Set<FileObject> files = new HashSet<FileObject>();
220254
if (sourceDirectory != null) {
221255
// create wp-config.php
222256
if (panel.isSelectedCreateConfig()) {

src/org/netbeans/modules/php/wordpress/commands/WordPressCli.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public final class WordPressCli {
8888

8989
// params
9090
private static final String HELP_PARAM = "--help"; // NOI18N
91+
public static final String LOCALE_PARAM = "--locale=%s"; // NOI18N
92+
public static final String VERSION_PARAM = "--version=%s"; // NOI18N
9193

9294
// XXX default?
9395
private final List<String> DEFAULT_PARAMS = Collections.emptyList();
@@ -127,12 +129,12 @@ public static String validate(String path) {
127129
* @param phpModule
128130
* @param options (--locale, --version)
129131
*/
130-
public void download(PhpModule phpModule, List<String> options) {
132+
public Future<Integer> download(PhpModule phpModule, List<String> options) {
131133
ArrayList<String> allCommands = new ArrayList<String>(options.size() + 1);
132134
allCommands.add(CORE_COMMAND);
133135
allCommands.add(DOWNLOAD_COMMAND);
134136
allCommands.addAll(options);
135-
runCommand(phpModule, allCommands, null);
137+
return runCommand(phpModule, allCommands);
136138
}
137139

138140
/**
@@ -277,6 +279,23 @@ public void runCommand(PhpModule phpModule, List<String> parameters, Runnable po
277279
.run(getExecutionDescriptor(postExecution));
278280
}
279281

282+
/**
283+
* Run Command.
284+
*
285+
* @param phpModule
286+
* @param parameters
287+
* @param postExecution
288+
*/
289+
public Future<Integer> runCommand(PhpModule phpModule, List<String> parameters) {
290+
PhpExecutable executable = getExecutable(phpModule);
291+
if (executable == null) {
292+
return null;
293+
}
294+
return executable.displayName(getDisplayName(phpModule, parameters.get(0)))
295+
.additionalParameters(getAllParameters(parameters))
296+
.run(getExecutionDescriptor(null));
297+
}
298+
280299
/**
281300
* Get PhpExecutable. Run php wp/wp-cli.phar [command]. Working directory is
282301
* source directory.

src/org/netbeans/modules/php/wordpress/ui/options/WordPressOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void setWpCliPath(String path) {
9595
getPreferences().put(WP_CLI_PATH, path);
9696
}
9797

98-
public String getWpCliDownlaodLocale() {
98+
public String getWpCliDownloadLocale() {
9999
return getPreferences().get(WP_CLI_DOWNLOAD_LOCALE, ""); // NOI18N
100100
}
101101

src/org/netbeans/modules/php/wordpress/ui/options/WordPressOptionsPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ void load() {
473473
setUrl(getOptions().getDownloadUrl());
474474
setLocalPath(getOptions().getLocalFilePath());
475475
setWpCliPath(getOptions().getWpCliPath());
476-
setWpCliDownloadLocale(getOptions().getWpCliDownlaodLocale());
477-
setWpCliDownloadLocale(getOptions().getWpCliDownloadVersion());
476+
setWpCliDownloadLocale(getOptions().getWpCliDownloadLocale());
477+
setWpCliDownloadVersion(getOptions().getWpCliDownloadVersion());
478478
setWpCliVersoin();
479479
}
480480

src/org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ CreateUnderscoresThemePanel.themeNameLabel.text=Theme Name:
3333
CreateUnderscoresThemePanel.authorLabel.text=Author:
3434
CreateThemePanel.themeNameLabel.text=Theme Name:
3535
CreateThemePanel.themeNameTextField.text=
36+
NewProjectConfigurationPanel.useWpCliRadioButton.text=Use wp-cli

src/org/netbeans/modules/php/wordpress/ui/wizards/NewProjectConfigurationPanel.form

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<Group type="103" groupAlignment="0" attributes="0">
4444
<Component id="formatCheckBox" alignment="0" min="-2" max="-2" attributes="0"/>
4545
<Component id="createConfigCheckBox" alignment="0" min="-2" max="-2" attributes="0"/>
46+
<Component id="useWpCliRadioButton" alignment="0" min="-2" max="-2" attributes="0"/>
4647
</Group>
4748
<EmptySpace min="0" pref="223" max="32767" attributes="0"/>
4849
</Group>
@@ -72,6 +73,8 @@
7273
<Component id="localFileLabel" alignment="3" min="-2" max="-2" attributes="0"/>
7374
</Group>
7475
<EmptySpace max="-2" attributes="0"/>
76+
<Component id="useWpCliRadioButton" min="-2" max="-2" attributes="0"/>
77+
<EmptySpace max="-2" attributes="0"/>
7578
<Component id="formatCheckBox" min="-2" max="-2" attributes="0"/>
7679
<EmptySpace max="-2" attributes="0"/>
7780
<Component id="createConfigCheckBox" min="-2" max="-2" attributes="0"/>
@@ -123,6 +126,16 @@
123126
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="useLocalFileRadioButtonActionPerformed"/>
124127
</Events>
125128
</Component>
129+
<Component class="javax.swing.JRadioButton" name="useWpCliRadioButton">
130+
<Properties>
131+
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
132+
<ComponentRef name="buttonGroup1"/>
133+
</Property>
134+
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
135+
<ResourceString bundle="org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties" key="NewProjectConfigurationPanel.useWpCliRadioButton.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
136+
</Property>
137+
</Properties>
138+
</Component>
126139
<Component class="javax.swing.JLabel" name="urlLabel">
127140
<Properties>
128141
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">

src/org/netbeans/modules/php/wordpress/ui/wizards/NewProjectConfigurationPanel.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import java.awt.Component;
4545
import javax.swing.JPanel;
4646
import javax.swing.JTextField;
47+
import org.netbeans.modules.php.api.util.StringUtils;
48+
import org.netbeans.modules.php.wordpress.ui.options.WordPressOptions;
4749
import org.netbeans.modules.php.wordpress.util.NetUtils;
4850

4951
/**
@@ -60,6 +62,21 @@ public class NewProjectConfigurationPanel extends javax.swing.JPanel {
6062
public NewProjectConfigurationPanel() {
6163
initComponents();
6264
useURLRadioButton.setSelected(true);
65+
init();
66+
}
67+
68+
private void init() {
69+
WordPressOptions options = WordPressOptions.getInstance();
70+
if (StringUtils.isEmpty(options.getWpCliPath())) {
71+
useWpCliRadioButton.setEnabled(false);
72+
useWpCliRadioButton.setVisible(false);
73+
}
74+
75+
if (StringUtils.isEmpty(options.getLocalFilePath())) {
76+
useLocalFileRadioButton.setEnabled(false);
77+
useLocalFileRadioButton.setVisible(false);
78+
}
79+
6380
}
6481

6582
public JTextField getUnzipStatusTextField() {
@@ -94,6 +111,10 @@ public boolean useLocalFile() {
94111
return useLocalFileRadioButton.isSelected();
95112
}
96113

114+
public boolean useWpCli() {
115+
return useWpCliRadioButton.isSelected();
116+
}
117+
97118
public boolean useFormatOption() {
98119
return formatCheckBox.isSelected();
99120
}
@@ -158,6 +179,7 @@ private void initComponents() {
158179
unzipStatusTextField = new javax.swing.JTextField();
159180
useURLRadioButton = new javax.swing.JRadioButton();
160181
useLocalFileRadioButton = new javax.swing.JRadioButton();
182+
useWpCliRadioButton = new javax.swing.JRadioButton();
161183
urlLabel = new javax.swing.JLabel();
162184
localFileLabel = new javax.swing.JLabel();
163185
formatCheckBox = new javax.swing.JCheckBox();
@@ -197,6 +219,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
197219
}
198220
});
199221

222+
buttonGroup1.add(useWpCliRadioButton);
223+
org.openide.awt.Mnemonics.setLocalizedText(useWpCliRadioButton, org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.useWpCliRadioButton.text")); // NOI18N
224+
200225
org.openide.awt.Mnemonics.setLocalizedText(urlLabel, org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.urlLabel.text")); // NOI18N
201226

202227
org.openide.awt.Mnemonics.setLocalizedText(localFileLabel, org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.localFileLabel.text")); // NOI18N
@@ -320,7 +345,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
320345
.addGroup(layout.createSequentialGroup()
321346
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
322347
.addComponent(formatCheckBox)
323-
.addComponent(createConfigCheckBox))
348+
.addComponent(createConfigCheckBox)
349+
.addComponent(useWpCliRadioButton))
324350
.addGap(0, 223, Short.MAX_VALUE)))
325351
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
326352
.addComponent(wpConfigPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
@@ -342,6 +368,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
342368
.addComponent(useLocalFileRadioButton)
343369
.addComponent(localFileLabel))
344370
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
371+
.addComponent(useWpCliRadioButton)
372+
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
345373
.addComponent(formatCheckBox)
346374
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
347375
.addComponent(createConfigCheckBox)
@@ -379,6 +407,7 @@ private void createConfigCheckBoxActionPerformed(java.awt.event.ActionEvent evt)
379407
setAllEnabled(wpConfigPanel, true);
380408
}
381409
}//GEN-LAST:event_createConfigCheckBoxActionPerformed
410+
382411
// Variables declaration - do not modify//GEN-BEGIN:variables
383412
private javax.swing.ButtonGroup buttonGroup1;
384413
private javax.swing.JCheckBox createConfigCheckBox;
@@ -401,6 +430,7 @@ private void createConfigCheckBoxActionPerformed(java.awt.event.ActionEvent evt)
401430
private javax.swing.JLabel urlLabel;
402431
private javax.swing.JRadioButton useLocalFileRadioButton;
403432
private javax.swing.JRadioButton useURLRadioButton;
433+
private javax.swing.JRadioButton useWpCliRadioButton;
404434
private javax.swing.JPanel wpConfigPanel;
405435
// End of variables declaration//GEN-END:variables
406436
}

0 commit comments

Comments
 (0)