Draft for excluding files based on the gitignore information.
authorGunnar Wrobel <p@rdus.de>
Tue, 2 Nov 2010 08:16:57 +0000 (09:16 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 2 Nov 2010 19:39:04 +0000 (20:39 +0100)
components/lib/Components/Helper/Root.php
components/lib/Components/Pear/Factory.php
components/lib/Components/Pear/Package/Contents/Factory.php
components/lib/Components/Pear/Package/Contents/Ignore.php

index fc8312c..e832b0f 100644 (file)
@@ -37,6 +37,13 @@ class Components_Helper_Root
     private $_root_path;
 
     /**
+     * Relative position of the path that has been used to determine the root positionq.
+     *
+     * @var string
+     */
+    private $_base = '';
+
+    /**
      * Constructor.
      *
      * @param string $path The helper will try to determine the root of the
@@ -55,6 +62,7 @@ class Components_Helper_Root
                     break;
                 }
             }
+            $this->_base .= basename($current) . DIRECTORY_SEPARATOR;
             $current = dirname($current);
             $i++;
         }
@@ -96,8 +104,19 @@ class Components_Helper_Root
      *
      * @return string The information from the gitignore file.
      */
-    public function fetchGitIgnore()
+    public function getGitIgnore()
     {
         return '';
     }
+
+    /**
+     * Return the relative position of the path originally used to determine the
+     * root position of the repository.
+     *
+     * @return string The relative path.
+     */
+    public function getBase()
+    {
+        return $this->_base;
+    }
 }
\ No newline at end of file
index f6d2f8d..4747e7c 100644 (file)
@@ -300,19 +300,6 @@ class Components_Pear_Factory
                     'clearcontents' => false,
                     'clearchangelog' => false,
                     'simpleoutput' => true,
-                    'dir_roles' =>
-                    array(
-                        'bin'       => 'script',
-                        'script'    => 'script',
-                        'doc'       => 'doc',
-                        'example'   => 'doc',
-                        'js'        => 'horde',
-                        'horde'     => 'horde',
-                        'lib'       => 'php',
-                        'migration' => 'data',
-                        'scripts'   => 'data',
-                        'test'      => 'test',
-                    ),
                 )
             )
         );
index 86c934a..0f23b78 100644 (file)
@@ -41,10 +41,23 @@ class Components_Pear_Package_Contents_Factory
         $root = new Components_Helper_Root(
             $package->_options['packagedirectory']
         );
+        $package->_options['dir_roles'] = array(
+            'bin'       => 'script',
+            'script'    => 'script',
+            'doc'       => 'doc',
+            'example'   => 'doc',
+            'js'        => 'horde',
+            'horde'     => 'horde',
+            'lib'       => 'php',
+            'migration' => 'data',
+            'scripts'   => 'data',
+            'test'      => 'test',
+        );
         return new Components_Pear_Package_Contents_List(
             $package->_options['packagedirectory'],
             new Components_Pear_Package_Contents_Ignore(
-                $root->fetchGitIgnore()
+                $root->getGitIgnore(),
+                $root->getBase()
             )
         );
     }
index 5d1883b..50118a3 100644 (file)
 class Components_Pear_Package_Contents_Ignore
 {
     /**
-     * The gitignore information.
+     * The regular expressions for files to ignore.
      *
-     * @var string
+     * @var array
      */
-    private $_gitignore;
+    private $_ignore = array();
+
+    /**
+     * The regular expressions for files to exclude from ignoring.
+     *
+     * @var array
+     */
+    private $_include = array();
 
     /**
      * Constructor.
      *
      * @param string $gitignore The gitignore information
+     * @param string $base      An optional base for the files that should be checked.
      */
-    public function __construct($gitignore)
+    public function __construct($gitignore, $base = '')
     {
-        $this->_gitignore = $gitignore;
+        $this->_base = $base;
     }
 
     /**
@@ -62,6 +70,57 @@ class Components_Pear_Package_Contents_Ignore
      */
     public function checkIgnore($file, $path, $return = 1)
     {
+        if (!$return) {
+            // This class is not about identifying included files.
+            return !$return;
+        }
+
+        if ($this->_matches($this->_ignore, $this->_base . $path)
+            && !$this->_matches($this->_include, $this->_base . $path)) {
+            return $return;
+        }
+        if ($this->_matches($this->_ignore, $file)
+            && !$this->_matches($this->_include, $file)) {
+            return $return;
+        }
         return !$return;
     }
+
+    private function _matches($matches, $path)
+    {
+        foreach ($matches as $match) {
+            preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find);
+            if (count($find)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Converts $s into a string that can be used with preg_match
+     *
+     * @param string $s string with wildcards ? and *
+     *
+     * @return string converts * to .*, ? to ., etc.
+     * @access private
+     */
+    private function _getRegExpableSearchString($s)
+    {
+        $y = '\/';
+        if (DIRECTORY_SEPARATOR == '\\') {
+            $y = '\\\\';
+        }
+
+        $s = str_replace('/', DIRECTORY_SEPARATOR, $s);
+        $x = strtr($s, array('?' => '.', '*' => '.*', '.' => '\\.', '\\' => '\\\\', '/' => '\\/',
+                             '[' => '\\[', ']' => '\\]', '-' => '\\-'));
+
+        if (strpos($s, DIRECTORY_SEPARATOR) !== false &&
+              strrpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) {
+            $x = "(?:.*$y$x?.*|$x.*)";
+        }
+
+        return $x;
+    }
 }
\ No newline at end of file