Refactor the CI setup module.
authorGunnar Wrobel <p@rdus.de>
Sun, 3 Oct 2010 17:40:36 +0000 (19:40 +0200)
committerGunnar Wrobel <p@rdus.de>
Sun, 3 Oct 2010 17:45:51 +0000 (19:45 +0200)
components/lib/Components/Module/CiSetup.php
components/lib/Components/Pear/InstallLocation.php
components/lib/Components/Pear/Package.php [new file with mode: 0644]
components/lib/Components/Runner/CiSetup.php [new file with mode: 0644]

index e27d375..2704c6a 100644 (file)
@@ -28,7 +28,7 @@
  * @link     http://pear.horde.org/index.php?package=Components
  */
 class Components_Module_CiSetup
-implements Components_Module
+extends Components_Module_Base
 {
     public function getOptionGroupTitle()
     {
@@ -67,73 +67,23 @@ implements Components_Module
                     'help'   => 'the path to the PEAR installation holding the required analysis tools'
                 )
             ),
+            new Horde_Argv_Option(
+                '-R',
+                '--pearrc',
+                array(
+                    'action' => 'store',
+                    'help'   => 'the path to the configuration of the PEAR installation'
+                )
+            ),
         );
     }
 
     public function handle(Components_Config $config)
     {
         $options = $config->getOptions();
+        //@todo Split into two different runners here
         if (!empty($options['cisetup']) | !empty($options['ciprebuild'])) {
-            $this->run($config);
-        }
-    }
-
-    public function run(Components_Config $config)
-    {
-        $options = $config->getOptions();
-
-        $pear = new PEAR();
-        $pear->setErrorHandling(PEAR_ERROR_DIE);
-
-        $arguments = $config->getArguments();
-        $pkgfile = $arguments[0] . DIRECTORY_SEPARATOR . 'package.xml';
-        $name = basename($arguments[0]);
-        if (basename(dirname($arguments[0])) == 'framework') {
-            $origin = 'framework' . DIRECTORY_SEPARATOR . $name;
-        } else {
-            $origin = $name;
-        }
-        $test_path = strtr($name, '_', '/');
-
-        $pkg     = new PEAR_PackageFile(new PEAR_Config());
-        $pf      = $pkg->fromPackageFile($pkgfile, PEAR_VALIDATE_NORMAL);
-        $description = $pf->getDescription();
-
-        if (!isset($options['toolsdir'])) {
-            $options['toolsdir'] = 'php-hudson-tools/workspace/pear/pear';
-        }
-
-        if (!empty($options['cisetup'])) {
-            $in = file_get_contents(
-                Components_Constants::getDataDirectory()
-                . DIRECTORY_SEPARATOR . 'hudson-component-config.xml.template',
-                'r'
-            );
-            file_put_contents(
-                $options['cisetup'] . DIRECTORY_SEPARATOR . 'config.xml',
-                sprintf($in, $origin, 'horde', $options['toolsdir'], $description)
-            );
-        }
-
-        if (!empty($options['ciprebuild'])) {
-            $in = file_get_contents(
-                Components_Constants::getDataDirectory()
-                . DIRECTORY_SEPARATOR . 'hudson-component-build.xml.template',
-                'r'
-            );
-            file_put_contents(
-                $options['ciprebuild'] . DIRECTORY_SEPARATOR . 'build.xml',
-                sprintf($in, $options['toolsdir'])
-            );
-            $in = file_get_contents(
-                Components_Constants::getDataDirectory()
-                . DIRECTORY_SEPARATOR . 'hudson-component-phpunit.xml.template',
-                'r'
-            );
-            file_put_contents(
-                $options['ciprebuild'] . DIRECTORY_SEPARATOR . 'phpunit.xml',
-                sprintf($in, $name, $test_path)
-            );
+            $this->_dependencies->getRunnerCiSetup()->run();
         }
     }
 }
index e036690..86d9111 100644 (file)
@@ -170,6 +170,9 @@ class Components_Pear_InstallLocation
         if (!file_exists($this->_config_file)) {
             $this->createPearConfig();
         }
+        if (!isset($GLOBALS['_PEAR_Config_instance'])) {
+            $GLOBALS['_PEAR_Config_instance'] = false;
+        }
         return PEAR_Config::singleton($this->_config_file);
     }
 
