From f919fa9d8179d285cc87a9724103f8a4e3b362d6 Mon Sep 17 00:00:00 2001 From: Mike Mitterer Date: Tue, 26 May 2015 17:36:34 +0200 Subject: [PATCH] feature: Extra Assets-Dir can be defined --- README.md | 56 +++++++++++++++++++++++----------------- lib/src/Application.dart | 9 +++++-- lib/src/Config.dart | 18 +++++++++---- lib/src/Generator.dart | 39 +++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 54ec89a..a3a56bf 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,28 @@ Before you read on - check out this video: ``` ├── .sitegen │   ├── refreshChromium-1.0.applescript +│ ├── html +│   │   ├── _assets +│   │   │   └── demo.scss +│   │   │ +│ │   ├── _content +│   │   │   ├── about +│   │   │   │   └── index.html +│   │   │   ├── index.html +│   │   │   ├── markdown.md +│   │   │   ├── piratenames.json +│   │   │   └── xtreme.html +│   │ │ +│   │   ├── _data +│   │   │   ├── xmen.yaml +│   │   │   └── families.json +│   │ │ +│   │ └── _templates +│   │   ├── default.html +│   │   └── info_page.html +│   │   │   └── site.yaml (Optional!) -├── html -│   ├── _content -│   │   ├── about -│   │   │   └── index.html -│   │   ├── index.html -│   │   ├── markdown.md -│   │   ├── piratenames.json -│   │   └── xtreme.html -│ │ -│   ├── _data -│   │   ├── xmen.yaml -│   │   └── families.json -│ │ -│ └── _templates -│   ├── default.html -│   └── info_page.html +│ └── web ├── about │   ├── index.html @@ -54,7 +59,7 @@ Before you read on - check out this video: This folder is also used to store autgenerated scripts - in the case above you can see the script to refresh Chromium on Mac. -**html/_content**: This is where **SiteGen** will look for your files to generate the site from. +**.sitegen/html/_content**: This is where **SiteGen** will look for your files to generate the site from. The following file-formats are supported: - .md @@ -66,7 +71,7 @@ The following file-formats are supported: - .scss - .css -**html/_data**: [optional] This is the place where you can store your data-files. +**.sitegen/html/_data**: [optional] This is the place where you can store your data-files. The following file-formats are supported: - .yaml @@ -82,8 +87,11 @@ Here is a sample how to use such data: {{/_data.xmen}} ``` - -**html/_templates**: The directory containing your HTML+Mustache templates. + +**.sitegen/html/_assets**: [optional] Additional assets that you don't want to have in _content. For example .scss +or .jpg files. + +**.sitegen/html/_templates**: The directory containing your HTML+Mustache templates. **web**: Following Dart conventions - this is your default output directory. @@ -104,9 +112,11 @@ Can be used in your template (default.html) as You can also use site.yaml to overwrite your **SiteGen** default configuration. Supported vars: -- content_dir: html/_content -- template_dir: html/_templates -- partials_dir: html/_partials +- content_dir: .sitegen/html/_content +- template_dir: .sitegen/html/_templates +- data_dir: .sitegen/html/_data +- partials_dir: .sitegen/html/_partials +- assets_dir: .sitegen/html/_assets - output_dir: web - workspace: . - date_format: dd.MM.yyyy diff --git a/lib/src/Application.dart b/lib/src/Application.dart index c07cf78..c40f642 100644 --- a/lib/src/Application.dart +++ b/lib/src/Application.dart @@ -71,6 +71,10 @@ class Application { watch(config.partialsfolder, config); } + if(_isFolderAvailable(config.assetsfolder)) { + watch(config.assetsfolder, config); + } + new Generator().generate(config); } watchScss(config.outputfolder, config); @@ -133,9 +137,10 @@ class Application { _logger.info('Observing $folder...'); - final File srcDir = new File(folder); + final Directory srcDir = new Directory(folder); + srcDir.watch(recursive: true).where((final file) => (!file.path.contains("packages"))).listen((final FileSystemEvent event) { - _logger.fine(event.toString()); + _logger.info(event.toString()); if(timerWatch == null) { timerWatch = new Timer(new Duration(milliseconds: 1000), () { new Generator().generate(config); diff --git a/lib/src/Config.dart b/lib/src/Config.dart index 696e54d..9f04ea3 100644 --- a/lib/src/Config.dart +++ b/lib/src/Config.dart @@ -7,11 +7,14 @@ part of sitegen; class Config { final Logger _logger = new Logger("sitegen.Config"); + static const String _CONFIG_FOLDER = ".sitegen"; + static const _CONF_CONTENT_DIR = 'content_dir'; static const _CONF_TEMPLATE_DIR = 'template_dir'; static const _CONF_OUTPUT_DIR = 'output_dir'; static const _CONF_DATA_DIR = 'data_dir'; static const _CONF_PARTIALS_DIR = 'partials_dir'; + static const _CONF_ASSETS_DIR = 'assets_dir'; static const _CONF_WORKSPACE_DIR = 'workspace'; static const _CONF_DATE_FORMAT = 'date_format'; static const _CONF_YAML_DELIMITER = 'yaml_delimeter'; @@ -30,10 +33,12 @@ class Config { _settings[Options._ARG_LOGLEVEL] = 'info'; - _settings[Config._CONF_CONTENT_DIR] = 'html/_content'; - _settings[Config._CONF_TEMPLATE_DIR] = 'html/_templates'; - _settings[Config._CONF_DATA_DIR] = 'html/_data'; - _settings[Config._CONF_PARTIALS_DIR] = 'html/_partials'; + _settings[Config._CONF_CONTENT_DIR] = '${_CONFIG_FOLDER}/html/_content'; + _settings[Config._CONF_TEMPLATE_DIR] = '${_CONFIG_FOLDER}/html/_templates'; + _settings[Config._CONF_DATA_DIR] = '${_CONFIG_FOLDER}/html/_data'; + _settings[Config._CONF_PARTIALS_DIR] = '${_CONFIG_FOLDER}/html/_partials'; + _settings[Config._CONF_ASSETS_DIR] = '${_CONFIG_FOLDER}/html/_assets'; + _settings[Config._CONF_OUTPUT_DIR] = 'web'; _settings[Config._CONF_WORKSPACE_DIR] = '.'; _settings[Config._CONF_DATE_FORMAT] = 'dd.MM.yyyy'; @@ -58,7 +63,7 @@ class Config { List get dirstoscan => _argResults.rest; - String get configfolder => ".sitegen"; + String get configfolder => _CONFIG_FOLDER; String get configfile => "site.yaml"; @@ -74,6 +79,8 @@ class Config { String get partialsfolder => _settings[Config._CONF_PARTIALS_DIR]; + String get assetsfolder => _settings[Config._CONF_ASSETS_DIR]; + String get workspace => _settings[Config._CONF_WORKSPACE_DIR]; String get dateformat => _settings[Config._CONF_DATE_FORMAT]; @@ -105,6 +112,7 @@ class Config { settings["Template folder"] = templatefolder; settings["Data folder"] = datafolder; settings["Partials folder"] = partialsfolder; + settings["Assets folder"] = assetsfolder; settings["Default template"] = defaulttemplate; settings["Output folder"] = outputfolder; diff --git a/lib/src/Generator.dart b/lib/src/Generator.dart index 6eed531..85be9d4 100644 --- a/lib/src/Generator.dart +++ b/lib/src/Generator.dart @@ -31,6 +31,7 @@ class Generator { final Directory outputDir = new Directory(path.absolute( config.outputfolder)); final Directory dataDir = new Directory(path.absolute( config.datafolder)); final Directory partialsDir = new Directory(path.absolute( config.partialsfolder)); + final Directory assetsDir = new Directory(path.absolute( config.assetsfolder)); Validate.isTrue(contentDir.existsSync(),"ContentDir ${contentDir.path} must exist!"); Validate.isTrue(templateDir.existsSync(),"Templatefolder ${templateDir.path} must exist!"); @@ -38,6 +39,7 @@ class Generator { final List files = _listContentFilesIn(contentDir); final List images = _listImagesFilesIn(contentDir); + final List assets = _listAssetsFilesIn(assetsDir); final List templates = _listTemplatesIn(templateDir); final List dataFiles = dataDir.existsSync() ? _listDataFilesIn(dataDir) : new List(); @@ -111,6 +113,18 @@ class Generator { _logger.info(" ${outputFile.path.replaceFirst(outputDir.path,"")} - copied!"); } + + for(final File asset in assets) { + final String relativeFileName = asset.path.replaceAll("${assetsDir.path}","").replaceFirst("/",""); + final String relativePath = path.dirname(relativeFileName).replaceFirst(".",""); + + final Directory outputPath = _createOutputPath(outputDir,relativePath); + final File outputFile = new File("${outputPath.path}/${path.basename(relativeFileName)}"); + asset.copySync(outputFile.path); + + _logger.info(" ${outputFile.path.replaceFirst(outputDir.path,"")} - copied!"); + } + } @@ -183,6 +197,10 @@ class Generator { } List _listContentFilesIn(final Directory contentDir) { + if(!contentDir.existsSync()) { + return new List(); + } + return contentDir.listSync(recursive: true) .where((file) => file is File && ( @@ -193,7 +211,8 @@ class Generator { file.path.endsWith(".json") || file.path.endsWith(".html") || file.path.endsWith(".scss") || - file.path.endsWith(".css") + file.path.endsWith(".css") || + file.path.endsWith(".svg") ) && !file.path.contains("packages") ).toList(); } @@ -209,6 +228,24 @@ class Generator { ) && !file.path.contains("packages") ).toList(); } + List _listAssetsFilesIn(final Directory contentDir) { + if(!contentDir.existsSync()) { + return new List(); + } + + return contentDir.listSync(recursive: true) + .where((file) => file is File && ( + + file.path.endsWith(".png") || + file.path.endsWith(".jpg") || + file.path.endsWith(".scss") || + file.path.endsWith(".css") || + file.path.endsWith(".svg") + + ) && !file.path.contains("packages") ).toList(); + } + + List _listTemplatesIn(final Directory templateDir) { return templateDir.listSync().where((file) => file is File && !file.path.contains("packages")).toList(); }