# build.php

Go to the documentation of this file.

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