|
| 1 | +<?php /* |
| 2 | ++------------------------------------------------------------------------------+ |
| 3 | +| Copyright (c) 1997-2023 The PHP Group | |
| 4 | ++------------------------------------------------------------------------------+ |
| 5 | +| This source file is subject to version 3.01 of the PHP license, | |
| 6 | +| that is bundled with this package in the file LICENSE, and is | |
| 7 | +| available through the world-wide-web at the following url: | |
| 8 | +| https://www.php.net/license/3_01.txt. | |
| 9 | +| If you did not receive a copy of the PHP license and are unable to | |
| 10 | +| obtain it through the world-wide-web, please send a note to | |
| 11 | +| [email protected], so we can mail you a copy immediately. | |
| 12 | ++------------------------------------------------------------------------------+ |
| 13 | +| Authors: André L F S Bacci <ae php.net> | |
| 14 | ++------------------------------------------------------------------------------+ |
| 15 | +| Description: Creates and maintains manual languages checkouts. | |
| 16 | ++------------------------------------------------------------------------------+ |
| 17 | +*/ |
| 18 | + |
| 19 | +// dir manual revcheck cloneUrl label |
| 20 | + |
| 21 | +lang( "de" , true , true , "[email protected]:php/doc-de.git" , "German" ); |
| 22 | +lang( "en" , true , false , "[email protected]:php/doc-en.git" , "English" ); |
| 23 | +lang( "es" , true , true , "[email protected]:php/doc-es.git" , "Spanish" ); |
| 24 | +lang( "fr" , true , true , "[email protected]:php/doc-fr.git" , "French" ); |
| 25 | +lang( "it" , true , true , "[email protected]:php/doc-it.git" , "Italian" ); |
| 26 | +lang( "ja" , true , true , "[email protected]:php/doc-ja.git" , "Japanese" ); |
| 27 | +lang( "pl" , false , true , "[email protected]:php/doc-pl.git" , "Polish" ); |
| 28 | +lang( "pt_BR" , true , true , "[email protected]:php/doc-pt_br.git" , "Brazilian Portuguese" ); |
| 29 | +lang( "ro" , false , true , "[email protected]:php/doc-ro.git" , "Romanian" ); |
| 30 | +lang( "ru" , true , true , "[email protected]:php/doc-ru.git" , "Russian" ); |
| 31 | +lang( "tr" , true , true , "[email protected]:php/doc-tr.git" , "Turkish" ); |
| 32 | +lang( "uk" , true , true , "[email protected]:php/doc-uk.git" , "Ukrainian" ); |
| 33 | +lang( "zh" , true , true , "[email protected]:php/doc-zh.git" , "Chinese (Simplified)" ); |
| 34 | + |
| 35 | +if ( count( $argv ) == 1 ) |
| 36 | + print_usage(); |
| 37 | +else |
| 38 | + run(); |
| 39 | +return; |
| 40 | + |
| 41 | +function print_usage() |
| 42 | +{ |
| 43 | + print <<<USAGE |
| 44 | +usage: [--clone] [--undo] [--pull] [--mark] [--quiet] |
| 45 | + [--list-cvs] [--list-ssv] [--rev] [--all] |
| 46 | + [lang] [lang] ... |
| 47 | +
|
| 48 | +Options that operates on local repositories: |
| 49 | +
|
| 50 | + --clone Clone a sibling language repo, if not exists |
| 51 | + --undo Restore and clean up repositories to a pristine state |
| 52 | + --pull Executes git pull |
| 53 | + --mark Creates/deletes marking files |
| 54 | + --quiet Set this option on git commands |
| 55 | +
|
| 56 | +Options that output simple listings: |
| 57 | +
|
| 58 | + --list-csv List selected langcodes separated with commas |
| 59 | + --list-ssv List selected langcodes separated with spaces |
| 60 | +
|
| 61 | +Options that select more languages to operate: |
| 62 | +
|
| 63 | + --rev Include languages with revcheck flag |
| 64 | + --all Include all languages |
| 65 | + --base Include doc-base repo |
| 66 | +
|
| 67 | +
|
| 68 | +USAGE; |
| 69 | +} |
| 70 | + |
| 71 | +function lang( string $code , bool $manual , bool $revcheck , string $cloneUrl , string $label ) |
| 72 | +{ |
| 73 | + $lang = new Lang( $code , $manual , $revcheck , $cloneUrl , $label ); |
| 74 | + Conf::$knowLangs[ $code ] = $lang; |
| 75 | +} |
| 76 | + |
| 77 | +class Conf |
| 78 | +{ |
| 79 | + static array $langs = []; // languages to operate |
| 80 | + static array $knowLangs = []; // all declared languages |
| 81 | + |
| 82 | + static bool $clone = false; |
| 83 | + static bool $undo = false; |
| 84 | + static bool $pull = false; |
| 85 | + static bool $mark = false; |
| 86 | + |
| 87 | + static string|null $quiet = null; |
| 88 | + |
| 89 | + static bool $listCsv = false; |
| 90 | + static bool $listSsv = false; |
| 91 | +} |
| 92 | + |
| 93 | +class Lang |
| 94 | +{ |
| 95 | + function __construct |
| 96 | + ( public string $code |
| 97 | + , public bool $manual |
| 98 | + , public bool $revcheck |
| 99 | + , public string $cloneUrl |
| 100 | + , public string $label |
| 101 | + , public string $path = "" ) |
| 102 | + { |
| 103 | + $this->path = realpath( __DIR__ . '/..' ) . "/{$code}"; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function run() |
| 108 | +{ |
| 109 | + global $argv; |
| 110 | + array_shift( $argv ); |
| 111 | + foreach( $argv as $arg ) |
| 112 | + { |
| 113 | + switch( $arg ) |
| 114 | + { |
| 115 | + case "--clone": Conf::$clone = true; break; |
| 116 | + case "--undo": Conf::$undo = true; break; |
| 117 | + case "--pull": Conf::$pull = true; break; |
| 118 | + case "--mark": Conf::$mark = true; break; |
| 119 | + |
| 120 | + case "--quiet": Conf::$quiet = "--quiet"; break; |
| 121 | + |
| 122 | + case "--list-csv": Conf::$listCsv = true; break; |
| 123 | + case "--list-ssv": Conf::$listSsv = true; break; |
| 124 | + |
| 125 | + case "--all": langAddAll(); break; |
| 126 | + case "--rev": langAddRev(); break; |
| 127 | + case "--base": langDocbase(); break; |
| 128 | + |
| 129 | + default: langAdd( $arg ); break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Default: languages with build manual flag |
| 134 | + |
| 135 | + if ( count( Conf::$langs ) == 0 ) |
| 136 | + foreach( Conf::$knowLangs as $lang ) |
| 137 | + if ( $lang->manual ) |
| 138 | + Conf::$langs[ $lang->code ] = $lang; |
| 139 | + |
| 140 | + // Exclusive listing commands |
| 141 | + |
| 142 | + if ( Conf::$listCsv || Conf::$listSsv ) |
| 143 | + { |
| 144 | + $lst = []; |
| 145 | + foreach( Conf::$langs as $lang ) |
| 146 | + $lst[] = $lang->code; |
| 147 | + if ( Conf::$listCsv ) |
| 148 | + print implode( ',' , $lst ); |
| 149 | + else |
| 150 | + print implode( ' ' , $lst ); |
| 151 | + exit; |
| 152 | + } |
| 153 | + |
| 154 | + // Composite commands |
| 155 | + |
| 156 | + echo "Selected languages:"; |
| 157 | + foreach( Conf::$langs as $lang ) |
| 158 | + echo ' ' . $lang->code; |
| 159 | + echo "\n"; |
| 160 | + |
| 161 | + gitAll(); |
| 162 | + dirMark(); |
| 163 | +} |
| 164 | + |
| 165 | +function langAdd( string $langCode ) |
| 166 | +{ |
| 167 | + foreach( Conf::$knowLangs as $lang ) |
| 168 | + { |
| 169 | + if ( $lang->code == $langCode ) |
| 170 | + { |
| 171 | + Conf::$langs[ $lang->code ] = $lang; |
| 172 | + return; |
| 173 | + } |
| 174 | + } |
| 175 | + fprintf( STDERR , "Unknown option or langcode: $langCode\n" ); |
| 176 | + exit(-1); |
| 177 | +} |
| 178 | + |
| 179 | +function langAddAll() |
| 180 | +{ |
| 181 | + foreach( Conf::$knowLangs as $lang ) |
| 182 | + Conf::$langs[ $lang->code ] = $lang; |
| 183 | +} |
| 184 | + |
| 185 | +function langAddRev() |
| 186 | +{ |
| 187 | + foreach( Conf::$knowLangs as $lang ) |
| 188 | + if ( $lang->manual || $lang->revcheck ) |
| 189 | + Conf::$langs[ $lang->code ] = $lang; |
| 190 | +} |
| 191 | + |
| 192 | +function langDocbase() |
| 193 | +{ |
| 194 | + $code = basename( __DIR__ ); |
| 195 | + $lang = new Lang( $code , false , false , "" , "" ); |
| 196 | + Conf::$langs[ $lang->code ] = $lang; |
| 197 | +} |
| 198 | + |
| 199 | +function gitAll() |
| 200 | +{ |
| 201 | + foreach( Conf::$langs as $lang ) |
| 202 | + { |
| 203 | + gitClone( $lang ); |
| 204 | + gitUndo ( $lang ); |
| 205 | + gitPull ( $lang ); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +function gitClone( Lang $lang ) |
| 210 | +{ |
| 211 | + if ( Conf::$clone == false ) |
| 212 | + return; |
| 213 | + if ( $lang->cloneUrl == "" ) |
| 214 | + return; |
| 215 | + |
| 216 | + if ( file_exists( $lang->path ) ) |
| 217 | + { |
| 218 | + echo "clone {$lang->code} (already exists)\n"; |
| 219 | + return; |
| 220 | + } |
| 221 | + else |
| 222 | + echo "clone {$lang->code}\n"; |
| 223 | + |
| 224 | + $cmd = array( 'git' , 'clone' , Conf::$quiet , $lang->cloneUrl , $lang->path ); |
| 225 | + cmdExecute( $cmd ); |
| 226 | +} |
| 227 | + |
| 228 | +function gitUndo( Lang $lang ) |
| 229 | +{ |
| 230 | + if ( Conf::$undo == false ) |
| 231 | + return; |
| 232 | + |
| 233 | + if ( ! file_exists( $lang->path ) ) |
| 234 | + { |
| 235 | + echo "undo {$lang->code}: path does not exists, skipping.\n"; |
| 236 | + return; |
| 237 | + } |
| 238 | + else |
| 239 | + echo "undo {$lang->code}\n"; |
| 240 | + |
| 241 | + $cmd = array( 'git' , '-C' , $lang->path , 'restore' , Conf::$quiet , '.' ); |
| 242 | + cmdExecute( $cmd ); |
| 243 | + |
| 244 | + $cmd = array( 'git' , '-C' , $lang->path , 'clean' , Conf::$quiet , '-f' , '-d' ); |
| 245 | + cmdExecute( $cmd ); |
| 246 | + |
| 247 | + $cmd = array( 'git' , '-C' , $lang->path , 'clean' , '--quiet' , '-fdx' ); |
| 248 | + cmdExecute( $cmd ); |
| 249 | +} |
| 250 | + |
| 251 | +function gitPull( Lang $lang ) |
| 252 | +{ |
| 253 | + if ( Conf::$pull == false ) |
| 254 | + return; |
| 255 | + |
| 256 | + if ( ! file_exists( $lang->path ) ) |
| 257 | + { |
| 258 | + echo "pull {$lang->code}: path does not exists, skipping.\n"; |
| 259 | + return; |
| 260 | + } |
| 261 | + else |
| 262 | + echo "pull {$lang->code}\n"; |
| 263 | + |
| 264 | + $cmd = array( 'git' , '-C' , $lang->path , 'pull' , Conf::$quiet ); |
| 265 | + cmdExecute( $cmd ); |
| 266 | +} |
| 267 | + |
| 268 | +function cmdExecute( array $parts ) |
| 269 | +{ |
| 270 | + $escaped = []; |
| 271 | + foreach( $parts as $part ) |
| 272 | + if ( $part != null ) |
| 273 | + $escaped[] = escapeshellarg( $part ); |
| 274 | + |
| 275 | + $cmd = implode( ' ' , $escaped ); |
| 276 | + $rsc = null; |
| 277 | + $ret = passthru( $cmd , $rsc ); |
| 278 | + |
| 279 | + if ( $ret === false || $rsc != 0 ) |
| 280 | + { |
| 281 | + echo "\nCommand failed, aborting: $cmd\n\n"; |
| 282 | + exit(-1); |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | +function dirMark() |
| 287 | +{ |
| 288 | + if ( Conf::$mark == false ) |
| 289 | + return; |
| 290 | + |
| 291 | + foreach( Conf::$langs as $lang ) |
| 292 | + { |
| 293 | + $text = $lang->label; |
| 294 | + |
| 295 | + if ( $lang->manual ) // Flags langDir as manual build |
| 296 | + { |
| 297 | + $path = "{$lang->path}/BUILDMAN"; |
| 298 | + |
| 299 | + if ( $lang->manual && ! file_exists( $path ) ) |
| 300 | + file_put_contents( $path , $text ); |
| 301 | + if ( ! $lang->manual && file_exists( $path ) ) |
| 302 | + unlink( $path ); |
| 303 | + } |
| 304 | + |
| 305 | + if ( $lang->manual ) // Flags langDir as genrevdb.php |
| 306 | + { |
| 307 | + $path = "{$lang->path}/BUILDREV"; |
| 308 | + |
| 309 | + if ( $lang->revcheck && ! file_exists( $path ) ) |
| 310 | + file_put_contents( $path , $text ); |
| 311 | + if ( ! $lang->revcheck && file_exists( $path ) ) |
| 312 | + unlink( $path ); |
| 313 | + } |
| 314 | + } |
| 315 | +} |
0 commit comments