sing-box/cmd/internal/update_android_version/main.go

85 lines
2.1 KiB
Go
Raw Permalink Normal View History

2023-08-27 21:14:10 +08:00
package main
import (
2024-12-04 21:25:16 +08:00
"flag"
2023-08-27 21:14:10 +08:00
"os"
"path/filepath"
2024-02-16 13:28:58 +08:00
"runtime"
2023-08-27 21:14:10 +08:00
"strconv"
"strings"
"github.com/sagernet/sing-box/cmd/internal/build_shared"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
)
2025-02-14 19:13:49 +08:00
var (
flagRunInCI bool
flagRunNightly bool
)
2024-12-04 21:25:16 +08:00
func init() {
flag.BoolVar(&flagRunInCI, "ci", false, "Run in CI")
2025-02-14 19:13:49 +08:00
flag.BoolVar(&flagRunNightly, "nightly", false, "Run nightly")
2024-12-04 21:25:16 +08:00
}
2023-08-27 21:14:10 +08:00
func main() {
2024-12-04 21:25:16 +08:00
flag.Parse()
newVersion := common.Must1(build_shared.ReadTag())
var androidPath string
if flagRunInCI {
androidPath = "clients/android"
} else {
androidPath = "../sing-box-for-android"
}
androidPath, err := filepath.Abs(androidPath)
2023-08-27 21:14:10 +08:00
if err != nil {
log.Fatal(err)
}
common.Must(os.Chdir(androidPath))
2024-02-16 13:28:58 +08:00
localProps := common.Must1(os.ReadFile("version.properties"))
2023-08-27 21:14:10 +08:00
var propsList [][]string
for _, propLine := range strings.Split(string(localProps), "\n") {
propsList = append(propsList, strings.Split(propLine, "="))
}
2024-02-16 13:28:58 +08:00
var (
versionUpdated bool
goVersionUpdated bool
)
2023-08-27 21:14:10 +08:00
for _, propPair := range propsList {
2024-02-16 13:28:58 +08:00
switch propPair[0] {
case "VERSION_NAME":
2024-12-04 21:25:16 +08:00
if propPair[1] != newVersion {
2025-02-11 14:41:41 +08:00
log.Info("updated version from ", propPair[1], " to ", newVersion)
2024-02-16 13:28:58 +08:00
versionUpdated = true
2024-12-04 21:25:16 +08:00
propPair[1] = newVersion
2024-02-16 13:28:58 +08:00
}
case "GO_VERSION":
if propPair[1] != runtime.Version() {
2025-02-11 14:41:41 +08:00
log.Info("updated Go version from ", propPair[1], " to ", runtime.Version())
2024-02-16 13:28:58 +08:00
goVersionUpdated = true
propPair[1] = runtime.Version()
2023-08-27 21:14:10 +08:00
}
}
}
2024-02-16 13:28:58 +08:00
if !(versionUpdated || goVersionUpdated) {
log.Info("version not changed")
return
2025-02-14 19:13:49 +08:00
} else if flagRunInCI && !flagRunNightly {
2025-02-11 14:41:41 +08:00
log.Fatal("version changed, commit changes first.")
2024-02-16 13:28:58 +08:00
}
2023-08-27 21:14:10 +08:00
for _, propPair := range propsList {
switch propPair[0] {
case "VERSION_CODE":
versionCode := common.Must1(strconv.ParseInt(propPair[1], 10, 64))
propPair[1] = strconv.Itoa(int(versionCode + 1))
log.Info("updated version code to ", propPair[1])
}
}
var newProps []string
for _, propPair := range propsList {
newProps = append(newProps, strings.Join(propPair, "="))
}
2024-02-16 13:28:58 +08:00
common.Must(os.WriteFile("version.properties", []byte(strings.Join(newProps, "\n")), 0o644))
2023-08-27 21:14:10 +08:00
}