Skip to content

Commit 507fb41

Browse files
Shorter scripts
1 parent 45f4316 commit 507fb41

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Logging is very descriptive allowing you to see what was done and what was skipp
288288
You can leverage the [repository service](https://github.com/wttech/acm/blob/main/core/src/main/java/dev/vml/es/acm/core/repo/Repo.java) (`repo`) to efficiently perform JCR operations such as reading, writing, and deleting nodes with concise, expressive code.
289289
The out-of-the-box AEM API often requires extensive boilerplate code and can behave unpredictably in certain scenarios. The [RepoResource](https://github.com/wttech/acm/blob/main/core/src/main/java/dev/vml/es/acm/core/repo/RepoResource.java) API streamlines these operations, making repository programming more enjoyable, concise, and reliable.
290290

291-
The repo service abstracts away the complexity of managing dry-run and auto-commit behaviors—features that are often error-prone and cumbersome to implement manuallyensuring safe, predictable, and streamlined repository operations.
291+
The repo service abstracts away the complexity of managing dry-run and auto-commit behaviors—features that are often error-prone and cumbersome to implement manuallyensuring safe, predictable, and streamlined repository operations.
292292

293293
```groovy
294294
void describeRun() {
@@ -307,16 +307,14 @@ void doRun() {
307307
println "Creating a folder structure in the temporary directory of the repository."
308308
def dataFolder = repo.get("/tmp/acm/demo/data").ensureFolder()
309309
for (int i = 0; i < 5; i++) {
310-
def child = dataFolder.child("child-\${i+1}")
311-
child.save(["foo": "bar"])
310+
def child = dataFolder.child("child-\${i+1}").save(["foo": "bar"])
312311
child.updateProperty("foo") { v -> v.toUpperCase() }
313312
}
314313
println "Folder '${dataFolder.path}' has now ${dataFolder.descendants().count()} descendant(s)."
315314
316315
println "Creating a post in the temporary directory of the repository."
317316
def postFolder = repo.get("/tmp/acm/demo/posts").ensureFolder()
318-
def post = postFolder.child("hello-world.yml")
319-
post.saveFile("application/x-yaml") { output ->
317+
def post = postFolder.child("hello-world.yml").saveFile("application/x-yaml") { output ->
320318
formatter.yml.write(output, [
321319
title: "Hello World",
322320
description: "This is a sample post.",

core/src/main/java/dev/vml/es/acm/core/repo/RepoResource.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ public RepoResource ensure(String resourceType) {
116116
return this;
117117
}
118118

119-
public Resource save(String key, Object value) {
120-
return save(Collections.singletonMap(key, value));
119+
public RepoResource save(String key, Object value) {
120+
save(Collections.singletonMap(key, value));
121+
return this;
121122
}
122123

123-
public Resource save(Map<String, Object> values) {
124+
public RepoResource save(Map<String, Object> values) {
124125
Resource result = resolve();
125126
if (result == null) {
126127
String parentPath = StringUtils.substringBeforeLast(path, "/");
@@ -134,7 +135,7 @@ public Resource save(Map<String, Object> values) {
134135
result = repo.getResourceResolver().create(parent, name, values);
135136
repo.commit(String.format("creating resource at path '%s'", path));
136137
LOG.info("Created resource at path '{}'", path);
137-
return result;
138+
return this;
138139
} catch (PersistenceException e) {
139140
throw new RepoException(String.format("Cannot save resource at path '%s'!", path), e);
140141
}
@@ -144,7 +145,7 @@ public Resource save(Map<String, Object> values) {
144145
repo.commit(String.format("updating resource at path '%s'", path));
145146
LOG.info("Updated resource at path '{}'", path);
146147
}
147-
return result;
148+
return this;
148149
}
149150

150151
public ValueMap properties() {
@@ -491,22 +492,25 @@ public RepoResourceState state() {
491492
return new RepoResourceState(path, true, resource.getValueMap());
492493
}
493494

494-
public void saveFile(String mimeType, Consumer<OutputStream> dataWriter) {
495+
public RepoResource saveFile(String mimeType, Consumer<OutputStream> dataWriter) {
495496
saveFileInternal(mimeType, null, dataWriter);
497+
return this;
496498
}
497499

498-
public void saveFile(String mimeType, File file) {
500+
public RepoResource saveFile(String mimeType, File file) {
499501
saveFile(mimeType, (OutputStream os) -> {
500502
try (InputStream is = Files.newInputStream(file.toPath())) {
501503
IOUtils.copy(is, os);
502504
} catch (IOException e) {
503505
throw new RepoException(String.format("Cannot write file '%s' to path '%s'!", file.getPath(), path), e);
504506
}
505507
});
508+
return this;
506509
}
507510

508-
public void saveFile(String mimeType, Object data) {
511+
public RepoResource saveFile(String mimeType, Object data) {
509512
saveFileInternal(mimeType, data, null);
513+
return this;
510514
}
511515

512516
private void saveFileInternal(String mimeType, Object data, Consumer<OutputStream> dataWriter) {

ui.content/src/main/content/jcr_root/conf/acm/settings/snippet/available/core/repo/demo.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ content: |
2626
println "Creating a folder structure in the temporary directory of the repository."
2727
def dataFolder = repo.get("/tmp/acm/demo/data").ensureFolder()
2828
for (int i = 0; i < 5; i++) {
29-
def child = dataFolder.child("child-\${i+1}")
30-
child.save(["foo": "bar"])
29+
def child = dataFolder.child("child-\${i+1}").save(["foo": "bar"])
3130
child.updateProperty("foo") { v -> v.toUpperCase() }
3231
}
3332
println "Folder '${dataFolder.path}' has now ${dataFolder.descendants().count()} descendant(s)."
@@ -38,8 +37,7 @@ content: |
3837
3938
println "Creating a post in the temporary directory of the repository."
4039
def postFolder = repo.get("/tmp/acm/demo/posts").ensureFolder()
41-
def post = postFolder.child("hello-world.yml")
42-
post.saveFile("application/x-yaml") { output ->
40+
def post = postFolder.child("hello-world.yml").saveFile("application/x-yaml") { output ->
4341
formatter.yml.write(output, [
4442
title: "Hello World",
4543
description: "This is a sample post.",

0 commit comments

Comments
 (0)