File.hx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package arm.sys;
  2. import haxe.io.Bytes;
  3. using StringTools;
  4. class File {
  5. #if krom_windows
  6. static inline var cmd_dir = "dir /b";
  7. static inline var cmd_copy = "copy";
  8. static inline var cmd_del = "del /f";
  9. #else
  10. static inline var cmd_dir = "ls";
  11. static inline var cmd_copy = "cp";
  12. static inline var cmd_del = "rm";
  13. #end
  14. public static function readDirectory(path: String): Array<String> {
  15. var save = Path.data() + Path.sep + "dir.txt";
  16. Krom.sysCommand(cmd_dir + ' "' + path + '" > "' + save + '"');
  17. var str = Bytes.ofData(Krom.loadBlob(save)).toString();
  18. var ar = str.split("\n");
  19. var files: Array<String> = [];
  20. for (file in ar) if (file.length > 0) files.push(file.rtrim());
  21. return files;
  22. }
  23. public static function createDirectory(path: String) {
  24. Krom.sysCommand("mkdir " + path);
  25. }
  26. public static function copy(srcPath: String, dstPath: String) {
  27. Krom.sysCommand(cmd_copy + ' "' + srcPath + '" "' + dstPath + '"');
  28. }
  29. public static function start(path: String) {
  30. #if krom_windows
  31. Krom.sysCommand('start "" "' + path + '"');
  32. #elseif krom_linux
  33. Krom.sysCommand('xdg-open "' + path + '"');
  34. #else
  35. Krom.sysCommand('open "' + path + '"');
  36. #end
  37. }
  38. public static function explorer(url: String) {
  39. #if krom_windows
  40. Krom.sysCommand('explorer "' + url + '"');
  41. #elseif krom_linux
  42. Krom.sysCommand('xdg-open "' + url + '"');
  43. #else
  44. Krom.sysCommand('open "' + url + '"');
  45. #end
  46. }
  47. public static function delete(path: String) {
  48. Krom.sysCommand(cmd_del + ' "' + path + '"');
  49. }
  50. public static function exists(path: String): Bool {
  51. #if krom_windows
  52. var exists = Krom.sysCommand('IF EXIST "' + path + '" EXIT /b 1');
  53. #else
  54. var exists = 1;
  55. // { test -e file && echo 1 || echo 0 }
  56. #end
  57. return exists == 1;
  58. }
  59. public static function download(url: String, dstPath: String) {
  60. #if krom_windows
  61. Krom.sysCommand('powershell -c "Invoke-WebRequest -Uri ' + url + " -OutFile '" + dstPath + "'");
  62. #else
  63. Krom.sysCommand("curl " + url + " -o " + dstPath);
  64. #end
  65. }
  66. public static function downloadBytes(url: String): Bytes {
  67. var save = Path.data() + Path.sep + "download.bin";
  68. download(url, save);
  69. return Bytes.ofData(Krom.loadBlob(save));
  70. }
  71. }