Skip to content

Commit

Permalink
Add nf-test and nextflow version to snapshots (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor authored Jan 27, 2024
1 parent d78856c commit b723447
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.concurrent.Callable;

import com.askimed.nf.test.nextflow.NextflowCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -33,6 +34,7 @@ public Integer call() throws Exception {

log.info(App.NAME + " " + App.VERSION);
log.info("Arguments: " + Arrays.toString(App.args));
log.info("Nextflow Version: " + NextflowCommand.getVersion());

if (!silent) {
printHeader();
Expand All @@ -48,7 +50,7 @@ private void printHeader() {
System.out.println();
System.out.println(Emoji.ROCKET + AnsiText.bold(" " + App.NAME + " " + App.VERSION));
System.out.println("https://code.askimed.com/nf-test");
System.out.println("(c) 2021 - 2023 Lukas Forer and Sebastian Schoenherr");
System.out.println("(c) 2021 - 2024 Lukas Forer and Sebastian Schoenherr");
System.out.println();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public SnapshotFile(String filename) {
String timestamp = object.get("timestamp").toString();
Object content = object.get("content");
SnapshotFileItem item = new SnapshotFileItem(timestamp, content);
if (object.containsKey("meta")) {
item.setMeta((Map<String, Object>) object.get("meta"));
}
snapshots.put(id, item);
}
log.debug("Load snapshots from file '{}'", filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.askimed.nf.test.App;
import com.askimed.nf.test.lang.extensions.util.SnapshotDiffUtil;

import com.askimed.nf.test.nextflow.NextflowCommand;
import groovy.json.JsonGenerator;
import groovy.json.JsonOutput;

Expand All @@ -14,16 +18,19 @@ public class SnapshotFileItem {

private String timestamp;

private Map<String, Object> meta = new HashMap<>();

public static DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");

public SnapshotFileItem(Object content) {
this.timestamp = createTimestamp();
this.content = content;
this(SnapshotFileItem.createTimestamp(), content);
}

public SnapshotFileItem(String timestamp, Object content) {
this.timestamp = timestamp;
this.content = content;
this.meta.put(App.NAME, App.VERSION);
this.meta.put("nextflow", NextflowCommand.getVersion());
}

public Object getContent() {
Expand All @@ -34,6 +41,14 @@ public String getTimestamp() {
return timestamp;
}

public Map<String, Object> getMeta() {
return meta;
}

public void setMeta(Map<String, Object> meta) {
this.meta = meta;
}

@Override
public boolean equals(Object object) {

Expand All @@ -55,7 +70,7 @@ public boolean equals(Object object) {

}

protected String createTimestamp() {
public static String createTimestamp() {
return DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now());
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/askimed/nf/test/nextflow/NextflowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,48 @@ public int printVersion() throws IOException {

}

private static String version = null;

public static String getVersion(){
if (version == null){
try {
version = new NextflowCommand().parseVersion();
} catch (Exception e){
version = "unknown";
}
}
return version;
}

public String parseVersion() throws IOException {

if (binary == null) {
throw new IOException(ERROR);
}

List<String> args = new Vector<String>();
args.add("-version");

Command nextflow = new Command(binary);
nextflow.setParams(args);
nextflow.setSilent(true);
StringBuffer output = new StringBuffer();
nextflow.writeStderr(output);
nextflow.execute();
String versionPattern = "version (\\d+\\.\\d+\\.\\d+)";
Pattern pattern = Pattern.compile(versionPattern);
Matcher matcher = pattern.matcher(output);

if (matcher.find()) {
return matcher.group(1);
} else {
return "unknown";
}

}



protected void writeParamsJson(Map<String, Object> params, File paramsFile) throws IOException {

BufferedWriter writer = new BufferedWriter(new FileWriter(paramsFile));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/askimed/nf/test/util/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class Command {

private String stderrFileName = null;

private StringBuffer stdout;

private StringBuffer stderr;

public Command(String cmd, String... params) {
this.cmd = cmd;
this.params = params;
Expand All @@ -38,6 +42,15 @@ public void setParams(List<String> params) {
}
}

public void writeStdout(StringBuffer stdout) {
this.stdout = stdout;
}

public void writeStderr(StringBuffer stderr) {
this.stderr = stderr;
}


public void saveStdOut(String filename) {
this.stdoutFileName = filename;
}
Expand Down Expand Up @@ -69,10 +82,12 @@ public int execute() {

Process process = builder.start();
CommandStreamHandler handler = new CommandStreamHandler(process.getInputStream(), stdoutFileName);
handler.setStringBuffer(stdout);
handler.setSilent(silent);
Thread inputStreamHandler = new Thread(handler);

CommandStreamHandler handler2 = new CommandStreamHandler(process.getErrorStream(), stderrFileName);
handler.setStringBuffer(stderr);
handler2.setSilent(silent);
Thread errorStreamHandler = new Thread(handler2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class CommandStreamHandler implements Runnable {

private String filename = null;

private StringBuffer memory;

public CommandStreamHandler(InputStream is) {
this.is = new BufferedReader(new InputStreamReader(is));
}
Expand All @@ -23,6 +25,10 @@ public void setSilent(boolean silent) {
this.silent = silent;
}

public void setStringBuffer(StringBuffer memory) {
this.memory = memory;
}

public void setFilename(String filename) {
this.filename = filename;
}
Expand All @@ -43,6 +49,9 @@ public void run() {

String line = null;
while ((line = is.readLine()) != null) {
if (memory != null) {
memory.append(line).append("\n");
}
if (!silent) {
System.out.println(" > " + line);
}
Expand Down

0 comments on commit b723447

Please sign in to comment.