prebuild.d 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env dub --single
  2. /+dub.sdl:
  3. name "prebuild"
  4. targetType "executable"
  5. +/
  6. struct Arg
  7. {
  8. string name;
  9. string value;
  10. }
  11. Arg parseArg(string arg)
  12. {
  13. import std.string: split;
  14. auto parts = arg.split(':');
  15. return Arg(parts[0][1..$], parts[1]);
  16. }
  17. void replaceContents(string inputFileName, string outputFileName, string[string] values)
  18. {
  19. import std.file: readText, write;
  20. import std.string: replace;
  21. auto content = readText(inputFileName);
  22. foreach (const ref key, value; values) {
  23. content = replace(content, key, value);
  24. }
  25. write(outputFileName, content);
  26. }
  27. int main(string[] args)
  28. {
  29. import std.process: environment, execute;
  30. import std.stdio: stderr;
  31. import std.string: chomp, strip, split;
  32. if (args.length < 3) {
  33. stderr.writefln("error: expected two arguments: -vcs:<vcs.in.d> -config:<config.d.in>");
  34. return 1;
  35. }
  36. const vcsInput = parseArg(args[1]);
  37. const configInput = parseArg(args[2]);
  38. const vcsOutput = chomp(vcsInput.value, ".in");
  39. const configOutput = chomp(configInput.value, ".in");
  40. const projectVersion = environment.get("DUB_PACKAGE_VERSION");
  41. if (!projectVersion) {
  42. stderr.writeln("error: failed to read DUB_PACKAGE_VERSION environment variable.");
  43. return 1;
  44. }
  45. auto git_rev = execute(["git", "rev-parse", "--short", "HEAD"]);
  46. if (git_rev.status != 0) {
  47. replaceContents(vcsInput.value, vcsOutput, ["@VCS_TAG@": "release"]);
  48. } else {
  49. replaceContents(vcsInput.value, vcsOutput, ["@VCS_TAG@": strip(git_rev.output)]);
  50. }
  51. string[string] conf;
  52. conf["@PROJECT_VERSION_STRING@"] = projectVersion;
  53. const versionSplit = projectVersion.split('.');
  54. const majorVersion = versionSplit[0];
  55. const minorVersion = versionSplit[1];
  56. const patchVersion = versionSplit[2];
  57. conf["@PROJECT_VERSION_MAJOR@"] = majorVersion;
  58. conf["@PROJECT_VERSION_MINOR@"] = minorVersion;
  59. conf["@PROJECT_VERSION_PATCH@"] = patchVersion;
  60. replaceContents(configInput.value, configOutput, conf);
  61. return 0;
  62. }