|
| 1 | +<?php |
| 2 | +/************************************************************** |
| 3 | +* Simple Desk Project - www.simpledesk.net * |
| 4 | +*************************************************************** |
| 5 | +* An advanced help desk modification built on SMF * |
| 6 | +*************************************************************** |
| 7 | +* * |
| 8 | +* * Copyright 2025 - SimpleDesk.net * |
| 9 | +* * |
| 10 | +* This file and its contents are subject to the license * |
| 11 | +* included with this distribution, license.txt, which * |
| 12 | +* states that this software is New BSD Licensed. * |
| 13 | +* Any questions, please contact SimpleDesk.net * |
| 14 | +* * |
| 15 | +*************************************************************** |
| 16 | +* File Info: build.php * |
| 17 | +**************************************************************/ |
| 18 | + |
| 19 | +// The path such as /usr/bin/git |
| 20 | +$git_path = '/usr/bin/git'; |
| 21 | + |
| 22 | +// The path to tar & zip |
| 23 | +$tar_path = '/usr/bin/tar'; |
| 24 | +$zip_path = '/usr/bin/zip'; |
| 25 | + |
| 26 | +/***************************************/ |
| 27 | +/***** END OF CONFIGURATION CHANGES ****/ |
| 28 | + |
| 29 | +global $args; |
| 30 | +parseArgs(); |
| 31 | + |
| 32 | +// Debugging? |
| 33 | +if (isset($_SERVER['USER'], $args) && !empty($args['debug'])) |
| 34 | + error_reporting(E_ALL); |
| 35 | + |
| 36 | +if (empty($args) || empty($args['src']) || empty($args['dst'])) |
| 37 | + die('missing critical settings'); |
| 38 | + |
| 39 | +// Get in the trunk. |
| 40 | +chdir($args['src']); |
| 41 | + |
| 42 | +if (!empty($args['skip-pull'])) |
| 43 | +{ |
| 44 | + $out = shell_exec($git_path . ' pull'); |
| 45 | + |
| 46 | + // No comprenda senior. |
| 47 | + if (strpos($out, 'From git://github.com') === false && strpos($out, 'Already up-to-date.') === false) |
| 48 | + die('GIT build returned an unexpected output: ' . $out); |
| 49 | +} |
| 50 | + |
| 51 | +// Try to find our version. |
| 52 | +$pkg_file = file_get_contents('package-info.xml'); |
| 53 | +preg_match('~<version>([^<]+)</version>~i', $pkg_file, $v); |
| 54 | + |
| 55 | +if (empty($v)) |
| 56 | + die('Unknown Version'); |
| 57 | + |
| 58 | +$version = strtr( |
| 59 | + ucFirst(trim($v[1])), |
| 60 | + array( |
| 61 | + ' ' => '-', |
| 62 | + 'Rc' => 'RC', |
| 63 | + ) |
| 64 | +); |
| 65 | + |
| 66 | +$package_file_base = 'SimpleDesk-' . $version; |
| 67 | + |
| 68 | +// Set our version file. |
| 69 | +$updated_version = false; |
| 70 | +if (empty($args['ignore-version'])) |
| 71 | +{ |
| 72 | + $version_file = $args['src'] . '/sd_source/Subs-SimpleDesk.php'; |
| 73 | + if (!file_exists($version_file) && empty($args['ignore-version'])) |
| 74 | + die('Version file does not exist'); |
| 75 | + $contents = file_get_contents($version_file); |
| 76 | + |
| 77 | + if (preg_match('~define\(\'SHD_VERSION\',\s+\'' . preg_quote($version) . '\'\);~i', $contents) == 1) |
| 78 | + { |
| 79 | + $new_contents = preg_replace('~define\(\'SHD_VERSION\',\s+\'([^\']+)\'\);~i', 'define(\'SHD_VERSION\', \'SimpleDesk ' . $version . '\');', $contents); |
| 80 | + if (is_null($new_contents) && empty($args['ignore-version'])) |
| 81 | + die('Error occured changing the version'); |
| 82 | + if (md5($contents) === md5($new_contents) && empty($args['ignore-version'])) |
| 83 | + die('Replacement failed, no changes made'); |
| 84 | + $updated_version = file_put_contents($version_file, $new_contents); |
| 85 | + } |
| 86 | + else |
| 87 | + $updated_version = true; |
| 88 | +} |
| 89 | + |
| 90 | +// Find all files modified since our last tag. |
| 91 | +if (empty($args['skip-updates'])) |
| 92 | +{ |
| 93 | + if (empty($args['tag'])) |
| 94 | + $git_tag = trim(shell_exec($git_path . ' describe --abbrev=0 --tags')); |
| 95 | + else |
| 96 | + $git_tag = $args['tag']; |
| 97 | + |
| 98 | + if (empty($git_tag)) |
| 99 | + die('Unable to locate a previous tag'); |
| 100 | + |
| 101 | + $commits = shell_exec($git_path . ' rev-list HEAD..' . escapeshellcmd($git_tag) . ' --objects'); |
| 102 | + |
| 103 | + if (empty($commits)) |
| 104 | + die('No commits found'); |
| 105 | + |
| 106 | + preg_match_all('~\w{40}\s([^\s]+)$~im', $commits, $matches); |
| 107 | + |
| 108 | + $files = array_filter(array_unique($matches[1]), function ($file) { |
| 109 | + return substr($file, -3) === 'php'; |
| 110 | + }); |
| 111 | + sort($files); |
| 112 | + |
| 113 | + if ($updated_version) |
| 114 | + $files[] = 'sd_source/Subs-SimpleDesk.php'; |
| 115 | + |
| 116 | + foreach($files as $file) |
| 117 | + { |
| 118 | + // * SimpleDesk Version: 2.1.0 * |
| 119 | + $oc = file_get_contents($args['src'] . '/' . $file); |
| 120 | + $nc = preg_replace_callback('~\*\ SimpleDesk Version: ([^\*]+)\*~i', function($m) use ($version) { |
| 121 | + return '* SimpleDesk Version: ' . str_pad($version, 40) . '*'; |
| 122 | + }, $oc); |
| 123 | + |
| 124 | + $nc = preg_replace('~Copyright (\d+) - SimpleDesk\.net~i', 'Copyright ' . date('Y') . ' - SimpleDesk.net', $nc); |
| 125 | + |
| 126 | + if (is_null($nc)) |
| 127 | + die('Error Updating file'); |
| 128 | + |
| 129 | + file_put_contents($args['src'] . '/' . $file, $nc); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// Build baby, build! |
| 134 | + |
| 135 | +if (file_exists($args['dst'] . '/SimpleDesk_' . $version . '.tgz')) |
| 136 | + unlink($args['dst'] . '/SimpleDesk_' . $version . '.tgz'); |
| 137 | +shell_exec($tar_path . ' --no-xattrs --no-acls' . (PHP_OS_FAMILY === 'Darwin' ? ' --no-mac-metadata --no-fflags' : '') .' --exclude=\'.git\' --exclude=\'.*\' --exclude=\'install-testdata.php\' --exclude=\'error_log\' --exclude=\'buildTools\' --exclude=\'node_modules\' -czf ' . $args['dst'] . '/' . $package_file_base . '.tgz *'); |
| 138 | + |
| 139 | +// Zip it, zip it good. |
| 140 | +if (file_exists($args['dst'] . '/SimpleDesk_' . $version . '.zip')) |
| 141 | + unlink($args['dst'] . '/SimpleDesk_' . $version . '.zip'); |
| 142 | +shell_exec($zip_path . ' -x ".git" ".*/" "install-testdata.php" "error_log" "buildTools/*" "node_modules.*" -1 ' . $args['dst'] . '/' . $package_file_base . '.zip -r *'); |
| 143 | + |
| 144 | +// FINALLY, we are done. |
| 145 | +exit; |
| 146 | + |
| 147 | +function parseArgs() |
| 148 | +{ |
| 149 | + global $args; |
| 150 | + |
| 151 | + if (!isset($_SERVER['argv'])) |
| 152 | + $_SERVER['argv'] = array(); |
| 153 | + |
| 154 | + // If its empty, force help. |
| 155 | + if (empty($_SERVER['argv'][1])) |
| 156 | + $_SERVER['argv'][1] = '--help'; |
| 157 | + |
| 158 | + // Lets get the path_to and path_from |
| 159 | + foreach ($_SERVER['argv'] as $i => $arg) |
| 160 | + { |
| 161 | + // Trim spaces. |
| 162 | + $arg = trim($arg); |
| 163 | + |
| 164 | + if (preg_match('~^--src=(.+)$~', $arg, $match) != 0) |
| 165 | + $args['src'] = substr($match[1], -1) == '/' ? substr($match[1], 0, -1) : $match[1]; |
| 166 | + elseif (preg_match('~^--dst=(.+)$~', $arg, $match) != 0) |
| 167 | + $args['dst'] = substr($match[1], -1) == '/' ? substr($match[1], 0, -1) : $match[1]; |
| 168 | + elseif (preg_match('~^--tag=(.+)$~', $arg, $match) != 0) |
| 169 | + $args['tag'] = substr($match[1], -1) == '/' ? substr($match[1], 0, -1) : $match[1]; |
| 170 | + elseif (preg_match('~^--(debug|ignore-version|skip-updates)$~', $arg, $match) != 0) |
| 171 | + $args[$match[1]] = true; |
| 172 | + elseif ($arg == '--help') |
| 173 | + { |
| 174 | + echo 'Build Tool |
| 175 | +Usage: /path/to/php ' . realpath(__FILE__) . ' -- [OPTION]... |
| 176 | + --src Path to SD (' . realpath($_SERVER['PWD']) . '). |
| 177 | + --dst Output directory for files (/tmp). |
| 178 | + --skip-pull Does not force a pull. |
| 179 | + --ignore-version Do not update version file. |
| 180 | + --debug Output debugging information.'; |
| 181 | + die; |
| 182 | + } |
| 183 | + |
| 184 | + if (empty($args['src'])) |
| 185 | + $args['src'] = realpath($_SERVER['PWD']) . '/'; |
| 186 | + if (empty($args['dst'])) |
| 187 | + $args['dst'] = realpath('/tmp'); |
| 188 | + |
| 189 | + // We have extra params. |
| 190 | + if (preg_match('~^--(.+)=(.+)$~', $arg, $match) != 0 && !array_key_exists($match[1], $_POST)) |
| 191 | + $_POST[$match[1]] = $match[2]; |
| 192 | + } |
| 193 | +} |
0 commit comments