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

Fixes #74 - Support selecting Java version for a workflow #75

Merged
merged 3 commits into from
Feb 19, 2024
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
38 changes: 36 additions & 2 deletions src/main/java/com/manorrock/parrot/Parrot.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private Workflow generateWorkflow(ParrotContext context) {
job.setRunsOn(context.getRunsOn());
jobs.put("validate", job);
workflow.setJobs(jobs);

LinkedHashMap<String, Object> login = new LinkedHashMap<>();
login.put("uses", "azure/login@v1");
HashMap<String, Object> with = new HashMap<>();
Expand All @@ -275,10 +276,12 @@ private Workflow generateWorkflow(ParrotContext context) {
with.put("enable-AzPSSession", "true");
}
login.put("with", with);
job.getSteps().add(login);

LinkedHashMap<String, Object> checkout = new LinkedHashMap<>();
checkout.put("uses", "actions/checkout@v3");
job.getSteps().add(login);
job.getSteps().add(checkout);

HashMap<String, Object> run = new HashMap<>();
YAMLLiteralBlock literalBlock = new YAMLLiteralBlock();
run.put("run", literalBlock);
Expand Down Expand Up @@ -356,6 +359,8 @@ private void processCommentSnippet(ParrotContext context, HtmlCommentBlock comme
processIncludeSnippet(context, matcher.group(2));
} else if (action.equals("includeOnce")) {
processIncludeOnceSnippet(context, matcher.group(2));
} else if (action.equals("javaVersion")) {
processJavaVersion(context, matcher.group(2));
} else if (action.equals("name")) {
processWorkflowName(context, matcher.group(2));
} else if (action.equals("pushPath")) {
Expand Down Expand Up @@ -390,6 +395,35 @@ private void processCronDirective(ParrotContext context, String cron) {
}
}

/**
* Process the Java version directive.
*
* <p>
* This directive sets the Java version for the workflow.
* </p>
*
* @param context the context.
* @param javaVersion the Java version.
*/
private void processJavaVersion(ParrotContext context, String javaVersion) {
/*
* - uses: actions/setup-java@v4
* with:
* distribution: 'temurin'
* java-version: '21'
*/
if (javaVersion != null) {
Job job = (Job) context.getWorkflow().getJobs().get("validate");
LinkedHashMap<String, Object> java = new LinkedHashMap<>();
java.put("uses", "actions/setup-java@v4");
HashMap<String, String> with = new HashMap<>();
with.put("distribution", "temurin");
with.put("java-version", javaVersion);
java.put("with", with);
job.getSteps().add(0, java);
}
}

/**
* Process the directOnly snippet.
*
Expand Down Expand Up @@ -582,7 +616,7 @@ private void processRunsOn(ParrotContext context, String runsOn) {
* @param context the context.
*/
private void processSkip(ParrotContext context) {
if (context.getSnippets().size() > 0) {
if (!context.getSnippets().isEmpty()) {
context.getSnippets().remove(0);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/manorrock/parrot/ParrotContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
*/
public class ParrotContext {

/**
* Stores the Java version.
*/
private String javaVersion;

/**
* Stores the runs-on.
*/
Expand Down Expand Up @@ -102,6 +107,15 @@ public File getCurrentFile() {
return fileStack.peek();
}

/**
* Get the Java version.
*
* @return the Java version.
*/
public String getJavaVersion() {
return javaVersion;
}

/**
* Get the runs-on.
*
Expand Down Expand Up @@ -191,6 +205,15 @@ public ShellScript getShellScript() {
public void setCurrentFile(File currentFile) {
fileStack.push(currentFile);
}

/**
* Set the Java version.
*
* @param javaVersion the Java version.
*/
public void setJavaVersion(String javaVersion) {
this.javaVersion = javaVersion;
}

/**
* Set the runs-on.
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/manorrock/parrot/ParrotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,21 @@ void testCronSnippet() throws IOException {
" schedule: \n" +
" - cron: '0 1 * * 2'"));
}

/**
* Test a Java version snippet.
*/
@Test
void testJavaVersionSnippet() throws IOException {
File inputFile = new File("src/test/resources/java.md");
File outputDirectory = new File("target/parrotTest");
outputDirectory.mkdirs();
Parrot parrot = new Parrot();
parrot.setBaseDirectory(new File("src/test/resources"));
parrot.setOutputDirectory(outputDirectory);
parrot.processFile(inputFile);
File outputFile = new File("target/parrotTest/java_md.yml");
String content = new String(Files.readAllBytes(outputFile.toPath()));
assertTrue(content.contains("java-version: '21'"));
}
}
5 changes: 5 additions & 0 deletions src/test/resources/java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Java version

This generates a Java version in the workflow:

<!-- workflow.javaVersion(21) -->
Loading