Skip to content

Commit

Permalink
Updated compass for SS3.0 and coding conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 10, 2012
1 parent 0d3cf0e commit 80be501
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 68 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The Compass module for SilverStripe seamlessly integrates Sass and Compass in
SilverStripe modules and themes. You write your styles in sass, and the module
automatically compiles these sass files to css.

Note: This does introduce a delay to your webpages while in devmode. This can
be significant (2-10s) while compass rebuilds your project. If this delay is
affecting you, I recommend using the compass gem directly.

## Maintainer Contact

* Hamish Friedlander (hfried)
Expand Down Expand Up @@ -31,7 +35,7 @@ Normal usage is to commit the built css files to your version control
repository during development. This means there are no extra software
requirements on the live servers.

* SilverStripe 2.4
* SilverStripe 3.0

## Documentation

Expand Down
10 changes: 6 additions & 4 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

// This triggers compass rebuilding when a ContentController is accessed
if(class_exists('SiteTree')) Object::add_extension('SiteTree', 'Compass_RebuildDecorator');
if(class_exists('LeftAndMain')) Object::add_extension('LeftAndMain', 'Compass_RebuildDecorator');
if(class_exists('SiteTree')) {
Object::add_extension('SiteTree', 'Compass_RebuildDecorator');
}

// Add the dev/compass tools into the URL map
Director::addRules(20, array('dev/compass' => 'Compass'));
if(class_exists('LeftAndMain')) {
Object::add_extension('LeftAndMain', 'Compass_RebuildDecorator');
}
7 changes: 7 additions & 0 deletions _config/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
Name: findrealestate
After: framework/routes#coreroutes
---
Director:
rules:
'dev/compass': 'Compass'
101 changes: 64 additions & 37 deletions code/Compass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,41 @@
* @package compass
*/
class Compass extends Controller {

static $url_handlers = array(

/**
* @var array
*/
public static $url_handlers = array(
'$Action' => '$Action'
);

/**
* @var bool Are compass errors actually errors, or should we just ignore them?
* True means complain, false means don't, null means don't complain on live servers, do otherwise
* @var bool Are compass errors actually errors, or should we just ignore
* them? True means complain, false means don't, null means don't complain
* on live servers, do otherwise.
*/
static $errors_are_errors = null;
public static $errors_are_errors = null;

/**
* @var bool Set to true to force no automatic rebuilding, even if isDev() is true or flush is passed and the gems are all available
* @var bool Set to true to force no automatic rebuilding, even if isDev()
* is true or flush is passed and the gems are all available.
*/
static $force_no_rebuild = false;
public static $force_no_rebuild = false;

/**
* @var float Which version of sass should we use
* @var float Which version of sass should we use.
*/
static $sass_version = '3';
public static $sass_version = '3';

/**
* @var string - preferred syntax to use
* @var string - preferred syntax to use.
*/
public static $syntax = "scss";

/**
* @var array map of required gems for each version
* @var array map of required gems for each version.
*/
static $required_gems = array(
public static $required_gems = array(
'2' => array(
'yard' => '', 'maruku' => '', 'haml' => '~> 2.2', 'compass' => '~> 0.8.0', 'compass-colors' => ''
),
Expand All @@ -46,9 +51,10 @@ class Compass extends Controller {
);

/**
* @var bool Internal cache variable - is the version of rubygems currently available good enough?
* @var bool Internal cache variable - is the version of rubygems currently
* available good enough?
*/
static $check_gems_result = null;
private static $check_gems_result = null;

/**
* Directory to store sass cache. Defaults to temp_folder/.sass.
Expand All @@ -58,6 +64,7 @@ class Compass extends Controller {
*/
private static $temp_dir;


protected function checkGems() {
if (self::$check_gems_result === null) {

Expand Down Expand Up @@ -87,21 +94,31 @@ static function error($message) {
return false;
}

function init() {
public function init() {
parent::init();

// We allow access to this controller regardless of live-status or ADMIN permission only if on CLI.
// Access to this controller is always allowed in "dev-mode", or of the user is ADMIN.
// We allow access to this controller regardless of live-status or
// ADMIN permission only if on CLI. Access to this controller is
// always allowed in "dev-mode", or of the user is ADMIN.

$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
if (!$canAccess) return Security::permissionFailure($this);

if (!$canAccess) {
return Security::permissionFailure($this);
}
}

/**
* Convert a css based theme to a sass based one
*
* Designed to be called as a sake command (sapphire/sake dev/compass/convert --theme=blackcandy)
* Designed to be called as a sake command
*
* <code>
* sapphire/sake dev/compass/convert --theme=blackcandy
* </code>
*
* Set $verbose to anything positive to output status (calling as a controller passed HTTPRequest, which is good enough)
* @param bool $verbose to anything positive to output
* status (calling as a controller passed HTTPRequest, which is good enough)
*/
function convert($verbose = false) {
$dir = null;
Expand Down Expand Up @@ -177,7 +194,6 @@ protected function recursivelyConvert($from, $to) {

/**
* Utility function that returns an array of all themes.
* Logic taken from late 2.4 ManifestBuilder - kept here for 2.3 and earlier 2.4 compatibility
*
* @return array
*/
Expand Down Expand Up @@ -226,16 +242,20 @@ protected function getAllModules() {
/**
* Convert the sass files to css files
*
* Called automatically on dev machines, and when flush=all. Can also be called as a sake command (sapphire/sake dev/compass/rebuild)
* Called automatically on dev machines, and when flush=all. Can also be
* called as a sake command (sapphire/sake dev/compass/rebuild).
*
* Set $verbose to anything positive to output status (calling as a controller passed HTTPRequest, which is good enough)
* Note that errors get output independent of this argument - use errors_are_errors = false to suppress them.
* Set $verbose to anything positive to output status (calling as a
* controller passed HTTPRequest, which is good enough).
*
* @param bool $verbose - use Compass::$errors_are_errors = false to suppress.
*/
function rebuild($verbose = false) {
// Make sure the gems we need are available
if (($error = $this->checkGems()) !== true) return self::error($error);

$dir = null;

if (@$_GET['theme']) $dir = THEMES_PATH . DIRECTORY_SEPARATOR . $_GET['theme'];
if (@$_GET['module']) $dir = BASE_PATH . DIRECTORY_SEPARATOR . $_GET['module'];

Expand Down Expand Up @@ -263,13 +283,16 @@ function rebuild($verbose = false) {
$this->rebuildDirectory($path);
}
}


}

if ($verbose) echo "\nRebuild succesfull\n";
}

/**
* Rebuild the scss files from a given directory
*
* @param string $dir
*/
protected function rebuildDirectory($dir) {
if (!is_dir($dir)) return self::error("Could not rebuild $dir, as it doesn't exist");

Expand All @@ -288,17 +311,21 @@ protected function rebuildDirectory($dir) {
}

/**
* Make sure the compass and haml gems are up to date
* Make sure the compass and haml gems are up to date.
*
* If the gems are not present, the system will install them automatically, but won't update them after that for speeds sake.
* Call this from sake to ensure you've got the most up-to-date version
* If the gems are not present, the system will install them automatically,
* but won't update them after that for speeds sake. Call this from sake to
* ensure you've got the most up-to-date version.
*
* Designed to be called as a sake command:
* <code>
* sapphire/sake dev/compass/updategems
* </code>
*
* Designed to be called as a sake command (sapphire/sake dev/compass/updategems)
* @param bool $verbose - use Compass::$errors_are_errors = false to suppress.
*
* Set $verbose to anything positive to output status (calling as a controller passed HTTPRequest, which is good enough)
* Note that errors get output independent of this argument - use errors_are_errors = false to suppress them.
*/
function updategems($verbose = false) {
public function updategems($verbose = false) {
foreach (self::$required_gems[self::$sass_version] as $gem => $version) {
if (is_numeric($gem)) { $gem = $version; $version = null; }
if ($error = Rubygems::require_gem($gem, $version, true)) echo $error;
Expand Down Expand Up @@ -336,7 +363,7 @@ public static function set_temp_dir($dir) {

/**
* Return the temp directory for storing the sass cache. Path should be writable
* by the web server/
* by the web server.
*
* @return string
*/
Expand All @@ -357,9 +384,9 @@ public static function get_temp_dir() {
*
* @package compass
*/
class Compass_RebuildDecorator extends DataObjectDecorator {
class Compass_RebuildDecorator extends DataExtension {

function init() {
public function init() {
// Don't auto-rebuild if explicitly disabled
if (Compass::$force_no_rebuild) return;

Expand All @@ -371,7 +398,7 @@ function init() {
if (Director::isDev() || @$_GET['flush']) singleton('Compass')->rebuild();
}

function contentcontrollerInit() {
public function contentcontrollerInit() {
return $this->init();
}
}
Loading

0 comments on commit 80be501

Please sign in to comment.