writeBuildAndRunScript writes the contents of a file that builds code and then runs the resulting program
(ctx *gcp.Context, sl *libcnb.Layer, cfg Config)
| 87 | |
| 88 | // writeBuildAndRunScript writes the contents of a file that builds code and then runs the resulting program |
| 89 | func writeBuildAndRunScript(ctx *gcp.Context, sl *libcnb.Layer, cfg Config) error { |
| 90 | sl.Launch = true |
| 91 | binDir := filepath.Join(sl.Path, "bin") |
| 92 | if err := ctx.MkdirAll(binDir, 0755); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | var cmd []string |
| 97 | if cfg.BuildCmd != nil { |
| 98 | cmd = append(cmd, strings.Join(cfg.BuildCmd, " ")) |
| 99 | } |
| 100 | if cfg.RunCmd != nil { |
| 101 | cmd = append(cmd, strings.Join(cfg.RunCmd, " ")) |
| 102 | } |
| 103 | |
| 104 | c := fmt.Sprintf("#!/bin/sh\n%s", strings.Join(cmd, " && ")) |
| 105 | br := filepath.Join(binDir, buildAndRun) |
| 106 | if err := ctx.WriteFile(br, []byte(c), os.FileMode(0755)); err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | c = fmt.Sprintf("#!/bin/sh\nwatchexec -r -e %s %s", strings.Join(cfg.Ext, ","), br) |
| 111 | wr := filepath.Join(binDir, WatchAndRun) |
| 112 | if err := ctx.WriteFile(wr, []byte(c), os.FileMode(0755)); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // installFileWatcher installs the `watchexec` file watcher. |
| 119 | func installFileWatcher(ctx *gcp.Context) error { |