Thread overview
implementing --version?
Nov 21, 2016
mab-on
Nov 21, 2016
Karabuta
Nov 21, 2016
mab-on
Nov 21, 2016
Basile B.
Nov 21, 2016
Basile B.
Nov 22, 2016
Meta
Nov 22, 2016
Meta
Nov 22, 2016
Jacob Carlborg
Nov 22, 2016
mab-on
November 21, 2016
I would like to implement a "--version" switch for a command line application.
Is there a clever way to do that without using the brain on every build? :)

A dub-solution would be nice - but i didnt find it.
November 21, 2016
On Monday, 21 November 2016 at 19:33:54 UTC, mab-on wrote:
> I would like to implement a "--version" switch for a command line application.
> Is there a clever way to do that without using the brain on every build? :)
>
> A dub-solution would be nice - but i didnt find it.

There is a package in the dub registry called commando (https://code.dlang.org/packages/commando) for that. Example usage can be found at https://github.com/SirTony/commando/tree/master/examples/find.

The README file describes the code found in the src folder.
November 21, 2016
On Monday, 21 November 2016 at 20:56:41 UTC, Karabuta wrote:
>
> There is a package in the dub registry called commando (https://code.dlang.org/packages/commando) for that.

Hm.. maybe i explained it wrong.
My problem is not to pass a argument to the application.

What i want is a clever mechanism to store the SemVer (or Commit-ID) in the binary at compiletime - automatically. Otherwise i have to think to update a const in the code every time i build a new Version.




November 21, 2016
On Monday, 21 November 2016 at 21:32:16 UTC, mab-on wrote:
> What i want is a clever mechanism to store the SemVer (or Commit-ID) in the binary at compiletime - automatically. Otherwise i have to think to update a const in the code every time i build a new Version.

    enum versionData = import("version.dat");
    enum min = anyCTFEFunctionThatParsesMin(versionData);
    enum maj = anyCTFEFunctionThatParsesMaj(versionData);
    // etc... you can even use compile-time regex...


The path to the file "version.dat" must be specified with the -J DMD command line.
Generating the file is another story, maybe a git script could do that.
November 21, 2016
On Monday, 21 November 2016 at 23:46:41 UTC, Basile B. wrote:
> On Monday, 21 November 2016 at 21:32:16 UTC, mab-on wrote:
>> What i want is a clever mechanism to store the SemVer (or Commit-ID) in the binary at compiletime - automatically. Otherwise i have to think to update a const in the code every time i build a new Version.
>
>     enum versionData = import("version.dat");
>     enum min = anyCTFEFunctionThatParsesMin(versionData);
>     enum maj = anyCTFEFunctionThatParsesMaj(versionData);
>     // etc... you can even use compile-time regex...
>
>
> The path to the file "version.dat" must be specified with the -J DMD command line.
> Generating the file is another story, maybe a git script could do that.

I have an example here:

https://github.com/BBasile/Coedit/blob/master/cesetup/cesetup.d#L165

I don't use semVer but you basically get the method.
November 22, 2016
On Monday, 21 November 2016 at 21:32:16 UTC, mab-on wrote:
> On Monday, 21 November 2016 at 20:56:41 UTC, Karabuta wrote:
>>
>> There is a package in the dub registry called commando (https://code.dlang.org/packages/commando) for that.
>
> Hm.. maybe i explained it wrong.
> My problem is not to pass a argument to the application.
>
> What i want is a clever mechanism to store the SemVer (or Commit-ID) in the binary at compiletime - automatically. Otherwise i have to think to update a const in the code every time i build a new Version.

You could write a little script to generate an increasing version and save it in a file. Then with Dub you can use a pre-build or pre-generate command to call that script, and in your D code do `enum version = import("version.txt");`. You can do whatever you want with this text; process it, convert it to an internal data structure, populate settings, etc.

I created a thread a little while ago that's somewhat on this topic... It was partially about Dub pre-build and pre-generate commands so you may find it useful.

http://forum.dlang.org/thread/mzipqtnimvexeddjtcju@forum.dlang.org
November 22, 2016
On Tuesday, 22 November 2016 at 00:41:42 UTC, Meta wrote:
> You could write a little script to generate an increasing version and save it in a file. Then with Dub you can use a pre-build or pre-generate command to call that script, and in your D code do `enum version = import("version.txt");`. You can do whatever you want with this text; process it, convert it to an internal data structure, populate settings, etc.
>
> I created a thread a little while ago that's somewhat on this topic... It was partially about Dub pre-build and pre-generate commands so you may find it useful.
>
> http://forum.dlang.org/thread/mzipqtnimvexeddjtcju@forum.dlang.org

Also another thread I created on the Dub subforum where the creator goes into a little more detail:

http://forum.rejectedsoftware.com/groups/rejectedsoftware.dub/thread/8012/
November 22, 2016
On 2016-11-21 22:32, mab-on wrote:
> On Monday, 21 November 2016 at 20:56:41 UTC, Karabuta wrote:
>>
>> There is a package in the dub registry called commando
>> (https://code.dlang.org/packages/commando) for that.
>
> Hm.. maybe i explained it wrong.
> My problem is not to pass a argument to the application.
>
> What i want is a clever mechanism to store the SemVer (or Commit-ID) in
> the binary at compiletime - automatically. Otherwise i have to think to
> update a const in the code every time i build a new Version.

Use "git describe" to get the latest tag and/or commit hash. Use the "preGenerateCommands" field in Dub to generate the version before compiling the application. Write the result of "git describe" to a file and import that file in the application.

Use std.getopt to parse the command line arguments.

https://github.com/jacob-carlborg/dstep/blob/master/dub.json#L14
https://github.com/jacob-carlborg/dstep/blob/master/dstep/Configuration.d#L18

-- 
/Jacob Carlborg
November 22, 2016
Thanks! These tips are exactly what i needed :)