Beispiel #1
0
 /**
  * Rewrites require js script.
  *
  * @param string Input module path
  * @param string Rewritten module javascript
  */
 public static function rewrite(\Adadgio\Common\Config $config, $modulePath, $kernelMode = false)
 {
     $script = file_get_contents($modulePath);
     $moduleBasename = basename($modulePath);
     $moduleRawname = Dependency::removeExtension($moduleBasename, Dependency::EXT_JS);
     // check if the rewrite happens inside the "_build" directory or the top directory (for kernel file)
     // depending on that option, use the config build dir to prefix the modules or just not prefix (for kernel file)
     $buildDirectory = true === $kernelMode ? '' : $config->get('build_dir');
     // when the module is a shim...
     if ($name = Dependency::isInGlobalpathes($modulePath)) {
         $moduleBasename = basename($buildDirectory) . '/' . $name;
         $moduleBasename = ltrim($moduleBasename, '/');
         // there might be exceptions for non AMD compliant modules
         if ($config->has('amd_compliance_wrap')) {
             $wrap = $config->get('amd_compliance_wrap');
             if (isset($wrap[$name])) {
                 $script = "define('{$moduleBasename}', function () { \n{$script}" . "\n" . "});";
             }
         }
     } else {
         // else rewrite a normal module
         $moduleBasename = self::unquote($moduleBasename);
         $moduleBasename = basename($buildDirectory) . '/' . Dependency::removeExtension($moduleBasename);
         $moduleBasename = ltrim($moduleBasename, '/');
     }
     // rewrite the module name itself when there are no dependencies by finding strings
     // like "define(function () {..." and adding the current module name after the define call
     $script = preg_replace_callback(static::DEFINE_MODU_REGEX, function ($match) use($config, $moduleBasename, $buildDirectory) {
         if (empty($moduleBasename)) {
             exit('Nooot');
         }
         return sprintf("define('%s' ,function", $moduleBasename);
     }, $script);
     // remove trailing commas in the script. if fixes a lot of stuf...
     // read and replace all the current module dependencies pathes inside the script so that
     // they are relative to the build directory by rewriting strings like "define(['dep1'], ['dep2']... function"
     $script = preg_replace_callback(static::DEFINE_DEPS_REGEX, function ($match) use($config, $moduleBasename, $buildDirectory) {
         // $def = $m[1];    // start of expression"define("
         // $mod = $m[2];    // module name "framework/Config/GlobalConfig"
         // $func = $m[3];   // rest of expression ""
         $imports = array_map('trim', array_filter(explode(',', $match[1])));
         $imports = array_filter($imports);
         foreach ($imports as $i => $import) {
             $basename = basename(self::unquote($import));
             $imports[$i] = self::quote(basename($buildDirectory) . '/' . $basename);
             // self::quote
         }
         $importStr = implode(',', $imports);
         return sprintf("define('%s', [%s], function", $moduleBasename, $importStr);
     }, $script);
     return $script;
 }
Beispiel #2
0
 /**
  * Asks question to setup configuration variables.
  *
  * @param  object \InputInterface
  * @param  object \OutputInterface
  * @return object \Config
  */
 private function configQuestions(InputInterface $input, OutputInterface $output)
 {
     $config = new Config();
     $helper = $this->getHelper('question');
     // question info text
     $output->writeln(' $> Please set the directory where MelodyJS framework is installed.');
     // prompt for the directory of the melody js framework is in
     $question = new Question(sprintf(' $> Full path to melody js framework? <info>[%s]</info>? ', $config->get('cwd') . '/../test'), '/../test');
     // validate that the input is a directory and
     // that both AppKernel(Dev|Prod).js files exist
     $question->setValidator(function ($answer) use($config) {
         $directory = rtrim($config->get('cwd') . '/' . trim($answer, '/'), '/');
         if (!is_dir($directory)) {
             throw new \RuntimeException('This is not a valid directory');
         }
         return $directory;
     });
     $question->setMaxAttempts(5);
     // set the base directory if melody js framework and from
     // that base project directory, add addition directories shortcuts
     $baseDir = $helper->ask($input, $output, $question);
     $buildDir = $baseDir . '/_build';
     $bundlesDir = $baseDir . '/bundles';
     $config->set('base_dir', $baseDir);
     $config->set('build_dir', $buildDir);
     $config->set('bundles_dir', $bundlesDir);
     // set base meldoy js directory
     // $config->set('base_dir', rtrim($config->get('cwd').'/'.trim($baseDir, '/'), '/'));
     //
     // // set the bundles directory (auto)
     // $config->set('bundles_dir', $config->get('base_dir').'/bundles');
     //
     // // set output build directory
     // $question = new Question(sprintf('<info>$> Set build relative output dir path from [%s]?</info> ', $config->get('cwd')), '/_build');
     // $outputDir = $helper->ask($input, $output, $question);
     // $config->set('output_dir', $config->get('cwd').'/'.trim($outputDir, '/'));
     return $config;
 }
Beispiel #3
0
 /**
  * Get shim dependencies for one module..
  *
  * @param  object \Config
  * @param  string Module short name
  * @return array  List of dependencies file pathes
  */
 private static function getShimDependencies(\Adadgio\Common\Config $config, $moduleName)
 {
     $dependencies = array();
     // get the dependency path
     $depId = $moduleName;
     $path = $config->get('base_dir') . '/' . self::$pathes[$depId] . '.js';
     $dependencies[$depId] = $path;
     if (!is_file($path)) {
         throw new \Exception(sprintf('Shim or global module dep file "%s" does not exist', $path));
     }
     // that global module might have shim dependencies, so add them also
     if (isset(self::$shims[$depId])) {
         foreach (self::$shims[$depId] as $subModuleName) {
             $dependencies = array_merge($dependencies, self::getShimDependencies($config, $subModuleName));
         }
     }
     return $dependencies;
 }