Skip to content

Commit 087e99d

Browse files
committed
Merge pull request #72 from likan999/master
Add a new option 'Generate Nano Protobuf'
2 parents 618c309 + b9f29ad commit 087e99d

9 files changed

+89
-9
lines changed

resources/messages/PbBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ compiler.validate.error.title=Protocol Buffers compiler
6565
compiler.validate.error.unsupported.os=Plugin compiler does not support this OS.
6666
compiler.validate.error.path.to.protoc=Could not find 'protoc' at path "{0}". Check the compiler configuration.
6767
compiler.validate.error.protoc.not.executable=The 'protoc' compiler file is not executable.
68+
compiler.validate.error.protoc.javaout.nano.not.supported=Module {0} uses nano protobuf but the 'protoc' compiler doesn't support --javanano_out option.
6869
compiler.validate.error.output.source.directory.not.exists="{0}" does not exist.
6970
compiler.validate.error.output.source.directory.not.set=The output source directory for module "{0}" has not been set.
7071
compiler.validate.error.output.source.directory.not.created=Could not created the output source directory "{0}" for module "{1}". Please check your configuration and/or file permissions.

src/protobuf/compiler/PbCompiler.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public void run(final Result<PsiDirectory> result) {
150150
for (String addtionalProtoPath : getModuleAdditionalProtoPaths(item)) {
151151
compilerCommand.append(" --proto_path=").append(addtionalProtoPath);
152152
}
153-
compilerCommand.append(" --java_out=").append(outputPath);
153+
if (item.getFacet().getConfiguration().isGenerateNanoProto()) {
154+
compilerCommand.append(" --javanano_out=");
155+
} else {
156+
compilerCommand.append(" --java_out=");
157+
}
158+
compilerCommand.append(outputPath);
154159
compilerCommand.append(" ").append(item.getPath());
155160
LOG.info("Invoking protoc: " + compilerCommand.toString());
156161
proc = Runtime.getRuntime().exec(compilerCommand.toString());
@@ -248,10 +253,21 @@ public boolean validateConfiguration(CompileScope compileScope) {
248253
return false;
249254
}
250255

256+
boolean compilerHasJavaNanoOutOption = hasJavaNanoOutOption(pathToCompiler);
257+
251258
for (Module module : modules) {
252259
PbFacet facet = FacetManager.getInstance(module).getFacetByType(PbFacetType.ID);
253260
if (facet != null) {
254-
String outputPath = facet.getConfiguration().getCompilerOutputPath();
261+
ProtobufFacetConfiguration configuration = facet.getConfiguration();
262+
263+
if (!compilerHasJavaNanoOutOption && configuration.isCompilationEnabled() && configuration.isGenerateNanoProto()) {
264+
Messages.showErrorDialog(PbBundle.message(
265+
"compiler.validate.error.protoc.javaout.nano.not.supported", module.getName()),
266+
PbBundle.message("compiler.validate.error.title"));
267+
return false;
268+
}
269+
270+
String outputPath = configuration.getCompilerOutputPath();
255271

256272
// Make sure the configuration has a value for the output source directory.
257273
if (outputPath == null) {
@@ -370,6 +386,21 @@ private void processLine(CompileContext context, String line, PbGenerationItem i
370386

371387
}
372388

389+
private boolean hasJavaNanoOutOption(String pathToCompiler) {
390+
try {
391+
Process proc = Runtime.getRuntime().exec(new String[] { pathToCompiler, "--help" });
392+
String[] helpLines = StreamUtil.readText(proc.getInputStream()).trim().split("\n");
393+
for (String line : helpLines) {
394+
if (line.contains("--javanano_out=")) {
395+
return true;
396+
}
397+
}
398+
} catch (IOException e) {
399+
e.printStackTrace();
400+
}
401+
return false;
402+
}
403+
373404
public VirtualFile getPresentableFile(CompileContext context, Module module, VirtualFile outputRoot, VirtualFile generatedFile) {
374405
// TODO: Map the generated file back to the original file.
375406
return generatedFile;

src/protobuf/settings/facet/ProtobufDefaultFacetSettingsEditor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ public JComponent createComponent() {
4242
@Override
4343
public boolean isModified() {
4444
return configuration.isCompilationEnabled() != commonSettingsEditor.getEnableCompilationCheckbox().isSelected() ||
45-
!configuration.getCompilerOutputPath().equals(commonSettingsEditor.getProtobufCompilerOutputPathField().getText());
45+
configuration.isGenerateNanoProto() != commonSettingsEditor.getGenerateNanoProtoCheckBox().isSelected() ||
46+
!configuration.getCompilerOutputPath().equals(commonSettingsEditor.getProtobufCompilerOutputPathField().getText()) ||
47+
!configuration.getAdditionalProtoPaths().equals(commonSettingsEditor.getProtobufAdditionalProtoPaths().getText());
4648
}
4749

4850
@Override
4951
public void apply() throws ConfigurationException {
5052
boolean isCompilationCheckboxEnabled = commonSettingsEditor.getEnableCompilationCheckbox().isSelected();
51-
configuration.setIsCompilationEnabled(isCompilationCheckboxEnabled);
5253
if (isCompilationCheckboxEnabled && !configuration.isCompilationEnabled()) {
5354
CompilerManager compilerManager = CompilerManager.getInstance(project);
5455
compilerManager.addCompilableFileType(PbFileType.PROTOBUF_FILE_TYPE);
@@ -61,13 +62,16 @@ public void apply() throws ConfigurationException {
6162
}
6263
}
6364

65+
configuration.setIsCompilationEnabled(isCompilationCheckboxEnabled);
66+
configuration.setGenerateNanoProto(commonSettingsEditor.getGenerateNanoProtoCheckBox().isSelected());
6467
configuration.setCompilerOutputPath(commonSettingsEditor.getProtobufCompilerOutputPathField().getText().trim());
6568
configuration.setCompilerOutputPath(commonSettingsEditor.getProtobufAdditionalProtoPaths().getText().trim());
6669
}
6770

6871
@Override
6972
public void reset() {
7073
commonSettingsEditor.getEnableCompilationCheckbox().setSelected(configuration.isCompilationEnabled());
74+
commonSettingsEditor.getGenerateNanoProtoCheckBox().setSelected(configuration.isGenerateNanoProto());
7175
commonSettingsEditor.getProtobufCompilerOutputPathField().setText(configuration.getCompilerOutputPath());
7276
commonSettingsEditor.getProtobufAdditionalProtoPaths().setText(configuration.getAdditionalProtoPaths());
7377
}

src/protobuf/settings/facet/ProtobufFacetCommonSettingsEditor.form

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<properties/>
99
<border type="none"/>
1010
<children>
11-
<grid id="bdbef" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
11+
<grid id="bdbef" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1212
<margin top="5" left="5" bottom="5" right="5"/>
1313
<constraints>
1414
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -25,33 +25,42 @@
2525
<thirdStateEnabled value="false"/>
2626
</properties>
2727
</component>
28+
<component id="39f9" class="com.intellij.util.ui.ThreeStateCheckBox" binding="generateNanoProtoCheckBox">
29+
<constraints>
30+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
31+
</constraints>
32+
<properties>
33+
<text value="Generate nano proto"/>
34+
<thirdStateEnabled value="false"/>
35+
</properties>
36+
</component>
2837
<component id="81a1c" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="protobufCompilerOutputPath">
2938
<constraints>
30-
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
39+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
3140
<preferred-size width="150" height="-1"/>
3241
</grid>
3342
</constraints>
3443
<properties/>
3544
</component>
3645
<component id="ed268" class="javax.swing.JLabel">
3746
<constraints>
38-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
47+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
3948
</constraints>
4049
<properties>
4150
<text resource-bundle="messages/PbBundle" key="compiler.configurable.source.directory"/>
4251
</properties>
4352
</component>
4453
<component id="a2f86" class="javax.swing.JLabel">
4554
<constraints>
46-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
55+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
4756
</constraints>
4857
<properties>
4958
<text resource-bundle="messages/PbBundle" key="facet.protobuf.additional.proto.paths.text"/>
5059
</properties>
5160
</component>
5261
<component id="c8a48" class="javax.swing.JTextField" binding="protobufAdditionalProtoPaths">
5362
<constraints>
54-
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
63+
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
5564
<preferred-size width="150" height="-1"/>
5665
</grid>
5766
</constraints>

src/protobuf/settings/facet/ProtobufFacetCommonSettingsEditor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class ProtobufFacetCommonSettingsEditor extends JComponent {
1313

1414
private JPanel mainPanel;
1515
private ThreeStateCheckBox enableCompilationCheckBox;
16+
private ThreeStateCheckBox generateNanoProtoCheckBox;
1617
private TextFieldWithBrowseButton protobufCompilerOutputPath;
1718
private JTextField protobufAdditionalProtoPaths;
1819

@@ -28,6 +29,10 @@ public ThreeStateCheckBox getEnableCompilationCheckbox() {
2829
return enableCompilationCheckBox;
2930
}
3031

32+
public ThreeStateCheckBox getGenerateNanoProtoCheckBox() {
33+
return generateNanoProtoCheckBox;
34+
}
35+
3136
public TextFieldWithBrowseButton getProtobufCompilerOutputPathField() {
3237
return protobufCompilerOutputPath;
3338
}

src/protobuf/settings/facet/ProtobufFacetConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class ProtobufFacetConfiguration implements FacetConfiguration, PersistentStateComponent<ProtobufFacetSettings> {
2727

2828
private boolean compilationEnabled = true;
29+
private boolean generateNanoProto = false;
2930
private String compilerOutputPath = "";
3031
private String additionalProtoPaths = "";
3132

@@ -48,6 +49,7 @@ public void writeExternal(Element element) throws WriteExternalException {
4849
public ProtobufFacetSettings getState() {
4950
ProtobufFacetSettings settings = new ProtobufFacetSettings();
5051
settings.COMPILE_PROTO = compilationEnabled;
52+
settings.GENERATE_NANO_PROTO = generateNanoProto;
5153
settings.COMPILER_OUTPUT_SOURCE_DIRECTORY = compilerOutputPath;
5254
settings.COMPILER_ADDITIONAL_PROTO_PATHS = additionalProtoPaths;
5355
return settings;
@@ -56,6 +58,7 @@ public ProtobufFacetSettings getState() {
5658
@Override
5759
public void loadState(ProtobufFacetSettings settings) {
5860
compilationEnabled = settings.COMPILE_PROTO;
61+
generateNanoProto = settings.GENERATE_NANO_PROTO;
5962
compilerOutputPath = settings.COMPILER_OUTPUT_SOURCE_DIRECTORY;
6063
additionalProtoPaths = settings.COMPILER_ADDITIONAL_PROTO_PATHS;
6164
}
@@ -68,6 +71,14 @@ public void setIsCompilationEnabled(boolean value) {
6871
compilationEnabled = value;
6972
}
7073

74+
public boolean isGenerateNanoProto() {
75+
return generateNanoProto;
76+
}
77+
78+
public void setGenerateNanoProto(boolean value) {
79+
generateNanoProto = value;
80+
}
81+
7182
public String getCompilerOutputPath() {
7283
return compilerOutputPath;
7384
}

src/protobuf/settings/facet/ProtobufFacetEditor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public ProtobufFacetEditor(FacetEditorContext editorContext, FacetValidatorsMana
3535
final Module module = editorContext.getModule();
3636

3737
commonSettingsEditor.getEnableCompilationCheckbox().setSelected(configuration.isCompilationEnabled());
38+
commonSettingsEditor.getGenerateNanoProtoCheckBox().setSelected(configuration.isGenerateNanoProto());
3839
commonSettingsEditor.getProtobufCompilerOutputPathField().setText(configuration.getCompilerOutputPath());
3940
commonSettingsEditor.getProtobufCompilerOutputPathField().addBrowseFolderListener(project, new CompilerOutputBrowseFolderActionListener(project, module, commonSettingsEditor.getProtobufCompilerOutputPathField()));
4041
commonSettingsEditor.getProtobufAdditionalProtoPaths().setText(configuration.getAdditionalProtoPaths());
@@ -59,17 +60,20 @@ public void onFacetInitialized(@NotNull final Facet facet) {
5960
@Override
6061
public boolean isModified() {
6162
boolean compilationEnabled = commonSettingsEditor.getEnableCompilationCheckbox().isSelected();
63+
boolean generateNanoProto = commonSettingsEditor.getGenerateNanoProtoCheckBox().isSelected();
6264
String outputPath = commonSettingsEditor.getProtobufCompilerOutputPathField().getText().trim();
6365
String additionalProtoPaths = commonSettingsEditor.getProtobufAdditionalProtoPaths().getText().trim();
6466

6567
return ((configuration.isCompilationEnabled() != compilationEnabled) ||
68+
(configuration.isGenerateNanoProto() != generateNanoProto) ||
6669
(!Comparing.equal(configuration.getCompilerOutputPath(), FileUtil.toSystemIndependentName(outputPath))) ||
6770
(!Comparing.equal(configuration.getAdditionalProtoPaths(), toSystemIndependentPaths(additionalProtoPaths))));
6871
}
6972

7073
@Override
7174
public void apply() throws ConfigurationException {
7275
configuration.setIsCompilationEnabled(commonSettingsEditor.getEnableCompilationCheckbox().isSelected());
76+
configuration.setGenerateNanoProto(commonSettingsEditor.getGenerateNanoProtoCheckBox().isSelected());
7377
configuration.setCompilerOutputPath(FileUtil.toSystemIndependentName(commonSettingsEditor.getProtobufCompilerOutputPathField().getText().trim()));
7478
configuration.setAdditionalProtoPaths(toSystemIndependentPaths(commonSettingsEditor.getProtobufAdditionalProtoPaths().getText()));
7579
}
@@ -96,6 +100,10 @@ public JCheckBox getEnableCompilationCheckbox() {
96100
return commonSettingsEditor.getEnableCompilationCheckbox();
97101
}
98102

103+
public JCheckBox getGenerateNanoProtoCheckbox() {
104+
return commonSettingsEditor.getGenerateNanoProtoCheckBox();
105+
}
106+
99107
public TextFieldWithBrowseButton getProtobufCompilerOutputPathField() {
100108
return commonSettingsEditor.getProtobufCompilerOutputPathField();
101109
}

src/protobuf/settings/facet/ProtobufFacetSettings.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class ProtobufFacetSettings {
99
// Is compilation of .proto files enabled?
1010
public boolean COMPILE_PROTO = true;
1111

12+
// Do we generate nano protobuf instead?
13+
public boolean GENERATE_NANO_PROTO = false;
14+
1215
// The directory where the files resulting from protoc invocation will be created.
1316
public String COMPILER_OUTPUT_SOURCE_DIRECTORY = "";
1417

src/protobuf/settings/facet/ProtobufMultipleFacetSettingsEditor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public JCheckBox fun(final FacetEditor facetEditor) {
3333
}
3434
});
3535

36+
// Bind to the generate nano proto checkbox.
37+
helper.bind(commonSettingsEditor.getGenerateNanoProtoCheckBox(), editors, new NotNullFunction<FacetEditor, JCheckBox>() {
38+
@NotNull
39+
public JCheckBox fun(final FacetEditor facetEditor) {
40+
return (facetEditor.getEditorTab(ProtobufFacetEditor.class)).getGenerateNanoProtoCheckbox();
41+
}
42+
});
43+
3644
// Bind to the output source directory text field.
3745
helper.bind(commonSettingsEditor.getProtobufCompilerOutputPathField().getTextField(), editors, new NotNullFunction<FacetEditor, JTextField>() {
3846
@NotNull

0 commit comments

Comments
 (0)