Skip to content

Commit 2c6e654

Browse files
committed
Updated to processor version 0.3.0
1 parent 9ee622b commit 2c6e654

7 files changed

Lines changed: 226 additions & 195 deletions

File tree

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,30 @@ buildscript() {
4040

4141
### Configure the Default memory
4242

43-
```gradle
43+
```groovy
44+
//utility imports
45+
import static org.jamplate.glucose.internal.util.Values.*
46+
import static org.jamplate.impl.unit.Action.*
47+
import static org.jamplate.util.Specs.*
48+
4449
jamplate {
45-
//here you can put all the default mappigns you want
46-
memory 'Text', 'Value'
47-
memory 'Object', [ "Key": "Value" ]
48-
memory 'Array', [ "item1", "item2" ]
49-
memory 'Dynamic', { memory -> 'Dynamic Variable' }
50-
memory "Random", { (long) (Math.random() * (1L << 60)) }
51-
memory "Document", { it.frame.instruction?.tree?.document() }
50+
//with the method `unit`, you create a new task for the module you give it to it
51+
//the method will return the created unit object for that module
52+
unit('main').spec.add(listener({ event ->
53+
if (event.action == PRE_EXEC) {
54+
//here you can put all the default mappings you want
55+
event.memory.set 'Text', text('Value')
56+
event.memory.set 'Object', object(["Key": "Value"])
57+
event.memory.set 'Array', array(["item1", "item2"])
58+
event.memory.set 'Dynamic', { memory -> 'Dynamic Variable' }
59+
event.memory.set "Random", { '' + (long) (Math.random() * (1L << 60)) }
60+
event.memory.set "Document", { '' + it.frame.instruction?.tree?.document() }
61+
}
62+
}))
63+
//with this method, the unit output will be treated as generated java source
64+
java('main')
65+
//with this method, the unit output will be treated as generated text
66+
source('main')
5267
}
5368
```
5469

build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group 'org.jamplate'
9-
version '0.2.3x'
9+
version '0.3.0'
1010

1111
repositories {
1212
mavenCentral()
@@ -16,8 +16,10 @@ repositories {
1616
}
1717

1818
dependencies {
19-
implementation 'org.jamplate:processor:0.2.3'
20-
implementation 'org.json:json:20210307'
19+
implementation 'org.jamplate:processor:0.3.0-beta-001'
20+
21+
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
22+
implementation 'org.jetbrains:annotations:20.1.0'
2123
}
2224

2325
pluginBundle {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=org.jamplate.gradle.JamplatePlugin

src/main/groovy/org/jamplate/gradle/JamplateExtension.groovy

Lines changed: 137 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,163 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
//file:noinspection GrMethodMayBeStatic
17+
//file:noinspection GrMethodMayBeStatic
1618
package org.jamplate.gradle
1719

18-
import org.jamplate.model.Compilation
19-
import org.jamplate.model.Memory
20-
import org.jamplate.model.Value
21-
import org.json.JSONArray
22-
import org.json.JSONObject
20+
import org.gradle.api.Project
21+
import org.gradle.api.file.SourceDirectorySet
22+
import org.gradle.api.model.ObjectFactory
23+
import org.gradle.api.plugins.JavaPluginConvention
24+
import org.gradle.api.tasks.SourceSet
25+
import org.jamplate.diagnostic.Diagnostic
26+
import org.jamplate.diagnostic.Message
27+
import org.jamplate.glucose.internal.memory.Address
28+
import org.jamplate.glucose.spec.GlucoseSpec
29+
import org.jamplate.impl.unit.Action
30+
import org.jamplate.impl.unit.UnitImpl
31+
import org.jamplate.unit.Unit
2332

24-
import java.util.function.Function
33+
import static org.jamplate.glucose.internal.util.Values.text
34+
import static org.jamplate.util.Specs.listener
2535

2636
class JamplateExtension {
27-
Map<String, Object> defaultMemory = new HashMap<>()
28-
Function<Compilation, Memory> memorySupplier
37+
Map<String, Unit> units = new HashMap<>()
2938

30-
void memory(String address, String value) {
31-
defaultMemory.put(address, value)
39+
Project project
40+
41+
/**
42+
* The factory passed by the plugin.
43+
*
44+
* @since 0.3.0 ~2021.07.11
45+
*/
46+
ObjectFactory factory
47+
48+
JamplateExtension(Project project, ObjectFactory factory) {
49+
Objects.requireNonNull(project, "project")
50+
Objects.requireNonNull(factory, "factory")
51+
this.project = project
52+
this.factory = factory
3253
}
3354

34-
void memory(String address, Number number) {
35-
defaultMemory.put(address, number)
55+
Unit unit(
56+
String module,
57+
taskName = defaultTaskName(module),
58+
input = defaultInputFile(module),
59+
output = defaultOutputFile(module)
60+
) {
61+
return units.computeIfAbsent(module, {
62+
Unit unit = new UnitImpl(new GlucoseSpec())
63+
64+
unit.spec.add listener({ event ->
65+
if (event.action == Action.PRE_EXEC) {
66+
event.memory.set Address.PROJECT, text(input)
67+
event.memory.set Address.OUTPUT, text(output)
68+
}
69+
})
70+
unit.spec.add(listener({ event ->
71+
if (event.action == Action.DIAGNOSTIC) {
72+
Diagnostic diagnostic = event.diagnostic
73+
74+
if (!diagnostic.empty) {
75+
Message message = diagnostic.first()
76+
Throwable exception = message.exception
77+
78+
diagnostic.flush(true)
79+
80+
if (exception != null)
81+
throw exception
82+
}
83+
}
84+
}))
85+
86+
//create the jamplate task
87+
project.tasks.create(taskName, ProcessJamplateTask) {
88+
it.input = input
89+
it.output = output
90+
it.unit = unit
91+
}
92+
93+
return unit
94+
})
3695
}
3796

38-
void memory(String address, Closure value) {
39-
defaultMemory.put address, (Value) {
40-
memory ->
41-
Object v = value(memory)
97+
void java(
98+
String module,
99+
taskName = defaultTaskName(module),
100+
input = defaultInputFile(module),
101+
output = defaultOutputFile(module)
102+
) {
103+
SourceSet sourceSet = project.convention.getPlugin(JavaPluginConvention).sourceSets.getByName(module)
104+
105+
//create new jamplates source set
106+
SourceDirectorySet jamSourceSet =
107+
factory
108+
.sourceDirectorySet(
109+
"${module}.jamplate",
110+
"Jamplate ${module} extends Java ${module}"
111+
)
112+
.srcDir input
113+
114+
//register the source set
115+
sourceSet.allSource.source jamSourceSet
116+
117+
//register the source set as a java source
118+
sourceSet.allJava.source jamSourceSet
42119

43-
if (v instanceof Map)
44-
return new JSONObject((Map) v).toString()
45-
if (v instanceof Collection)
46-
return new JSONArray((Collection) v).toString()
47-
if (v instanceof String)
48-
return v
120+
//register the folder containing the generated code as java source folder
121+
sourceSet.java.srcDir output
49122

50-
return String.valueOf(v)
123+
//make the java compile task run after the jamplate task
124+
project.tasks.named(sourceSet.compileJavaTaskName) {
125+
it.dependsOn taskName
51126
}
52127
}
53128

54-
void memory(String address, List value) {
55-
defaultMemory.put(address, value)
129+
void source(
130+
String module,
131+
taskName = defaultTaskName(module),
132+
input = defaultInputFile(module),
133+
output = defaultOutputFile(module)
134+
) {
135+
//create new jamplates source set
136+
factory
137+
.sourceDirectorySet(
138+
"${module}.jamplate",
139+
"Jamplate ${module}"
140+
)
141+
.srcDir input
142+
143+
project.tasks.named('build') { it.dependsOn(taskName) }
144+
}
145+
146+
protected String defaultTaskName(String module) {
147+
return "process${module == "main" ? "" : camelCase(module)}Jamplate"
56148
}
57149

58-
void memory(String address, Map value) {
59-
defaultMemory.put(address, new JSONObject(value))
150+
protected File defaultInputFile(String module) {
151+
return new File(project.projectDir, "src/$module/jamplate")
60152
}
61153

62-
void memory(Map map) {
63-
defaultMemory.putAll(map)
154+
protected File defaultOutputFile(String module) {
155+
return new File(project.buildDir, "jamplate/$module")
64156
}
65157

66-
void replaceMemorySupplier(Function<Compilation, Memory> memorySupplier) {
67-
this.memorySupplier = memorySupplier
158+
/**
159+
* Return the given {@code string} formatted as {@code camelCase}.
160+
*
161+
* @param string the string to be formatted as {@code camelCase}.
162+
* @return the given {@code string} formatted as {@code camelCase}.
163+
* @throws NullPointerException if the given {@code string} is null.
164+
* @since 0.0.1 ~2020.09.15
165+
*/
166+
protected static String camelCase(String string) {
167+
Objects.requireNonNull(string, "string")
168+
return string.length() <= 1 ?
169+
string.toUpperCase() :
170+
string.substring(0, 1)
171+
.toUpperCase() +
172+
string.substring(1, string.length())
173+
.toLowerCase()
68174
}
69175
}

src/main/groovy/org/jamplate/gradle/JamplatePlugin.groovy

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ package org.jamplate.gradle
1717

1818
import org.gradle.api.Plugin
1919
import org.gradle.api.Project
20-
import org.gradle.api.file.SourceDirectorySet
2120
import org.gradle.api.model.ObjectFactory
2221
import org.gradle.api.plugins.JavaPlugin
23-
import org.gradle.api.plugins.JavaPluginConvention
24-
import org.gradle.api.tasks.SourceSet
2522

2623
import javax.inject.Inject
2724

@@ -37,12 +34,6 @@ class JamplatePlugin implements Plugin<Project> {
3734
* @since 0.0.1 ~2020.09.15
3835
*/
3936
final ObjectFactory factory
40-
/**
41-
* The {@link Project} passed when applying this plugin.
42-
*
43-
* @since 0.0.1 ~2020.09.15
44-
*/
45-
Project project
4637

4738
@Inject
4839
JamplatePlugin(ObjectFactory factory) {
@@ -51,108 +42,8 @@ class JamplatePlugin implements Plugin<Project> {
5142

5243
@Override
5344
void apply(Project project) {
54-
this.project = project
5545
project.plugins.apply JavaPlugin
5646

57-
project.extensions.add 'jamplate', JamplateExtension
58-
59-
this.jam 'jamplate', 'main'
60-
this.jam 'jamplate', 'test'
61-
}
62-
63-
/**
64-
* Create a task that generates {@code java} code from {@code jamplate} code for the given source
65-
* name {@code jam}. With the name of the given {@code taskName}. To be generated to the given {@code module}
66-
* (like 'main' or 'test').
67-
* <p>
68-
* The given {@code input} file will be the directory where the sources will be read by the task.
69-
* <p>
70-
* The given {@code output} file will be where the task outputs the generated java code.
71-
* <p>
72-
* This method will make the given {@code sourceSet} compiles the sources generated by the task.
73-
* <p>
74-
* The given {@code taskName} will be the name of the task.
75-
* <p>
76-
* If the given {@code allJavaSource} is true, the IDE will treat the {@code jamplate} code as a
77-
* {@code java} code.
78-
*
79-
* @param jam the name of the source folder.
80-
* @param module the name of the module ('main', 'test', etc...).
81-
* @param input the directory where the sources is located.
82-
* @param output the directory to output to.
83-
* @param sourceSet the sourceSet to be configured to compile the output.
84-
* @param taskName the name of the task.
85-
* @param allJavaSource true, will make IDEs treat the {@code jamplate} code as {@code java} code.
86-
* @throws NullPointerException if teh given {@code jam} or {@code module} or {@code input} or {@code output}
87-
* or {@code sourceSet} or {@code taskName} is null.
88-
* @since 0.0.1 ~2020.09.15
89-
*/
90-
protected void jam(
91-
String jam,
92-
String module,
93-
File input = new File(project.projectDir, "src/$module/$jam"),
94-
File output = new File(project.buildDir, "jam/$module/java"),
95-
SourceSet sourceSet = project.convention.getPlugin(JavaPluginConvention).sourceSets.getByName(module),
96-
String taskName = "process${module.equalsIgnoreCase('main') ? '' : camelCase(module)}Jamplate",
97-
boolean allJavaSource = true
98-
) {
99-
Objects.requireNonNull(jam, "jam")
100-
Objects.requireNonNull(module, "module")
101-
Objects.requireNonNull(input, "input")
102-
Objects.requireNonNull(output, "output")
103-
Objects.requireNonNull(sourceSet, "sourceSet")
104-
Objects.requireNonNull(taskName, "taskName")
105-
106-
if (input.directory) {
107-
//create new jamplates source set
108-
SourceDirectorySet jamSourceSet =
109-
factory
110-
.sourceDirectorySet(
111-
"${module}.jam",
112-
"Jamplate ${jam} extends Java ${module}"
113-
)
114-
.srcDir input
115-
116-
//register the source set
117-
sourceSet.allSource.source jamSourceSet
118-
119-
//register the source set as a java source
120-
if (allJavaSource)
121-
sourceSet.allJava.source jamSourceSet
122-
123-
//register the folder containing the generated code as java source folder
124-
sourceSet.java.srcDir output
125-
126-
//create the jamplate task
127-
project.tasks.create(taskName, ProcessJamplateTask) {
128-
JamplateExtension extension = project.extensions.getByType(JamplateExtension)
129-
it.input = input
130-
it.output = output
131-
it.defaultMemory = extension.defaultMemory
132-
it.memorySupplier = extension.memorySupplier
133-
}
134-
//make the java compile task run after the jamplate task
135-
project.tasks.named(sourceSet.compileJavaTaskName) {
136-
it.dependsOn taskName
137-
}
138-
}
139-
}
140-
141-
/**
142-
* Return the given {@code string} formatted as {@code camelCase}.
143-
*
144-
* @param string the string to be formatted as {@code camelCase}.
145-
* @return the given {@code string} formatted as {@code camelCase}.
146-
* @throws NullPointerException if the given {@code string} is null.
147-
* @since 0.0.1 ~2020.09.15
148-
*/
149-
private static String camelCase(String string) {
150-
Objects.requireNonNull(string, "string")
151-
return string.length() <= 1 ?
152-
string.toUpperCase() :
153-
string.substring(0, 1)
154-
.toUpperCase() +
155-
string.substring(1, string.length())
156-
.toLowerCase()
47+
project.extensions.create 'jamplate', JamplateExtension, project, factory
15748
}
15849
}

0 commit comments

Comments
 (0)