diff --git a/hshassets/__init__.py b/hshassets/__init__.py index 6a3d0df5bd62d7b9b533696dacf5760e7548aedb..b7b1ef4fb68eaa3192bf800d69512453eb534641 100644 --- a/hshassets/__init__.py +++ b/hshassets/__init__.py @@ -1 +1,3 @@ +import logging +logger = logging.getLogger('hshassets') default_app_config = "hshassets.apps.HshassetsConfig" diff --git a/hshassets/app_settings/defaults.py b/hshassets/app_settings/defaults.py index b45d1079ddab8c45ce09f534fa3ab2ea8f0670ff..6144f9d6d7af62e1c41d950dfbbc188e6c0fd6e2 100644 --- a/hshassets/app_settings/defaults.py +++ b/hshassets/app_settings/defaults.py @@ -14,3 +14,14 @@ PIWIK_SITE_ID = None # 'f4' - light blue for faculty 4, "Wirtschaft und Informatik" - rgb(220, 50, 5) # 'f5' - light blue for faculty 5, "Diakonie, Gesundheit und Soziales" - rgb(210, 60, 150) COLOR_SCHEME = 'service' + +# need some dependecies already available in another app for your own scss stuff? +# no problem - by default we already add the hshassets/assets/sass/lib for you +# but you might define your own, just add your tuple like this: +# ('app_name', ['path/relative/to/appname/assets', 'another/relative/to/appname/assets']), +# NOTE: the app must be added to INSTALLED_APPS +# TODO: if you provide a str instead of tuple this path might be treated absolute or rel to project root! +# read utils.get_scss_include_paths() for further information +SCSS_INCLUDE_PATHS = [ + ('hshassets', ['sass/lib/']), +] \ No newline at end of file diff --git a/hshassets/management/commands/watchassets.py b/hshassets/management/commands/watchassets.py index 8ab82aa1d41c024e91e43b3e34981bc1c5936cc7..5d745531b37506c572953d4f642314457e9838a8 100644 --- a/hshassets/management/commands/watchassets.py +++ b/hshassets/management/commands/watchassets.py @@ -10,12 +10,16 @@ from watchdog.observers import Observer class AssetBuildHandler(events.PatternMatchingEventHandler): + def __init__(self, scss_include_paths, *args, **kwargs): + super().__init__(*args, **kwargs) + self.scss_include_paths = scss_include_paths + def on_modified(self, event): if isinstance(event, events.FileModifiedEvent): print('File modified: \033[35m{}\033[0m'.format(event.src_path)) try: app_name, app_directories = utils.discover_app(event.src_path) - utils.build_specific(app_name, app_directories, event.src_path) + utils.build_specific(app_name, app_directories, event.src_path, self.scss_include_paths) except Exception as e: print('Failed! \033[31m(✖_✖)\033[0m\n') traceback.print_exc() @@ -47,10 +51,12 @@ class Command(BaseCommand): observer = Observer() - build_handler = AssetBuildHandler(patterns=['*.js', '*.sass', '*.scss', '*.css']) - image_handler = ImageHandler(patterns=['*']) + asset_directories = utils.get_asset_directories() + scss_include_paths = utils.get_scss_include_paths(asset_directories) - for app_name, app_directories in utils.get_asset_directories().items(): + build_handler = AssetBuildHandler(scss_include_paths, patterns=['*.js', '*.sass', '*.scss', '*.css']) + image_handler = ImageHandler(patterns=['*']) + for app_name, app_directories in asset_directories.items(): if not os.path.isdir(app_directories['static_path']): os.makedirs(app_directories['static_path']) diff --git a/hshassets/utils.py b/hshassets/utils.py index 5941c144f8dbe1d5d43c4ced74ec3f409aa0705a..3ef9a375ad4fd753322199f5d70adc4450e715cd 100644 --- a/hshassets/utils.py +++ b/hshassets/utils.py @@ -1,6 +1,7 @@ from django.conf import settings -from hshassets.app_settings import COLOR_SCHEME +from hshassets import logger +from hshassets.app_settings import COLOR_SCHEME, SCSS_INCLUDE_PATHS from importlib import import_module @@ -32,6 +33,26 @@ def get_asset_directories(): return asset_apps +def get_scss_include_paths(asset_directories): + """ Get additional include_paths for the scss parser - to be able to load e.g. bulma without getting insane """ + include_paths = [] + for app_name, rel_paths in SCSS_INCLUDE_PATHS: + if app_name in asset_directories.keys(): + app_meta = asset_directories[app_name] + for rel_path in rel_paths: + lib_path = os.path.join(app_meta['asset_path'], rel_path) + if os.path.exists(lib_path): + include_paths.append(lib_path) + else: + logger.warning('You specified an additional scss path which can not be found: {}'.format(lib_path)) + else: + logger.warning( + 'You specified the app {} in "SCSS_INCLUDE_PATHS" that can not be found. Is it in INSTALLED_APPS?' + .format(app_name) + ) + return include_paths + + def insert_corporate_design_import(filepath): content = '' tmp_filename = '/sass/_cd_init.tmp.scss' @@ -51,7 +72,7 @@ def remove_corporate_design_import(filepath): os.remove(filepath) -def build_scss(app_name, app_directories, verbose=True): +def build_scss(app_name, app_directories, include_paths, verbose=True): if verbose: print('Building \033[36msass\033[0m for app "{}" ...'.format(app_name)) @@ -75,10 +96,11 @@ def build_scss(app_name, app_directories, verbose=True): with open(style_file, 'r') as cdfile: content = cdfile.read() + '\n\n' + content - sass_path = app_directories['asset_path'] + '/sass' + # add this apps sass folder to the include paths + include_paths = [os.path.join(app_directories['asset_path'], 'sass')] + include_paths - expanded_output = sass.compile(string=content, include_paths=[sass_path], output_style='expanded') - compressed_output = sass.compile(string=content, include_paths=[sass_path], output_style='compressed') + expanded_output = sass.compile(string=content, include_paths=include_paths, output_style='expanded') + compressed_output = sass.compile(string=content, include_paths=include_paths, output_style='compressed') with open(app_directories['static_path'] + '/styles.css', 'w') as outfile: outfile.write(expanded_output) @@ -176,25 +198,27 @@ def discover_app(file_path): app_directories = current_app_directories break - return (app_name, app_directories) + return app_name, app_directories def do_everything(verbose=False): - for app_name, app_directories in get_asset_directories().items(): + asset_directories = get_asset_directories() + include_paths = get_scss_include_paths(asset_directories) + for app_name, app_directories in asset_directories.items(): if os.path.isdir(app_directories['static_path']): shutil.rmtree(app_directories['static_path']) os.makedirs(app_directories['static_path']) - build_scss(app_name, app_directories, verbose=verbose) + build_scss(app_name, app_directories, include_paths, verbose=verbose) build_javascript(app_name, app_directories, verbose=verbose) copy_images(app_name, app_directories, verbose=verbose) copy_fonts(app_name, app_directories, verbose=verbose) -def build_specific(app_name, app_directories, file_path): +def build_specific(app_name, app_directories, file_path, scss_include_paths): if file_path.endswith(('.sass', '.scss', '.css')): - build_scss(app_name, app_directories) + build_scss(app_name, app_directories, scss_include_paths) elif file_path.endswith(('.js')): build_javascript(app_name, app_directories)