Skip to content

Commit c0534ff

Browse files
authored
Merge pull request #32 from maks/fix-runzone
fix deprecation, rm old logging
2 parents b0ca771 + 7e7b9cd commit c0534ff

File tree

10 files changed

+95
-78
lines changed

10 files changed

+95
-78
lines changed

analysis_options.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
include: package:lints/recommended.yaml
2+
13
analyzer:
2-
# language:
3-
# strict-casts: true
4-
# strict-inference: true
5-
# strict-raw-types: true
4+
language:
5+
strict-casts: true
6+
strict-inference: true
7+
strict-raw-types: true
68
exclude:
79
- lib/**/*.g.dart
810
- lib/**/*.freezed.dart

bin/dsg.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'dart:io';
44
import 'package:bonsai/bonsai.dart';
55
import 'package:dsg/dsg.dart';
66

7-
Future main(List<String> arguments) async {
7+
Future<void> main(List<String> arguments) async {
88
const debug = true;
99
if (debug) {
1010
Log.init(true);

lib/src/application.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,21 @@ class Application {
103103

104104
final ip = config.ip;
105105
final port = config.port;
106-
final MY_HTTP_ROOT_PATH =
106+
final myHttpRootPath =
107107
config.docroot; //Platform.script.resolve(folder).toFilePath();
108108

109109
late VirtualDirectory virtDir;
110-
void _directoryHandler(final Directory dir, final HttpRequest request) {
110+
void directoryHandler(final Directory dir, final HttpRequest request) {
111111
log('$dir');
112112
final indexUri = Uri.file(dir.path).resolve('index.html');
113113
virtDir.serveFile(File(indexUri.toFilePath()), request);
114114
}
115115

116-
virtDir = VirtualDirectory(MY_HTTP_ROOT_PATH)
116+
virtDir = VirtualDirectory(myHttpRootPath)
117117
..allowDirectoryListing = true
118118
..followLinks = true
119119
..jailRoot = false;
120-
virtDir.directoryHandler = _directoryHandler;
120+
virtDir.directoryHandler = directoryHandler;
121121

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

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

139139
virtDir.serveRequest(request);
140140
});
141141
});
142-
},
143-
onError: (Object e, StackTrace stackTrace) =>
144-
Log.e('Error running http server: $e $stackTrace'));
142+
}, (error, stack) {
143+
Log.e('Error running http server: $error $stack');
144+
});
145145
}
146146

