build.php

Go to the documentation of this file.
00001 #!/usr/bin/php
00002 <?php
00003     /* $Id: build.php 4687 2007-12-09 18:57:18Z voxus $ */
00004 
00005     function help()
00006     {
00007 ?>
00008 Usage: build.php [options] [project-configuration-file.inc.php] [metaconfiguration.xml]
00009 
00010 Possible options:
00011 
00012     --only-containers:
00013         update (or rewrite if combined with --force) containers only.
00014     
00015     --no-schema:
00016         do not generate DB schema.
00017     
00018     --no-integrity-check:
00019         do not try to test classes integrity.
00020     
00021     --no-schema-check:
00022         do not try to diff DB schemas.
00023     
00024     --syntax-check:
00025         check generated files with `php -l`.
00026     
00027     --drop-stale-files:
00028         remove found stale files.
00029     
00030     --force:
00031         regenerate all files.
00032     
00033     --dry-run:
00034         print the results of building without actually changing any files.
00035     
00036     --no-color:
00037         do not use colored output.
00038     
00039     --dto-mode:
00040         do not generate DB schema & not build containers
00041 
00042 <?php
00043         exit(1);
00044     }
00045     
00046     function init()
00047     {
00048         define('ONPHP_META_BUILDERS', ONPHP_META_PATH.'builders'.DIRECTORY_SEPARATOR);
00049         define('ONPHP_META_PATTERNS', ONPHP_META_PATH.'patterns'.DIRECTORY_SEPARATOR);
00050         define('ONPHP_META_TYPES', ONPHP_META_PATH.'types'.DIRECTORY_SEPARATOR);
00051         
00052         set_include_path(
00053             get_include_path().PATH_SEPARATOR
00054             .ONPHP_META_BUILDERS.PATH_SEPARATOR
00055             .ONPHP_META_PATTERNS.PATH_SEPARATOR
00056             .ONPHP_META_TYPES.PATH_SEPARATOR
00057         );
00058         
00059         if (!defined('ONPHP_META_DAO_DIR'))
00060             define(
00061                 'ONPHP_META_DAO_DIR',
00062                 PATH_CLASSES.'DAOs'.DIRECTORY_SEPARATOR
00063             );
00064         
00065         if (!defined('ONPHP_META_BUSINESS_DIR'))
00066             define(
00067                 'ONPHP_META_BUSINESS_DIR',
00068                 PATH_CLASSES.'Business'.DIRECTORY_SEPARATOR
00069             );
00070         
00071         if (!defined('ONPHP_META_PROTO_DIR'))
00072             define(
00073                 'ONPHP_META_PROTO_DIR',
00074                 PATH_CLASSES.'Proto'.DIRECTORY_SEPARATOR
00075             );
00076 
00077         define('ONPHP_META_AUTO_DIR', PATH_CLASSES.'Auto'.DIRECTORY_SEPARATOR);
00078         
00079         if (!defined('ONPHP_META_AUTO_BUSINESS_DIR'))
00080             define(
00081                 'ONPHP_META_AUTO_BUSINESS_DIR',
00082                 ONPHP_META_AUTO_DIR
00083                 .'Business'.DIRECTORY_SEPARATOR
00084             );
00085         
00086         define(
00087             'ONPHP_META_AUTO_PROTO_DIR',
00088             ONPHP_META_AUTO_DIR
00089             .'Proto'.DIRECTORY_SEPARATOR
00090         );
00091         
00092         if (!defined('ONPHP_META_AUTO_DAO_DIR'))
00093             define(
00094                 'ONPHP_META_AUTO_DAO_DIR',
00095                 ONPHP_META_AUTO_DIR
00096                 .'DAOs'.DIRECTORY_SEPARATOR
00097             );
00098         
00099         if (!defined('ONPHP_META_AUTO_DTO_DIR'))
00100             define(
00101                 'ONPHP_META_AUTO_DTO_DIR',
00102                 ONPHP_META_AUTO_DIR
00103                 .'DTOs'.DIRECTORY_SEPARATOR
00104             );
00105         
00106         
00107         if (!is_dir(ONPHP_META_DAO_DIR))
00108             mkdir(ONPHP_META_DAO_DIR, 0755, true);
00109         
00110         if (!is_dir(ONPHP_META_AUTO_DTO_DIR))
00111             mkdir(ONPHP_META_AUTO_DTO_DIR, 0755, true);
00112             
00113         if (!is_dir(ONPHP_META_AUTO_DIR))
00114             mkdir(ONPHP_META_AUTO_DIR, 0755, true);
00115         
00116         if (!is_dir(ONPHP_META_AUTO_BUSINESS_DIR))
00117             mkdir(ONPHP_META_AUTO_BUSINESS_DIR, 0755);
00118             
00119         if (!is_dir(ONPHP_META_AUTO_PROTO_DIR))
00120             mkdir(ONPHP_META_AUTO_PROTO_DIR, 0755);
00121         
00122         if (!is_dir(ONPHP_META_AUTO_DAO_DIR))
00123             mkdir(ONPHP_META_AUTO_DAO_DIR, 0755);
00124         
00125         if (!is_dir(ONPHP_META_BUSINESS_DIR))
00126             mkdir(ONPHP_META_BUSINESS_DIR, 0755, true);
00127         
00128         if (!is_dir(ONPHP_META_PROTO_DIR))
00129             mkdir(ONPHP_META_PROTO_DIR, 0755, true);
00130     }
00131     
00132     function stop($message = null)
00133     {
00134         echo $message."\n\n";
00135         
00136         help();
00137         
00138         die();
00139     }
00140     
00141     // paths
00142     $pathConfig = $pathMeta = null;
00143     
00144     // switches
00145     $metaForce = $metaOnlyContainers = $metaNoSchema =
00146     $metaNoSchemaCheck = $metaSyntaxCheck = $metaDropStaleFiles =
00147     $metaNoIntegrityCheck = $metaDryRun = $metaNoColor = $metaNoContainers = false;
00148     
00149     $args = $_SERVER['argv'];
00150     array_shift($args);
00151     
00152     if ($args) {
00153         foreach ($args as $arg) {
00154             if ($arg[0] == '-') {
00155                 switch ($arg) {
00156                     case '--only-containers':
00157                         $metaOnlyContainers = true;
00158                         break;
00159                     
00160                     case '--no-schema':
00161                         $metaNoSchema = true;
00162                         break;
00163                     
00164                     case '--no-integrity-check':
00165                         $metaNoIntegrityCheck = true;
00166                         break;
00167                     
00168                     case '--no-schema-check':
00169                         $metaNoSchemaCheck = true;
00170                         break;
00171                     
00172                     case '--syntax-check':
00173                         $metaSyntaxCheck = true;
00174                         break;
00175                     
00176                     case '--drop-stale-files':
00177                         $metaDropStaleFiles = true;
00178                         break;
00179                     
00180                     case '--force':
00181                         $metaForce = true;
00182                         break;
00183                     
00184                     case '--dry-run':
00185                         $metaDryRun = true;
00186                         break;
00187                     
00188                     case '--no-color':
00189                         $metaNoColor = true;
00190                         break;
00191                     
00192                     case '--dto-mode':
00193                         $metaNoSchema       = true;
00194                         $metaNoContainers   = true;
00195                         break;
00196                     
00197                     default:
00198                         stop('Unknown switch: '.$arg);
00199                 }
00200             } else {
00201                 if (file_exists($arg)) {
00202                     $extension = pathinfo($arg, PATHINFO_EXTENSION);
00203                     
00204                     // respecting paths order described in help()
00205                     if (!$pathConfig) {
00206                         $pathConfig = $arg;
00207                     } elseif (!$pathMeta) {
00208                         $pathMeta = $arg;
00209                     } else {
00210                         stop('Unknown path: '.$arg);
00211                     }
00212                 } else {
00213                     stop('Unknown option: '.$arg);
00214                 }
00215             }
00216         }
00217     }
00218     
00219     // manual includes due to unincluded yet project's config
00220     $metaRoot =
00221         dirname(dirname($_SERVER['argv'][0]))
00222         .DIRECTORY_SEPARATOR
00223         .'classes'
00224         .DIRECTORY_SEPARATOR;
00225     
00226     require $metaRoot.'ConsoleMode.class.php';
00227     require $metaRoot.'MetaOutput.class.php';
00228     require $metaRoot.'TextOutput.class.php';
00229     require $metaRoot.'ColoredTextOutput.class.php';
00230     
00231     if (
00232         isset($_SERVER['TERM'])
00233         && (
00234             $_SERVER['TERM'] == 'xterm'
00235             || $_SERVER['TERM'] == 'linux'
00236         )
00237         && !$metaNoColor
00238     ) {
00239         $out = new ColoredTextOutput();
00240     } else {
00241         $out = new TextOutput();
00242     }
00243     
00244     $out = new MetaOutput($out);
00245     
00246     if (!$pathConfig) {
00247         $out->warning('Trying to guess path to project\'s configuration file: ');
00248         
00249         foreach (
00250             array(
00251                 'config.inc.php',
00252                 'src/config.inc.php'
00253             )
00254             as $path
00255         ) {
00256             if (file_exists($path)) {
00257                 $pathConfig = $path;
00258                 
00259                 $out->remark($path)->logLine('.');
00260                 
00261                 break;
00262             }
00263         }
00264         
00265         if (!$pathConfig) {
00266             $out->errorLine('failed.');
00267         }
00268     }
00269     
00270     if (!$pathMeta) {
00271         $out->warning('Trying to guess path to MetaConfiguration file: ');
00272         
00273         foreach (
00274             array(
00275                 'config.xml',
00276                 'meta/config.xml'
00277             )
00278             as $path
00279         ) {
00280             if (file_exists($path)) {
00281                 $pathMeta = $path;
00282                 
00283                 $out->remark($path)->logLine('.');
00284                 
00285                 break;
00286             }
00287         }
00288         
00289         if (!$pathMeta) {
00290             $out->errorLine('failed.');
00291         }
00292     }
00293     
00294     if ($pathMeta && $pathConfig) {
00295         require $pathConfig;
00296         
00297         init();
00298         
00299         $out->
00300             newLine()->
00301             infoLine('onPHP-'.ONPHP_VERSION.': MetaConfiguration builder.', true)->
00302             newLine();
00303         
00304         try {
00305             $meta =
00306                 MetaConfiguration::me()->
00307                 setOutput($out)->
00308                 load(ONPHP_META_PATH.'internal.xml', false);
00309             
00310             $out->info('Known internal classes: ');
00311             foreach ($meta->getClassList() as $class) {
00312                 $out->info($class->getName().', ', true);
00313             }
00314             $out->infoLine("that's all.")->newLine();
00315             
00316             $meta->
00317                 setDryRun($metaDryRun)->
00318                 load($pathMeta)->
00319                 setForcedGeneration($metaForce);
00320             
00321             if (
00322                 $metaOnlyContainers
00323                 && !$metaNoContainers
00324             ) {
00325                 $meta->buildContainers();
00326             } else {
00327                 $meta->buildClasses();
00328                 
00329                 if (!$metaNoContainers)
00330                     $meta->buildContainers();
00331                 
00332                 if (!$metaNoSchema)
00333                     $meta->buildSchema();
00334                 
00335                 if (!$metaNoSchemaCheck)
00336                     $meta->buildSchemaChanges();
00337             }
00338             
00339             $meta->checkForStaleFiles($metaDropStaleFiles);
00340             
00341             if ($metaSyntaxCheck)
00342                 $meta->checkSyntax();
00343             
00344             if (!$metaNoIntegrityCheck)
00345                 $meta->checkIntegrity();
00346         } catch (BaseException $e) {
00347             $out->
00348                 newLine()->
00349                 errorLine($e->getMessage(), true)->
00350                 newLine()->
00351                 logLine(
00352                     $e->getTraceAsString()
00353                 );
00354         }
00355     } else {
00356         $out->getOutput()->resetAll()->newLine();
00357         
00358         stop('Can not continue.');
00359     }
00360         
00361     $out->getOutput()->resetAll();
00362     $out->newLine();
00363 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4