diff --git a/examples/todo-list/go.mod b/examples/todo-list/go.mod index 78c5f340..d0a42bc8 100644 --- a/examples/todo-list/go.mod +++ b/examples/todo-list/go.mod @@ -6,7 +6,7 @@ require ( entgo.io/ent v0.14.1 github.com/go-chi/chi/v5 v5.1.0 github.com/google/uuid v1.6.0 - github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f + github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 github.com/mattn/go-sqlite3 v1.14.16 ) diff --git a/examples/todo-list/go.sum b/examples/todo-list/go.sum index e5e8c535..39bc27e2 100644 --- a/examples/todo-list/go.sum +++ b/examples/todo-list/go.sum @@ -35,6 +35,10 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f h1:A4MEslfWXKvqsZt1DbvZpguzVAbyjCRclEZNt6TjSRw= github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ= +github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 h1:254AqceggeJ0yLXUud3IiYfNjRnetfgcyuCVIlDtgbc= +github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ= +github.com/maddalax/htmgo/framework v0.1.0 h1:bWMPlCCuoW2Fa++YUk7yhpRLHyU3d5GcvQ0gp+bDO+g= +github.com/maddalax/htmgo/framework v0.1.0/go.mod h1:TA7KCaKhurpXceQrzClJOHqRsUSd5NL64ZngSg+I3oc= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= diff --git a/framework-ui/go.mod b/framework-ui/go.mod index e21382f7..c51e9057 100644 --- a/framework-ui/go.mod +++ b/framework-ui/go.mod @@ -2,7 +2,7 @@ module github.com/maddalax/htmgo/framework-ui go 1.23.0 -require github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f +require github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 require ( github.com/go-chi/chi/v5 v5.1.0 // indirect diff --git a/htmgo-site/go.mod b/htmgo-site/go.mod index 44c1627a..c9510501 100644 --- a/htmgo-site/go.mod +++ b/htmgo-site/go.mod @@ -5,7 +5,7 @@ go 1.23.0 require ( github.com/go-chi/chi/v5 v5.1.0 github.com/google/uuid v1.6.0 - github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f + github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 github.com/yuin/goldmark v1.7.4 github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc ) diff --git a/htmgo-site/go.sum b/htmgo-site/go.sum index f5fac914..5b4a894e 100644 --- a/htmgo-site/go.sum +++ b/htmgo-site/go.sum @@ -14,6 +14,10 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f h1:A4MEslfWXKvqsZt1DbvZpguzVAbyjCRclEZNt6TjSRw= github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ= +github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 h1:254AqceggeJ0yLXUud3IiYfNjRnetfgcyuCVIlDtgbc= +github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ= +github.com/maddalax/htmgo/framework v0.1.0 h1:bWMPlCCuoW2Fa++YUk7yhpRLHyU3d5GcvQ0gp+bDO+g= +github.com/maddalax/htmgo/framework v0.1.0/go.mod h1:TA7KCaKhurpXceQrzClJOHqRsUSd5NL64ZngSg+I3oc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/templates/starter/go.mod b/templates/starter/go.mod index 97eabce5..17997b1c 100644 --- a/templates/starter/go.mod +++ b/templates/starter/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( github.com/go-chi/chi/v5 v5.1.0 - github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f + github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 ) require github.com/google/uuid v1.6.0 // indirect diff --git a/templates/starter/go.sum b/templates/starter/go.sum index 28a8e36d..5d320a3a 100644 --- a/templates/starter/go.sum +++ b/templates/starter/go.sum @@ -6,6 +6,10 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f h1:A4MEslfWXKvqsZt1DbvZpguzVAbyjCRclEZNt6TjSRw= github.com/maddalax/htmgo/framework v0.0.0-20240926194146-5bbd35d2728f/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ= +github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286 h1:254AqceggeJ0yLXUud3IiYfNjRnetfgcyuCVIlDtgbc= +github.com/maddalax/htmgo/framework v0.0.0-20240926195523-4d003b6db286/go.mod h1:JBHqkKSv6frIsSlT1itIU0hUTW9NgDcyM6Cf6npK0bQ= +github.com/maddalax/htmgo/framework v0.1.0 h1:bWMPlCCuoW2Fa++YUk7yhpRLHyU3d5GcvQ0gp+bDO+g= +github.com/maddalax/htmgo/framework v0.1.0/go.mod h1:TA7KCaKhurpXceQrzClJOHqRsUSd5NL64ZngSg+I3oc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/tools/update-htmgo-dep.go b/tools/update-htmgo-dep.go new file mode 100644 index 00000000..0ecc3ba6 --- /dev/null +++ b/tools/update-htmgo-dep.go @@ -0,0 +1,133 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "net/http" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" +) + +const frameworkRepo = "github.com/maddalax/htmgo/framework" +const githubAPIURL = "https://api.github.com/repos/maddalax/htmgo/commits" + +// Commit represents the structure of a commit object returned by the GitHub API. +type Commit struct { + Sha string `json:"sha"` +} + +func main() { + // Get the latest commit hash from the remote repository. + latestCommitHash, err := getLatestCommitHash() + if err != nil { + fmt.Println("Error getting latest commit hash:", err) + return + } + + // Start from the current directory. + rootDir, err := os.Getwd() + if err != nil { + fmt.Println("Error getting working directory:", err) + return + } + + wg := sync.WaitGroup{} + // Walk through directories. + err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // Check if the directory contains a go.mod file. + if info.IsDir() && fileExists(filepath.Join(path, "go.mod")) { + // Check if the go.mod contains 'github.com/maddalax/htmgo/framework'. + if containsFrameworkDependency(filepath.Join(path, "go.mod")) { + wg.Add(1) + go func() { + defer wg.Done() + // Run go get github.com/maddalax/htmgo/framework@. + fmt.Printf("Running 'go get' with latest commit hash in %s\n", path) + cmd := exec.Command("go", "get", fmt.Sprintf("%s@%s", frameworkRepo, latestCommitHash)) + cmd.Dir = path // Set the working directory to the current path. + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Println("Error running go get:", err) + } else { + fmt.Println("Updated framework in", path) + } + }() + } + } + + return nil + }) + + wg.Wait() + + if err != nil { + fmt.Println("Error walking through directories:", err) + } + +} + +// fileExists checks if a file exists at the given path. +func fileExists(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} + +// containsFrameworkDependency checks if 'github.com/maddalax/htmgo/framework' is in the go.mod file. +func containsFrameworkDependency(goModPath string) bool { + file, err := os.Open(goModPath) + if err != nil { + fmt.Println("Error opening go.mod file:", err) + return false + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if strings.Contains(scanner.Text(), frameworkRepo) { + return true + } + } + + if err := scanner.Err(); err != nil { + fmt.Println("Error reading go.mod file:", err) + } + + return false +} + +// getLatestCommitHash fetches the latest commit hash from the GitHub API for the framework repository. +func getLatestCommitHash() (string, error) { + // Create an HTTP request to the GitHub API to get the latest commit. + resp, err := http.Get(githubAPIURL) + if err != nil { + return "", fmt.Errorf("failed to fetch latest commit: %v", err) + } + defer resp.Body.Close() + + // Check if the response status is OK. + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to fetch latest commit: status code %d", resp.StatusCode) + } + + // Parse the JSON response. + var commits []Commit + if err := json.NewDecoder(resp.Body).Decode(&commits); err != nil { + return "", fmt.Errorf("failed to decode JSON response: %v", err) + } + + if len(commits) == 0 { + return "", fmt.Errorf("no commits found in repository") + } + + // Return the SHA of the latest commit. + return commits[0].Sha, nil +}