app.d 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 std.process;
  9. import iopipe.json.serialize;
  10. import iopipe.json.parser;
  11. import std.exception;
  12. import iopipe.traits;
  13. version(X86)
  14. enum arch="x86";
  15. else version(X86_64)
  16. enum arch="x86_64";
  17. else version(ARM)
  18. enum arch="arm";
  19. else version(AArch64)
  20. enum arch="arm64";
  21. else
  22. static assert(false, "Unsupported architecture");
  23. // establish the runtime
  24. version(CRuntime_Microsoft)
  25. enum CRT="MSVC";
  26. else version(CRuntime_Glibc)
  27. enum CRT="glibc";
  28. else version(CppRuntime_Clang)
  29. enum CRT="llvm";
  30. else
  31. static assert(false, "Unsupported runtime");
  32. enum baseDir = buildPath("install", "lib", os.to!string, arch.to!string, CRT.to!string);
  33. int main()
  34. {
  35. writeln("raylib-d library installation");
  36. // look at the dub.selections.json file
  37. auto dubConfig = execute(["dub", "describe"]);
  38. string raylibdPath;
  39. if(dubConfig.status != 0)
  40. {
  41. stderr.writeln("Error executing dub describe: ", dubConfig.output);
  42. return dubConfig.status;
  43. }
  44. char[] getRaylibPath(char[] jsonStr)
  45. {
  46. auto tokens = jsonTokenizer(jsonStr);
  47. enforce(tokens.parseTo("packages"), "Could not find packages in dub json output!");
  48. auto nt = tokens.next.token;
  49. enforce(nt == JSONToken.ArrayStart, "Expected array start in packages");
  50. while(nt != JSONToken.ArrayEnd)
  51. {
  52. tokens.releaseParsed();
  53. tokens.startCache;
  54. enforce(tokens.parseTo("name"), "Could not find package name in json file");
  55. auto n = tokens.next;
  56. jsonExpect(n, JSONToken.String, "Expected string for package name");
  57. if(n.data(tokens.chain) == "raylib-d")
  58. {
  59. tokens.rewind;
  60. tokens.parseTo("path");
  61. auto p = tokens.next;
  62. jsonExpect(p, JSONToken.String, "Expected string for path");
  63. return p.data(tokens.chain);
  64. }
  65. tokens.rewind;
  66. tokens.endCache;
  67. nt = tokens.skipItem.token;
  68. }
  69. throw new Exception("Could not find raylib-d dependency for current project!");
  70. }
  71. try {
  72. auto path = getRaylibPath(dubConfig.output.dup);
  73. auto libpath = buildPath(path, baseDir);
  74. writeln("Copying library files from ", libpath);
  75. foreach(ent; dirEntries(libpath, SpanMode.shallow))
  76. {
  77. auto newLoc = buildPath(".", ent.name.baseName);
  78. version(Posix)
  79. {
  80. if(ent.isSymlink)
  81. {
  82. // recreate the symlink
  83. auto origln = readLink(ent.name);
  84. writefln("Creating symlink %s -> %s", newLoc, origln);
  85. symlink(origln, newLoc);
  86. continue;
  87. }
  88. }
  89. writeln("Installing library file ", newLoc);
  90. copy(ent.name, newLoc, PreserveAttributes.yes);
  91. }
  92. } catch(Exception ex) {
  93. stderr.writeln("Error: ", ex.msg);
  94. return 1;
  95. }
  96. return 0;
  97. }