Skip to content

Commit b88f8ed

Browse files
committed
More memory customizations
1 parent a4e7601 commit b88f8ed

5 files changed

Lines changed: 46 additions & 11 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ jamplate {
4545
memory 'Text', 'Value'
4646
memory 'Object', '{"Key": "Value"}'
4747
memory 'Array', '["item1", "item2"]'
48+
memory 'Dynamic', { memory -> 'Dynamic Variable'}
49+
memory "Random", {'' + (long) (Math.random() * (1L << 60))}
50+
memory "Document", {'' + it.frame.instruction?.tree?.document()}
4851
}
4952
```
5053

build.gradle

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

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

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

1818
dependencies {
19-
implementation 'org.jamplate:processor:0.2.2'
19+
implementation 'org.jamplate:processor:0.2.3'
20+
implementation 'org.json:json:20210307'
2021
}
2122

2223
pluginBundle {

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,42 @@
1515
*/
1616
package org.jamplate.gradle
1717

18+
import org.jamplate.model.Compilation
19+
import org.jamplate.model.Memory
20+
import org.jamplate.model.Value
21+
import org.json.JSONObject
22+
23+
import java.util.function.Function
24+
1825
class JamplateExtension {
19-
Map defaultMemory = new HashMap<>()
26+
Map<String, Object> defaultMemory = new HashMap<>()
27+
Function<Compilation, Memory> memorySupplier
28+
29+
void memory(String address, String value) {
30+
defaultMemory.put(address, value)
31+
}
32+
33+
void memory(String address, Number number) {
34+
defaultMemory.put(address, number)
35+
}
2036

21-
void memory(String address, Object value) {
37+
void memory(String address, Value value) {
2238
defaultMemory.put(address, value)
2339
}
2440

41+
void memory(String address, List value) {
42+
defaultMemory.put(address, value)
43+
}
44+
45+
void memory(String address, Map value) {
46+
defaultMemory.put(address, new JSONObject(value))
47+
}
48+
2549
void memory(Map map) {
2650
defaultMemory.putAll(map)
2751
}
52+
53+
void replaceMemorySupplier(Function<Compilation, Memory> memorySupplier) {
54+
this.memorySupplier = memorySupplier
55+
}
2856
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class JamplatePlugin implements Plugin<Project> {
129129
it.input = input
130130
it.output = output
131131
it.defaultMemory = extension.defaultMemory
132+
it.memorySupplier = extension.memorySupplier
132133
}
133134
//make the java compile task run after the jamplate task
134135
project.tasks.named(sourceSet.compileJavaTaskName) {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import org.jamplate.impl.model.FileDocument
2727
import org.jamplate.model.Compilation
2828
import org.jamplate.model.Document
2929
import org.jamplate.model.Environment
30+
import org.jamplate.model.Memory
31+
32+
import java.util.function.Function
3033

3134
@SuppressWarnings('GrMethodMayBeStatic')
3235
class ProcessJamplateTask extends DefaultTask {
@@ -35,22 +38,19 @@ class ProcessJamplateTask extends DefaultTask {
3538
@OutputDirectory
3639
File output
3740

38-
protected Map defaultMemory
41+
protected Map<String, Object> defaultMemory
42+
protected Function<Compilation, Memory> memorySupplier
3943

4044
@TaskAction
4145
void processJamplate() throws IOException {
42-
jamplate(input, output, defaultMemory)
43-
}
44-
45-
protected static void jamplate(File input, File output, Map memoryDefaults) {
4646
Objects.requireNonNull(input, "input")
4747
Objects.requireNonNull(output, "output")
4848

4949
Document[] documents = FileDocument.hierarchy(input)
5050

5151
Environment environment = new EnvironmentImpl()
5252

53-
environment.meta[Meta.MEMORY] = memoryDefaults
53+
environment.meta[Meta.MEMORY] = defaultMemory
5454
environment.meta[Meta.PROJECT] = input
5555
environment.meta[Meta.OUTPUT] = output
5656

@@ -69,7 +69,9 @@ class ProcessJamplateTask extends DefaultTask {
6969
.filter({ compilation -> compilation.rootTree.document().toString().endsWith(".jamplate") })
7070
.toArray({ len -> new Compilation[len] })
7171

72-
boolean executed = Jamplate.execute(environment, jamplates)
72+
boolean executed = this.memorySupplier == null ?
73+
Jamplate.execute(environment, jamplates) :
74+
Jamplate.execute(environment, this.memorySupplier, jamplates)
7375

7476
if (!executed) {
7577
System.err.println("Runtime Error\n")

0 commit comments

Comments
 (0)