Skip to content

Commit aed53c0

Browse files
committed
refactoring
1 parent 3709a33 commit aed53c0

File tree

8 files changed

+128
-29
lines changed

8 files changed

+128
-29
lines changed

jbbp-plugins/jbbp-gradle/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def jbbpVersion = project.hasProperty('jbbp_version') ? project.getProperty('jbbp_version') : "1.3.0-SNAPSHOT"
2-
def metalibVersion = project.hasProperty('meta_lib_version') ? project.getProperty('meta_lib_version') : "1.1.2"
2+
def metaLibVersion = project.hasProperty('meta_lib_version') ? project.getProperty('meta_lib_version') : "1.1.2"
33

44
group = 'com.igormaznitsa'
55
version = jbbpVersion
@@ -16,8 +16,8 @@ dependencies {
1616

1717
compile "commons-io:commons-io:2.5"
1818
compile "com.igormaznitsa:jbbp:" + jbbpVersion
19-
compile "com.igormaznitsa:meta-annotations:" + metalibVersion
20-
compile "com.igormaznitsa:meta-utils:" + metalibVersion
19+
compile "com.igormaznitsa:meta-annotations:" + metaLibVersion
20+
compile "com.igormaznitsa:meta-utils:" + metaLibVersion
2121

2222
testCompile 'junit:junit:4.12'
2323
}
@@ -50,13 +50,13 @@ sourceSets {
5050
pluginBundle {
5151
website = 'http://www.igormaznitsa.com/'
5252
vcsUrl = 'https://github.com/raydac/java-binary-block-parser'
53-
description = 'JBBP translator!'
54-
tags = ['jbbp','converter']
53+
description = 'JBBP script translator!'
54+
tags = ['jbbp', 'converter']
5555

5656
plugins {
5757
JBBPPlugin {
58-
id = 'com.igormaznitsa.jbbp'
59-
displayName = 'Translator of JBBPscripts into executable sources'
58+
id = 'com.igormaznitsa.gradle.jbbp'
59+
displayName = 'Translator of JBBP scripts into executable sources'
6060
}
6161
}
6262
}

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/AbstractJBBPTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final void doAction() {
6060
if (ext == null) {
6161
ext = new JBBPExtension(getProject());
6262
}
63+
ext.prepare(getProject());
6364
doTaskAction(ext);
6465
}
6566

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/JBBPCleanTask.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import java.io.File;
88
import java.io.IOException;
99

10+
/**
11+
* Task allows to delete generated files.
12+
*
13+
* @since 1.3.0
14+
*/
1015
public class JBBPCleanTask extends AbstractJBBPTask {
1116

1217
@Override
@@ -17,21 +22,21 @@ protected void doTaskAction(@Nonnull final JBBPExtension ext) {
1722
.setPackageName(ext.packageName)
1823
.setOutputDir(ext.output);
1924

20-
for(final File aScript : findScripts(ext)) {
21-
getLogger().debug("Script file : "+aScript);
25+
for (final File aScript : findScripts(ext)) {
26+
getLogger().debug("Script file : " + aScript);
2227
parameters.setScriptFile(aScript);
2328
try {
2429
for (final File f : ext.target.getTranslator().translate(parameters, true)) {
25-
if (f.isFile()){
26-
if (f.delete()){
27-
getLogger().info("File "+f+" has been deleted");
30+
if (f.isFile()) {
31+
if (f.delete()) {
32+
getLogger().info("File " + f + " has been deleted");
2833
} else {
29-
getLogger().error("Can't delete file "+f);
34+
getLogger().error("Can't delete file " + f);
3035
}
3136
}
3237
}
33-
}catch(IOException ex){
34-
throw new GradleException("Error during processing JBBP script "+aScript, ex);
38+
} catch (IOException ex) {
39+
throw new GradleException("Error during processing JBBP script " + aScript, ex);
3540
}
3641
}
3742

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/JBBPExtension.java

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,116 @@
1212
import java.util.Map;
1313
import java.util.Set;
1414

15+
/**
16+
* JBBP extension settings.
17+
*
18+
* @since 1.3.0
19+
*/
1520
public class JBBPExtension {
16-
public static final String NAME = "jbbp";
21+
/**
22+
* The Registered name of the extension.
23+
*/
24+
public static final String EXT_NAME = "jbbp";
1725

26+
/**
27+
* Target of translation.
28+
*/
1829
public Target target = Target.JAVA_1_6;
30+
31+
/**
32+
* Flag to generate getters and setters in result class, all fields will be protected ones.
33+
*/
1934
public boolean addGettersSetters = false;
35+
36+
/**
37+
* Make the result class as abstract one even if it doesn't have abstract methods.
38+
*/
2039
public boolean doAbstract = false;
40+
41+
/**
42+
* Interfaces to be implemented by the generated class.
43+
*/
2144
public Set<String> interfaces = new HashSet<String>();
45+
46+
/**
47+
* Set of parser flags.
48+
*/
2249
public Set<ParserFlags> parserFlags = new HashSet<ParserFlags>();
50+
51+
/**
52+
* Allowed custom types for JBBP scripts.
53+
*/
2354
public Set<String> customTypes = new HashSet<String>();
55+
56+
/**
57+
* Interface names mapped to generated sub-classes, mapped interface will be used in getters instead of the sub-class type.
58+
*/
2459
public Map<String, String> mapSubClassInterfaces = new HashMap<String, String>();
60+
61+
/**
62+
* Encoding for text input.
63+
*/
2564
public String inEncoding = "UTF-8";
65+
66+
/**
67+
* Encoding for text ouptut.
68+
*/
2669
public String outEncoding = "UTF-8";
27-
public File headCommentFile = null;
70+
71+
/**
72+
* Text to be placed in the head of generated class as comments.
73+
*/
2874
public String headComment = null;
29-
public File customTextFile = null;
75+
76+
/**
77+
* Text file which content will be added into head of generated class as comments.
78+
*/
79+
public File headCommentFile = null;
80+
81+
/**
82+
* Text to be inserted into body of generated class.
83+
*/
3084
public String customText = null;
85+
86+
/**
87+
* Text file which content will be injected into body of generated class.
88+
*/
89+
public File customTextFile = null;
90+
91+
/**
92+
* Super-class for the generated class.
93+
*/
3194
public String superClass = null;
95+
96+
/**
97+
* Package name to override the package name extracted from JBBP script name.
98+
*/
3299
public String packageName = null;
100+
101+
/**
102+
* Output folder for result classes.
103+
*/
33104
public File output;
105+
106+
/**
107+
* Source folder to find JBBP scripts.
108+
*/
34109
public ConfigurableFileTree source;
35110

36111
public JBBPExtension(@Nonnull final Project project) {
37112
this.source = project.fileTree("src/jbbp");
38-
this.source.include("**/*.jbbp");
39-
this.output = new File(project.getBuildDir(), "generated-jbbp-dir");
113+
}
114+
115+
public void prepare(@Nonnull final Project project) {
116+
if (this.source == null) {
117+
this.source = project.fileTree("src/jbbp");
118+
}
119+
if (this.source.getIncludes().isEmpty()) {
120+
this.source.include("**/*.jbbp");
121+
}
122+
if (this.output == null) {
123+
this.output = new File(project.getBuildDir(), "generated-jbbp-dir");
124+
}
40125
}
41126

42127
}

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/JBBPGenerateTask.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,30 @@
2424
import javax.annotation.Nullable;
2525
import java.io.File;
2626
import java.io.IOException;
27-
import java.util.*;
28-
27+
import java.util.HashSet;
28+
import java.util.Locale;
29+
import java.util.Set;
30+
31+
/**
32+
* Task to translate found JBBP scripts in source files.
33+
*
34+
* @since 1.3.0
35+
*/
2936
public class JBBPGenerateTask extends AbstractJBBPTask {
3037

38+
/**
39+
* Flag to register the output folder in Java source folders at the end of process.
40+
*/
3141
@Input
3242
@Optional
3343
protected boolean addSource = true;
3444

3545
@Override
3646
protected void doTaskAction(@Nonnull final JBBPExtension ext) {
37-
final Target target = GetUtils.ensureNonNull(ext.target,Target.JAVA_1_6);
47+
final Target target = GetUtils.ensureNonNull(ext.target, Target.JAVA_1_6);
3848

3949
final Set<String> normalizedCustomTypeNames = new HashSet<String>();
40-
if (ext.customTypes!=null) {
50+
if (ext.customTypes != null) {
4151
for (final String s : ext.customTypes) {
4252
final String trimmed = s.trim();
4353
final String normalized = trimmed.toLowerCase(Locale.ENGLISH);
@@ -97,7 +107,7 @@ public JBBPAbstractField readCustomFieldType(@Nonnull final JBBPBitInputStream i
97107
parameters.setScriptFile(aScript).assertAllOk();
98108
getLogger().info("Detected JBBP script file : " + aScript);
99109
try {
100-
final Set<File> files = target.getTranslator().translate(parameters,false);
110+
final Set<File> files = target.getTranslator().translate(parameters, false);
101111
getLogger().debug("Converted " + aScript + " into " + files);
102112
for (final File f : files) {
103113
getLogger().info(String.format("JBBP script '%s' has been converted into '%s'", aScript.getName(), f.getName()));
@@ -109,7 +119,7 @@ public JBBPAbstractField readCustomFieldType(@Nonnull final JBBPBitInputStream i
109119

110120

111121
if (this.addSource) {
112-
getLogger().debug("Registering path to java sources : " + Assertions.assertNotNull("Output must not be null",ext.output));
122+
getLogger().debug("Registering path to java sources : " + Assertions.assertNotNull("Output must not be null", ext.output));
113123
if (getProject().getPlugins().hasPlugin(JavaPlugin.class)) {
114124
final JavaPluginConvention javaPluginConvention = getProject().getConvention().getPlugin(JavaPluginConvention.class);
115125
final SourceSet main = javaPluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/JBBPPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class JBBPPlugin implements Plugin<Project> {
99

1010
@Override
1111
public void apply(@Nonnull final Project project) {
12-
project.getExtensions().create(JBBPExtension.NAME,JBBPExtension.class,project);
12+
project.getExtensions().create(JBBPExtension.EXT_NAME, JBBPExtension.class, project);
1313
project.getTasks().create("jbbpGenerate", JBBPGenerateTask.class);
1414
project.getTasks().create("jbbpClean", JBBPCleanTask.class);
1515
}

jbbp-plugins/jbbp-gradle/src/test/groovy/com/igormaznitsa/jbbp/plugin/gradle/JBBPPluginTest.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import org.gradle.api.Project
42
import org.gradle.testfixtures.ProjectBuilder
53
import org.junit.Test

0 commit comments

Comments
 (0)