diff --git a/build.go b/build.go index 3a83136..a2463c1 100644 --- a/build.go +++ b/build.go @@ -160,6 +160,30 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st dec.LikeDistros = false } + fn, ok := dec.GetFunc("version") + if ok { + log.Info("Executing version()").Send() + + buf := &bytes.Buffer{} + + err = fn( + ctx, + interp.Dir(filepath.Dir(script)), + interp.StdIO(os.Stdin, buf, os.Stderr), + ) + if err != nil { + return nil, nil, err + } + + newVer := strings.TrimSpace(buf.String()) + err = setVersion(ctx, runner, newVer) + if err != nil { + return nil, nil, err + } + + log.Info("Updating version").Str("new", newVer).Send() + } + var vars BuildVars err = dec.DecodeVars(&vars) if err != nil { @@ -249,7 +273,7 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st return nil, nil, err } - fn, ok := dec.GetFunc("prepare") + fn, ok = dec.GetFunc("prepare") if ok { log.Info("Executing prepare()").Send() @@ -259,26 +283,6 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st } } - fn, ok = dec.GetFunc("version") - if ok { - log.Info("Executing version()").Send() - - buf := &bytes.Buffer{} - - err = fn( - ctx, - interp.Dir(srcdir), - interp.StdIO(os.Stdin, buf, os.Stderr), - ) - if err != nil { - return nil, nil, err - } - - vars.Version = strings.TrimSpace(buf.String()) - - log.Info("Updating version").Str("new", vars.Version).Send() - } - fn, ok = dec.GetFunc("build") if ok { log.Info("Executing build()").Send() @@ -617,6 +621,14 @@ func archMatches(architectures []string) bool { return slices.Contains(architectures, arch) } +func setVersion(ctx context.Context, r *interp.Runner, to string) error { + fl, err := syntax.NewParser().Parse(strings.NewReader("version='"+to+"'"), "") + if err != nil { + return err + } + return r.Run(ctx, fl) +} + func uniq(ss ...*[]string) { for _, s := range ss { slices.Sort(*s) diff --git a/docs/build-scripts.md b/docs/build-scripts.md index a420570..7c5c2ec 100644 --- a/docs/build-scripts.md +++ b/docs/build-scripts.md @@ -226,15 +226,11 @@ The rest of the scripts are available in all packages. This section documents user-defined functions that can be added to build scripts. Any functions marked with `(*)` are required. -All functions are executed in the `$srcdir` directory - -### prepare - -The `prepare()` function runs first. It is meant to prepare the sources for building and packaging. This is the function in which patches should be applied, for example, by the `patch` command, and where tools like `go generate` should be executed. +All functions except for `version()` are executed in the `$srcdir` directory ### version -The `version()` function updates the `version` variable. This allows for automatically deriving the version from sources. This is most useful for git packages, which usually don't need to be changed, so their `version` variable stays the same. +The `version()` function is the first to run. It updates the `version` variable. This allows for automatically deriving the version from sources. This is most useful for git packages, which usually don't need to be changed, so their `version` variable stays the same. An example of using this for git: @@ -247,6 +243,12 @@ version() { The AUR equivalent is the [`pkgver()` function](https://wiki.archlinux.org/title/VCS_package_guidelines#The_pkgver()_function) +This function does not run in `$srcdir` because it is executed before the source directory is even created. Instead, it runs in `$repodir`. + +### prepare + +The `prepare()` function is meant to prepare the sources for building and packaging. This is the function in which patches should be applied, for example, by the `patch` command, and where tools like `go generate` should be executed. + ### build The `build()` function is where the package is actually built. Use the same commands that would be used to manually compile the software. Often, this function is just one line: