Skip to content

Commit

Permalink
Merge pull request #32 from maks/fix-runzone
Browse files Browse the repository at this point in the history
fix deprecation, rm old logging
  • Loading branch information
maks authored Jun 29, 2023
2 parents b0ca771 + 7e7b9cd commit c0534ff
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 78 deletions.
10 changes: 6 additions & 4 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
include: package:lints/recommended.yaml

analyzer:
# language:
# strict-casts: true
# strict-inference: true
# strict-raw-types: true
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
exclude:
- lib/**/*.g.dart
- lib/**/*.freezed.dart
Expand Down
2 changes: 1 addition & 1 deletion bin/dsg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:io';
import 'package:bonsai/bonsai.dart';
import 'package:dsg/dsg.dart';

Future main(List<String> arguments) async {
Future<void> main(List<String> arguments) async {
const debug = true;
if (debug) {
Log.init(true);
Expand Down
20 changes: 10 additions & 10 deletions lib/src/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ class Application {

final ip = config.ip;
final port = config.port;
final MY_HTTP_ROOT_PATH =
final myHttpRootPath =
config.docroot; //Platform.script.resolve(folder).toFilePath();

late VirtualDirectory virtDir;
void _directoryHandler(final Directory dir, final HttpRequest request) {
void directoryHandler(final Directory dir, final HttpRequest request) {
log('$dir');
final indexUri = Uri.file(dir.path).resolve('index.html');
virtDir.serveFile(File(indexUri.toFilePath()), request);
}

virtDir = VirtualDirectory(MY_HTTP_ROOT_PATH)
virtDir = VirtualDirectory(myHttpRootPath)
..allowDirectoryListing = true
..followLinks = true
..jailRoot = false;
virtDir.directoryHandler = _directoryHandler;
virtDir.directoryHandler = directoryHandler;

Future<HttpServer> connect;
if (config.usesecureconnection) {
Expand All @@ -130,18 +130,18 @@ class Application {
connect = HttpServer.bind(ip, int.parse(port));
}

runZoned(() {
connect.then((final server) {
log('Server running $ip on port: $port, $MY_HTTP_ROOT_PATH');
runZonedGuarded(() {
connect.then((final server) {
log('Server running $ip on port: $port, $myHttpRootPath');
server.listen((final request) {
log('${request.connectionInfo?.remoteAddress.address}:${request.connectionInfo?.localPort} - ${request.method} ${request.uri}');

virtDir.serveRequest(request);
});
});
},
onError: (Object e, StackTrace stackTrace) =>
Log.e('Error running http server: $e $stackTrace'));
}, (error, stack) {
Log.e('Error running http server: $error $stack');
});
}

void watch(final String folder, final Config config) {
Expand Down
31 changes: 17 additions & 14 deletions lib/src/config.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: constant_identifier_names

part of dsg;

/// Defines default-configurations.
Expand Down Expand Up @@ -36,12 +38,12 @@ class Config {
Config(this._argResults) {
_settings[Options._ARG_LOGLEVEL] = 'info';

_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_LISTINGS_DIR] = '${_CONFIG_FOLDER}/html/_listings';
_settings[Config._CONF_PARTIALS_DIR] = '${_CONFIG_FOLDER}/html/_partials';
_settings[Config._CONF_ASSETS_DIR] = '${_CONFIG_FOLDER}/html/_assets';
_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_LISTINGS_DIR] = '$_CONFIG_FOLDER/html/_listings';
_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] = '.';
Expand Down Expand Up @@ -182,8 +184,9 @@ class Config {
void printSettings() {
int getMaxKeyLength() {
var length = 0;
settings.keys
.forEach((final String key) => length = math.max(length, key.length));
for (var key in settings.keys) {
length = math.max(length, key.length);
}
return length;
}

Expand All @@ -199,14 +202,14 @@ class Config {

print('Settings:');
settings.forEach((final String key, final value) {
print(' ${prepareKey(key)} ${value}');
print(' ${prepareKey(key)} $value');
});
}

void printSiteKeys() {
print('Keys for ${configfile}:');
print('Keys for $configfile:');
_settings.forEach((final String key, final dynamic value) {
print(' ${(key + ':').padRight(20)} $value');
print(' ${('$key:').padRight(20)} $value');
});
}

Expand Down Expand Up @@ -234,17 +237,17 @@ class Config {
}

void _overwriteSettingsWithConfigFile() {
final file = File('${configfolder}/${configfile}');
final file = File('$configfolder/$configfile');
if (!file.existsSync()) {
return;
}
final map = yaml.loadYaml(file.readAsStringSync()) as yaml.YamlMap;
_settings.keys.forEach((final String key) {
for (var key in _settings.keys) {
if (map.containsKey(key)) {
_settings[key] = map[key];
print('Found $key in $configfile: ${map[key]}');
}
});
}
}

Map<String, String> _toMap(final dynamic configOption) {
Expand Down
73 changes: 38 additions & 35 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of dsg;
/// Takes a template string (such as a Mustache template) and renders it out to an HTML string
/// using the given input values/options.
///
typedef TemplateRenderer = String Function(String template, Map options, PartialsResolver resolver);
typedef TemplateRenderer = String Function(String template, Map<dynamic, dynamic> options, PartialsResolver resolver);

/// Resolved partial-names into mustache.Templates
typedef PartialsResolver = mustache.Template Function(String name);
Expand All @@ -15,12 +15,13 @@ String _outputFormat = 'MMMM dd yyyy';
///
/// Uses [Mustache templates](https://pub.dartlang.org/packages/mustache) by default.
///
TemplateRenderer renderTemplate = (final String? source, final Map options, final PartialsResolver resolver) {
TemplateRenderer renderTemplate =
(final String? source, final Map<dynamic, dynamic> options, final PartialsResolver resolver) {
final template = mustache.Template(source ?? '', htmlEscapeValues: false, partialResolver: resolver, lenient: true);

final inFormat = 'yyyy-MM-dd';

final formatDate = (mustache.LambdaContext ctx) => '${_parseDate(ctx.renderString(), inFormat, _outputFormat)}';
formatDate(mustache.LambdaContext ctx) => _parseDate(ctx.renderString(), inFormat, _outputFormat);

options['formatDate'] = formatDate;

Expand All @@ -31,6 +32,7 @@ TemplateRenderer renderTemplate = (final String? source, final Map options, fina

class Generator {
/// Mustache-Renderer strips out newlines
// ignore: constant_identifier_names
static const String _NEWLINE_PROTECTOR = '@@@#@@@';

/// Render and output your static site (WARNING: overwrites existing HTML files in output directory).
Expand Down Expand Up @@ -69,11 +71,11 @@ class Generator {
log('Generating .html files...');

for (final file in files) {
final relativeFileName = file.path.replaceAll('${contentDir.path}', '').replaceFirst('/', '');
final relativeFileName = file.path.replaceAll(contentDir.path, '').replaceFirst('/', '');
final relativePath = path.dirname(relativeFileName).replaceFirst('.', '');
final extension = path.extension(relativeFileName).replaceFirst('.', '').toLowerCase();

log('\nFile: ${relativeFileName}, Path: $relativePath');
log('\nFile: $relativeFileName, Path: $relativePath');

final fileContents = file.readAsStringSync();
fm.FrontMatterDocument fmDocument;
Expand Down Expand Up @@ -133,7 +135,7 @@ class Generator {
templateContent, pageOptions, _partialsResolver(partialsDir, isMarkdownSupported: config.usemarkdown)),
config);

final outputFilename = '${path.basenameWithoutExtension(relativeFileName)}.${outputExtension}';
final outputFilename = '${path.basenameWithoutExtension(relativeFileName)}.$outputExtension';
final outputPath = _createOutputPath(outputDir, relativePath);
final outputFile = File('${outputPath.path}/$outputFilename');

Expand All @@ -143,7 +145,7 @@ class Generator {
}

for (final image in images) {
final relativeFileName = image.path.replaceAll('${contentDir.path}', '').replaceFirst('/', '');
final relativeFileName = image.path.replaceAll(contentDir.path, '').replaceFirst('/', '');
final relativePath = path.dirname(relativeFileName).startsWith('.')
? path.dirname(relativeFileName)
: path.dirname(relativeFileName).replaceFirst('.', '');
Expand All @@ -157,7 +159,7 @@ class Generator {
}

for (final asset in assets) {
final relativeFileName = asset.path.replaceAll('${assetsDir.path}', '').replaceFirst('/', '');
final relativeFileName = asset.path.replaceAll(assetsDir.path, '').replaceFirst('/', '');
final relativePath = path.dirname(relativeFileName).replaceFirst('.', '');

final outputPath = _createOutputPath(outputDir, relativePath);
Expand All @@ -182,13 +184,13 @@ class Generator {
///
void _resolvePartialsInYamlBlock(
final Directory partialsDir, final Map<String, dynamic> pageOptions, bool useMarkdown) {
pageOptions.keys.forEach((final String key) {
for (var key in pageOptions.keys) {
if (pageOptions[key] is String && (pageOptions[key] as String).contains('->')) {
final partial = (pageOptions[key] as String).replaceAll(RegExp(r'[^>]*>'), '');
pageOptions[key] = renderTemplate(
'{{>${partial}}}', pageOptions, _partialsResolver(partialsDir, isMarkdownSupported: useMarkdown));
'{{>$partial}}', pageOptions, _partialsResolver(partialsDir, isMarkdownSupported: useMarkdown));
}
});
}
}

/// Returns a partials-Resolver. The partials-Resolver gets a dot separated name. This name is translated
Expand Down Expand Up @@ -225,7 +227,7 @@ class Generator {

Directory _createOutputPath(final Directory outputDir, final String relativePath) {
final relPath = relativePath.isNotEmpty ? '/' : '';
final outputPath = Directory('${outputDir.path}${relPath}${relativePath}');
final outputPath = Directory('${outputDir.path}$relPath$relativePath');
if (!outputPath.existsSync()) {
outputPath.createSync(recursive: true);
}
Expand Down Expand Up @@ -318,9 +320,9 @@ class Generator {
.toList();
}

bool _isMarkdownSupported(final bool markdownForSite, final Map page_options) {
bool _isMarkdownSupported(final bool markdownForSite, final Map<dynamic, dynamic> pageOptions) {
return markdownForSite ||
(page_options.containsKey('markdown_templating') && page_options['markdown_templating'] as bool);
(pageOptions.containsKey('markdown_templating') && pageOptions['markdown_templating'] as bool);
}

Map<String, dynamic> _fillInDefaultPageOptions(final String defaultDateFormat, final File file,
Expand All @@ -331,22 +333,22 @@ class Generator {
pageOptions['_site'] = siteOptions;

/// See [DateFormat](https://api.dartlang.org/docs/channels/stable/latest/intl/DateFormat.html) for formatting options
final date_format = DateFormat(defaultDateFormat);
final dateFormat = DateFormat(defaultDateFormat);

if (pageOptions.containsKey('date_format')) {
final page_date_format = DateFormat(pageOptions['date_format'] as String);
pageOptions['_date'] = page_date_format.format(file.lastModifiedSync());
final pageDateFormat = DateFormat(pageOptions['date_format'] as String);
pageOptions['_date'] = pageDateFormat.format(file.lastModifiedSync());
} else {
pageOptions['_date'] = date_format.format(file.lastModifiedSync());
pageOptions['_date'] = dateFormat.format(file.lastModifiedSync());
}

return pageOptions;
}

Map _getDataMap(final List<File> dataFiles) {
Map<dynamic, dynamic> _getDataMap(final List<File> dataFiles) {
final dataMap = <String, dynamic>{};

dataFiles.forEach((final File file) {
for (var file in dataFiles) {
if (file.existsSync()) {
dynamic data;
if (path.extension(file.path) == '.yaml') {
Expand All @@ -358,7 +360,7 @@ class Generator {
final filename = path.basenameWithoutExtension(file.path).toLowerCase();
dataMap[filename] = data;
}
});
}

return dataMap;
}
Expand All @@ -380,13 +382,13 @@ class Generator {
if (relativeFileName.contains('/')) {
nestingLevel = relativeFileName.split('/').length - 1;
for (var counter = 0; counter < nestingLevel; counter++) {
backPath = backPath + '../';
backPath = '$backPath../';
}
}

final pathWithoutExtension = path.withoutExtension(relativeFileName);
// final String portablePath = pathWithoutExtension.replaceAll( RegExp('(/|\\\\\)'),':');
final pageIndicator = pathWithoutExtension.replaceAll(RegExp('(/|\\\\\)'), '_');
final pageIndicator = pathWithoutExtension.replaceAll(RegExp('(/|\\\\)'), '_');
pageOptions['_page'] = {
'filename': pathWithoutExtension,
'pageindicator': pageIndicator,
Expand All @@ -402,17 +404,18 @@ class Generator {
}

File _getTemplateFor(
final File file, final Map page_options, final List<File> templates, final String defaultTemplate) {
final File file, final Map<dynamic, dynamic> pageOptions, final List<File> templates,
final String defaultTemplate) {
final filenameWithoutExtension = path.basenameWithoutExtension(file.path);
final filepath = path.normalize(file.path);

File template;
//_logger.info('Templates: ${templates}, Default: ${defaultTemplate}');

try {
if (page_options.containsKey('template')) {
if (pageOptions.containsKey('template')) {
template = templates
.firstWhere((final File file) => path.basenameWithoutExtension(file.path) == page_options['template']);
.firstWhere((final File file) => path.basenameWithoutExtension(file.path) == pageOptions['template']);
} else if (defaultTemplate.isNotEmpty) {
template = templates.firstWhere((final File file) {
return path.basenameWithoutExtension(file.path) == path.basenameWithoutExtension(defaultTemplate);
Expand All @@ -432,12 +435,12 @@ class Generator {
/// Currently only supports replacing Unix-style relative paths.
///
String _fixPathRefs(String html, final Config config) {
var relative_output = path.relative(config.outputfolder, from: config.templatefolder);
var relativeOutput = path.relative(config.outputfolder, from: config.templatefolder);

relative_output = '$relative_output/'.replaceAll('\\', '/');
relativeOutput = '$relativeOutput/'.replaceAll('\\', '/');
//_logger.info(relative_output);

html = html.replaceAll('src="$relative_output', 'src="').replaceAll('href="$relative_output', 'href="');
html = html.replaceAll('src="$relativeOutput', 'src="').replaceAll('href="$relativeOutput', 'href="');

return html;
}
Expand All @@ -448,25 +451,25 @@ class Generator {
final Map<String, dynamic> pageOptions, final Config config) {
check(relativeFileName).isNotEmpty();

log(' --- ${(relativeFileName + " ").padRight(76, "-")}');
log(' --- ${("$relativeFileName ").padRight(76, "-")}');

void _showMap(final Map<String, dynamic> values, final int nestingLevel) {
void showMap(final Map<String, dynamic> values, final int nestingLevel) {
values.forEach((final String key, final dynamic value) {
log(' ${"".padRight(nestingLevel * 2)} $key.');

if (value is Map) {
_showMap(value as Map<String, dynamic>, nestingLevel + 1);
showMap(value as Map<String, dynamic>, nestingLevel + 1);
} else {
var valueAsString =
value.toString().replaceAll(RegExp('(\n|\r|\\s{2,}|${_NEWLINE_PROTECTOR})', multiLine: true), '');
value.toString().replaceAll(RegExp('(\n|\r|\\s{2,}|$_NEWLINE_PROTECTOR)', multiLine: true), '');

valueAsString = valueAsString.substring(0, math.min(50, math.max(valueAsString.length, 0)));
log(' ${"".padRight(nestingLevel * 2)} $key -> [${valueAsString}]');
log(' ${"".padRight(nestingLevel * 2)} $key -> [$valueAsString]');
}
});
}

_showMap(pageOptions, 0);
showMap(pageOptions, 0);
log(' ${"".padRight(80, "-")}');
}
}
Loading

0 comments on commit c0534ff

Please sign in to comment.