Skip to content

Commit

Permalink
detect FIFTPATH automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
neodix42 committed Jun 3, 2024
1 parent 60cbbea commit d84c8bc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ton.java.smartcontract;

import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.java.Log;
Expand All @@ -13,10 +14,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.*;

import static java.util.Objects.nonNull;

Expand All @@ -34,6 +32,12 @@ public class FuncCompiler {
String fiftAsmLibraryPath;
String fiftSmartcontLibraryPath;

@Ignore
private String funcExecutable;
@Ignore
private String fiftExecutable;


public static class FuncCompilerBuilder {
}

Expand All @@ -42,9 +46,69 @@ public static FuncCompilerBuilder builder() {
}

private static class CustomFuncCompilerBuilder extends FuncCompilerBuilder {
private String errorMsg = "Make sure you have fift and func installed. See https://github.com/ton-blockchain/packages for instructions.";
private String funcAbsolutePath;
private String fiftAbsolutePath;

@Override
public FuncCompiler build() {

if (StringUtils.isEmpty(super.funcExecutablePath)) {
System.out.println("checking if func is installed...");

try {
ProcessBuilder pb = new ProcessBuilder("func", "-h").redirectErrorStream(true);
Process p = pb.start();
p.waitFor(5, TimeUnit.SECONDS);
if (p.exitValue() != 2) {
throw new Error("Cannot execute simple func command.\n" + errorMsg);
}
funcAbsolutePath = detectAbsolutePath("func");

System.out.println("func found at " + funcAbsolutePath);
super.funcExecutable = "func";

} catch (Exception e) {
e.printStackTrace();
throw new Error("Cannot execute simple func command.\n" + errorMsg);
}
} else {
System.out.println("using " + super.funcExecutablePath);
super.funcExecutable = super.funcExecutablePath;
}

if (StringUtils.isEmpty(super.fiftExecutablePath)) {
System.out.println("checking if fift is installed...");
try {
ProcessBuilder pb = new ProcessBuilder("fift", "-h").redirectErrorStream(true);
Process p = pb.start();
p.waitFor(5, TimeUnit.SECONDS);
if (p.exitValue() != 2) {
throw new Error("Cannot execute simple fift command.\n" + errorMsg);
}
fiftAbsolutePath = detectAbsolutePath("fift");
System.out.println("fift found at " + fiftAbsolutePath);
super.fiftExecutable = "fift";
} catch (Exception e) {
throw new Error("Cannot execute simple fift command.\n" + errorMsg);
}
} else {
System.out.println("using " + super.fiftExecutablePath);
super.fiftExecutable = super.fiftExecutablePath;
}

if (StringUtils.isEmpty(super.fiftAsmLibraryPath)) {
super.fiftAsmLibraryPath = new File(fiftAbsolutePath).getParent() + File.separator + ".." + File.separator +
"lib" + File.separator + "ton" + File.separator + "bin" + File.separator + "lib";
}

if (StringUtils.isEmpty(super.fiftSmartcontLibraryPath)) {
super.fiftSmartcontLibraryPath = new File(fiftAbsolutePath).getParent() + File.separator + ".." + File.separator +
"lib" + File.separator + "ton" + File.separator + "bin" + File.separator + "smartcont";
}

System.out.println("using FIFTPATH: " + super.fiftAsmLibraryPath + ":" + super.fiftSmartcontLibraryPath);

return super.build();
}
}
Expand All @@ -66,16 +130,16 @@ public String compile() throws IOException, ExecutionException, InterruptedExcep
return Utils.bytesToHex(bocContent);
}

