Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Script for Makefile Version Updates in Examples Directory #3712

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions build/scripts/bump-image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -38,11 +37,38 @@ var (
"site": true,
"test": true,
}
versionPatternInMakefile *regexp.Regexp
)

func init() {
flag.StringVar(&imageName, "imageName", "", "Image name to update")
flag.StringVar(&version, "version", "", "Version to update")
versionPatternInMakefile = regexp.MustCompile(`version\s*:=\s*\d+\.\d+`)
}

func imageNamePrefix(imageName string) string {
// Exceptions list
exceptions := map[string]bool{
"simple-genai-game-server": true,
"simple-game-server": true,
}

// Use the first two words for exceptions, otherwise use the first word
separator := "-"
if exceptions[imageName] {
return firstTwoWords(imageName, separator)
}

parts := strings.Split(imageName, separator)
return parts[0] // Only use the first word for non-exceptions
}

func firstTwoWords(s, separator string) string {
parts := strings.Split(s, separator)
if len(parts) >= 2 {
return parts[0] + separator + parts[1]
}
return parts[0]
}

func main() {
Expand All @@ -65,7 +91,12 @@ func main() {
return err
}
if !info.IsDir() && filepath.Ext(path) != ".md" {
err = updateFileVersion(path, newVersion)
// Update the version in Makefiles located within the directories that are relevant to the given imageName.
if folder == "examples" && strings.HasPrefix(filepath.Base(filepath.Dir(path)), imageNamePrefix(imageName)) && filepath.Base(path) == "Makefile" {
err = updateMakefileVersion(path, newVersion)
} else {
err = updateFileVersion(path, newVersion)
}
if err != nil {
log.Printf("Error updating file %s: %v", path, err)
}
Expand All @@ -91,7 +122,7 @@ func incrementVersion(version string) string {
}

minor++
return fmt.Sprintf("%s.%d", parts[0], minor)
return parts[0] + "." + strconv.Itoa(minor)
}

func updateFileVersion(filePath, newVersion string) error {
Expand All @@ -104,3 +135,14 @@ func updateFileVersion(filePath, newVersion string) error {

return os.WriteFile(filePath, []byte(content), 0o644)
}

func updateMakefileVersion(filePath, newVersion string) error {
input, err := os.ReadFile(filePath)
if err != nil {
return err
}

content := versionPatternInMakefile.ReplaceAllString(string(input), "version := "+newVersion)

return os.WriteFile(filePath, []byte(content), 0o644)
}
Loading