-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage.go
More file actions
106 lines (93 loc) · 2.44 KB
/
language.go
File metadata and controls
106 lines (93 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
type language string
const (
langPNPM language = "pnpm"
langNPM language = "npm"
langYarn language = "yarn"
langGo language = "go"
langRust language = "rust"
)
func buildProject(cfg *config) error {
switch cfg.lang {
case langPNPM:
if err := runCommandStreaming(cfg.workdir, "pnpm", "run", "build"); err != nil {
return fmt.Errorf("build project with pnpm: %w", err)
}
case langNPM:
if err := runCommandStreaming(cfg.workdir, "npm", "run", "build"); err != nil {
return fmt.Errorf("build project with npm: %w", err)
}
case langYarn:
if err := runCommandStreaming(cfg.workdir, "yarn", "build"); err != nil {
return fmt.Errorf("build project with yarn: %w", err)
}
case langGo:
if err := runCommandStreaming(cfg.workdir, "go", "build", "./..."); err != nil {
return fmt.Errorf("build project with go: %w", err)
}
case langRust:
if err := runCommandStreaming(cfg.workdir, "cargo", "build", "--release"); err != nil {
return fmt.Errorf("build project with cargo: %w", err)
}
default:
return fmt.Errorf("unsupported language %q", cfg.lang)
}
return nil
}
func determineLanguage(workdir, requested string) (language, error) {
requested = strings.TrimSpace(strings.ToLower(requested))
if requested == "" {
return detectLanguage(workdir)
}
switch requested {
case string(langNPM), "node", "ts", "typescript", "javascript", "nodejs":
return langNPM, nil
case string(langPNPM):
return langPNPM, nil
case string(langYarn):
return langYarn, nil
case string(langGo), "golang":
return langGo, nil
case string(langRust), "cargo":
return langRust, nil
default:
return "", fmt.Errorf("unsupported --lang value %q", requested)
}
}
func detectLanguage(workdir string) (language, error) {
type indicator struct {
lang language
file string
}
orderedIndicators := []indicator{
{lang: langPNPM, file: "pnpm-lock.yaml"},
{lang: langYarn, file: "yarn.lock"},
{lang: langNPM, file: "package-lock.json"},
{lang: langGo, file: "go.mod"},
{lang: langRust, file: "Cargo.toml"},
}
for _, ind := range orderedIndicators {
if fileExists(filepath.Join(workdir, ind.file)) {
return ind.lang, nil
}
}
if fileExists(filepath.Join(workdir, "package.json")) {
return langPNPM, nil
}
return langPNPM, nil
}
func fileExists(path string) bool {
if path == "" {
return false
}
if _, err := os.Stat(path); err == nil {
return true
}
return false
}