Skip to content

Instantly share code, notes, and snippets.

@cjcenizal
Created May 29, 2014 18:02
Show Gist options
  • Save cjcenizal/3d383755a16e05a96ca4 to your computer and use it in GitHub Desktop.
Save cjcenizal/3d383755a16e05a96ca4 to your computer and use it in GitHub Desktop.
Organize src for import by layer
# This script reorganizes the src folder to enable importing styles per layer.
#
# - Deletes pre-existing _index.scss files from each package
# - Creates layer-based import files for each package
# - Populates these files with the correct imports
# - Renames object folder to objects, and module folder to modules
# - Prints `@import` statements for use in main.scss, to import all layers
#
# Usage:
# - cd into the dir that contains this script
# - copy/paste the `src` dir into the dir you're now in
# - run `python [name of this script].py [full path to src folder]`
# - overwrite the original `src` dir with this one
import os
import sys
from os import listdir
from os.path import isfile, join
rootdir = sys.argv[1]
layers = [
"core",
"functions",
"settings",
"mixins",
"objects",
"modules",
"trumps"
]
ignoredFiles = [
".DS_Store"
]
layerImportStatements = {}
indexImportStatement = ""
for layer in layers:
layerImportStatements[layer] = []
indexImportStatement += "@import \"" + layer + "\";\n"
# All top-level package folders in src.
folders = [ f for f in listdir(rootdir) if not isfile(join(rootdir,f)) ]
# For every top-level package folder.
for folder in folders:
folderDir = rootdir + "/" + folder + "/"
# Delete index file.
os.remove(folderDir + "_index.scss")
# For every layer subfolder in the package.
subFolders = [ s for s in listdir(folderDir) if not isfile(join(folderDir,s)) ]
for subFolder in subFolders:
# Clean up: rename object and module folders to plural form.
if subFolder == "object":
os.rename(folderDir + subFolder, folderDir + "/objects")
subFolder = "objects"
if subFolder == "module":
os.rename(folderDir + subFolder, folderDir + "/modules")
subFolder = "modules"
# Get layer name for current subfolder.
layer = subFolder
# Store a layer import statement for use in the project.
importFilePath = folder + "/" + layer
importStatement = "@import \"[relative_bower_location]/" + importFilePath + "\";"
layerImportStatements[layer].append(importStatement)
# For every file in each subfolder, write an import statement to the
# correct import file.
subFolderDir = folderDir + subFolder
files = [ f for f in listdir(subFolderDir) if isfile(join(subFolderDir,f)) ]
for file in files:
# Ignore certain files.
if file not in ignoredFiles:
importStatement = "@import \"" + subFolder + "/" + file.replace(".scss", "").replace("_", "") + "\";\n"
layerImportFileName = folderDir + "_" + layer + ".scss"
layerImportFile = open(layerImportFileName, "a")
layerImportFile.write(importStatement)
layerImportFile.close()
print("-----------------------------------------------------------------------")
# Print out layer import statements in order.
for layer in layers:
print("\n// Import " + layer + ".\n")
for importStatement in layerImportStatements[layer]:
print(importStatement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment