June 05, 2023

Take vibe.d, it has many examples in its repository:

https://github.com/vibe-d/vibe.d/tree/master/examples

However these are a bit hard to build or run individually, or even to build all of them at once. There's two ways that I know of to build or run a specific example:

$ cd examples/auth_basic/
$ dub run

Or:

$ dub run --root=examples/auth_basic

And in fact for building the entire set of examples as part of vibe.d's test-suite there is a helper script in https://github.com/vibe-d/vibe.d/blob/a68fd936638f7cca1e314dbd30a04166d01b836e/run-ci.sh#L49-L54 which does this:

if [[ $PARTS =~ (^|,)examples(,|$) ]]; then
    for ex in $(\ls -1 examples/); do
        echo "[INFO] Building example $ex"
        (cd examples/$ex && dub build --compiler=$DC $DUB_ARGS && dub clean)
    done
fi

It would be nice if library sample code was better integrated with dub so it's more easy to build & run, and maybe even enable parallel builds this way.

We have support for subpackages and building with dub build mylibrary:subpackage, but that serves a different use-case. We don't want to bundle example code as part of the library binary code.


For one use-case I'd love to get rid of the build.d script in https://github.com/AndrejMitrovic/DWinProgramming/blob/master/build.d.

It builds all the examples in https://github.com/AndrejMitrovic/DWinProgramming/tree/master/Samples, it can even do it in parallel.

I'd like to do this with dub but I don't see a way to do it currently.

Maybe this needs to be a new dub feature. Not sure what to call it.. they're not subpackages, so maybe collections?

I can imagine something like this in the library's root dub.sdl:

collections "./examples"

And the example projects as usual would have their own dub file:

dependency "mylibrary" path="../../"

And then being able to run these with dub run examples:hello or dub run :hello, or build all examples in parallel with dub build examples (perhaps with a parallel switch).