Skip to content

Commit ad5c632

Browse files
committed
Implement build command
1 parent db4f067 commit ad5c632

File tree

4 files changed

+101
-25
lines changed

4 files changed

+101
-25
lines changed

build.go

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path/filepath"
8+
"regexp"
79
"strings"
810

911
"github.com/Bananenpro/cli"
1012
"github.com/code-game-project/go-utils/cgfile"
13+
"github.com/code-game-project/go-utils/exec"
1114
"github.com/code-game-project/go-utils/modules"
1215
)
1316

@@ -43,9 +46,81 @@ func Build() error {
4346
}
4447
}
4548

46-
func buildClient(gameName, packageName, output, url string) error {
49+
var artifactRegex = regexp.MustCompile(`^-\d+\.\d+\.\d+\.jar$`)
50+
51+
func buildClient(gameName, packageName, output, url string) (err error) {
4752
cli.BeginLoading("Building...")
48-
// TODO
53+
54+
gameDir := filepath.Join("src", "main", "java")
55+
pkgDir := filepath.Join(strings.Split(packageName, ".")...)
56+
gameDir = filepath.Join(gameDir, pkgDir, toOneWord(gameName))
57+
58+
err = replaceInFile(filepath.Join(gameDir, "Game.java"), "throw new RuntimeException(\"The CG_GAME_URL environment variable must be set.\")", "return \""+url+"\"")
59+
if err != nil {
60+
return err
61+
}
62+
defer func() {
63+
err2 := replaceInFile(filepath.Join(gameDir, "Game.java"), "return \""+url+"\"", "throw new RuntimeException(\"The CG_GAME_URL environment variable must be set.\")")
64+
if err == nil && err2 != nil {
65+
err = err2
66+
}
67+
}()
68+
69+
err = os.RemoveAll("target")
70+
if err != nil {
71+
return fmt.Errorf("Failed to remove target directory: %w", err)
72+
}
73+
_, err = exec.Execute(true, "mvn", "-B", "clean", "package")
74+
if err != nil {
75+
return err
76+
}
77+
78+
pkgParts := strings.Split(packageName, ".")
79+
artifactName := pkgParts[len(pkgParts)-1]
80+
81+
if output != "" {
82+
outputIsFile := false
83+
if outStat, err := os.Stat(output); err != nil {
84+
if strings.HasSuffix(output, ".jar") {
85+
os.MkdirAll(filepath.Dir(output), 0o755)
86+
outputIsFile = true
87+
} else {
88+
os.MkdirAll(output, 0o755)
89+
}
90+
} else {
91+
outputIsFile = !outStat.IsDir()
92+
}
93+
94+
files, err := os.ReadDir("target")
95+
if err != nil {
96+
return err
97+
}
98+
moved := false
99+
for _, f := range files {
100+
if f.IsDir() {
101+
continue
102+
}
103+
if strings.HasPrefix(f.Name(), artifactName) && artifactRegex.MatchString(strings.TrimPrefix(f.Name(), artifactName)) {
104+
if outputIsFile {
105+
err = os.Rename(filepath.Join("target", f.Name()), output)
106+
if err != nil {
107+
return fmt.Errorf("Couldn't move artifact to output location: %w", err)
108+
}
109+
} else {
110+
err = os.Rename(filepath.Join("target", f.Name()), filepath.Join(output, f.Name()))
111+
if err != nil {
112+
return fmt.Errorf("Couldn't move artifact to output location: %w", err)
113+
}
114+
}
115+
moved = true
116+
break
117+
}
118+
}
119+
if !moved {
120+
return errors.New("Couldn't move artifact to output location: failed to find generated artifact")
121+
}
122+
}
123+
49124
cli.FinishLoading()
50125
return nil
51126
}

run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func Run() error {
4444
}
4545

4646
func runClient(url, packageName string, args []string) error {
47-
_, err := cgExec.Execute(true, "mvn", "-q", "-B", "compile")
47+
_, err := cgExec.Execute(true, "mvn", "-q", "-B", "clean", "compile")
4848
if err != nil {
4949
return err
5050
}

templates/new/client/pom.xml.tmpl

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,30 @@
7272
<plugin>
7373
<artifactId>maven-project-info-reports-plugin</artifactId>
7474
<version>3.4.0</version>
75-
</plugin>
76-
<plugin>
77-
<artifactId>maven-shade-plugin</artifactId>
78-
<version>3.3.0</version>
79-
<configuration>
80-
<relocations>
81-
<relocation>
82-
<pattern>de.themoep</pattern>
83-
<shadedPattern>io.github.apfelcreme.Pipes.libs</shadedPattern>
84-
</relocation>
85-
</relocations>
86-
</configuration>
87-
<executions>
88-
<execution>
89-
<phase>package</phase>
90-
<goals>
91-
<goal>shade</goal>
92-
</goals>
93-
</execution>
94-
</executions>
9575
</plugin>
9676
</plugins>
9777
</pluginManagement>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-shade-plugin</artifactId>
82+
<version>3.4.1</version>
83+
<executions>
84+
<execution>
85+
<phase>package</phase>
86+
<goals>
87+
<goal>shade</goal>
88+
</goals>
89+
<configuration>
90+
<transformers>
91+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
92+
<mainClass>de.julianh.guess.App</mainClass>
93+
</transformer>
94+
</transformers>
95+
</configuration>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
</plugins>
98100
</build>
99101
</project>
100-

update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func updateClient(projectName, libraryVersion string, config *cgfile.CodeGameFil
7777
}
7878

7979
cli.BeginLoading("Updating java-client...")
80-
exec.Execute(true, "mvn", "compile")
80+
exec.Execute(true, "mvn", "dependency:resolve")
8181
exec.Execute(true, "mvn", "dependency:sources")
8282
exec.Execute(true, "mvn", "dependency:resolve", "-Dclassifier=javadoc")
8383
cli.FinishLoading()

0 commit comments

Comments
 (0)