Skip to content

Commit

Permalink
Always use latest compatible cg-gen-events patch version
Browse files Browse the repository at this point in the history
  • Loading branch information
juho05 committed Jun 1, 2022
1 parent 3db9792 commit 8ef8c0d
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions external/cg_gen_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,55 +42,64 @@ func LatestCGEVersion() (string, error) {
return strings.TrimPrefix(strings.Join(strings.Split(tag, ".")[:2], "."), "v"), nil
}

func InstallCGGenEvents(cgeVersion string) error {
exeName := fmt.Sprintf("cg-gen-events-%s", strings.ReplaceAll(cgeVersion, ".", "_"))
func InstallCGGenEvents(version string) (string, error) {
exeName := fmt.Sprintf("cg-gen-events_%s", strings.ReplaceAll(version, ".", "-"))
if runtime.GOOS == "windows" {
exeName = exeName + ".exe"
}

if _, err := os.Stat(filepath.Join(cgGenEventsPath, exeName)); err == nil {
return nil
}

version, err := GithubTagFromVersion("code-game-project", "cg-gen-events", cgeVersion)
if err != nil {
return err
return exeName, nil
}

filename := fmt.Sprintf("cg-gen-events-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
filename = fmt.Sprintf("cg-gen-events-%s-%s.zip", runtime.GOOS, runtime.GOARCH)
}

res, err := http.Get(fmt.Sprintf("https://github.com/code-game-project/cg-gen-events/releases/download/%s/%s", version, filename))
res, err := http.Get(fmt.Sprintf("https://github.com/code-game-project/cg-gen-events/releases/download/v%s/%s", version, filename))
if err != nil {
return err
return "", err
}
defer res.Body.Close()

err = os.MkdirAll(cgGenEventsPath, 0755)
if err != nil {
return err
return "", err
}

if runtime.GOOS == "windows" {
return UnzipFile(res.Body, "cg-gen-events.exe", filepath.Join(cgGenEventsPath, exeName))
return exeName, UnzipFile(res.Body, "cg-gen-events.exe", filepath.Join(cgGenEventsPath, exeName))
}
return UntargzFile(res.Body, "cg-gen-events", filepath.Join(cgGenEventsPath, exeName))
return exeName, UntargzFile(res.Body, "cg-gen-events", filepath.Join(cgGenEventsPath, exeName))
}

func CGGenEvents(outputDir, url, cgeVersion, language string) error {
exeName := fmt.Sprintf("cg-gen-events-%s", strings.ReplaceAll(cgeVersion, ".", "_"))
if runtime.GOOS == "windows" {
exeName = exeName + ".exe"
version, err := GithubTagFromVersion("code-game-project", "cg-gen-events", cgeVersion)
if err != nil {
return err
}
version = strings.TrimPrefix(version, "v")

exeName, err := InstallCGGenEvents(version)
if err != nil {
return err
}

if _, err := os.Stat(filepath.Join(cgGenEventsPath, exeName)); err != nil {
err = InstallCGGenEvents(cgeVersion)
if err != nil {
return err
binaries, err := os.ReadDir(cgGenEventsPath)
if err != nil {
return err
}
for _, b := range binaries {
info, err := b.Info()
if err == nil && info.Name() != exeName && strings.HasPrefix(info.Name(), fmt.Sprintf("cg-gen-events_%s", strings.ReplaceAll(cgeVersion, ".", "-"))) {
os.Remove(filepath.Join(cgGenEventsPath, info.Name()))
}
}

return Execute(filepath.Join(cgGenEventsPath, exeName), url, "--languages", language, "--output", outputDir)
out, err := ExecuteHidden(filepath.Join(cgGenEventsPath, exeName), url, "--languages", language, "--output", outputDir)
if err != nil {
cli.Error(out)
}
return err
}

0 comments on commit 8ef8c0d

Please sign in to comment.