Sun, 14 Dec 2025 16:51:29 +0100
init repo
package main import ( "bytes" "encoding/xml" "fmt" "log" "os" "os/exec" "path" "text/template" ) type Config struct { XMLName xml.Name `xml:"config"` BuildEnv []struct { Name string `xml:"name,attr"` Host string `xml:"host"` User string `xml:"user"` } `xml:"buildenv"` Repository []struct { Path string `xml:"path"` BuildEnvs string `xml:"buildenvs"` Build []struct { IsPlatform string `xml:"isplatform,attr"` NotPlatform string `xml:"not,attr"` Test []string `xml:"test"` Var []struct { Name string `xml:"name,attr"` Value string `xml:",chardata"` } Configure string `xml:"configure"` Compile string `xml:"compile"` Check string `xml:"check"` } `xml:"build"` } `xml:"repository"` } func main() { data, err := os.ReadFile("testconfig.xml") if err != nil { log.Fatal(err) } config := &Config{} if err := xml.Unmarshal(data, &config); err != nil { log.Fatal(err) } templateStr, err := os.ReadFile("build.template") if err != nil { log.Fatal(err) } tpl, err := template.New("").Parse(string(templateStr)) if err != nil { log.Fatal(err) } err = os.Mkdir("tmp", 0755) if err != nil && !os.IsExist(err) { log.Fatal(err) } tmpDir := "tmp" for _, repo := range config.Repository { // create a build directory, that contains the repository source // and build scripts buildPath := path.Join(tmpDir, "build") if err = os.RemoveAll(buildPath); err != nil { log.Fatal(err) } if err = os.RemoveAll(path.Join(tmpDir, "build.tar")); err != nil { log.Fatal(err) } if err = os.RemoveAll(path.Join(tmpDir, "build.tar.gz")); err != nil { log.Fatal(err) } err = os.Mkdir(buildPath, 0755) if err != nil { log.Fatal(err) } // update repository and copy it to the build directory cmd := exec.Command("hg", "pull") var outb, errb bytes.Buffer cmd.Dir = repo.Path cmd.Stdout = &outb cmd.Stderr = &errb err = cmd.Run() if err != nil { log.Print("hg: ", errb.String()) log.Fatal(err) } outb.Reset() errb.Reset() cmd = exec.Command("hg", "update") cmd.Dir = repo.Path cmd.Stdout = &outb cmd.Stderr = &errb err = cmd.Run() if err != nil { log.Print("hg: ", errb.String()) log.Fatal(err) } // copy repository to the tmp build directory cmd = exec.Command("cp", "-r", repo.Path, path.Join(buildPath, "src")) err = cmd.Run() if err != nil { log.Fatal(err) } // create build script file, err := os.Create(path.Join(buildPath, "build.sh")) if err != nil { log.Fatal(err) } err = tpl.Execute(file, repo) if err != nil { log.Fatal(err) } file.Close() // create tarball cmd = exec.Command("tar", "cvf", "../build.tar", "src", "build.sh") cmd.Dir = buildPath if err := cmd.Run(); err != nil { log.Print("tar error") log.Fatal(err) } fmt.Println(path.Join(buildPath, "build.tar")) cmd = exec.Command("gzip", path.Join(tmpDir, "build.tar")) if err := cmd.Run(); err != nil { log.Print("gzip error") log.Fatal(err) } // TODO: upload build.tar.gz to buildenvs and execute the build.sh script remotely } }