polybuild.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "polybuild.h"
  2. #include "zip.h"
  3. vector<BuildArg> args;
  4. #define MAXFILENAME (256)
  5. String getArg(String argName) {
  6. for(int i=0; i < args.size(); i++) {
  7. if(args[i].name == argName) {
  8. return args[i].value;
  9. }
  10. }
  11. return "";
  12. }
  13. uLong filetime(
  14. const char *f,
  15. tm_zip *tmzip,
  16. uLong *dt)
  17. {
  18. int ret=0;
  19. struct stat s; /* results of stat() */
  20. struct tm* filedate;
  21. time_t tm_t=0;
  22. if (strcmp(f,"-")!=0)
  23. {
  24. char name[MAXFILENAME+1];
  25. int len = strlen(f);
  26. if (len > MAXFILENAME)
  27. len = MAXFILENAME;
  28. strncpy(name, f,MAXFILENAME-1);
  29. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  30. name[ MAXFILENAME ] = '\0';
  31. if (name[len - 1] == '/')
  32. name[len - 1] = '\0';
  33. /* not all systems allow stat'ing a file with / appended */
  34. if (stat(name,&s)==0)
  35. {
  36. tm_t = s.st_mtime;
  37. ret = 1;
  38. }
  39. }
  40. filedate = localtime(&tm_t);
  41. tmzip->tm_sec = filedate->tm_sec;
  42. tmzip->tm_min = filedate->tm_min;
  43. tmzip->tm_hour = filedate->tm_hour;
  44. tmzip->tm_mday = filedate->tm_mday;
  45. tmzip->tm_mon = filedate->tm_mon ;
  46. tmzip->tm_year = filedate->tm_year;
  47. return ret;
  48. }
  49. void addFolderToZip(zipFile z, String folderPath, String parentFolder) {
  50. std::vector<OSFileEntry> files = OSBasics::parseFolder(folderPath, false);
  51. for(int i=0; i < files.size(); i++) {
  52. if(files[i].type == OSFileEntry::TYPE_FILE) {
  53. printf("Adding %s\n", files[i].name.c_str());
  54. zip_fileinfo zi;
  55. zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  56. zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  57. zi.dosDate = 0;
  58. zi.internal_fa = 0;
  59. zi.external_fa = 0;
  60. filetime(files[i].fullPath.c_str(),&zi.tmz_date,&zi.dosDate);
  61. String pathInZip;
  62. if(parentFolder == "") {
  63. pathInZip = files[i].name;
  64. } else {
  65. pathInZip = parentFolder + "/" + files[i].name;
  66. }
  67. zipOpenNewFileInZip(z, pathInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 2);
  68. FILE *f = fopen(files[i].fullPath.c_str(), "rb");
  69. fseek(f, 0, SEEK_END);
  70. long fileSize = ftell(f);
  71. fseek(f, 0, SEEK_SET);
  72. char *buf = (char*) malloc(fileSize);
  73. fread(buf, fileSize, 1, f);
  74. zipWriteInFileInZip(z, buf, fileSize);
  75. free(buf);
  76. fclose(f);
  77. zipCloseFileInZip(z);
  78. } else {
  79. addFolderToZip(z, files[i].fullPath.c_str(), files[i].name);
  80. }
  81. }
  82. }
  83. int main(int argc, char **argv) {
  84. printf("Polycode build tool v0.1.0\n");
  85. for(int i=0; i < argc; i++) {
  86. String argString = String(argv[i]);
  87. vector<String> bits = argString.split("=");
  88. if(bits.size() == 2) {
  89. BuildArg arg;
  90. arg.name = bits[0];
  91. arg.value = bits[1];
  92. args.push_back(arg);
  93. }
  94. }
  95. if(getArg("--project") == "") {
  96. printf("\n\nInput project missing. Use --project=projectPath to specify.\n\n");
  97. return 0;
  98. }
  99. if(getArg("--out") == "") {
  100. printf("\n\nOutput file not specified. Use --out=outfile.polyapp to specify.\n\n");
  101. return 0;
  102. }
  103. zipFile z = zipOpen(getArg("--out").c_str(), 0);
  104. addFolderToZip(z, getArg("--project"), "");
  105. zipClose(z, "");
  106. return 1;
  107. }