update_version.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /* Update Zint version number in various files */
  3. /*
  4. libzint - the open source barcode library
  5. Copyright (C) 2020-2024 Robin Stuart <rstuart114@gmail.com>
  6. */
  7. /* SPDX-License-Identifier: BSD-3-Clause */
  8. /* Run from project directory
  9. *
  10. * php tools/update_version.php ZINT_VERSION_MAJOR ZINT_VERSION_MINOR ZINT_VERSION_RELEASE [ZINT_VERSION_BUILD]
  11. *
  12. * e.g. before release
  13. * php tools/update_version.php 3 4 5
  14. * cd docs; make
  15. * after release
  16. * php tools/update_version.php 3 4 5 9
  17. * cd docs; make
  18. */
  19. $basename = basename(__FILE__);
  20. $dirname = dirname(__FILE__);
  21. $data_dirname = $dirname . '/../';
  22. if ($argc < 4) {
  23. exit("$basename: ZINT_VERSION_MAJOR ZINT_VERSION_MINOR ZINT_VERSION_RELEASE [ZINT_VERSION_BUILD]" . PHP_EOL);
  24. }
  25. $major = $argv[1];
  26. $minor = $argv[2];
  27. $release = $argv[3];
  28. $build = $argc > 4 ? $argv[4] : "0";
  29. if (!ctype_digit($major) || !ctype_digit($minor) || !ctype_digit($release) || !ctype_digit($build)) {
  30. exit("$basename: ZINT_VERSION_MAJOR ZINT_VERSION_MINOR ZINT_VERSION_RELEASE [ZINT_VERSION_BUILD] must be numeric" . PHP_EOL);
  31. }
  32. $major = (int) $major;
  33. $minor = (int) $minor;
  34. $release = (int) $release;
  35. $build = (int) $build;
  36. if ($major === 0) {
  37. exit("$basename: ZINT_VERSION_MAJOR zero" . PHP_EOL);
  38. }
  39. if ($build && $build !== 9) {
  40. exit("$basename: ZINT_VERSION_BUILD not 9" . PHP_EOL);
  41. }
  42. $v_base_str = $v_str = "$major.$minor.$release";
  43. if ($build) {
  44. $v_str .= ".$build";
  45. }
  46. $v_str_dev = $build ? $v_str . ' (dev)' : $v_str;
  47. $rc_str1 = "$major,$minor,$release,$build";
  48. $rc_str2 = "$major.$minor.$release.$build";
  49. $year = date("Y");
  50. /* `$to_do` is no. of lines that should get replaced/changed, not no. of replacements */
  51. function version_replace($to_do, $file, $match_pattern, $replace_pattern, $replace_str) {
  52. global $basename;
  53. if (($get = file_get_contents($file)) === false) {
  54. exit("$basename: ERROR: Could not read file \"$file\"" . PHP_EOL);
  55. }
  56. $lines = explode("\n", $get);
  57. $done = 0;
  58. foreach ($lines as $li => $line) {
  59. if (preg_match($match_pattern, $line)) {
  60. $cnt = 0;
  61. $lines[$li] = preg_replace($replace_pattern, $replace_str, $line, -1, $cnt);
  62. if ($cnt === 0 || $lines[$li] === NULL) {
  63. exit("$basename: ERROR: Could not replace \"$match_pattern\" in file \"$file\"" . PHP_EOL);
  64. }
  65. $done++;
  66. }
  67. if ($done === $to_do) {
  68. break;
  69. }
  70. }
  71. if ($done !== $to_do) {
  72. exit("$basename: ERROR: Only did $done replacements of $to_do in file \"$file\"" . PHP_EOL);
  73. }
  74. if (!file_put_contents($file, implode("\n", $lines))) {
  75. exit("$basename: ERROR: Could not write file \"$file\"" . PHP_EOL);
  76. }
  77. }
  78. function rc_replace($file, $rc_str1, $rc_str2, $year = '') {
  79. global $basename;
  80. if (($get = file_get_contents($file)) === false) {
  81. exit("$basename: ERROR: Could not read file \"$file\"" . PHP_EOL);
  82. }
  83. $match_pattern1 = '/#define[ \t]+VER_FILEVERSION[ \t]+/';
  84. $match_pattern2 = '/#define[ \t]+VER_FILEVERSION_STR[ \t]+/';
  85. $lines = explode("\n", $get);
  86. $done = 0;
  87. foreach ($lines as $li => $line) {
  88. if (preg_match($match_pattern1, $line)) {
  89. $cnt = 0;
  90. $lines[$li] = preg_replace('/[0-9,]+/', $rc_str1, $line, 1, $cnt);
  91. if ($cnt === 0 || $lines[$li] === NULL) {
  92. exit("$basename: ERROR: Could not replace \"$match_pattern1\" in file \"$file\"" . PHP_EOL);
  93. }
  94. $done++;
  95. } else if (preg_match($match_pattern2, $line)) {
  96. $cnt = 0;
  97. $lines[$li] = preg_replace('/[0-9.]+/', $rc_str2, $line, 1, $cnt);
  98. if ($cnt === 0 || $lines[$li] === NULL) {
  99. exit("$basename: ERROR: Could not replace \"$match_pattern2\" in file \"$file\"" . PHP_EOL);
  100. }
  101. $done++;
  102. }
  103. if ($done === 2) {
  104. break;
  105. }
  106. }
  107. if ($done !== 2) {
  108. exit("$basename: ERROR: Only did $done replacements of 2 in file \"$file\"" . PHP_EOL);
  109. }
  110. if ($year !== '') {
  111. $match_pattern = '/VALUE[ \t]+"LegalCopyright",[ \t]+"Copyright /';
  112. $done = 0;
  113. foreach ($lines as $li => $line) {
  114. if (preg_match($match_pattern, $line)) {
  115. $cnt = 0;
  116. $lines[$li] = preg_replace('/[0-9]+/', $year, $line, 1, $cnt);
  117. if ($cnt === 0 || $lines[$li] === NULL) {
  118. exit("$basename: ERROR: Could not replace \"$match_pattern\" in file \"$file\"" . PHP_EOL);
  119. }
  120. $done++;
  121. break;
  122. }
  123. }
  124. if ($done !== 1) {
  125. exit("$basename: ERROR: Failed to replace Copyright year in file \"$file\"" . PHP_EOL);
  126. }
  127. }
  128. if (!file_put_contents($file, implode("\n", $lines))) {
  129. exit("$basename: ERROR: Could not write file \"$file\"" . PHP_EOL);
  130. }
  131. }
  132. function year_replace($file, $year) {
  133. global $basename;
  134. if (($get = file_get_contents($file)) === false) {
  135. exit("$basename: ERROR: Could not read file \"$file\"" . PHP_EOL);
  136. }
  137. $match_pattern = '/Copyright /';
  138. $lines = explode("\n", $get);
  139. $done = 0;
  140. foreach ($lines as $li => $line) {
  141. if (preg_match($match_pattern, $line)) {
  142. $cnt = 0;
  143. $lines[$li] = preg_replace('/[0-9]+/', $year, $line, 1, $cnt);
  144. if ($cnt === 0 || $lines[$li] === NULL) {
  145. exit("$basename: ERROR: Could not replace \"$match_pattern\" in file \"$file\"" . PHP_EOL);
  146. }
  147. $done++;
  148. break;
  149. }
  150. }
  151. if ($done !== 1) {
  152. exit("$basename: ERROR: Failed to replace Copyright year in file \"$file\"" . PHP_EOL);
  153. }
  154. if (!file_put_contents($file, implode("\n", $lines))) {
  155. exit("$basename: ERROR: Could not write file \"$file\"" . PHP_EOL);
  156. }
  157. }
  158. // CMakeLists.txt
  159. $file = $data_dirname . 'CMakeLists.txt';
  160. if (($get = file_get_contents($file)) === false) {
  161. exit("$basename: ERROR: Could not read file \"$file\"" . PHP_EOL);
  162. }
  163. $lines = explode("\n", $get);
  164. $done = 0;
  165. foreach ($lines as $li => $line) {
  166. if (preg_match('/\(ZINT_VERSION_(MAJOR|MINOR|RELEASE|BUILD)/', $line, $matches)) {
  167. $cnt = 0;
  168. $mmr = $matches[1] === "MAJOR" ? $major : ($matches[1] === "MINOR" ? $minor : ($matches[1] === "RELEASE" ? $release : $build));
  169. $lines[$li] = preg_replace('/[0-9]+\)/', $mmr . ')', $line, 1, $cnt);
  170. if ($cnt === 0 || $lines[$li] === NULL) {
  171. exit("$basename: ERROR: Could not replace ZINT_VERSION_{$matches[1]} in file \"$file\"" . PHP_EOL);
  172. }
  173. $done++;
  174. }
  175. if ($done === 4) {
  176. break;
  177. }
  178. }
  179. if ($done !== 4) {
  180. exit("$basename: ERROR: Only did $done replacements of 4 in file \"$file\"" . PHP_EOL);
  181. }
  182. if (!file_put_contents($file, implode("\n", $lines))) {
  183. exit("$basename: ERROR: Could not write file \"$file\"" . PHP_EOL);
  184. }
  185. // README
  186. year_replace($data_dirname . 'README', $year);
  187. // README.linux
  188. version_replace(4, $data_dirname . 'README.linux', '/zint-[0-9]/', '/[0-9][0-9.]+/', $v_base_str);
  189. // zint.spec
  190. version_replace(1, $data_dirname . 'zint.spec', '/^Version:/', '/[0-9.]+/', $v_base_str);
  191. // zint.nsi
  192. version_replace(1, $data_dirname . 'zint.nsi', '/^!define +PRODUCT_VERSION/', '/"[0-9.]+"/', '"' . $v_str . '"');
  193. // backend/libzint.rc
  194. rc_replace($data_dirname . 'backend/libzint.rc', $rc_str1, $rc_str2, $year);
  195. // backend/zint.h
  196. version_replace(1, $data_dirname . 'backend/zint.h', '/^ \* Version: /', '/[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?( \(dev\))?/', $v_str_dev);
  197. // backend/zintconfig.h
  198. $file = $data_dirname . 'backend/zintconfig.h';
  199. if (($get = file_get_contents($file)) === false) {
  200. exit("$basename: ERROR: Could not read file \"$file\"" . PHP_EOL);
  201. }
  202. $lines = explode("\n", $get);
  203. $done = 0;
  204. foreach ($lines as $li => $line) {
  205. if (preg_match('/define[ \t]+ZINT_VERSION_(MAJOR|MINOR|RELEASE)[ \t]+/', $line, $matches)) {
  206. $cnt = 0;
  207. $mmr = $matches[1] === "MAJOR" ? $major : ($matches[1] === "MINOR" ? $minor : $release);
  208. $lines[$li] = preg_replace('/[0-9]+/', $mmr, $line, 1, $cnt);
  209. if ($cnt === 0 || $lines[$li] === NULL) {
  210. exit("$basename: ERROR: Could not replace ZINT_VERSION_{$matches[1]} in file \"$file\"" . PHP_EOL);
  211. }
  212. $done++;
  213. } elseif (preg_match('/define[ \t]+ZINT_VERSION_BUILD[ \t]+/', $line)) {
  214. $cnt = 0;
  215. $lines[$li] = preg_replace('/(BUILD[ \t]+)[0-9]+/', '${1}' . $build, $line, 1, $cnt);
  216. if ($cnt === 0 || $lines[$li] === NULL) {
  217. exit("$basename: ERROR: Could not replace ZINT_VERSION_BUILD in file \"$file\"" . PHP_EOL);
  218. }
  219. $done++;
  220. }
  221. if ($done === 4) {
  222. break;
  223. }
  224. }
  225. if ($done !== 4) {
  226. exit("$basename: ERROR: Only did $done replacements of 4 in file \"$file\"" . PHP_EOL);
  227. }
  228. if (!file_put_contents($file, implode("\n", $lines))) {
  229. exit("$basename: ERROR: Could not write file \"$file\"" . PHP_EOL);
  230. }
  231. // backend/Makefile.mingw
  232. version_replace(1, $data_dirname . 'backend/Makefile.mingw', '/^ZINT_VERSION:=-DZINT_VERSION=/', '/[0-9.]+/', $v_str);
  233. // backend_tcl/configure.ac
  234. version_replace(1, $data_dirname . 'backend_tcl/configure.ac', '/^AC_INIT\(\[zint\],[ \t]*\[/', '/[0-9.]+/', $v_base_str);
  235. // backend_tcl/zint_tcl.dsp
  236. version_replace(2, $data_dirname . 'backend_tcl/zint_tcl.dsp', '/ZINT_VERSION="\\\\"/', '/ZINT_VERSION="\\\\"[0-9.]+\\\\""/', 'ZINT_VERSION="\\"' . $v_str . '\\""');
  237. // backend_tcl/lib/zint/pkgIndex.tcl
  238. version_replace(1, $data_dirname . 'backend_tcl/lib/zint/pkgIndex.tcl', '/zint /', '/zint [0-9.]+/', 'zint ' . $v_base_str . '');
  239. // backend_tcl/licence.txt
  240. year_replace($data_dirname . 'backend_tcl/licence.txt', $year);
  241. // frontend/zint.rc
  242. rc_replace($data_dirname . 'frontend/zint.rc', $rc_str1, $rc_str2, $year);
  243. // frontend/Makefile.mingw
  244. version_replace(1, $data_dirname . 'frontend/Makefile.mingw', '/^ZINT_VERSION:=-DZINT_VERSION=/', '/[0-9.]+/', $v_str);
  245. // backend_qt/backend_vc8.pro
  246. version_replace(1, $data_dirname . 'backend_qt/backend_vc8.pro', '/^VERSION[ \t]*=/', '/[0-9.]+/', $v_str);
  247. -
  248. // backend_qt/backend_qt.pro
  249. version_replace(1, $data_dirname . 'backend_qt/backend_qt.pro', '/ZINT_VERSION="/', '/[0-9.]+/', $v_str);
  250. version_replace(1, $data_dirname . 'backend_qt/backend_qt.pro', '/^VERSION[ \t]*=/', '/[0-9.]+/', $v_str);
  251. // docs/manual.pmd
  252. version_replace(1, $data_dirname . 'docs/manual.pmd', '/^% Version /', '/[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?( \(dev\))?/', $v_str);
  253. version_replace(1, $data_dirname . 'docs/manual.pmd', '/^The current stable version of Zint/', '/[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?/', $v_base_str);
  254. // docs/zint.1.pmd
  255. version_replace(1, $data_dirname . 'docs/zint.1.pmd', '/^% ZINT\(1\) Version /', '/[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?( \(dev\))?/', $v_str);
  256. // frontend_qt/res/qtZint.rc
  257. rc_replace($data_dirname . 'frontend_qt/res/qtZint.rc', $rc_str1, $rc_str2, $year);
  258. // win32/libzint.vcxproj
  259. version_replace(2, $data_dirname . 'win32/libzint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  260. // win32/zint.vcxproj
  261. version_replace(2, $data_dirname . 'win32/zint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  262. // win32/zint_cmdline_vc6/zint.rc
  263. rc_replace($data_dirname . 'win32/zint_cmdline_vc6/zint.rc', $rc_str1, $rc_str2, $year);
  264. // win32/zint_cmdline_vc6/zint_cmdline_vc6.dsp
  265. version_replace(2, $data_dirname . 'win32/zint_cmdline_vc6/zint_cmdline_vc6.dsp', '/ZINT_VERSION="/', '/ZINT_VERSION="\\\\"[0-9.]+\\\\""/', 'ZINT_VERSION="\\"' . $v_str . '\\""');
  266. // win32/vs2008/libzint.vcproj
  267. version_replace(2, $data_dirname . 'win32/vs2008/libzint.vcproj', '/ZINT_VERSION=&quot;/', '/&quot;[0-9.]+/', '&quot;' . $v_str);
  268. // win32/vs2008/zint.vcproj
  269. version_replace(2, $data_dirname . 'win32/vs2008/zint.vcproj', '/ZINT_VERSION=&quot;/', '/&quot;[0-9.]+/', '&quot;' . $v_str);
  270. // win32/vs2015/libzint.vcxproj
  271. version_replace(6, $data_dirname . 'win32/vs2015/libzint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  272. // win32/vs2015/zint.vcxproj
  273. version_replace(6, $data_dirname . 'win32/vs2015/zint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  274. // win32/vs2017/libzint.vcxproj
  275. version_replace(2, $data_dirname . 'win32/vs2017/libzint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  276. // win32/vs2017/zint.vcxproj
  277. version_replace(2, $data_dirname . 'win32/vs2017/zint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  278. // win32/vs2019/libzint.vcxproj
  279. version_replace(2, $data_dirname . 'win32/vs2019/libzint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  280. // win32/vs2019/zint.vcxproj
  281. version_replace(2, $data_dirname . 'win32/vs2019/zint.vcxproj', '/ZINT_VERSION="/', '/ZINT_VERSION="[0-9.]+"/', 'ZINT_VERSION="' . $v_str . '"');
  282. // Leaving auto-generated files:
  283. // backend_tcl/configure (PACKAGE_VERSION and PACKAGE_STRING) - generated by autoconf from configure.ac
  284. // frontend_qt/Inno_Setup_qtzint.iss (MyAppVersion)
  285. print PHP_EOL;
  286. print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' . PHP_EOL;
  287. print '!!! REMEMBER: update release date in manual and man page !!!' . PHP_EOL;
  288. print '!!! REMEMBER: cd docs; make !!!' . PHP_EOL;
  289. print '!!! REMEMBER: run "autoconf" and "./configure" in "backend_tcl/" !!!' . PHP_EOL;
  290. print '!!! REMEMBER: update version and date in "ChangeLog" !!!' . PHP_EOL;
  291. print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' . PHP_EOL;
  292. print PHP_EOL;
  293. /* vim: set ts=4 sw=4 et : */