/** * Compile the given SASS source * * @param Source $source Sass source * @throws Wikia\Sass\Exception * @return string CSS stylesheet */ public function compile(Source $source) { wfProfileIn(__METHOD__); $tempDir = $this->tempDir ?: sys_get_temp_dir(); //replace \ to / is needed because escapeshellcmd() replace \ into spaces (?!!) $outputFile = str_replace('\\', '/', tempnam($tempDir, uniqid('Sass'))); $tempDir = str_replace('\\', '/', $tempDir); $sassVariables = urldecode(http_build_query($this->sassVariables, '', ' ')); $debugMode = $this->useSourceMaps ? '--debug-in' : ''; $hasLocalFile = $source->hasPermanentFile(); $localFile = $source->getLocalFile(); $inputFile = $hasLocalFile ? $localFile : '-s'; $cmd = "{$this->sassExecutable} {$inputFile} {$outputFile} --scss -t {$this->outputStyle} " . "-I {$this->rootDir} {$debugMode} " . "--cache-location {$tempDir}/sass2 -r {$this->rootDir}/extensions/wikia/SASS/wikia_sass.rb {$sassVariables}"; $cmd = escapeshellcmd($cmd) . " 2>&1"; if (!$hasLocalFile) { $cmd = escapeshellcmd("cat {$localFile}") . " | " . $cmd; } $sassOutput = wfShellExec($cmd, $status); if ($status !== 0) { // 0 => success if (file_exists($outputFile)) { unlink($outputFile); } $this->error("SASS rendering failed", ['cmd' => $cmd, 'output' => preg_replace('#\\n\\s+#', ' ', trim($sassOutput)), 'status' => $status]); throw new Wikia\Sass\Exception("SASS compilation failed: {$sassOutput}\nFull commandline: {$cmd}"); } $styles = file_get_contents($outputFile); unlink($outputFile); if ($styles === false) { $this->error("Reading SASS file failed", ['input' => $inputFile, 'output' => $outputFile]); throw new Wikia\Sass\Exception("Reading SASS file failed"); } wfProfileOut(__METHOD__); return $styles; }
/** * Create Sass source instance associated with regular file * * @param $context SassSourceContext Context * @param $fileName string File path */ public function __construct(Context $context, $fileName) { if (!is_file($fileName)) { throw new \Wikia\Sass\Exception(__METHOD__ . ': File is not a regular file: ' . $fileName); } parent::__construct($context); $this->fileName = realpath($fileName); $this->currentDir = dirname($this->fileName); $this->humanName = $this->fileName; }