flamenco/cmd/update-version/linereplacer.go
Sybren A. Stüvel c1a728dc2f Version updates via Makefile
Flamenco now no longer uses the Git tags + hash for the application
version, but an explicit `VERSION` variable in the `Makefile`.

After changing the `VERSION` variable in the `Makefile`, run
`make update-version`.

Not every part of Flamenco looks at this variable, though. Most
importantly: the Blender add-on needs special handling, because that
doesn't just take a version string but a tuple of integers. Running
`make update-version` updates the add-on's `bl_info` dict with the new
version. If the version has any `-blabla` suffix (like `3.0-beta0`) it
will also set the `warning` field to explain that it's not a stable
release.
2022-07-25 16:08:07 +02:00

64 lines
1.6 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"github.com/rs/zerolog/log"
)
// updateLines calls replacer() on each line of the given file, and replaces it
// with the returned value.
// Returns whether the file changed at all.
func updateLines(filename string, replacer func(string) string) (bool, error) {
logger := log.With().Str("filename", filename).Logger()
logger.Info().Msg("updating file")
// Read the file contents:
input, err := os.ReadFile(filename)
if err != nil {
return false, fmt.Errorf("reading from %s: %w", filename, err)
}
// Replace the lines:
anythingChanged := false
lines := strings.Split(string(input), "\n")
for idx := range lines {
replaced := replacer(lines[idx])
if replaced == lines[idx] {
continue
}
logger.Info().
Str("old", strings.TrimSpace(lines[idx])).
Str("new", strings.TrimSpace(replaced)).
Msg("replacing line")
lines[idx] = replaced
anythingChanged = true
}
if !anythingChanged {
logger.Info().Msg("file did not change, will not touch it")
return false, nil
}
// Write the file contents to a temporary location:
output := strings.Join(lines, "\n")
tempname := filename + "~"
err = os.WriteFile(tempname, []byte(output), 0644)
if err != nil {
return false, fmt.Errorf("writing to %s: %w", tempname, err)
}
// Move the temporary file onto the input filename:
if err := os.Remove(filename); err != nil {
return false, fmt.Errorf("removing %s: %w", filename, err)
}
if err := os.Rename(tempname, filename); err != nil {
return false, fmt.Errorf("renaming %s to %s: %w", tempname, filename, err)
}
return true, nil
}