app.d 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import std.stdio;
  2. import std.file;
  3. import std.path;
  4. import std.compiler;
  5. import std.system;
  6. import std.format;
  7. import std.conv;
  8. import argparse;
  9. import std.process;
  10. import iopipe.json.serialize;
  11. import iopipe.json.parser;
  12. import std.exception;
  13. import iopipe.traits;
  14. version(X86)
  15. enum arch="x86";
  16. else version(X86_64)
  17. enum arch="x86_64";
  18. else version(ARM)
  19. enum arch="arm";
  20. else version(AArch64)
  21. enum arch="arm64";
  22. else
  23. static assert(false, "Unsupported architecture");
  24. // establish the runtime
  25. version(CRuntime_Microsoft)
  26. enum CRT="MSVC";
  27. else version(CRuntime_Glibc)
  28. enum CRT="glibc";
  29. else version(CppRuntime_Clang)
  30. enum CRT="llvm";
  31. else
  32. static assert(false, "Unsupported runtime");
  33. @(Command("install").Description("Install the raylib-d binary C libraries to a location that can be used for linking"))
  34. struct Args {
  35. @(NamedArgument().Description("Copy libraries to the local directory"))
  36. bool local;
  37. }
  38. enum baseDir = buildPath("install", "lib", os.to!string, arch.to!string, CRT.to!string);
  39. int realMain(Args args)
  40. {
  41. writeln("raylib-d library installation");
  42. // look at the dub.selections.json file
  43. auto dubConfig = execute(["dub", "describe"]);
  44. string raylibdPath;
  45. if(dubConfig.status != 0)
  46. {
  47. stderr.writeln("Error executing dub describe: ", dubConfig.output);
  48. return dubConfig.status;
  49. }
  50. char[] getRaylibPath(char[] jsonStr)
  51. {
  52. auto tokens = jsonTokenizer(jsonStr);
  53. enforce(tokens.parseTo("packages"), "Could not find packages in dub json output!");
  54. auto nt = tokens.next.token;
  55. enforce(nt == JSONToken.ArrayStart, "Expected array start in packages");
  56. while(nt != JSONToken.ArrayEnd)
  57. {
  58. tokens.releaseParsed();
  59. tokens.startCache;
  60. enforce(tokens.parseTo("name"), "Could not find package name in json file");
  61. auto n = tokens.next;
  62. jsonExpect(n, JSONToken.String, "Expected string for package name");
  63. if(n.data(tokens.chain) == "raylib-d")
  64. {
  65. tokens.rewind;
  66. tokens.parseTo("path");
  67. auto p = tokens.next;
  68. jsonExpect(p, JSONToken.String, "Expected string for path");
  69. return p.data(tokens.chain);
  70. }
  71. tokens.rewind;
  72. tokens.endCache;
  73. nt = tokens.skipItem.token;
  74. }
  75. throw new Exception("Could not find raylib-d dependency for current project!");
  76. }
  77. try {
  78. auto path = getRaylibPath(dubConfig.output.dup);
  79. auto libpath = buildPath(path, baseDir);
  80. foreach(ent; dirEntries(libpath, SpanMode.shallow))
  81. {
  82. copy(ent.name, buildPath(".", ent.name.baseName));
  83. }
  84. } catch(Exception ex) {
  85. stderr.writeln("Error: ", ex.msg);
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. mixin CLI!Args.main!realMain;