Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
cballou committed Oct 19, 2011
0 parents commit dd0c6be
Show file tree
Hide file tree
Showing 4 changed files with 706 additions and 0 deletions.
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2011 Corey Ballou

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
About Watchdog
--------------

Watchdog is a PHP class implementing inotify which watches for changes to files in
any given number of directories specified in the input. It will not
recursively check directories, so you must implicitely specify each directory
you want watched.

Although it was developed for the purpose of monitoring dynamic css files for
changes and auto-regenerating their output, it can easily be transformed into
a watchdog for just about anything.

Requirements
------------

Watchdog requires you to have the PHP PEAR library inotify installed. If you have
PEAR installed, you may run the following from the command line to install inotify:

```bash
sudo pecl install inotify
```

Prerequisites
-------------

Watchdog is merely an intermediary between you and your dynamic CSS files. It
does not handle any form of parsing internally, and subsequently passes off
proper executable commands to the command line for compiling. For these reasons,
watchdog assumes that you have one or more of the following modules installed
(depending on your particular necessities):

* SASS
* LESS
* Stylus

Each of these applications have their own set of dependencies.

* SASS - Requires Ruby and RubyGems
* LESS - Requires nodejs and npm
* Stylus - Requires nodejs and npm

"sass: command not found"
---------------------

If you are receiving the error "sass: command not found", you will need to
ensure you have the SASS ruby gem installed:

```bash
sudo gem install sass`
```

"lessc: command not found"
---------------------

If you are receiving the error "lessc: command not found", you will need to
ensure you have the LESS npm module installed globally:

```bash
sudo npm install -g less
```

"stylus: command not found"
---------------------

If you are receiving the error "stylus: command not found", you will need to
ensure you have the stylus npm module installed globally:

```bash
sudo npm install -g stylus
```

Otherwise, you are left with modules installed to your base installation directory.

I'm still getting command not found
---------------------
For Node.JS NPM modules (stylus and less), ensure you have the NODE_PATH variable
appropriately set:
```bash
echo 'export NODE_PATH=/usr/local/lib/node_modules' >> ~/.profile
source ~/.profile
```
161 changes: 161 additions & 0 deletions cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
// ensure we're in the command line
if (PHP_SAPI !== 'cli') {
die('You may only execute the watchdog from the command line.');
} else if (!function_exists('inotify_init')) {
die('You must have the PEAR inotify package installed.');
}

// generate command line options
$shortopts = "";
$shortopts .= "t:"; // specify file extensions to watch
$shortopts .= "w:"; // specify directories to watch
$shortopts .= "c:"; // alternatively specify a config file to load
$shortopts .= "m"; // output the minified version
$shortopts .= "h"; // help
$longopts = array(
"types:", // specify types to auto convert
"watch:", // specify directories to watch
"minify", // output the minified version
"config:", // alternatively specify a config file to load
"help" // help
);

// current list of valid file extensions
$validExtensions = array(
'sass' => 1,
'scss' => 1,
'styl' => 1,
'less' => 1
);

// retrieve options
$options = getopt($shortopts, $longopts);

// check for help
if (isset($options['h']) || isset($options['help'])) {
$output = "Watchdog is a monitor for popular dynamic stylesheet languages.
Usage:
watchdog [options]
Options:
-t\t--types <extension>\t\tSpecify the file extensions to watch, separated by commas with no spaces (supports sass, scss, less, styl).
-w\t--watch <directories>\t\tSpecify the directories to watch for file changes, separated by commas with no spaces.
-c\t--config <file>\t\t\tSpecify an absolute/relative path to a config file to load options.
-m\t--minify\t\t\tFlag to indicate you wish to minify/compress output.
-h\t--help\t\t\t\tThis help documentation.
Example Config File (written in PHP, requires return):
<?php
return array(
'types' => array('sass', 'scss', 'less', 'styl'),
'watch' => array(
'/path/to/public/css',
'/path/to/alternate/css'
),
'minify' => true
);
";
die($output);
}

// check for a config file to parse
if (!empty($options['c']) || !empty($options['config'])) {
if (!empty($options['c'])) {
if (!is_file($options['c'])) {
die('The configuration file ' . $options['c'] . ' does not exist.');
}
$conf = $options['c'];
} else if (!empty($options['config'])) {
if (!is_file($options['config'])) {
die('The configuration file ' . $options['config'] . ' does not exist.');
}
$conf = $options['config'];
}

// load the config file
$config = include_once($conf);

// console log
echo 'Loading the config file ' . $conf . PHP_EOL;
}

// determine directories to watch
if (!empty($config['watch'])) {
$directoryWatch = $config['watch'];
} else if (!empty($options['w'])) {
$directoryWatch = $options['w'];
} else if (!empty($options['watch'])) {
$directoryWatch = $options['watch'];
} else {
$directoryWatch = './';
}

if (!is_array($directoryWatch)) {
$directoryWatch = explode(',', $directoryWatch);
}

// do some basic validation on the watch directories
foreach ($directoryWatch as $k => $d) {
if (!is_file($d) && !is_dir($d)) {
unset($directoryWatch[$k]);
echo $d . ' is not a valid file or directory. Removing from watchdog.' . PHP_EOL;
}
}

if (empty($directoryWatch)) {
die('No valid directories are set to be watched. Aborting.');
}

// console log
echo 'Watching the following directories:' . PHP_EOL;
echo ' ' . implode(PHP_EOL . ' ', $directoryWatch) . PHP_EOL . PHP_EOL;

// determine file extensions to watch for
if (!empty($config['types'])) {
$fileExtensions = $config['types'];
} else if (!empty($options['t'])) {
$fileExtensions = $options['t'];
} else if (!empty($options['types'])) {
$fileExtensions = $options['types'];
} else {
$fileExtensions = array(
'sass',
'scss',
'styl',
'less'
);
}

if (!is_array($fileExtensions)) {
$fileExtensions = explode(',', $fileExtensions);
}

// do some basic validation on the file extensions
foreach ($fileExtensions as $k => $f) {
if (!isset($validExtensions[$f])) {
unset($fileExtensions[$k]);
echo $f . ' is not a valid file extension. Removing from watchdog.' . PHP_EOL;
}
}

if (empty($fileExtensions)) {
die('No valid file extensions are set to be watched. Aborting.');
}

// console log
echo 'Watching the following file extensions: ' . implode(', ', $fileExtensions) . PHP_EOL . PHP_EOL;

// determine if we're minifying file output
$minified = false;
if (!empty($config['minify'])) {
$minified = true;
} else if (isset($options['m'])) {
$minified = true;
} else if (isset($options['minify'])) {
$minified = true;
}
Loading

0 comments on commit dd0c6be

Please sign in to comment.