Thread overview
How to make project with main application and cli application in the same folder?
Apr 21, 2024
alex
Apr 21, 2024
Monkyyy
Apr 21, 2024
Mike Parker
Apr 25, 2024
alex
April 21, 2024
Hi guys. Trying to play with vibe-d and want to create separate web app, and cli app which can add admin users. When I just keep both files app.d and cli.d in source folder, I get an error that I can't have more then 1 main function.

I already asked chatGPT, and it replied that I need to use subpackages. I put cli into separate folder, and now I get an error, that it can't see some model from main app(I want to reuse UserModel for both, cli and web app). How can I make it visible for both cli and web without making copy?

My folder:

project
 | -- dub.json
 | -- source/
       | -- app.d
       | -- user_model.d
 | -- cli/
       | -- source
              | -- cli.d

dub.json:
{
	"name": "project",
	"subPackages": [
        {
            "name": "app",
            "sourcePaths": ["source"],
            "mainSourceFile": "source/app.d",
            "targetType": "executable",
			"dependencies": {
				"vibe-d": "~>0.9"
			}
        },
        {
            "name": "cli",
            "sourcePaths": ["cli/source"],
            "mainSourceFile": "cli/source/cli.d",
            "targetType": "executable",
			"dependencies": {
				"vibe-d": "~>0.9"
			}
        }
    ]
}
April 21, 2024
On Sunday, 21 April 2024 at 08:44:38 UTC, alex wrote:
> Hi guys. Trying to play with vibe-d and want to create separate web app, and cli app which can add admin users. When I just keep both files app.d and cli.d in source folder, I get an error that I can't have more then 1 main function.
>
> [...]

This is the issue that made me quit dub entirely, you'll be told "you shouldn'twant that" or very complicated git sub project nonsense

I suggest seeing if vibe can be ported to plain d and writing a build script
April 21, 2024

On Sunday, 21 April 2024 at 08:44:38 UTC, alex wrote:

>

Hi guys. Trying to play with vibe-d and want to create separate web app, and cli app which can add admin users. When I just keep both files app.d and cli.d in source folder, I get an error that I can't have more then 1 main function.

You can do this using configurations. Whichever you list first will be the default. Then you can use -c configName or --config=configName to build the other one.

You'll want to exclude one of the main functions when building the configuration to which it doesn't belong. You can do that with version specifications (e.g., add a cli version in the cli configuration, then vesrion(cli) void main {...} in the code). Alternatively, if the files the main functions are in are self-contained, then you can just exclude the one you don't need in each configuration with the excludeSourceFiles directive.

Configurations:
https://dub.pm/dub-guide/recipe/#configurations

April 25, 2024

On Sunday, 21 April 2024 at 16:41:08 UTC, Mike Parker wrote:

>

On Sunday, 21 April 2024 at 08:44:38 UTC, alex wrote:

>

Hi guys. Trying to play with vibe-d and want to create separate web app, and cli app which can add admin users. When I just keep both files app.d and cli.d in source folder, I get an error that I can't have more then 1 main function.

You can do this using configurations. Whichever you list first will be the default. Then you can use -c configName or --config=configName to build the other one.

You'll want to exclude one of the main functions when building the configuration to which it doesn't belong. You can do that with version specifications (e.g., add a cli version in the cli configuration, then vesrion(cli) void main {...} in the code). Alternatively, if the files the main functions are in are self-contained, then you can just exclude the one you don't need in each configuration with the excludeSourceFiles directive.

Configurations:
https://dub.pm/dub-guide/recipe/#configurations

Thanks, that really helped me.

For everyone who has the same trouble I can attach my working solution based on sub packages and configurations. Here is my dub.json:

{
    "name": "cool",
	"subPackages": [
        {
            "name": "app",
            "sourcePaths": ["source"],
            "mainSourceFile": "source/app.d",
            "targetType": "executable",
			"configurations": [
				{
					"name": "app",
					"targetType": "executable",
					"versions": ["app"]
				}
			]
        },
        {
            "name": "cli",
            "sourcePaths": ["source"],
            "mainSourceFile": "source/cli.d",
            "targetType": "executable",
			"configurations": [
				{
					"name": "cli",
					"targetType": "executable",
					"versions": ["cli"]
				}
			]
        }
    ]
}

Main functions:
app.d:

  version(app)
  void main() {...}

cli.d:

  version(cli)
  void main() {...}

And here how I run/build it:

> dub run :cli
> dub run :app

OR

> dub build :cli
> dub build :app

During building, it will create executable like "{name_of_project}_app" or "{name_of_project}_cli".