func updateBoilerplate()

in tools/releaser/main.go [249:285]


func updateBoilerplate(repoRoot string, boilerplate string) error {
	const (
		startMarker = `load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")` + "\n"
		endMarker   = "zig_toolchains()\n"
	)

	for _, gotpath := range _boilerplateFiles {
		f := path.Join(repoRoot, gotpath)
		data, err := os.ReadFile(f)
		if err != nil {
			return err
		}
		dataStr := string(data)

		// all boilerplate starts with startMarker and ends with endMarker.
		// Our goal is to write the right string between the two.
		startMarkerIdx := strings.Index(dataStr, startMarker)
		if startMarkerIdx == -1 {
			return fmt.Errorf("%q does not contain start marker %q...", gotpath, startMarker[0:16])
		}

		endMarkerIdx := strings.Index(dataStr, endMarker)
		if endMarkerIdx == -1 {
			return fmt.Errorf("%q does not contain end marker %q...", gotpath, endMarker[0:16])
		}

		preamble := dataStr[0:startMarkerIdx]
		epilogue := dataStr[endMarkerIdx+len(endMarker):]
		newBoilerplate := preamble + boilerplate + epilogue

		if err := os.WriteFile(f, []byte(newBoilerplate), 0644); err != nil {
			return fmt.Errorf("write %q: %w", f, err)
		}
	}

	return nil
}