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

Defining targets in bash

bash targets

~/iwant-tutorial $ find as-iwant-tutorial-developer/i-have/wsdef/src
as-iwant-tutorial-developer/i-have/wsdef/src
as-iwant-tutorial-developer/i-have/wsdef/src/main
as-iwant-tutorial-developer/i-have/wsdef/src/main/java
as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com
as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example
as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef
as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef/IwanttutorialWorkspaceFactory.java
as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef/IwanttutorialWorkspace.java
~/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.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.fluentjava.iwant.api.core.HelloTarget;
import org.fluentjava.iwant.api.bash.TargetImplementedInBash;
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"));
List<Target> t = new ArrayList<>();
t.addAll(TargetImplementedInBash.instancesFromDefaultIndexSh(ctx));
return t;
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.end());
}
}

Let's see where we need to define the index.

~/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)
Please define targets in /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/_index.sh

We create an index with one target.

~/iwant-tutorial $ mkdir as-iwant-tutorial-developer/i-have/wsdef/src/main/bash
~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/_index.sh"
targets() {
target hello-from-bash
}

We define the target in a script named after the target name.

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/hello-from-bash.sh"
path() {
echo "Hello from bash" > "$IWANT_DEST"
}

Now we can evaluate our first target defined in bash.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
hello-from-bash
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/hello-from-bash/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.bash.TargetImplementedInBash hello-from-bash)
--- Refreshing /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/hello-from-bash.sh
Hello from bash

target with ingredients

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/_index.sh"
targets() {
target hello-from-bash
target target-with-ingredients
}
~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/target-with-ingredients.sh"
ingredients() {
source-dep INGR1 source-ingredient
target-dep INGR2 hello-from-bash
}
path() {
echo "Target derived from ingredients:" > "$IWANT_DEST"
echo "--- $INGR1:" >> "$IWANT_DEST"
cat "$INGR1" >> "$IWANT_DEST"
echo "--- $INGR2:" >> "$IWANT_DEST"
cat "$INGR2" >> "$IWANT_DEST"
}
~/iwant-tutorial $ echo "source-ingredient-content" > source-ingredient
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
hello-from-bash
target-with-ingredients
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/target-with-ingredients/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.bash.TargetImplementedInBash target-with-ingredients)
--- Refreshing /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/target-with-ingredients.sh
Target derived from ingredients:
--- /home/hacker/iwant-tutorial/source-ingredient:
source-ingredient-content
--- /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/hello-from-bash:
Hello from bash

parameterized target

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/target-with-parameters.sh"
ingredients() {
param PARAM1 "$1"
param PARAM2 "$2"
}
path() {
echo "Target derived from parameters:" > "$IWANT_DEST"
echo "--- PARAM1: $PARAM1" >> "$IWANT_DEST"
echo "--- PARAM2: $PARAM2" >> "$IWANT_DEST"
}

name, script name, then arguments for ingredients

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/_index.sh"
targets() {
target hello-from-bash
target target-with-ingredients
target target-with-parameters-v1 target-with-parameters.sh 1 one
target target-with-parameters-v2 target-with-parameters.sh 2 two
}
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
hello-from-bash
target-with-ingredients
target-with-parameters-v1
target-with-parameters-v2
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/target-with-parameters-v1/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.bash.TargetImplementedInBash target-with-parameters-v1)
--- Refreshing /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/target-with-parameters.sh 1 one
Target derived from parameters:
--- PARAM1: 1
--- PARAM2: one
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/target-with-parameters-v2/as-path | xargs -r cat
(0/1 D! org.fluentjava.iwant.api.bash.TargetImplementedInBash target-with-parameters-v2)
--- Refreshing /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/i-have/wsdef/src/main/bash/target-with-parameters.sh 2 two
Target derived from parameters:
--- PARAM1: 2
--- PARAM2: two