|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/utils" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | +) |
| 13 | + |
| 14 | +func genInfoFlutter(r *Runtime) map[string]string { |
| 15 | + flutterFolder := "flutter" |
| 16 | + installDir := path.Join(Config.RuntimesDirectory(), flutterFolder) |
| 17 | + |
| 18 | + return map[string]string{ |
| 19 | + "installDir": installDir, |
| 20 | + "flutter": path.Join(installDir, "bin", "dart"), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func InstallFlutter(flutterRuntime *Runtime) error { |
| 25 | + |
| 26 | + log.Println("Fetching flutter...") |
| 27 | + downloadFlutterURL := getFlutterDownloadURL(flutterRuntime) |
| 28 | + // Extract filename from URL |
| 29 | + fileName := filepath.Base(downloadFlutterURL) |
| 30 | + localPath := filepath.Join(Config.RuntimesDirectory(), fileName) |
| 31 | + |
| 32 | + // Check if file already exists |
| 33 | + if _, err := os.Stat(localPath); err == nil { |
| 34 | + log.Printf("File %s already exists, skipping download", fileName) |
| 35 | + return nil |
| 36 | + } |
| 37 | + dartTar, err := utils.DownloadFile(downloadFlutterURL, Config.RuntimesDirectory()) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + // deflate node archive |
| 43 | + t, err := os.Open(dartTar) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + defer t.Close() |
| 48 | + err = utils.ExtractZip(t, Config.RuntimesDirectory()) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func InstallFlutterDartAnalyzer(flutterRuntime *Runtime, dartAnalyzer *Runtime, registry string) error { |
| 57 | + log.Println("Installing Dart Analyzer") |
| 58 | + cmd := exec.Command(flutterRuntime.Info()["flutter"], "pub", "add", "flutter_lints", "--dev") |
| 59 | + stdout, err := cmd.Output() |
| 60 | + if err != nil { |
| 61 | + fmt.Println("Error installing Dart Analyzer:", err) |
| 62 | + fmt.Println(string(stdout)) |
| 63 | + } |
| 64 | + // Print the output |
| 65 | + fmt.Println(string(stdout)) |
| 66 | + return err |
| 67 | +} |
| 68 | + |
| 69 | +func getFlutterDownloadURL(flutterRuntime *Runtime) string { |
| 70 | + goos := runtime.GOOS |
| 71 | + goarch := runtime.GOARCH |
| 72 | + |
| 73 | + // Map Go architecture to Dart architecture |
| 74 | + var arch string |
| 75 | + switch goarch { |
| 76 | + case "386": |
| 77 | + arch = "ia32" |
| 78 | + case "amd64": |
| 79 | + arch = "x64" |
| 80 | + case "arm": |
| 81 | + arch = "arm" |
| 82 | + case "arm64": |
| 83 | + arch = "arm64" |
| 84 | + default: |
| 85 | + arch = goarch |
| 86 | + } |
| 87 | + |
| 88 | + var os string |
| 89 | + switch goos { |
| 90 | + case "darwin": |
| 91 | + os = "macos" |
| 92 | + case "linux": |
| 93 | + os = "linux" |
| 94 | + case "windows": |
| 95 | + os = "windows" |
| 96 | + default: |
| 97 | + os = goos |
| 98 | + } |
| 99 | + |
| 100 | + downloadURL := fmt.Sprintf("https://storage.googleapis.com/flutter_infra_release/releases/stable/%s/flutter_%s_%s_%s-stable.zip", os, os, arch, flutterRuntime.Version()) |
| 101 | + fmt.Println("Downloading Flutter from:", downloadURL) |
| 102 | + return downloadURL |
| 103 | +} |
0 commit comments