Skip to content

Commit 2c0be8d

Browse files
committed
add dart/flutter install dependencies
1 parent 99f0c99 commit 2c0be8d

File tree

8 files changed

+370
-17
lines changed

8 files changed

+370
-17
lines changed

.codacy/codacy.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
runtimes:
22
3+
4+
35
tools:
46
7+

cmd/install.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func fetchRuntimes(config *cfg.ConfigType) {
4040
if err != nil {
4141
log.Fatal(err)
4242
}
43+
case "flutter":
44+
err := cfg.InstallFlutter(r)
45+
if err != nil {
46+
log.Fatal(err)
47+
}
4348
default:
4449
log.Fatal("Unknown runtime:", r.Name())
4550
}
@@ -60,9 +65,17 @@ func fetchTools(config *cfg.ConfigType) {
6065
case "dartanalyzer":
6166
// dartanalyzer needs dart runtime
6267
dartRuntime := config.Runtimes()["dart"]
63-
err := cfg.InstallDartAnalyzer(dartRuntime, tool, registry)
64-
if err != nil {
65-
log.Fatal(err)
68+
if config.Runtimes()["flutter"] != nil {
69+
flutterRuntime := config.Runtimes()["flutter"]
70+
err := cfg.InstallFlutterDartAnalyzer(flutterRuntime, tool, registry)
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
} else {
75+
err := cfg.InstallDartAnalyzer(dartRuntime, tool, registry)
76+
if err != nil {
77+
log.Fatal(err)
78+
}
6679
}
6780
default:
6881
log.Fatal("Unknown tool:", tool.Name())

config/dart-utils.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 genInfoDart(r *Runtime) map[string]string {
15+
dartSdkFolder := "dart-sdk"
16+
installDir := path.Join(Config.RuntimesDirectory(), dartSdkFolder)
17+
18+
return map[string]string{
19+
"installDir": installDir,
20+
"dart": path.Join(installDir, "bin", "dart"),
21+
}
22+
}
23+
24+
func InstallDartAnalyzer(dartRuntime *Runtime, dartAnalyzer *Runtime, registry string) error {
25+
log.Println("Installing Dart Analyzer")
26+
cmd := exec.Command(dartRuntime.Info()["dart"], "pub", "add", "--dev flutter_lints")
27+
stdout, err := cmd.Output()
28+
if err != nil {
29+
fmt.Println("Error installing Dart Analyzer:", err)
30+
fmt.Println(string(stdout))
31+
}
32+
// Print the output
33+
fmt.Println(string(stdout))
34+
return err
35+
}
36+
37+
func InstallDart(dartRuntime *Runtime) error {
38+
39+
log.Println("Fetching dart...")
40+
downloadDartURL := getDartDownloadURL(dartRuntime)
41+
// Extract filename from URL
42+
fileName := filepath.Base(downloadDartURL)
43+
localPath := filepath.Join(Config.RuntimesDirectory(), fileName)
44+
45+
// Check if file already exists
46+
if _, err := os.Stat(localPath); err == nil {
47+
log.Printf("File %s already exists, skipping download", fileName)
48+
return nil
49+
}
50+
dartTar, err := utils.DownloadFile(downloadDartURL, Config.RuntimesDirectory())
51+
if err != nil {
52+
return err
53+
}
54+
55+
// deflate node archive
56+
t, err := os.Open(dartTar)
57+
if err != nil {
58+
return err
59+
}
60+
defer t.Close()
61+
err = utils.ExtractZip(t, Config.RuntimesDirectory())
62+
if err != nil {
63+
return err
64+
}
65+
66+
return nil
67+
}
68+
69+
func getDartDownloadURL(dartRuntime *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/dart-archive/channels/stable/release/%s/sdk/dartsdk-%s-%s-release.zip", dartRuntime.Version(), os, arch)
101+
return downloadURL
102+
}

config/flutter-utils.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

config/runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ func (r *Runtime) populateInfo() {
3232
r.info = genInfoNode(r)
3333
case "eslint":
3434
r.info = genInfoEslint(r)
35+
case "dart":
36+
r.info = genInfoDart(r)
37+
case "flutter":
38+
r.info = genInfoFlutter(r)
39+
/*case "dart":
40+
r.info = genInfoDart(r)*/
3541
}
3642
}
3743

go.mod

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,34 @@ require (
88
)
99

1010
require (
11+
github.com/STARRY-S/zip v0.2.1 // indirect
1112
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
1214
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
github.com/sorairolake/lzip-go v0.3.5 // indirect
1316
)
1417

1518
require (
16-
github.com/andybalholm/brotli v1.0.4 // indirect
17-
github.com/bodgit/plumbing v1.2.0 // indirect
18-
github.com/bodgit/sevenzip v1.3.0 // indirect
19-
github.com/bodgit/windows v1.0.0 // indirect
19+
github.com/andybalholm/brotli v1.1.1 // indirect
20+
github.com/bodgit/plumbing v1.3.0 // indirect
21+
github.com/bodgit/sevenzip v1.6.0 // indirect
22+
github.com/bodgit/windows v1.0.1 // indirect
2023
github.com/connesc/cipherio v0.2.1 // indirect
21-
github.com/dsnet/compress v0.0.1 // indirect
24+
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
2225
github.com/golang/snappy v0.0.4 // indirect
23-
github.com/hashicorp/errwrap v1.0.0 // indirect
26+
github.com/hashicorp/errwrap v1.1.0 // indirect
2427
github.com/hashicorp/go-multierror v1.1.1 // indirect
2528
github.com/inconshreveable/mousetrap v1.1.0 // indirect
26-
github.com/klauspost/compress v1.15.9 // indirect
27-
github.com/klauspost/pgzip v1.2.5 // indirect
29+
github.com/klauspost/compress v1.17.11 // indirect
30+
github.com/klauspost/pgzip v1.2.6 // indirect
2831
github.com/mholt/archiver/v4 v4.0.0-alpha.8
29-
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
30-
github.com/pierrec/lz4/v4 v4.1.15 // indirect
32+
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 // indirect
33+
github.com/pierrec/lz4/v4 v4.1.21 // indirect
3134
github.com/spf13/pflag v1.0.5 // indirect
3235
github.com/stretchr/testify v1.9.0
3336
github.com/therootcompany/xz v1.0.1 // indirect
34-
github.com/ulikunitz/xz v0.5.10 // indirect
35-
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
36-
golang.org/x/text v0.3.8 // indirect
37+
github.com/ulikunitz/xz v0.5.12 // indirect
38+
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
39+
golang.org/x/text v0.20.0 // indirect
3740
gopkg.in/yaml.v3 v3.0.1
3841
)

0 commit comments

Comments
 (0)