package com.petukhovsky.jvaluer.invoker;

import com.petukhovsky.jvaluer.JValuer;
import com.petukhovsky.jvaluer.commons.invoker.Invoker;
import com.petukhovsky.jvaluer.commons.run.RunInfo;
import com.petukhovsky.jvaluer.commons.run.RunOptions;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Created by Arthur Petukhovsky on 6/13/2016.
 */
public class CustomInvoker implements Invoker {
    private final String pattern;
    private final Path executable;

    public CustomInvoker(String executable, String pattern) {
        this.pattern = pattern;
        this.executable = Paths.get(executable);
    }

    @Override
    public RunInfo run(JValuer jValuer, RunOptions options) {
        options = options.setArgs(processPattern(pattern, options.getArgs(), options.getExe().toString())).setExe(this.executable);
        return jValuer.invokeDefault(options);
    }

    private String processPattern(String pattern, String args, String executable) {
        return pattern.replaceAll("\\{args\\}", args)
                .replaceAll("\\{exe\\}", "\"" + executable + "\"");
    }
}

@@ -998,22 +998,15 @@
 lace
-All(%22%5C%5C
+(%22
 %7Bargs
-%5C%5C
 %7D%22, 
@@ -1014,33 +1014,16 @@
 rgs)
-%0A                
 .replace
 All(
@@ -1022,21 +1022,14 @@
 lace
-All(%22%5C%5C
+(%22
 %7Bexe
-%5C%5C
 %7D%22, 

replace fix