147147
void watch(final String folder, final Config config) {

lib/src/config.dart

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: constant_identifier_names
2+
13
part of dsg;
24

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

39-
_settings[Config._CONF_CONTENT_DIR] = '${_CONFIG_FOLDER}/html/_content';
40-
_settings[Config._CONF_TEMPLATE_DIR] = '${_CONFIG_FOLDER}/html/_templates';
41-
_settings[Config._CONF_DATA_DIR] = '${_CONFIG_FOLDER}/html/_data';
42-
_settings[Config._CONF_LISTINGS_DIR] = '${_CONFIG_FOLDER}/html/_listings';
43-
_settings[Config._CONF_PARTIALS_DIR] = '${_CONFIG_FOLDER}/html/_partials';
44-
_settings[Config._CONF_ASSETS_DIR] = '${_CONFIG_FOLDER}/html/_assets';
41+
_settings[Config._CONF_CONTENT_DIR] = '$_CONFIG_FOLDER/html/_content';
42+
_settings[Config._CONF_TEMPLATE_DIR] = '$_CONFIG_FOLDER/html/_templates';
43+
_settings[Config._CONF_DATA_DIR] = '$_CONFIG_FOLDER/html/_data';
44+
_settings[Config._CONF_LISTINGS_DIR] = '$_CONFIG_FOLDER/html/_listings';
45+
_settings[Config._CONF_PARTIALS_DIR] = '$_CONFIG_FOLDER/html/_partials';
46+
_settings[Config._CONF_ASSETS_DIR] = '$_CONFIG_FOLDER/html/_assets';
4547

4648
_settings[Config._CONF_OUTPUT_DIR] = 'web';
4749
_settings[Config._CONF_WORKSPACE_DIR] = '.';
@@ -182,8 +184,9 @@ class Config {
182184
void printSettings() {
183185
int getMaxKeyLength() {
184186
var length = 0;
185-
settings.keys
186-
.forEach((final String key) => length = math.max(length, key.length));
187+
for (var key in settings.keys) {
188+
length = math.max(length, key.length);
189+
}
187190
return length;
188191
}
189192

@@ -199,14 +202,14 @@ class Config {
199202

200203
print('Settings:');
201204
settings.forEach((final String key, final value) {
202-
print(' ${prepareKey(key)} ${value}');
205+
print(' ${prepareKey(key)} $value');
203206
});
204207
}
205208

206209
void printSiteKeys() {
207-
print('Keys for ${configfile}:');
210+
print('Keys for $configfile:');
208211
_settings.forEach((final String key, final dynamic value) {
209-
print(' ${(key + ':').padRight(20)} $value');
212+
print(' ${('$key:').padRight(20)} $value');
210213
});
211214
}
212215

@@ -234,17 +237,17 @@ class Config {
234237
}
235238

236239
void _overwriteSettingsWithConfigFile() {
237-
final file = File('${configfolder}/${configfile}');
240+
final file = File('$configfolder/$configfile');
238241
if (!file.existsSync()) {
239242
return;
240243
}
241244
final map = yaml.loadYaml(file.readAsStringSync()) as yaml.YamlMap;
242-
_settings.keys.forEach((final String key) {
245+
for (var key in _settings.keys) {
243246
if (map.containsKey(key)) {
244247
_settings[key] = map[key];
245248
print('Found $key in $configfile: ${map[key]}');
246249
}
247-
});
250+
}
248251
}
249252

250253
Map<String, String> _toMap(final dynamic configOption) {

lib/src/generator.dart

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ part of dsg;
33
/// Takes a template string (such as a Mustache template) and renders it out to an HTML string
44
/// using the given input values/options.
55
///
6-
typedef TemplateRenderer = String Function(String template, Map options, PartialsResolver resolver);
6+
typedef TemplateRenderer = String Function(String template, Map<dynamic, dynamic> options, PartialsResolver resolver);
77

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

2122
final inFormat = 'yyyy-MM-dd';
2223

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

2526
options['formatDate'] = formatDate;
2627

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

3233
class Generator {
3334
/// Mustache-Renderer strips out newlines
35+
// ignore: constant_identifier_names
3436
static const String _NEWLINE_PROTECTOR = '@@@#@@@';
3537

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

7173
for (final file in files) {
72-
final relativeFileName = file.path.replaceAll('${contentDir.path}', '').replaceFirst('/', '');
74+
final relativeFileName = file.path.replaceAll(contentDir.path, '').replaceFirst('/', '');
7375
final relativePath = path.dirname(relativeFileName).replaceFirst('.', '');
7476
final extension = path.extension(relativeFileName).replaceFirst('.', '').toLowerCase();
7577

76-
log('\nFile: ${relativeFileName}, Path: $relativePath');
78+
log('\nFile: $relativeFileName, Path: $relativePath');
7779

7880
final fileContents = file.readAsStringSync();
7981
fm.FrontMatterDocument fmDocument;
@@ -133,7 +135,7 @@ class Generator {
133135
templateContent, pageOptions, _partialsResolver(partialsDir, isMarkdownSupported: config.usemarkdown)),
134136
config);
135137

136-
final outputFilename = '${path.basenameWithoutExtension(relativeFileName)}.${outputExtension}';
138+
final outputFilename = '${path.basenameWithoutExtension(relativeFileName)}.$outputExtension';
137139
final outputPath = _createOutputPath(outputDir, relativePath);
138140
final outputFile = File('${outputPath.path}/$outputFilename');
139141

@@ -143,7 +145,7 @@ class Generator {
143145
}
144146

145147
for (final image in images) {
146-
final relativeFileName = image.path.replaceAll('${contentDir.path}', '').replaceFirst('/', '');
148+
final relativeFileName = image.path.replaceAll(contentDir.path, '').replaceFirst('/', '');
147149
final relativePath = path.dirname(relativeFileName).startsWith('.')
148150
? path.dirname(relativeFileName)
149151
: path.dirname(relativeFileName).replaceFirst('.', '');
@@ -157,7 +159,7 @@ class Generator {
157159
}
158160

159161
for (final asset in assets) {
160-
final relativeFileName = asset.path.replaceAll('${assetsDir.path}', '').replaceFirst('/', '');
162+
final relativeFileName = asset.path.replaceAll(assetsDir.path, '').replaceFirst('/', '');
161163
final relativePath = path.dirname(relativeFileName).replaceFirst('.', '');
162164

163165
final outputPath = _createOutputPath(outputDir, relativePath);
@@ -182,13 +184,13 @@ class Generator {
182184
///
183185
void _resolvePartialsInYamlBlock(
184186
final Directory partialsDir, final Map<String, dynamic> pageOptions, bool useMarkdown) {
185-
pageOptions.keys.forEach((final String key) {
187+
for (var key in pageOptions.keys) {
186188
if (pageOptions[key] is String && (pageOptions[key] as String).contains('->')) {
187189
final partial = (pageOptions[key] as String).replaceAll(RegExp(r'[^>]*>'), '');
188190
pageOptions[key] = renderTemplate(
189-
'{{>${partial}}}', pageOptions, _partialsResolver(partialsDir, isMarkdownSupported: useMarkdown));
191+
'{{>$partial}}', pageOptions, _partialsResolver(partialsDir, isMarkdownSupported: useMarkdown));
190192
}
191-
});
193+
}
192194
}
193195

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

226228
Directory _createOutputPath(final Directory outputDir, final String relativePath) {
227229
final relPath = relativePath.isNotEmpty ? '/' : '';
228-
final outputPath = Directory('${outputDir.path}${relPath}${relativePath}');
230+
final outputPath = Directory('${outputDir.path}$relPath$relativePath');
229231
if (!outputPath.existsSync()) {
230232
outputPath.createSync(recursive: true);
231233
}
@@ -318,9 +320,9 @@ class Generator {
318320
.toList();
319321
}
320322

321-
bool _isMarkdownSupported(final bool markdownForSite, final Map page_options) {
323+
bool _isMarkdownSupported(final bool markdownForSite, final Map<dynamic, dynamic> pageOptions) {
322324
return markdownForSite ||
323-
(page_options.containsKey('markdown_templating') && page_options['markdown_templating'] as bool);
325+
(pageOptions.containsKey('markdown_templating') && pageOptions['markdown_templating'] as bool);
324326
}
325327

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

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

336338
if (pageOptions.containsKey('date_format')) {
337-
final page_date_format = DateFormat(pageOptions['date_format'] as String);
338-
pageOptions['_date'] = page_date_format.format(file.lastModifiedSync());
339+
final pageDateFormat = DateFormat(pageOptions['date_format'] as String);
340+
pageOptions['_date'] = pageDateFormat.format(file.lastModifiedSync());
339341
} else {
340-
pageOptions['_date'] = date_format.format(file.lastModifiedSync());
342+
pageOptions['_date'] = dateFormat.format(file.lastModifiedSync());
341343
}
342344

343345
return pageOptions;
344346
}
345347

346-
Map _getDataMap(final List<File> dataFiles) {
348+
Map<dynamic, dynamic> _getDataMap(final List<File> dataFiles) {
347349
final dataMap = <String, dynamic>{};
348350

349-
dataFiles.forEach((final File file) {
351+
for (var file in dataFiles) {
350352
if (file.existsSync()) {
351353
dynamic data;
352354
if (path.extension(file.path) == '.yaml') {
@@ -358,7 +360,7 @@ class Generator {
358360
final filename = path.basenameWithoutExtension(file.path).toLowerCase();
359361
dataMap[filename] = data;
360362
}
361-
});
363+
}
362364

363365
return dataMap;
364366
}
@@ -380,13 +382,13 @@ class Generator {
380382
if (relativeFileName.contains('/')) {
381383
nestingLevel = relativeFileName.split('/').length - 1;
382384
for (var counter = 0; counter < nestingLevel; counter++) {
383-
backPath = backPath + '../';
385+
backPath = '$backPath../';
384386
}
385387
}
386388

387389
final pathWithoutExtension = path.withoutExtension(relativeFileName);
388390
// final String portablePath = pathWithoutExtension.replaceAll( RegExp('(/|\\\\\)'),':');
389-
final pageIndicator = pathWithoutExtension.replaceAll(RegExp('(/|\\\\\)'), '_');
391+
final pageIndicator = pathWithoutExtension.replaceAll(RegExp('(/|\\\\)'), '_');
390392
pageOptions['_page'] = {
391393
'filename': pathWithoutExtension,
392394
'pageindicator': pageIndicator,
@@ -402,17 +404,18 @@ class Generator {
402404
}
403405

404406
File _getTemplateFor(
405-
final File file, final Map page_options, final List<File> templates, final String defaultTemplate) {
407+
final File file, final Map<dynamic, dynamic> pageOptions, final List<File> templates,
408+
final String defaultTemplate) {
406409
final filenameWithoutExtension = path.basenameWithoutExtension(file.path);
407410
final filepath = path.normalize(file.path);
408411

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

412415
try {
413-
if (page_options.containsKey('template')) {
416+
if (pageOptions.containsKey('template')) {
414417
template = templates
415-
.firstWhere((final File file) => path.basenameWithoutExtension(file.path) == page_options['template']);
418+
.firstWhere((final File file) => path.basenameWithoutExtension(file.path) == pageOptions['template']);
416419
} else if (defaultTemplate.isNotEmpty) {
417420
template = templates.firstWhere((final File file) {
418421
return path.basenameWithoutExtension(file.path) == path.basenameWithoutExtension(defaultTemplate);
@@ -432,12 +435,12 @@ class Generator {
432435
/// Currently only supports replacing Unix-style relative paths.
433436
///
434437
String _fixPathRefs(String html, final Config config) {
435-
var relative_output = path.relative(config.outputfolder, from: config.templatefolder);
438+
var relativeOutput = path.relative(config.outputfolder, from: config.templatefolder);
436439

437-
relative_output = '$relative_output/'.replaceAll('\\', '/');
440+
relativeOutput = '$relativeOutput/'.replaceAll('\\', '/');
438441
//_logger.info(relative_output);
439442

440-
html = html.replaceAll('src="$relative_output', 'src="').replaceAll('href="$relative_output', 'href="');
443+
html = html.replaceAll('src="$relativeOutput', 'src="').replaceAll('href="$relativeOutput', 'href="');
441444

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

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

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

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

463466
valueAsString = valueAsString.substring(0, math.min(50, math.max(valueAsString.length, 0)));
464-
log(' ${"".padRight(nestingLevel * 2)} $key -> [${valueAsString}]');
467+
log(' ${"".padRight(nestingLevel * 2)} $key -> [$valueAsString]');
465468
}
466469
});
467470
}
468471

469-
_showMap(pageOptions, 0);
472+
showMap(pageOptions, 0);
470473
log(' ${"".padRight(80, "-")}');
471474
}
472475
}

0 commit comments

Comments
 (0)