package.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace BsPaySdk;
  3. require_once dirname(__FILE__). "/BsPaySdk/init.php";
  4. $rootDir = opendir(dirname(__FILE__));
  5. $packageName = "php_sdk_" . substr_replace(SDK_VERSION, "", 0, 4) . "_" . date("Ymd");
  6. $build = dirname(__FILE__) . "/" . $packageName;
  7. if (is_dir($build)) {
  8. del_tree($build);
  9. unlink($packageName.".zip");
  10. }
  11. mkdir($build, 0777, true);
  12. # 将被过滤掉的打包文件
  13. $filter = array(
  14. "/\blog\b/i", // 排除 log
  15. "/\bconfig_[\w]*.json\b/i", // 排除测试用的 config
  16. "/\bjson_bak.json\b/i", // 排除测试用的 config
  17. "/\bpackage.php\b/i", // 排除打包脚本自身
  18. "/\bphp_sdk_v[\w]*\b/i", // 排除打包文件夹
  19. "/\bWORK_FLOW.md\b/i", // 排除内部文件
  20. "/\ServerImage\b/i", // 排除服务镜像文件夹
  21. );
  22. # do copy
  23. while ($file = readdir($rootDir)) {
  24. // filter
  25. if (substr(strtolower($file), 0, 1) == "." || match_name($file, $filter)) {
  26. continue;
  27. }
  28. echo $file . "\n";
  29. if (is_dir($file)) {
  30. full_copy($file, $build . "/" . $file, $filter);
  31. } else {
  32. copy($file, $build . "/" . $file);
  33. }
  34. }
  35. # do zip(只在mac系统下执行,其他操作系统保留build目录自行手动处理)
  36. if (PHP_OS == "Darwin") {
  37. $shell = "zip -q -r $packageName.zip ./$packageName";
  38. echo $shell;
  39. system($shell, $status);
  40. # delete temp file
  41. del_tree($build);
  42. }
  43. # =====================================================
  44. /**
  45. * 复制文件夹及内部文件
  46. *
  47. * @param $source
  48. * @param $target
  49. * @param null $filter
  50. */
  51. function full_copy($source, $target, $filter=null)
  52. {
  53. if (is_dir($source)) {
  54. mkdir($target);
  55. $d = dir($source);
  56. while (FALSE !== ($entry = $d->read())) {
  57. if ($entry == '.' || $entry == '..' || match_name($entry, $filter)) {
  58. continue;
  59. }
  60. $Entry = $source . '/' . $entry;
  61. if (is_dir($Entry)) {
  62. full_copy($Entry, $target . '/' . $entry, $filter);
  63. continue;
  64. }
  65. copy($Entry, $target . '/' . $entry);
  66. }
  67. $d->close();
  68. } else {
  69. copy($source, $target);
  70. }
  71. }
  72. /**
  73. * 删除文件夹及内部文件
  74. *
  75. * @param $dir
  76. * @return bool
  77. */
  78. function del_tree($dir)
  79. {
  80. $files = array_diff(scandir($dir), array('.', '..'));
  81. foreach ($files as $file) {
  82. (is_dir("$dir/$file")) ? del_tree("$dir/$file") : unlink("$dir/$file");
  83. }
  84. return rmdir($dir);
  85. }
  86. /**
  87. * 判断名称是否匹配集合
  88. *
  89. * @param string $name
  90. * @param array $set 正则表达式集合
  91. * @return bool|void
  92. */
  93. function match_name($name, $set)
  94. {
  95. foreach ($set as $value) {
  96. if (preg_match($value, $name)) {
  97. return true;
  98. }
  99. }
  100. }