-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.go
More file actions
139 lines (122 loc) · 3.69 KB
/
build.go
File metadata and controls
139 lines (122 loc) · 3.69 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Bananenpro/cli"
"github.com/code-game-project/go-utils/cgfile"
"github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/modules"
)
func Build() error {
config, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
data, err := modules.ReadCommandConfig[modules.BuildData]()
if err != nil {
return err
}
packageConf, ok := config.LangConfig["package"]
if !ok {
return errors.New("Missing language config field `package` in .codegame.json!")
}
packageName := packageConf.(string)
if packageConf == "" {
return errors.New("Empty language config field `package` in .codegame.json!")
}
if data.OS != "current" || data.Arch != "current" {
return errors.New("Cross compilation is not supported for Java applications.")
}
switch config.Type {
case "client":
return buildClient(config.Game, packageName, data.Output, config.URL)
default:
return fmt.Errorf("Unknown project type: %s", config.Type)
}
}
var artifactRegex = regexp.MustCompile(`^-\d+\.\d+\.\d+\.jar$`)
func buildClient(gameName, packageName, output, url string) (err error) {
cli.BeginLoading("Building...")
gameDir := filepath.Join("src", "main", "java")
pkgDir := filepath.Join(strings.Split(packageName, ".")...)
gameDir = filepath.Join(gameDir, pkgDir, toOneWord(gameName))
err = replaceInFile(filepath.Join(gameDir, "Game.java"), "throw new RuntimeException(\"The CG_GAME_URL environment variable must be set.\")", "return \""+url+"\"")
if err != nil {
return err
}
defer func() {
err2 := replaceInFile(filepath.Join(gameDir, "Game.java"), "return \""+url+"\"", "throw new RuntimeException(\"The CG_GAME_URL environment variable must be set.\")")
if err == nil && err2 != nil {
err = err2
}
}()
err = os.RemoveAll("target")
if err != nil {
return fmt.Errorf("Failed to remove target directory: %w", err)
}
_, err = exec.Execute(true, "mvn", "-B", "clean", "package")
if err != nil {
return err
}
pkgParts := strings.Split(packageName, ".")
artifactName := pkgParts[len(pkgParts)-1]
if output != "" {
outputIsFile := false
if outStat, err := os.Stat(output); err != nil {
if strings.HasSuffix(output, ".jar") {
os.MkdirAll(filepath.Dir(output), 0o755)
outputIsFile = true
} else {
os.MkdirAll(output, 0o755)
}
} else {
outputIsFile = !outStat.IsDir()
}
files, err := os.ReadDir("target")
if err != nil {
return err
}
moved := false
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasPrefix(f.Name(), artifactName) && artifactRegex.MatchString(strings.TrimPrefix(f.Name(), artifactName)) {
if outputIsFile {
err = os.Rename(filepath.Join("target", f.Name()), output)
if err != nil {
return fmt.Errorf("Couldn't move artifact to output location: %w", err)
}
} else {
err = os.Rename(filepath.Join("target", f.Name()), filepath.Join(output, f.Name()))
if err != nil {
return fmt.Errorf("Couldn't move artifact to output location: %w", err)
}
}
moved = true
break
}
}
if !moved {
return errors.New("Couldn't move artifact to output location: failed to find generated artifact")
}
}
cli.FinishLoading()
return nil
}
func replaceInFile(filename, old, new string) error {
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
}
content = []byte(strings.ReplaceAll(string(content), old, new))
err = os.WriteFile(filename, content, 0o644)
if err != nil {
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
}
return nil
}