Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested tokens #3

Merged
merged 3 commits into from
Feb 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/org/jenkinsci/plugins/tokenmacro/TokenMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ public abstract class TokenMacro implements ExtensionPoint {
public abstract String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName, Map<String, String> arguments, ListMultimap<String, String> argumentMultimap)
throws MacroEvaluationException, IOException, InterruptedException;

/**
* Returns true if this object allows for nested content replacements.
*
* @return true
* ... to have the replaced text passed again to {@link #expand(AbstractBuild, TaskListener, String)} for additional expansion.
*/
public boolean hasNestedContent() {
return false;
}

/**
* All registered extension points.
*/
Expand Down Expand Up @@ -157,6 +167,9 @@ public static String expand(AbstractBuild<?,?> context, TaskListener listener, S
for (TokenMacro tm : all) {
if (tm.acceptsMacroName(tokenName)) {
replacement = tm.evaluate(context,listener,tokenName,map,args);
if(tm.hasNestedContent()) {
replacement = expand(context,listener,replacement);
}
break;
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/jenkinsci/plugins/tokenmacro/TokenMacroTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public void testBasics() throws Exception {
assertEquals("{abc=[def, ghi], jkl=[true]}",TokenMacro.expand(b,listener,"${TEST,abc=\"def\",abc=\"ghi\",jkl=true}"));
}

public void testNested() throws Exception {
FreeStyleProject p = createFreeStyleProject("foo");
FreeStyleBuild b = p.scheduleBuild2(0).get();

listener = new StreamTaskListener(System.out);

assertEquals("{abc=[def, ghi], jkl=[true]}",TokenMacro.expand(b,listener,"${TEST_NESTED}"));
}

@TestExtension
public static class TestMacro extends TokenMacro {
@Override
Expand All @@ -41,4 +50,22 @@ public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, Strin
return argumentMultimap.toString();
}
}

@TestExtension
public static class NestedTestMacro extends TokenMacro {
@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals("TEST_NESTED");
}

@Override
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName, Map<String, String> arguments, ListMultimap<String, String> argumentMultimap) throws MacroEvaluationException, IOException, InterruptedException {
return "${TEST,abc=\"def\",abc=\"ghi\",jkl=true}";
}

@Override
public boolean hasNestedContent() {
return true;
}
}
}