Thread overview
Local library with dub
Apr 20, 2021
JG
Apr 20, 2021
Andre Pany
Apr 20, 2021
JG
Apr 21, 2021
Mike Parker
Apr 21, 2021
JG
Apr 21, 2021
Jordan Wilson
Apr 22, 2021
JG
April 20, 2021

Hi

I want to put some code together in a local library that is then used by several other projects. I am running into a few problems. Firstly when I try and configure the code to be a library (dub init, add d files to source, and remove source/app.d - perhaps this wrong) dub test no longer seems to work?

Secondly I am having problems getting dub to add the library code to other projects. It seems to work if I run dub add-local path/to/library and then add the appropriate dependencies to the projects dub.json file. However, I didn't manage to find documentation that explains exactly how this should work. Also there seems to be some mention getting this to work with versions (in the documentation about dub add-path), which I couldn't follow.

Does anyone know in more detail how this works or how I can find out?

April 20, 2021

On Tuesday, 20 April 2021 at 17:15:15 UTC, JG wrote:

>

Hi

I want to put some code together in a local library that is then used by several other projects. I am running into a few problems. Firstly when I try and configure the code to be a library (dub init, add d files to source, and remove source/app.d - perhaps this wrong) dub test no longer seems to work?

Secondly I am having problems getting dub to add the library code to other projects. It seems to work if I run dub add-local path/to/library and then add the appropriate dependencies to the projects dub.json file. However, I didn't manage to find documentation that explains exactly how this should work. Also there seems to be some mention getting this to work with versions (in the documentation about dub add-path), which I couldn't follow.

Does anyone know in more detail how this works or how I can find out?

You need to add 2 configurations. The first configuration is used automatically when you execute dub build. Just name the config debug with targetType library. Add a second configuration with name unittest and targetType executable and attribute mainSourceFile pointing to a module containing your main function for your test. For example name this module testapp.d.
The module testapp.d you need to exclude in your configuration debug using attribute excludeSourceFiles.

The configuration unittest is automatically used when you execute dub test.

Kind regards
Andre

April 20, 2021

On Tuesday, 20 April 2021 at 18:11:18 UTC, Andre Pany wrote:

>

On Tuesday, 20 April 2021 at 17:15:15 UTC, JG wrote:

>

Hi

I want to put some code together in a local library that is then used by several other projects. I am running into a few problems. Firstly when I try and configure the code to be a library (dub init, add d files to source, and remove source/app.d - perhaps this wrong) dub test no longer seems to work?

Secondly I am having problems getting dub to add the library code to other projects. It seems to work if I run dub add-local path/to/library and then add the appropriate dependencies to the projects dub.json file. However, I didn't manage to find documentation that explains exactly how this should work. Also there seems to be some mention getting this to work with versions (in the documentation about dub add-path), which I couldn't follow.

Does anyone know in more detail how this works or how I can find out?

You need to add 2 configurations. The first configuration is used automatically when you execute dub build. Just name the config debug with targetType library. Add a second configuration with name unittest and targetType executable and attribute mainSourceFile pointing to a module containing your main function for your test. For example name this module testapp.d.
The module testapp.d you need to exclude in your configuration debug using attribute excludeSourceFiles.

The configuration unittest is automatically used when you execute dub test.

Kind regards
Andre

Thank you very much. In case someone wants more specific instructions:
(a) add a file source/test.d containing:
void main(){}
(b) add configurations to dub.json:
"configurations": [
{ "name": "debug",
"excludedSourceFiles": ["source/test.d"],
"targetType": "library"},
{ "name": "unittest",
"mainSourceFile": "source/test.d",
"targetType": "executable"}]

This still leaves open the question of how to include a version of such a library in another project via dub.

April 21, 2021

On Tuesday, 20 April 2021 at 18:43:28 UTC, JG wrote:

>

This still leaves open the question of how to include a version of such a library in another project via dub.

Execute dub add-local followed by the path to the project's root directory (the directory containing the dub.json/sdl) and a version number in semver format.

dub add-local path 0.1.0

Then you can use the package just as you would any other dub dependency.

April 21, 2021

On Wednesday, 21 April 2021 at 00:39:41 UTC, Mike Parker wrote:

>

On Tuesday, 20 April 2021 at 18:43:28 UTC, JG wrote:

>

This still leaves open the question of how to include a version of such a library in another project via dub.

Execute dub add-local followed by the path to the project's root directory (the directory containing the dub.json/sdl) and a version number in semver format.

dub add-local path 0.1.0

Then you can use the package just as you would any other dub dependency.

Thanks. I suppose this means that if you want to able to use multiple versions you have to keep each version in a separate directory and add them individually, and there is no way of being able to get the appropriate version from say a git repository?

April 21, 2021

On Wednesday, 21 April 2021 at 15:07:25 UTC, JG wrote:

>

On Wednesday, 21 April 2021 at 00:39:41 UTC, Mike Parker wrote:

>

On Tuesday, 20 April 2021 at 18:43:28 UTC, JG wrote:

>

This still leaves open the question of how to include a version of such a library in another project via dub.

Execute dub add-local followed by the path to the project's root directory (the directory containing the dub.json/sdl) and a version number in semver format.

dub add-local path 0.1.0

Then you can use the package just as you would any other dub dependency.

Thanks. I suppose this means that if you want to able to use multiple versions you have to keep each version in a separate directory and add them individually, and there is no way of being able to get the appropriate version from say a git repository?

You can specify git repos as dependencies, if this helps.
https://dlang.org/changelog/2.094.0.html#git-paths

Jordan

April 22, 2021

On Wednesday, 21 April 2021 at 19:15:19 UTC, Jordan Wilson wrote:

>

On Wednesday, 21 April 2021 at 15:07:25 UTC, JG wrote:

>

On Wednesday, 21 April 2021 at 00:39:41 UTC, Mike Parker wrote:

>

[...]

Thanks. I suppose this means that if you want to able to use multiple versions you have to keep each version in a separate directory and add them individually, and there is no way of being able to get the appropriate version from say a git repository?

You can specify git repos as dependencies, if this helps.
https://dlang.org/changelog/2.094.0.html#git-paths

Jordan
Thanks, I will see if I can use this.