|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "builder/installer" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/roemer/gotaskr/execr" |
| 12 | + "github.com/roemer/gover" |
| 13 | +) |
| 14 | + |
| 15 | +////////// |
| 16 | +// Configuration |
| 17 | +////////// |
| 18 | + |
| 19 | +// Regex with 2-3 digits like 1.0 or 1.79.0 |
| 20 | +var threeDigitRegex *regexp.Regexp = regexp.MustCompile(`^?(\d+)\.(\d+)(?:\.(\d+))?$`) |
| 21 | + |
| 22 | +// Full Regex versioning, like 1.0.0-alpha.2 |
| 23 | +var semVerRegex *regexp.Regexp = regexp.MustCompile(`^?(\d+)\.(\d+)(?:\.(\d+))?(?:-([a-z]+)(?:\.?(\d+))?)?$`) |
| 24 | + |
| 25 | +////////// |
| 26 | +// Main |
| 27 | +////////// |
| 28 | + |
| 29 | +func main() { |
| 30 | + if err := runMain(); err != nil { |
| 31 | + fmt.Printf("Error: %v\n", err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func runMain() error { |
| 37 | + // Handle the flags |
| 38 | + version := flag.String("version", "lts", "") |
| 39 | + rustupVersion := flag.String("rustupVersion", "latest", "") |
| 40 | + profile := flag.String("profile", "minimal", "") |
| 41 | + components := flag.String("components", "rustfmt,rust-analyzer,rust-src,clippy", "") |
| 42 | + enableWindowsTarget := flag.Bool("enableWindowsTarget", false, "") |
| 43 | + flag.Parse() |
| 44 | + |
| 45 | + // Create and process the feature |
| 46 | + feature := installer.NewFeature("PF Rust", true, |
| 47 | + &rustupComponent{ |
| 48 | + ComponentBase: installer.NewComponentBase("rustup", *rustupVersion), |
| 49 | + profile: *profile, |
| 50 | + }, |
| 51 | + &rustComponent{ |
| 52 | + ComponentBase: installer.NewComponentBase("rust", *version), |
| 53 | + components: *components, |
| 54 | + profile: *profile, |
| 55 | + }, |
| 56 | + &buildEssentialComponent{ |
| 57 | + ComponentBase: installer.NewComponentBase("build-essential", installer.VERSION_SYSTEM_DEFAULT), |
| 58 | + }, |
| 59 | + ) |
| 60 | + // Optional component |
| 61 | + if *enableWindowsTarget { |
| 62 | + feature.AddComponents(&windowsTargetComponent{ |
| 63 | + ComponentBase: installer.NewComponentBase("windows-target", installer.VERSION_IRRELEVANT), |
| 64 | + }) |
| 65 | + } |
| 66 | + // Last component |
| 67 | + feature.AddComponents(&permissionsComponent{ |
| 68 | + ComponentBase: installer.NewComponentBase("permissions", installer.VERSION_IRRELEVANT), |
| 69 | + }) |
| 70 | + return feature.Process() |
| 71 | +} |
| 72 | + |
| 73 | +////////// |
| 74 | +// Implementation |
| 75 | +////////// |
| 76 | + |
| 77 | +type rustupComponent struct { |
| 78 | + *installer.ComponentBase |
| 79 | + profile string |
| 80 | +} |
| 81 | + |
| 82 | +func (c *rustupComponent) GetAllVersions() ([]*gover.Version, error) { |
| 83 | + allTags, err := installer.Tools.GitHub.GetTags("rust-lang", "rustup") |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + return installer.Tools.Versioning.ParseVersionsFromList(allTags, threeDigitRegex, true) |
| 88 | +} |
| 89 | + |
| 90 | +func (c *rustupComponent) InstallVersion(version *gover.Version) error { |
| 91 | + // Download the file |
| 92 | + archPart, err := installer.Tools.System.MapArchitecture(map[string]string{ |
| 93 | + installer.AMD64: "x86_64", |
| 94 | + installer.ARM64: "aarch64", |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + fileName := "rustup-init" |
| 100 | + downloadUrl := fmt.Sprintf("https://static.rust-lang.org/rustup/archive/%s/%s-unknown-linux-gnu/rustup-init", version.Raw, archPart) |
| 101 | + if err := installer.Tools.Download.ToFile(downloadUrl, fileName, "Rustup-Init"); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + // Install it |
| 105 | + if err := os.Chmod(fileName, os.ModePerm); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + if err := execr.Run(true, "./"+fileName, "-y", "--default-toolchain", "none", "--no-modify-path", "--profile", c.profile); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + // Cleanup |
| 112 | + if err := os.Remove(fileName); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +type rustComponent struct { |
| 119 | + *installer.ComponentBase |
| 120 | + profile string |
| 121 | + components string |
| 122 | +} |
| 123 | + |
| 124 | +func (c *rustComponent) GetAllVersions() ([]*gover.Version, error) { |
| 125 | + allTags, err := installer.Tools.GitHub.GetTags("rust-lang", "rust") |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + return installer.Tools.Versioning.ParseVersionsFromList(allTags, semVerRegex, true) |
| 130 | +} |
| 131 | + |
| 132 | +func (c *rustComponent) InstallVersion(version *gover.Version) error { |
| 133 | + // Install it |
| 134 | + if err := execr.Run(true, "rustup", "toolchain", "install", "--profile", c.profile, "--no-self-update", version.Raw); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + // Installing the components |
| 138 | + fmt.Printf("Installing components: %s\n", c.components) |
| 139 | + args := []string{ |
| 140 | + "component", |
| 141 | + "add", |
| 142 | + } |
| 143 | + for _, component := range strings.Split(c.components, ",") { |
| 144 | + trimmed := strings.TrimSpace(component) |
| 145 | + if trimmed != "" { |
| 146 | + args = append(args, trimmed) |
| 147 | + } |
| 148 | + } |
| 149 | + if err := execr.Run(true, "rustup", args...); err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +type buildEssentialComponent struct { |
| 156 | + *installer.ComponentBase |
| 157 | +} |
| 158 | + |
| 159 | +func (c *buildEssentialComponent) InstallVersion(version *gover.Version) error { |
| 160 | + return installer.Tools.System.InstallPackages("build-essential") |
| 161 | +} |
| 162 | + |
| 163 | +type windowsTargetComponent struct { |
| 164 | + *installer.ComponentBase |
| 165 | +} |
| 166 | + |
| 167 | +func (c *windowsTargetComponent) InstallVersion(version *gover.Version) error { |
| 168 | + if err := execr.Run(true, "rustup", "target", "add", "x86_64-pc-windows-gnu"); err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + if err := installer.Tools.System.InstallPackages("mingw-w64"); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +type permissionsComponent struct { |
| 178 | + *installer.ComponentBase |
| 179 | +} |
| 180 | + |
| 181 | +func (c *permissionsComponent) InstallVersion(version *gover.Version) error { |
| 182 | + return execr.Run(true, "chmod", "-R", "777", os.Getenv("RUSTUP_HOME"), os.Getenv("CARGO_HOME")) |
| 183 | +} |
0 commit comments