This chapter continues from where Creating the workspace definition left us.

Using a script/program define target content

In case java-based tools are not enough, you can use a script to define content for a target. Each script invocation gets a dedicated temporary directory in which to create temporary files, if needed.

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef/IwanttutorialWorkspace.java"
package com.example.wsdef;
import java.util.Arrays;
import java.util.List;
import org.fluentjava.iwant.api.core.Concatenated;
import org.fluentjava.iwant.api.core.Concatenated.ConcatenatedBuilder;
import org.fluentjava.iwant.api.core.HelloTarget;
import org.fluentjava.iwant.api.core.ScriptGenerated;
import org.fluentjava.iwant.api.model.SideEffect;
import org.fluentjava.iwant.api.model.Target;
import org.fluentjava.iwant.api.wsdef.SideEffectDefinitionContext;
import org.fluentjava.iwant.api.wsdef.TargetDefinitionContext;
import org.fluentjava.iwant.api.wsdef.Workspace;
import org.fluentjava.iwant.eclipsesettings.EclipseSettings;
public class IwanttutorialWorkspace implements Workspace {
@Override
public List<? extends Target> targets(TargetDefinitionContext ctx) {
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"));
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"),
scriptGenerated());
}
private static Target scriptGenerated() {
ConcatenatedBuilder script = Concatenated.named("shellScript");
script.string("#!/bin/bash\n");
script.string("set -eu\n");
script.string("DEST=$1\n");
script.string("echo Running $0\n");
script.string("echo 'We have a dedicated temporary dir:'\n");
script.string("pwd\n");
script.string("echo It is ok to create temporary files\n");
script.string("touch tmpfile\n");
script.string("ls -F\n");
script.string("echo Generating $DEST\n");
script.string("echo 'Hello from script' > \"$DEST\"\n");
return ScriptGenerated.named("scriptGenerated").byScript(script.end());
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.end());
}
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
hello
scriptGenerated
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/scriptGenerated/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.core.Concatenated shellScript)
(0/1 D! org.fluentjava.iwant.api.core.ScriptGenerated scriptGenerated)
Running /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0/script
We have a dedicated temporary dir:
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/temp/w-0
It is ok to create temporary files
script*
tmpfile
Generating /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/scriptGenerated
Hello from script
Output asserted