public String executeFunc(String... params) throws ExecutionException, InterruptedException {
Pair<Process, Future<String>> result = execute("func", params);
private String executeFunc(String... params) throws ExecutionException, InterruptedException {
Pair<Process, Future<String>> result = execute(funcExecutable, params);

return result.getRight().get();
}

public String executeFift(String... params) {
private String executeFift(String... params) {
String[] withInclude = new String[]{"-I", fiftAsmLibraryPath + ":" + fiftSmartcontLibraryPath};
String[] all = ArrayUtils.addAll(withInclude, params);
Pair<Process, Future<String>> result = execute("fift", all);
Pair<Process, Future<String>> result = execute(fiftExecutable, all);
if (nonNull(result)) {
try {
return result.getRight().get();
Expand All @@ -88,7 +152,7 @@ public String executeFift(String... params) {
}
}

public Pair<Process, Future<String>> execute(String pathToBinary, String... command) {
private Pair<Process, Future<String>> execute(String pathToBinary, String... command) {

String[] withBinaryCommand = new String[]{pathToBinary};

Expand Down Expand Up @@ -131,4 +195,20 @@ public Pair<Process, Future<String>> execute(String pathToBinary, String... comm
return null;
}
}

private static String detectAbsolutePath(String executable) {
try {
ProcessBuilder pb;
if (Utils.getOS() == Utils.OS.WINDOWS) {
pb = new ProcessBuilder("where", executable).redirectErrorStream(true);
} else {
pb = new ProcessBuilder("which", executable).redirectErrorStream(true);
}
Process p = pb.start();
p.waitFor(5, TimeUnit.SECONDS);
return IOUtils.toString(p.getInputStream(), Charset.defaultCharset());
} catch (Exception e) {
throw new Error("Cannot detect absolute path to executable " + executable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@RunWith(JUnit4.class)
public class TestFuncCompiler {
/**
* Make sure you have fift and func installed. See <a href="https://github.com/ton-blockchain/packages">packages</a> for instructions.
* Make sure you have fift and func installed in your system. See <a href="https://github.com/ton-blockchain/packages">packages</a> for instructions.
* Example is based on new-wallet-v4r2.fc smart contract. You can specify path to any smart contract.
*/
@Test
Expand All @@ -36,8 +36,10 @@ public void testFuncCompiler() throws URISyntaxException, InterruptedException,
FuncCompiler smcFunc = FuncCompiler.builder()
// .contractPath("C:/stablecoin/contracts/jetton-minter.fc")
.contractPath(contractAbsolutePath)
.fiftAsmLibraryPath("C:/ProgramData/chocolatey/lib/ton/bin/lib") // todo detect automatically
.fiftSmartcontLibraryPath("C:/ProgramData/chocolatey/lib/ton/bin/smartcont")
// .fiftExecutablePath("C:/ProgramData/chocolatey/bin/fift")
// .funcExecutablePath("C:/ProgramData/chocolatey/bin/func")
// .fiftAsmLibraryPath("C:/ProgramData/chocolatey/lib/ton/bin/lib")
// .fiftSmartcontLibraryPath("C:/ProgramData/chocolatey/lib/ton/bin/smartcont")
.build();

String codeCellHex = smcFunc.compile();
Expand All @@ -49,7 +51,8 @@ public void testFuncCompiler() throws URISyntaxException, InterruptedException,
.storeUint(42, 32) // wallet id
.storeBytes(keyPair.getPublicKey())
.storeUint(0, 1) //plugins dict empty
.endCell().toHex();
.endCell()
.toHex();

log.info("codeCellHex {}", codeCellHex);
log.info("dataCellHex {}", dataCellHex);
Expand Down Expand Up @@ -81,8 +84,8 @@ public void testFuncCompiler() throws URISyntaxException, InterruptedException,

Cell deployMessageBody = CellBuilder.beginCell()
.storeUint(42, 32) // wallet-id
.storeInt(-1, 32) // valid-until
.storeUint(0, 32) //seqno
.storeInt(-1, 32) // valid-until
.storeUint(0, 32) //seqno
.endCell();

smc.deploy(deployMessageBody);
Expand Down

0 comments on commit d84c8bc

Please sign in to comment.