Skip to content

Instantly share code, notes, and snippets.

@nzbart
Created March 26, 2014 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzbart/9791503 to your computer and use it in GitHub Desktop.
Save nzbart/9791503 to your computer and use it in GitHub Desktop.
Show the hierarchy of sass file imports
param([string][parameter(mandatory)]$ParentFile, [string][parameter(mandatory)]$RootFile, [int][parameter(mandatory)]$Depth, [string][parameter(mandatory)]$IncludePath, [switch]$RenderDotFormat, [switch]$DoNotRecurse)
function RenderImports([string][parameter(mandatory)]$ParentFile, [string][parameter(mandatory)]$RootFile, [int][parameter(mandatory)]$Depth, [string][parameter(mandatory)]$IncludePath, [switch]$RenderDotFormat, [switch]$DoNotRecurse)
{
$ErrorActionPreference = 'Stop'
function GetRelativePath([string][parameter(mandatory)]$ParentFile, [string][parameter(mandatory)]$RootFile)
{
pushd (Split-Path -Parent $ParentFile)
try {
(Resolve-Path -Relative $RootFile) -replace '^\.\\', ''
}
finally {
popd
}
}
function EscapeDotCharacters([string][parameter(mandatory)]$value)
{
return $value -replace '\\', '\\'
}
if($RenderDotFormat) {
if($Depth -eq 0) {
echo "digraph g{"
}
$left = EscapeDotCharacters $ParentFile
$right = EscapeDotCharacters $RootFile
echo "`"$left`" -> `"$right`";"
}
else {
$indent = New-Object System.String ' ', ($Depth * 4)
$relativePath = GetRelativePath $ParentFile (Resolve-Path $RootFile)
echo "$indent $relativePath"
}
if(!$DoNotRecurse) {
cat $RootFile | % {
if($_ -match '^\s*@import\s*"([^"]+)"') {
$file = $Matches[1]
$leaf = Split-Path -Leaf $file
$parent = Split-Path -Parent $file
$rootDir = Split-Path -Parent $RootFile
$path = Join-Path $rootDir "$parent\_$leaf.scss"
if((Test-Path $path)) {
RenderImports $RootFile $path ($Depth + 1) $IncludePath -RenderDotFormat:$RenderDotFormat
}
else {
$path = Join-Path $IncludePath "$parent\_$leaf.scss"
if(!(Test-Path $path)) {
throw "Unable to find import: $file."
}
RenderImports $RootFile $path ($Depth + 1) $IncludePath -RenderDotFormat:$RenderDotFormat -DoNotRecurse
}
}
}
}
if($RenderDotFormat) {
if($Depth -eq 0) {
echo "}"
}
}
}
RenderImports $ParentFile $RootFile $Depth $IncludePath -RenderDotFormat:$RenderDotFormat -DoNotRecurse:$DoNotRecurse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment