diff --git a/cmd/common/utils.go b/cmd/common/utils.go index d65ed32..c797ae0 100644 --- a/cmd/common/utils.go +++ b/cmd/common/utils.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os" + "os/exec" "path/filepath" "strings" "time" @@ -159,3 +160,38 @@ func ToStringSlice(args []any) []string { } return result } + +// Gets a build command for either Golang or Typescript based on the filename +func GetBuildCmd(inputFile string, outputFile string, rootFolder string) *exec.Cmd { + isTypescriptWorkflow := strings.HasSuffix(inputFile, ".ts") + + var buildCmd *exec.Cmd + if isTypescriptWorkflow { + buildCmd = exec.Command( + "bun", + "cre-compile", + inputFile, + outputFile, + ) + } else { + // The build command for reproducible and trimmed binaries. + // -trimpath removes all file system paths from the compiled binary. + // -ldflags="-buildid= -w -s" further reduces the binary size: + // -buildid= removes the build ID, ensuring reproducibility. + // -w disables DWARF debugging information. + // -s removes the symbol table. + buildCmd = exec.Command( + "go", + "build", + "-o", outputFile, + "-trimpath", + "-ldflags=-buildid= -w -s", + inputFile, + ) + buildCmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm", "CGO_ENABLED=0") + } + + buildCmd.Dir = rootFolder + + return buildCmd +} diff --git a/cmd/workflow/deploy/compile.go b/cmd/workflow/deploy/compile.go index 8531f14..6bcb3ab 100644 --- a/cmd/workflow/deploy/compile.go +++ b/cmd/workflow/deploy/compile.go @@ -5,11 +5,12 @@ import ( "encoding/base64" "fmt" "os" - "os/exec" "path/filepath" "strings" "github.com/andybalholm/brotli" + + cmdcommon "github.com/smartcontractkit/cre-cli/cmd/common" ) func (h *handler) Compile() error { @@ -43,24 +44,7 @@ func (h *handler) Compile() error { tmpWasmFileName := "tmp.wasm" workflowMainFile := filepath.Base(h.inputs.WorkflowPath) - - // The build command for reproducible and trimmed binaries. - // -trimpath removes all file system paths from the compiled binary. - // -ldflags="-buildid= -w -s" further reduces the binary size: - // -buildid= removes the build ID, ensuring reproducibility. - // -w disables DWARF debugging information. - // -s removes the symbol table. - buildCmd := exec.Command( - "go", - "build", - "-o", tmpWasmFileName, - "-trimpath", - "-ldflags=-buildid= -w -s", - workflowMainFile, - ) - - buildCmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm", "CGO_ENABLED=0") - buildCmd.Dir = workflowRootFolder + buildCmd := cmdcommon.GetBuildCmd(workflowMainFile, tmpWasmFileName, workflowRootFolder) h.log.Debug(). Str("Workflow directory", buildCmd.Dir). Str("Command", buildCmd.String()). diff --git a/cmd/workflow/simulate/simulate.go b/cmd/workflow/simulate/simulate.go index b9a6c82..3182cb8 100644 --- a/cmd/workflow/simulate/simulate.go +++ b/cmd/workflow/simulate/simulate.go @@ -10,7 +10,6 @@ import ( "math" "math/big" "os" - "os/exec" "os/signal" "path/filepath" "strconv" @@ -38,6 +37,7 @@ import ( simulator "github.com/smartcontractkit/chainlink/v2/core/services/workflows/cmd/cre/utils" v2 "github.com/smartcontractkit/chainlink/v2/core/services/workflows/v2" + cmdcommon "github.com/smartcontractkit/cre-cli/cmd/common" "github.com/smartcontractkit/cre-cli/internal/runtime" "github.com/smartcontractkit/cre-cli/internal/settings" "github.com/smartcontractkit/cre-cli/internal/validation" @@ -182,35 +182,8 @@ func (h *handler) Execute(inputs Inputs) error { workflowRootFolder := filepath.Dir(inputs.WorkflowPath) tmpWasmFileName := "tmp.wasm" workflowMainFile := filepath.Base(inputs.WorkflowPath) - isTypescriptWorkflow := strings.HasSuffix(workflowMainFile, ".ts") - - var buildCmd *exec.Cmd - if isTypescriptWorkflow { - buildCmd = exec.Command( - "bun", - "cre-compile", - workflowMainFile, - tmpWasmFileName, - ) - } else { - // The build command for reproducible and trimmed binaries. - // -trimpath removes all file system paths from the compiled binary. - // -ldflags="-buildid= -w -s" further reduces the binary size: - // -buildid= removes the build ID, ensuring reproducibility. - // -w disables DWARF debugging information. - // -s removes the symbol table. - buildCmd = exec.Command( - "go", - "build", - "-o", tmpWasmFileName, - "-trimpath", - "-ldflags=-buildid= -w -s", - workflowMainFile, - ) - buildCmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm", "CGO_ENABLED=0") - } - - buildCmd.Dir = workflowRootFolder + buildCmd := cmdcommon.GetBuildCmd(workflowMainFile, tmpWasmFileName, workflowRootFolder) + h.log.Debug(). Str("Workflow directory", buildCmd.Dir). Str("Command", buildCmd.String()).