CheckTools checks if tools exist and repair them if necessary, finally make sure tool paths are in PATH
(ctx context.Context, tools ...string)
| 28 | |
| 29 | // CheckTools checks if tools exist and repair them if necessary, finally make sure tool paths are in PATH |
| 30 | func CheckTools(ctx context.Context, tools ...string) error { |
| 31 | // Remove duplicated tools. |
| 32 | var uniqueTools []string |
| 33 | for _, tool := range tools { |
| 34 | if !slices.Contains(uniqueTools, tool) { |
| 35 | uniqueTools = append(uniqueTools, tool) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Determine current architecture. |
| 40 | arch := runtime.GOARCH |
| 41 | switch arch { |
| 42 | case "amd64", "x86_64": |
| 43 | arch = "x86_64" |
| 44 | case "arm64": |
| 45 | arch = "aarch64" |
| 46 | } |
| 47 | |
| 48 | // Read and decode static file. |
| 49 | staticFile := fmt.Sprintf("static/%s-%s.toml", arch, runtime.GOOS) |
| 50 | bytes, err := static.ReadFile(staticFile) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | var buildTools BuildTools |
| 55 | if err := toml.Unmarshal(bytes, &buildTools); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | confToolsFile := filepath.Join(dirs.WorkspaceDir, "conf", "buildtools", arch+"-"+runtime.GOOS+".toml") |
| 60 | if fileio.PathExists(confToolsFile) { |
| 61 | bytes, err := os.ReadFile(confToolsFile) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | var confBuildTools BuildTools |
| 66 | if err := toml.Unmarshal(bytes, &confBuildTools); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | buildTools = buildTools.merge(confBuildTools) |
| 70 | } |
| 71 | |
| 72 | // Check if need to install python3 and msys2. |
| 73 | for _, tool := range uniqueTools { |
| 74 | len := strings.Count(tool, ":") |
| 75 | if len == 0 { |
| 76 | continue |
| 77 | } |
| 78 | if len != 1 { |
| 79 | return fmt.Errorf("invalid tool format: %s", tool) |
| 80 | } |
| 81 | |
| 82 | if strings.HasPrefix(tool, "msys2:") && !slices.Contains(uniqueTools, "msys2") { |
| 83 | uniqueTools = append(uniqueTools, "msys2") |
| 84 | continue |
| 85 | } |
| 86 | if strings.HasPrefix(tool, "python3:") && !slices.Contains(uniqueTools, "python3") { |
| 87 | uniqueTools = append(uniqueTools, "python3") |