app.d 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 std.string;
  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. enum baseDir = buildPath("install", "lib", os.to!string, arch.to!string, CRT.to!string);
  34. int main()
  35. {
  36. writeln("raylib-d library installation");
  37. // look at the dub.selections.json file
  38. auto dubConfig = execute(["dub", "describe"], null, Config.stderrPassThrough);
  39. string raylibdPath;
  40. if(dubConfig.status != 0)
  41. {
  42. stderr.writeln("Error executing dub describe");
  43. return dubConfig.status;
  44. }
  45. char[] getRaylibPath(char[] jsonStr)
  46. {
  47. auto tokens = jsonTokenizer(jsonStr);
  48. enforce(tokens.parseTo("packages"), "Could not find packages in dub json output!");
  49. auto nt = tokens.next.token;
  50. enforce(nt == JSONToken.ArrayStart, "Expected array start in packages");
  51. while(nt != JSONToken.ArrayEnd)
  52. {
  53. tokens.releaseParsed();
  54. tokens.startCache;
  55. enforce(tokens.parseTo("name"), "Could not find package name in json file");
  56. auto n = tokens.next;
  57. jsonExpect(n, JSONToken.String, "Expected string for package name");
  58. if(n.data(tokens.chain) == "raylib-d")
  59. {
  60. tokens.rewind;
  61. tokens.parseTo("path");
  62. auto p = tokens.next;
  63. jsonExpect(p, JSONToken.String, "Expected string for path");
  64. return p.data(tokens.chain);
  65. }
  66. tokens.rewind;
  67. tokens.endCache;
  68. nt = tokens.skipItem.token;
  69. }
  70. throw new Exception("Could not find raylib-d dependency for current project!");
  71. }
  72. try {
  73. auto path = getRaylibPath(dubConfig.output.dup);
  74. auto libpath = buildPath(path, baseDir);
  75. writeln("Copying library files from ", libpath);
  76. foreach(ent; dirEntries(libpath, SpanMode.shallow))
  77. {
  78. auto newLoc = buildPath(".", ent.name.baseName(".lnk"));
  79. version(Posix)
  80. {
  81. if(ent.isSymlink)
  82. {
  83. // recreate the symlink
  84. auto origln = readLink(ent.name);
  85. writefln("Creating symlink %s -> %s", newLoc, origln);
  86. symlink(origln, newLoc);
  87. continue;
  88. }
  89. else if(ent.name.endsWith(".lnk"))
  90. {
  91. // dub workaround. This is really a symlink but wasn't
  92. // properly downloaded by dub.
  93. auto origln = cast(char[])read(ent.name);
  94. writefln("Creating symlink %s -> %s", newLoc, origln);
  95. symlink(origln, newLoc);
  96. continue;
  97. }
  98. }
  99. writeln("Installing library file ", newLoc);
  100. copy(ent.name, newLoc, PreserveAttributes.yes);
  101. }
  102. } catch(Exception ex) {
  103. stderr.writeln("Error: ", ex.msg);
  104. return 1;
  105. }
  106. return 0;
  107. }