root/trunk/src/main/php/info/phing/tasks/stubJsMinTask.php @ 984

Revision 984, 3.2 kB (checked in by mikey, 3 years ago)

various code improvements

RevLine 
[773]1<?php
2/**
3 * Task to minimy javascript files.
4 *
5 * @author      Frank Kleine <mikey@stubbles.net>
6 * @package     stubbles
7 * @subpackage  phing
8 */
9/**
10 * Uses the Phing Task
11 */
12require_once 'phing/Task.php';
13/**
14 * Task to minimy javascript files.
15 *
16 * @package     stubbles
17 * @subpackage  phing
18 */
19class stubJsMinTask extends Task
20{
21    /**
[787]22     * path to JSmin
23     *
24     * @var  string
25     */
26    protected $jsMinPath;
27    /**
[773]28     * the source files
29     *
30     * @var  FileSet
31     */
32    protected $filesets    = array();
33    /**
34     * Whether the build should fail, if
35     * errors occured
36     *
37     * @var boolean
38     */
39    protected $failonerror = false;
40    /**
41     * directory to put minified javascript files into
42     *
43     * @var  string
44     */
45    protected $targetDir;
46
47    /**
[787]48     * sets the path where JSmin can be found
49     *
50     * @param  string  $jsMinPath
51     */
52    public function setJsMinPath($jsMinPath)
53    {
54        $this->jsMinPath = $jsMinPath;
55    }
56
57    /**
[773]58     *  Nested creator, adds a set of files (nested fileset attribute).
59     */
60    public function createFileSet()
61    {
62        $num = array_push($this->filesets, new FileSet());
63        return $this->filesets[$num - 1];
64    }
65
66    /**
67     * Whether the build should fail, if an error occured.
68     *
69     * @param boolean $value
70     */
71    public function setFailonerror($value)
72    {
73        $this->failonerror = $value;
74    }
75
76    /**
77     * sets the directory where minified javascript files should be put inot
78     *
79     * @param  string  $targetDir
80     */
81    public function setTargetDir($targetDir)
82    {
83        $this->targetDir = $targetDir;
84    }
85
86    /**
87     * The init method: Do init steps.
88     */
89    public function init()
90    {
[787]91        return true;
[773]92    }
93
94    /**
95     * The main entry point method.
96     */
97    public function main()
98    {
[787]99        if (class_exists('JSMin', false) == false) {
100            require_once $this->jsMinPath;
101        }
102       
[773]103        foreach ($this->filesets as $fs) {
104            try {
105                $files    = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
106                $fullPath = realpath($fs->getDir($this->project));
107                foreach ($files as $file) {
108                    $this->log('Minifying file ' . $file);
109                    try {
[787]110                        $target = $this->targetDir . '/' . str_replace($fullPath, '', str_replace('.js', '-min.js', $file));
[773]111                        if (file_exists(dirname($target)) == false) {
112                            mkdir(dirname($target), 0700, true);
113                        }
114                       
115                        file_put_contents($target, JSMin::minify(file_get_contents($fullPath . '/' . $file)));
116                    } catch (JSMinException $jsme) {
117                        $this->log("Could not minify file $file: " . $jsme->getMessage(), Project::MSG_ERR);
118                    }
119                }
120            } catch (BuildException $be) {
121                // directory doesn't exist or is not readable
122                if ($this->failonerror) {
123                    throw $be;
124                } else {
125                    $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
126                }
127            }
128        }
129    }
130}
131?>
Note: See TracBrowser for help on using the browser.