diff --git a/components/lib/Components/Pear/Package.php b/components/lib/Components/Pear/Package.php
new file mode 100644 (file)
index 0000000..b145414
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+/**
+ * Components_Pear_Package:: provides package handling mechanisms.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Components
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Components
+ */
+
+/**
+ * Components_Pear_Package:: provides package handling mechanisms.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Components
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Components
+ */
+class Components_Pear_Package
+{
+    /**
+     * The output handler.
+     *
+     * @param Component_Output
+     */
+    private $_output;
+
+    /**
+     * The PEAR environment for the package.
+     *
+     * @param Components_Pear_InstallLocation
+     */
+    private $_environment;
+
+    /**
+     * The package representation.
+     *
+     * @param PEAR_PackageFile_v2
+     */
+    private $_package_file;
+
+    /**
+     * Constructor.
+     *
+     * @param Component_Output $output The output handler.
+     */
+    public function __construct(Components_Output $output)
+    {
+        $this->_output = $output;
+    }
+
+    /**
+     * Define the surrounding PEAR environment for the package.
+     *
+     * @param Components_Pear_InstallLocation
+     *
+     * @return NULL
+     */
+    public function setEnvironment(Components_Pear_InstallLocation $environment)
+    {
+        $this->_environment = $environment;
+    }
+
+    /**
+     * Return the PEAR environment for this package.
+     *
+     * @return Components_Pear_InstallLocation
+     */
+    public function getEnvironment()
+    {
+        if ($this->_environment === null) {
+            throw new Component_Exception('You need to set the environment first!');
+        }
+        return $this->_environment;
+    }
+
+    /**
+     * Define the package to work on.
+     *
+     * @param string $package_xml_patch Path to the package.xml file.
+     *
+     * @return NULL
+     */
+    public function setPackage($package_xml_path)
+    {
+        $config = $this->getEnvironment()->getPearConfig();
+        $pkg = new PEAR_PackageFile($config);
+        $this->_package_file = $pkg->fromPackageFile($package_xml_path, PEAR_VALIDATE_NORMAL);
+        if ($this->_package_file instanceOf PEAR_Error) {
+            throw new Components_Exception($this->_package_file->getMessage());
+        }
+    }
+
+    /**
+     * Return the PEAR Package representation.
+     *
+     * @return PEAR_PackageFile
+     */
+    public function getPackageFile()
+    {
+        if ($this->_environment === null || $this->_package_file === null) {
+            throw new Components_Exception('You need to set the environment and the path to the package file first!');
+        }
+        return $this->_package_file;
+    }
+
+}
diff --git a/components/lib/Components/Runner/CiSetup.php b/components/lib/Components/Runner/CiSetup.php
new file mode 100644 (file)
index 0000000..2d78cef
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+/**
+ * Components_Runner_CiSetup:: prepares a continuous integration setup for a
+ * component.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Components
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Components
+ */
+
+/**
+ * Components_Runner_CiSetup:: prepares a continuous integration setup for a
+ * component.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Components
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Components
+ */
+class Components_Runner_CiSetup
+{
+    /**
+     * The configuration for the current job.
+     *
+     * @var Components_Config
+     */
+    private $_config;
+
+    /**
+     * The package handler.
+     *
+     * @var Components_Pear_Package
+     */
+    private $_package;
+
+    /**
+     * Constructor.
+     *
+     * @param Components_Config               $config   The configuration for the
+     *                                                  current job.
+     * @param Components_Pear_InstallLocation $location Represents the install
+     *                                                  location and its
+     *                                                  corresponding configuration.
+     * @param Components_Pear_Package         $package  Package handler.
+     */
+    public function __construct(
+        Components_Config $config,
+        Components_Pear_InstallLocation $location,
+        Components_Pear_Package $package
+    ) {
+        $this->_config   = $config;
+        $this->_location = $location;
+        $this->_package  = $package;
+    }
+
+    public function run()
+    {
+        $options = $this->_config->getOptions();
+        $arguments = $this->_config->getArguments();
+        $pkgfile = $arguments[0] . DIRECTORY_SEPARATOR . 'package.xml';
+        $name = basename($arguments[0]);
+        if (basename(dirname($arguments[0])) == 'framework') {
+            $origin = 'framework' . DIRECTORY_SEPARATOR . $name;
+        } else {
+            $origin = $name;
+        }
+        $test_path = strtr($name, '_', '/');
+
+        if (!isset($options['toolsdir'])) {
+            $options['toolsdir'] = 'php-hudson-tools/workspace/pear/pear';
+        }
+        if (!isset($options['pearrc'])) {
+            throw new Components_Exception(
+                'You are required to set the path to a PEAR environment for this package'
+            );
+        }
+
+        $this->_location->setLocation(
+            dirname($options['pearrc']),
+            basename($options['pearrc'])
+        );
+        $this->_package->setEnvironment($this->_location);
+        $this->_package->setPackage($pkgfile);
+        $description = $this->_package->getPackageFile()->getDescription();
+
+        if (!empty($options['cisetup'])) {
+            $in = file_get_contents(
+                Components_Constants::getDataDirectory()
+                . DIRECTORY_SEPARATOR . 'hudson-component-config.xml.template',
+                'r'
+            );
+            file_put_contents(
+                $options['cisetup'] . DIRECTORY_SEPARATOR . 'config.xml',
+                sprintf($in, $origin, 'horde', $options['toolsdir'], $description)
+            );
+        }
+
+        if (!empty($options['ciprebuild'])) {
+            $in = file_get_contents(
+                Components_Constants::getDataDirectory()
+                . DIRECTORY_SEPARATOR . 'hudson-component-build.xml.template',
+                'r'
+            );
+            file_put_contents(
+                $options['ciprebuild'] . DIRECTORY_SEPARATOR . 'build.xml',
+                sprintf($in, $options['toolsdir'])
+            );
+            $in = file_get_contents(
+                Components_Constants::getDataDirectory()
+                . DIRECTORY_SEPARATOR . 'hudson-component-phpunit.xml.template',
+                'r'
+            );
+            file_put_contents(
+                $options['ciprebuild'] . DIRECTORY_SEPARATOR . 'phpunit.xml',
+                sprintf($in, $name, $test_path)
+            );
+        }
+    }
+}