Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tsteur/5469347 to your computer and use it in GitHub Desktop.
Save tsteur/5469347 to your computer and use it in GitHub Desktop.
Finds unused styles within a Titanium Mobile - Alloy project. In order to execute it you need to place it within the root of your Titanium project and execute it using `php find_unused_styles_in_titanium_mobile_alloy.php`. PHP should be preinstalled on almost all Macs. Script only searches for app/styles/*.tss files. It does not perform a search…
<?php
foreach (getAllTssFilenames() as $tssfile){
echo "\nSearching for unused styles in $tssfile:\n";
outputUnusedStyles($tssfile);
}
function getAllTssFilenames()
{
return glob('app/styles/*.tss');
}
function outputUnusedStyles($tssfile)
{
$selectors = getSelectorsWithinStyleFile($tssfile);
$relatedViewContent = getContentOfRelatedViewFile($tssfile);
foreach ($selectors as $selector) {
if (!usesSelector($relatedViewContent, $selector)) {
echo "$selector\n";
}
}
}
function getSelectorsWithinStyleFile($tssfile)
{
$content = file_get_contents($tssfile);
$matches = array();
preg_match_all('/\"(.[^\"]*)\":/', $content, $matches);
if (!empty($matches[1])) {
return $matches[1];
}
return array();
}
function getContentOfRelatedViewFile($tssfile)
{
$relatedViewFile = getFilenameOfRelatedViewFile($tssfile);
if (!file_exists($relatedViewFile)) {
return '';
}
return file_get_contents($relatedViewFile);
}
function getFilenameOfRelatedViewFile($tssfile)
{
return sprintf('app/views/%s.xml', basename($tssfile, '.tss'));
}
function isIdSelector($selector)
{
return '#' === substr($selector, 0, 1);
}
function isClassSelector($selector)
{
return '.' === substr($selector, 0, 1);
}
function removeDeviceQueryFromSelector($selector)
{
$posOfStartDeviceQuery = strpos($selector, '[');
if (false === $posOfStartDeviceQuery) {
return $selector;
}
return substr($selector, 0, $posOfStartDeviceQuery);
}
function containsClass($viewContent, $classSelector)
{
$classSelector = removeDeviceQueryFromSelector($classSelector);
$className = substr($classSelector, 1);
$contentToFind = sprintf('class="%s"', $className);
return false !== strpos($viewContent, $contentToFind);
}
function containsId($viewContent, $idSelector)
{
$idSelector = removeDeviceQueryFromSelector($idSelector);
$idToFind = substr($idSelector, 1);
$contentToFind = sprintf('id="%s"', $idToFind);
return false !== strpos($viewContent, $contentToFind);
}
function containsElement($viewContent, $elementSelector)
{
$elementSelector = removeDeviceQueryFromSelector($elementSelector);
$contentToFind = sprintf('<%s', $elementSelector);
return false !== strpos($viewContent, $contentToFind);
}
function usesSelector($viewContent, $selector)
{
if (isClassSelector($selector)) {
return containsClass($viewContent, $selector);
} elseif (isIdSelector($selector)) {
return containsId($viewContent, $selector);
}
return containsElement($viewContent, $selector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment