hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 655b285db25226fa483323e0a4fa50fc1e603e7a
parent 8a4e54679eef4b81c1e2160528d969d528638d4d
Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
Date:   Tue,  1 Oct 2024 18:03:30 +0200

cmd/hare: Print dependencies to a t

The new 'hare deps -t' option changes the output to a plain text one.
This format can easily be consumed by field-based programs such as
tsort(1) (to a t!) or the more frequent utilities like sh(1) (while
read loops) or awk(1).

To illustrate how it renders, here is the output for the hare-compress
package, as of a4bb204305ef67a39dc1af3e8e843a2a4f5bee45:

    $ hare deps -Dst ~/src/hare-compress/
    compress::flate endian
    compress::flate errors
    compress::flate io
    compress::gzip bufio
    compress::gzip bytes
    compress::gzip compress::flate
    compress::gzip endian
    compress::gzip errors
    compress::gzip hash
    compress::gzip hash::crc32
    compress::gzip io
    compress::gzip memio
    compress::gzip time
    compress::zlib compress::flate
    compress::zlib endian
    compress::zlib errors
    compress::zlib hash
    compress::zlib hash::adler32
    compress::zlib io

The first column gives the effective list of modules provided by this
package. The second column gives required modules, once we filter out
the provided modules.

There is no compress module, because there are no sources. Adding such a
source file without depending on other modules would slightly change the
output:

    $ cat >~/src/hare-compress/compress/version.ha <<EOF
    export const version_string = "hare-compress x.y.z";
    EOF
    $ hare deps -Dst ~/src/hare-compress/
    compress -
    compress::flate endian
    compress::flate errors
    compress::flate io
    compress::gzip bufio
    compress::gzip bytes
    compress::gzip compress::flate
    compress::gzip endian
    compress::gzip errors
    compress::gzip hash
    compress::gzip hash::crc32
    compress::gzip io
    compress::gzip memio
    compress::gzip time
    compress::zlib compress::flate
    compress::zlib endian
    compress::zlib errors
    compress::zlib hash
    compress::zlib hash::adler32
    compress::zlib io

Note the presence of a 'compress -' line expressing the presence of
the module and its lack of dependencies.

The motivation is to enable module processing in package managers,
in the non-recursive mode, to derive virtual provides from the first
field and virtual requires from the second one, from the authoritative
tool chain.

Signed-off-by: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>

Diffstat:
Mcmd/hare/deps.ha | 12++++++++++++
Mcmd/hare/main.ha | 1+
Mdocs/hare-deps.1.scd | 7+++++++
3 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/cmd/hare/deps.ha b/cmd/hare/deps.ha @@ -14,6 +14,7 @@ use sort::cmp; type deps_fmt = enum { DOT, TERM, + TEXT, }; type link = struct { @@ -41,6 +42,8 @@ fn deps(name: str, cmd: *getopt::command) (void | error) = { merge_tags(&tags, opt.1)?; case 's' => submodules = true; + case 't' => + goal = deps_fmt::TEXT; case => abort(); }; @@ -99,6 +102,15 @@ fn deps(name: str, cmd: *getopt::command) (void | error) = { }; }; fmt::println("}")!; + case deps_fmt::TEXT => + for (let mod .. mods) { + if (module::gathered(&mod) && len(mod.deps) == 0) { + fmt::printfln("{} -", mod.name)!; + } else for (let dep .. mod.deps) { + const child = mods[dep.0]; + fmt::printfln("{} {}", mod.name, child.name)!; + }; + }; }; }; diff --git a/cmd/hare/main.ha b/cmd/hare/main.ha @@ -49,6 +49,7 @@ const help: []getopt::help = [ ('d', "print dot syntax for use with graphviz"), ('s', "recursively collect submodules"), ('T', "tagset", "set/unset build tags"), + ('t', "print text output for field-based parsing"), "[path|module]", ]: []getopt::help), ("run", [ diff --git a/docs/hare-deps.1.scd b/docs/hare-deps.1.scd @@ -36,6 +36,13 @@ characters. directory. A path should be a source directory to reliably collect dependencies. +*-t* + Print the dependency tree as a field-based plain text output. Each + line has two fields, a module and one of its dependencies. If the + module (or one of its submodules with the *-s* option) does not have + dependencies, the second field contains just a dash. The text output + is a suitable input for *tsort*(1). + *-T* _tagset_ Set or unset build tags. See *BUILD TAGS* in *hare-module*(5).