|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + goWebuiRepo = "github.com/webui-dev/go-webui/v2" |
| 13 | + webuiRepo = "github.com/webui-dev/webui" |
| 14 | +) |
| 15 | + |
| 16 | +func goWebuiVersion(version string) string { |
| 17 | + return fmt.Sprintf("%s@%s", goWebuiRepo, version) |
| 18 | +} |
| 19 | + |
| 20 | +func webuiVersion(version string) string { |
| 21 | + return fmt.Sprintf("%s@%s", webuiRepo, version) |
| 22 | +} |
| 23 | + |
| 24 | +func main() { |
| 25 | + // Ensure the current directory is part of a go module. |
| 26 | + if !fileExists("go.mod") { |
| 27 | + errExit(`error: failed to find go.mod file in current directory. |
| 28 | + To set up the go-webui module, use this script in a module directory.`) |
| 29 | + } |
| 30 | + goTools := GetGo() |
| 31 | + // Run go commands |
| 32 | + iferrExit(goTools.Cmd("mod", "tidy").Run()) |
| 33 | + iferrExit(goTools.Cmd("get", goWebuiVersion("main")).Run()) |
| 34 | + iferrExit(goTools.CmdSilent("get", webuiVersion("main")).Run()) |
| 35 | + |
| 36 | + // Retrieve GOPATH (use environment variable if defined, otherwise go env) |
| 37 | + goPath, err := goTools.Gopath() |
| 38 | + iferrExit(err) |
| 39 | + |
| 40 | + // Parse the first matching version lines from go.sum |
| 41 | + // For go-webui/v2: |
| 42 | + goWebuiFullVersion := field(headN1(grep(goWebuiRepo, "go.sum")), 2) |
| 43 | + // For webui: |
| 44 | + webuiFullVersion := field(headN1(grep(webuiRepo, "go.sum")), 2) |
| 45 | + |
| 46 | + // Construct paths based on the parsed versions |
| 47 | + goWebuiPath := filepath.Join(goPath, "pkg", "mod", goWebuiVersion(goWebuiFullVersion)) |
| 48 | + webuiPath := filepath.Join(goPath, "pkg", "mod", webuiVersion(webuiFullVersion)) |
| 49 | + |
| 50 | + // Validate that these paths actually exist |
| 51 | + iferrExit(validatePaths(goWebuiPath, webuiPath)) |
| 52 | + linkName := filepath.Join(goWebuiPath, "webui") |
| 53 | + // Exit if link already exists in the directory of the used go-webui version. |
| 54 | + if dirExists(linkName) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // Store original permissions. |
| 59 | + // Not strictly necessary, yet we ensure end without changes to the original permissions. |
| 60 | + ogPerms, err := getPerms(goWebuiPath) |
| 61 | + iferrExit(err) |
| 62 | + iferrExit(os.Chmod(goWebuiPath, 0200+ogPerms)) |
| 63 | + // Linking allows using WebUI C even in cases of multiple go-webui versions without creating bloat. |
| 64 | + if err := os.Symlink(webuiPath, linkName); err != nil { |
| 65 | + if runtime.GOOS == "windows" { |
| 66 | + // (Requires Administrator privileges or Developer Mode) |
| 67 | + errExit("mklink failed. Run as Administrator if needed.") |
| 68 | + } |
| 69 | + errExit(err.Error()) |
| 70 | + } |
| 71 | + // Restore original permissions. |
| 72 | + iferrExit(os.Chmod(goWebuiPath, ogPerms)) |
| 73 | + iferrExit(goTools.Cmd("mod", "tidy").Run()) |
| 74 | +} |
| 75 | + |
| 76 | +func validatePaths(goWebuiPath, webuiPath string) error { |
| 77 | + var errs []error |
| 78 | + if !dirExists(goWebuiPath) { |
| 79 | + errs = append(errs, fmt.Errorf("failed to find go-webui in '%s'", goWebuiPath)) |
| 80 | + } |
| 81 | + if !dirExists(webuiPath) { |
| 82 | + errs = append(errs, fmt.Errorf("failed to find webui in '%s'", webuiPath)) |
| 83 | + } |
| 84 | + return errors.Join(errs...) |
| 85 | +} |
| 86 | + |
| 87 | +func iferrExit(err error) { |
| 88 | + if err != nil { |
| 89 | + fmt.Println(err.Error()) |
| 90 | + os.Exit(1) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func errExit(msg string) { |
| 95 | + fmt.Println(msg) |
| 96 | + os.Exit(1) |
| 97 | +} |
0 commit comments