www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - implementing --version?

reply mab-on <code mab-on.net> writes:
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.
Nov 21 2016
parent reply Karabuta <karabutaworld gmail.com> writes:
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.
Nov 21 2016
parent reply mab-on <code mab-on.net> writes:
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.
Nov 21 2016
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
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.
Nov 21 2016
parent Basile B. <b2.temp gmx.com> writes:
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.
Nov 21 2016
prev sibling next sibling parent reply Meta <jared771 gmail.com> writes:
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
Nov 21 2016
parent Meta <jared771 gmail.com> writes:
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/
Nov 21 2016
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Nov 21 2016
parent mab-on <code mab-on.net> writes:
Thanks! These tips are exactly what i needed :)
Nov 22 2016