github.com/secoba/wails/v2@v2.6.4/tools/release/release.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "os" 6 "os/exec" 7 "strconv" 8 "strings" 9 "time" 10 11 "github.com/samber/lo" 12 13 "github.com/secoba/wails/v2/internal/s" 14 ) 15 16 const versionFile = "../../cmd/wails/internal/version.txt" 17 18 func checkError(err error) { 19 if err != nil { 20 println(err.Error()) 21 os.Exit(1) 22 } 23 } 24 25 // TODO:This can be replaced with "https://github.com/coreos/go-semver/blob/main/semver/semver.go" 26 func updateVersion() string { 27 currentVersionData, err := os.ReadFile(versionFile) 28 checkError(err) 29 currentVersion := string(currentVersionData) 30 vsplit := strings.Split(currentVersion, ".") 31 minorVersion, err := strconv.Atoi(vsplit[len(vsplit)-1]) 32 checkError(err) 33 minorVersion++ 34 vsplit[len(vsplit)-1] = strconv.Itoa(minorVersion) 35 newVersion := strings.Join(vsplit, ".") 36 err = os.WriteFile(versionFile, []byte(newVersion), 0o755) 37 checkError(err) 38 return newVersion 39 } 40 41 func runCommand(name string, arg ...string) { 42 cmd := exec.Command(name, arg...) 43 cmd.Stdout = os.Stdout 44 cmd.Stderr = os.Stderr 45 err := cmd.Run() 46 checkError(err) 47 } 48 49 func IsPointRelease(currentVersion string, newVersion string) bool { 50 // The first n-1 parts of the version should be the same 51 if currentVersion[:len(currentVersion)-2] != newVersion[:len(newVersion)-2] { 52 return false 53 } 54 // split on the last dot in the string 55 currentVersionSplit := strings.Split(currentVersion, ".") 56 newVersionSplit := strings.Split(newVersion, ".") 57 // compare the 58 // if the last part of the version is the same, it's a point release 59 currentMinor := lo.Must(strconv.Atoi(currentVersionSplit[len(currentVersionSplit)-1])) 60 newMinor := lo.Must(strconv.Atoi(newVersionSplit[len(newVersionSplit)-1])) 61 return newMinor == currentMinor+1 62 } 63 64 func main() { 65 var newVersion string 66 var isPointRelease bool 67 if len(os.Args) > 1 { 68 newVersion = os.Args[1] 69 currentVersion, err := os.ReadFile(versionFile) 70 checkError(err) 71 err = os.WriteFile(versionFile, []byte(newVersion), 0o755) 72 checkError(err) 73 isPointRelease = IsPointRelease(string(currentVersion), newVersion) 74 } else { 75 newVersion = updateVersion() 76 } 77 78 // Update ChangeLog 79 s.CD("../../../website") 80 81 // Read in `src/pages/changelog.mdx` 82 changelogData, err := os.ReadFile("src/pages/changelog.mdx") 83 checkError(err) 84 changelog := string(changelogData) 85 // Split on the line that has `## [Unreleased]` 86 changelogSplit := strings.Split(changelog, "## [Unreleased]") 87 // Get today's date in YYYY-MM-DD format 88 today := time.Now().Format("2006-01-02") 89 // Add the new version to the top of the changelog 90 newChangelog := changelogSplit[0] + "## [Unreleased]\n\n## " + newVersion + " - " + today + changelogSplit[1] 91 // Write the changelog back 92 err = os.WriteFile("src/pages/changelog.mdx", []byte(newChangelog), 0o755) 93 checkError(err) 94 95 if !isPointRelease { 96 runCommand("npx", "-y", "pnpm", "install") 97 98 s.ECHO("Generating new Docs for version: " + newVersion) 99 100 runCommand("npx", "pnpm", "run", "docusaurus", "docs:version", newVersion) 101 102 runCommand("npx", "pnpm", "run", "write-translations") 103 104 // Load the version list/* 105 versionsData, err := os.ReadFile("versions.json") 106 checkError(err) 107 var versions []string 108 err = json.Unmarshal(versionsData, &versions) 109 checkError(err) 110 oldestVersion := versions[len(versions)-1] 111 s.ECHO(oldestVersion) 112 versions = versions[0 : len(versions)-1] 113 newVersions, err := json.Marshal(&versions) 114 checkError(err) 115 err = os.WriteFile("versions.json", newVersions, 0o755) 116 checkError(err) 117 118 s.ECHO("Removing old version: " + oldestVersion) 119 s.CD("versioned_docs") 120 s.RMDIR("version-" + oldestVersion) 121 s.CD("../versioned_sidebars") 122 s.RM("version-" + oldestVersion + "-sidebars.json") 123 s.CD("..") 124 125 runCommand("npx", "pnpm", "run", "build") 126 } 127 }