How to make project with main application and cli application in the same folder?

alex ashustrich at gmail.com
Thu Apr 25 07:36:11 UTC 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:

```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:
```d
   version(app)
   void main() {...}
```

cli.d:
```d
   version(cli)
   void main() {...}
```

And here how I run/build it:
```make
> dub run :cli
> dub run :app
```
OR
```make
> dub build :cli
> dub build :app
```

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


More information about the Digitalmars-d-learn mailing list