From 311d7065ea774c21e585b443015522dc779ed3bf Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 21 Apr 2016 21:46:25 -0400 Subject: [PATCH 01/22] Initial commit --- .gitignore | 27 +++++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 2 ++ 3 files changed, 50 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7c280441 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# See https://www.dartlang.org/tools/private-files.html + +# Files and directories created by pub +.buildlog +.packages +.project +.pub/ +build/ +**/packages/ + +# Files created by dart2js +# (Most Dart developers will use pub build to compile Dart, use/modify these +# rules if you intend to use dart2js directly +# Convention is to use extension '.dart.js' for Dart compiled to Javascript to +# differentiate from explicit Javascript files) +*.dart.js +*.part.js +*.js.deps +*.js.map +*.info.json + +# Directory created by dartdoc +doc/api/ + +# Don't commit pubspec lock file +# (Library packages only! Remove pattern if developing an application package) +pubspec.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..eb4ce33e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 angel-dart + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..d2951a0b --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# angel_hbs +Handlebars view generator for Angel. From 2be4f7c77b8a719cc4017ee499f8d62b20536155 Mon Sep 17 00:00:00 2001 From: regiostech Date: Thu, 21 Apr 2016 22:22:33 -0400 Subject: [PATCH 02/22] Done --- .gitignore | 6 +- .idea/libraries/Dart_Packages.xml | 363 ++++++++++++++++++++++++++ .idea/runConfigurations/All_Tests.xml | 6 + .idea/vcs.xml | 6 + README.md | 15 ++ lib/angel_mustache.dart | 19 ++ pubspec.yaml | 11 + test/all_tests.dart | 27 ++ test/foo/bar.mustache | 1 + test/hello.mustache | 1 + 10 files changed, 453 insertions(+), 2 deletions(-) create mode 100644 .idea/libraries/Dart_Packages.xml create mode 100644 .idea/runConfigurations/All_Tests.xml create mode 100644 .idea/vcs.xml create mode 100644 lib/angel_mustache.dart create mode 100644 pubspec.yaml create mode 100644 test/all_tests.dart create mode 100644 test/foo/bar.mustache create mode 100644 test/hello.mustache diff --git a/.gitignore b/.gitignore index 7c280441..f47e7629 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ build/ **/packages/ # Files created by dart2js -# (Most Dart developers will use pub build to compile Dart, use/modify these +# (Most Dart developers will use pub build to compile Dart, use/modify these # rules if you intend to use dart2js directly # Convention is to use extension '.dart.js' for Dart compiled to Javascript to # differentiate from explicit Javascript files) @@ -22,6 +22,8 @@ build/ # Directory created by dartdoc doc/api/ -# Don't commit pubspec lock file +# Don't commit pubspec lock file # (Library packages only! Remove pattern if developing an application package) pubspec.lock + +.idea \ No newline at end of file diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml new file mode 100644 index 00000000..5b99bf65 --- /dev/null +++ b/.idea/libraries/Dart_Packages.xml @@ -0,0 +1,363 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/All_Tests.xml b/.idea/runConfigurations/All_Tests.xml new file mode 100644 index 00000000..a824b209 --- /dev/null +++ b/.idea/runConfigurations/All_Tests.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d2951a0b..8704f7d8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # angel_hbs Handlebars view generator for Angel. + +# Installation +In `pubspec.yaml`: + + dependencies: + angel_mustache: ">= 1.0.0-dev < 2.0.0" + +# Usage +``` +app.configure(mustache(new Directory('views'))); +``` + +``` +res.render('hello', {'name': 'world'}); +``` \ No newline at end of file diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart new file mode 100644 index 00000000..33caed50 --- /dev/null +++ b/lib/angel_mustache.dart @@ -0,0 +1,19 @@ +library angel_mustache; + +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:mustache4dart/mustache4dart.dart' show render; + +mustache(Directory viewsDirectory, {String fileExtension: '.mustache'}) { + return (Angel app) { + app.viewGenerator = (String name, [Map data]) async { + String viewPath = name + fileExtension; + File viewFile = new File.fromUri( + viewsDirectory.absolute.uri.resolve(viewPath)); + if (await viewFile.exists()) { + return render(await viewFile.readAsString(), data ?? {}); + } else throw new FileSystemException( + 'View "$name" was not found.', viewPath); + }; + }; +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..aa69ef87 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,11 @@ +name: angel_mustache +description: Mustache view generator for Angel. +author: thosakwe +homepage: https://github.com/angel-dart/angel_mustache +version: 1.0.0-dev +dependencies: + angel_framework: ">=0.0.0-dev < 0.1.0" + mustache4dart: ">= 1.0.0 < 2.0.0" +dev_dependencies: + http: ">= 0.11.3 < 0.12.0" + test: ">= 0.12.13 < 0.13.0" \ No newline at end of file diff --git a/test/all_tests.dart b/test/all_tests.dart new file mode 100644 index 00000000..2b87dcfb --- /dev/null +++ b/test/all_tests.dart @@ -0,0 +1,27 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mustache/angel_mustache.dart'; +import 'package:test/test.dart'; + +main() { + Angel angel = new Angel(); + angel.configure(mustache(new Directory('/test'))); + + test('can render templates', () async { + var hello = await angel.viewGenerator('hello', {'name': 'world'}); + var bar = await angel.viewGenerator('foo/bar', {'framework': 'angel'}); + + expect(hello, equals("Hello, world!")); + expect(bar, equals("angel_framework")); + }); + + test('throws if view is not found', () { + expect( + new Future(() async { + var fails = await angel.viewGenerator( + 'fail', {'this_should': 'fail'}); + print(fails); + }), throws); + }); +} \ No newline at end of file diff --git a/test/foo/bar.mustache b/test/foo/bar.mustache new file mode 100644 index 00000000..d71ee58f --- /dev/null +++ b/test/foo/bar.mustache @@ -0,0 +1 @@ +{{framework}}_framework \ No newline at end of file diff --git a/test/hello.mustache b/test/hello.mustache new file mode 100644 index 00000000..e7773335 --- /dev/null +++ b/test/hello.mustache @@ -0,0 +1 @@ +Hello, {{name}}! \ No newline at end of file From e1459c54f57943ecf6702ad5e9027713aaac4dd3 Mon Sep 17 00:00:00 2001 From: regiostech Date: Thu, 21 Apr 2016 22:22:49 -0400 Subject: [PATCH 03/22] Bye idea --- .idea/libraries/Dart_Packages.xml | 363 -------------------------- .idea/runConfigurations/All_Tests.xml | 6 - .idea/vcs.xml | 6 - 3 files changed, 375 deletions(-) delete mode 100644 .idea/libraries/Dart_Packages.xml delete mode 100644 .idea/runConfigurations/All_Tests.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml deleted file mode 100644 index 5b99bf65..00000000 --- a/.idea/libraries/Dart_Packages.xml +++ /dev/null @@ -1,363 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations/All_Tests.xml b/.idea/runConfigurations/All_Tests.xml deleted file mode 100644 index a824b209..00000000 --- a/.idea/runConfigurations/All_Tests.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 6af38e049439816fc9b35f72b51cbc6a65624756 Mon Sep 17 00:00:00 2001 From: regiostech Date: Mon, 2 May 2016 19:28:37 -0400 Subject: [PATCH 04/22] Async, partials --- README.md | 7 ++++++- lib/angel_mustache.dart | 22 +++++++++++++++++++--- pubspec.yaml | 2 +- test/all_tests.dart | 9 +++++++-- test/partials/name.mustache | 1 + test/with-partial.mustache | 1 + 6 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 test/partials/name.mustache create mode 100644 test/with-partial.mustache diff --git a/README.md b/README.md index 8704f7d8..26035bcd 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,9 @@ app.configure(mustache(new Directory('views'))); ``` res.render('hello', {'name': 'world'}); -``` \ No newline at end of file +``` + +# Options +- **partialsPath**: A path within the viewsDirectory to search for partials in. + Default is `./partials`. *Include the leading dot, please*. +- **fileExtension**: The file extension to search for. Default is `.mustache`. \ No newline at end of file diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index 33caed50..6688a2cd 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -4,14 +4,30 @@ import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:mustache4dart/mustache4dart.dart' show render; -mustache(Directory viewsDirectory, {String fileExtension: '.mustache'}) { - return (Angel app) { +mustache(Directory viewsDirectory, + {String fileExtension: '.mustache', String partialsPath: './partials'}) { + Directory partialsDirectory = new Directory.fromUri( + viewsDirectory.uri.resolve(partialsPath)); + return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { + var partialsProvider; + partialsProvider = (String name) { + String viewPath = name + fileExtension; + File viewFile = new File.fromUri( + partialsDirectory.absolute.uri.resolve(viewPath)); + if (viewFile.existsSync()) { + return render(viewFile.readAsStringSync(), data ?? {}, + partial: partialsProvider); + } else throw new FileSystemException( + 'View "$name" was not found.', viewPath); + }; + String viewPath = name + fileExtension; File viewFile = new File.fromUri( viewsDirectory.absolute.uri.resolve(viewPath)); if (await viewFile.exists()) { - return render(await viewFile.readAsString(), data ?? {}); + return render(await viewFile.readAsString(), data ?? {}, + partial: partialsProvider); } else throw new FileSystemException( 'View "$name" was not found.', viewPath); }; diff --git a/pubspec.yaml b/pubspec.yaml index aa69ef87..22265922 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: angel_mustache description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache -version: 1.0.0-dev +version: 1.0.1 dependencies: angel_framework: ">=0.0.0-dev < 0.1.0" mustache4dart: ">= 1.0.0 < 2.0.0" diff --git a/test/all_tests.dart b/test/all_tests.dart index 2b87dcfb..96619326 100644 --- a/test/all_tests.dart +++ b/test/all_tests.dart @@ -4,9 +4,9 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:angel_mustache/angel_mustache.dart'; import 'package:test/test.dart'; -main() { +main() async { Angel angel = new Angel(); - angel.configure(mustache(new Directory('/test'))); + await angel.configure(mustache(new Directory('/test'))); test('can render templates', () async { var hello = await angel.viewGenerator('hello', {'name': 'world'}); @@ -24,4 +24,9 @@ main() { print(fails); }), throws); }); + + test("partials", () async { + var withPartial = await angel.viewGenerator('with-partial'); + expect(withPartial, equals("Hello, world!")); + }); } \ No newline at end of file diff --git a/test/partials/name.mustache b/test/partials/name.mustache new file mode 100644 index 00000000..04fea064 --- /dev/null +++ b/test/partials/name.mustache @@ -0,0 +1 @@ +world \ No newline at end of file diff --git a/test/with-partial.mustache b/test/with-partial.mustache new file mode 100644 index 00000000..74c0e738 --- /dev/null +++ b/test/with-partial.mustache @@ -0,0 +1 @@ +Hello, {{> name}}! \ No newline at end of file From 8a1b2c0dac90301f2465dea4d3d9be62bfd1cfd6 Mon Sep 17 00:00:00 2001 From: regiostech Date: Tue, 21 Jun 2016 19:07:19 -0400 Subject: [PATCH 05/22] Updated Angel dep --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 22265922..3f0ad6fe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,9 +2,9 @@ name: angel_mustache description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache -version: 1.0.1 +version: 1.0.2 dependencies: - angel_framework: ">=0.0.0-dev < 0.1.0" + angel_framework: ">=1.0.0-dev < 2.0.0" mustache4dart: ">= 1.0.0 < 2.0.0" dev_dependencies: http: ">= 0.11.3 < 0.12.0" From 3fdb43ec50f6d491df9c76cea30679334553538c Mon Sep 17 00:00:00 2001 From: Cory Forward Date: Tue, 4 Apr 2017 17:05:16 -0600 Subject: [PATCH 06/22] Production Cache 1. View files are cached in production mode 2. Fixed test/all_tests.dart path issue --- lib/angel_mustache.dart | 27 +++++++--------- lib/src/cache.dart | 71 +++++++++++++++++++++++++++++++++++++++++ test/all_tests.dart | 2 +- 3 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 lib/src/cache.dart diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index 6688a2cd..d26d8db1 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -3,33 +3,28 @@ library angel_mustache; import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:mustache4dart/mustache4dart.dart' show render; +import 'package:angel_mustache/src/cache.dart'; mustache(Directory viewsDirectory, {String fileExtension: '.mustache', String partialsPath: './partials'}) { + Directory partialsDirectory = new Directory.fromUri( - viewsDirectory.uri.resolve(partialsPath)); + viewsDirectory.uri.resolve(partialsPath)); + + MustacheCacheController cache = new MustacheCacheController( + viewsDirectory, partialsDirectory, fileExtension); + return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { var partialsProvider; partialsProvider = (String name) { - String viewPath = name + fileExtension; - File viewFile = new File.fromUri( - partialsDirectory.absolute.uri.resolve(viewPath)); - if (viewFile.existsSync()) { - return render(viewFile.readAsStringSync(), data ?? {}, + String template = cache.get_partial(name, app); + return render(template, data ?? {}, partial: partialsProvider); - } else throw new FileSystemException( - 'View "$name" was not found.', viewPath); }; - String viewPath = name + fileExtension; - File viewFile = new File.fromUri( - viewsDirectory.absolute.uri.resolve(viewPath)); - if (await viewFile.exists()) { - return render(await viewFile.readAsString(), data ?? {}, - partial: partialsProvider); - } else throw new FileSystemException( - 'View "$name" was not found.', viewPath); + String viewTemplate = await cache.get_view(name, app); + return render(viewTemplate, data ?? {}, partial: partialsProvider); }; }; } \ No newline at end of file diff --git a/lib/src/cache.dart b/lib/src/cache.dart new file mode 100644 index 00000000..7d58336d --- /dev/null +++ b/lib/src/cache.dart @@ -0,0 +1,71 @@ +import 'dart:io'; +import 'dart:collection'; + +import 'package:angel_framework/angel_framework.dart'; + +class MustacheCacheController { + HashMap viewCache = new HashMap(); + HashMap partialCache = new HashMap(); + + /** + * The directory of the mustache views + */ + Directory viewDirectory; + + /** + * The directory of mustache partials + */ + Directory partialsDirectory; + + /** + * Default file extension associated with a view file + */ + String fileExtension; + + MustacheCacheController( + [this.viewDirectory, this.partialsDirectory, this.fileExtension]); + + get_view(String viewName, Angel app) async { + if (app.isProduction) { + if (viewCache.containsKey(viewName)) { + return viewCache[viewName]; + } + } + + String viewPath = viewName + this.fileExtension; + File viewFile = + new File.fromUri(this.viewDirectory.absolute.uri.resolve(viewPath)); + + if (await viewFile.exists()) { + String viewTemplate = await viewFile.readAsString(); + if (app.isProduction) { + this.viewCache[viewName] = viewTemplate; + } + return viewTemplate; + } else + throw new FileSystemException( + 'View "$viewName" was not found.', viewPath); + } + + get_partial(String partialName, Angel app) { + if (app.isProduction) { + if (partialCache.containsKey(partialName)) { + return partialCache[partialName]; + } + } + + String viewPath = partialName + this.fileExtension; + File viewFile = + new File.fromUri(partialsDirectory.absolute.uri.resolve(viewPath)); + + if (viewFile.existsSync()) { + String partialTemplate = viewFile.readAsStringSync(); + if (app.isProduction) { + this.partialCache[partialName] = partialTemplate; + } + return partialTemplate; + } else + throw new FileSystemException( + 'View "$partialName" was not found.', viewPath); + } +} diff --git a/test/all_tests.dart b/test/all_tests.dart index 96619326..f5f77a35 100644 --- a/test/all_tests.dart +++ b/test/all_tests.dart @@ -6,7 +6,7 @@ import 'package:test/test.dart'; main() async { Angel angel = new Angel(); - await angel.configure(mustache(new Directory('/test'))); + await angel.configure(mustache(new Directory('./test'))); test('can render templates', () async { var hello = await angel.viewGenerator('hello', {'name': 'world'}); From 392aa993ffbb731ac182a297cff701e2cb41cbe8 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 14:37:26 -0400 Subject: [PATCH 07/22] Update README.md --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 26035bcd..2b666c8f 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,27 @@ -# angel_hbs -Handlebars view generator for Angel. +# mustache +Mustache (Handlebars) view generator for Angel. # Installation In `pubspec.yaml`: - dependencies: - angel_mustache: ">= 1.0.0-dev < 2.0.0" +```yaml +dependencies: + angel_mustache: ^1.0.0 +``` + +If your project imports [`package:angel_common`](https://github.com/angel-dart/common), +then this is already installed. # Usage ``` -app.configure(mustache(new Directory('views'))); -``` +// Run the plug-in +await app.configure(mustache(new Directory('views'))); -``` +// Render `hello.mustache` res.render('hello', {'name': 'world'}); ``` # Options - **partialsPath**: A path within the viewsDirectory to search for partials in. Default is `./partials`. *Include the leading dot, please*. -- **fileExtension**: The file extension to search for. Default is `.mustache`. \ No newline at end of file +- **fileExtension**: The file extension to search for. Default is `.mustache`. From 6a3f439406b687c6df5eb742e5453eff6f015d41 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 14:37:47 -0400 Subject: [PATCH 08/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b666c8f..108e25a6 100644 --- a/README.md +++ b/README.md @@ -23,5 +23,5 @@ res.render('hello', {'name': 'world'}); # Options - **partialsPath**: A path within the viewsDirectory to search for partials in. - Default is `./partials`. *Include the leading dot, please*. + Default is `./partials`. - **fileExtension**: The file extension to search for. Default is `.mustache`. From 2ab4b5ffd4dcbe287740581472acaa5d793660d0 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 14:38:35 -0400 Subject: [PATCH 09/22] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 108e25a6..69bc5c09 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # mustache -Mustache (Handlebars) view generator for Angel. +Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel) +web server framework. # Installation In `pubspec.yaml`: @@ -13,7 +14,7 @@ If your project imports [`package:angel_common`](https://github.com/angel-dart/c then this is already installed. # Usage -``` +```dart // Run the plug-in await app.configure(mustache(new Directory('views'))); From a3e5eab1a2534277defa6061c73542734c1c1a49 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 14:38:59 -0400 Subject: [PATCH 10/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 69bc5c09..8a7bf00b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ then this is already installed. await app.configure(mustache(new Directory('views'))); // Render `hello.mustache` -res.render('hello', {'name': 'world'}); +await res.render('hello', {'name': 'world'}); ``` # Options From a6ae7844f7503bfe919769f68901406f81084914 Mon Sep 17 00:00:00 2001 From: Cory Forward Date: Wed, 5 Apr 2017 14:38:03 -0600 Subject: [PATCH 11/22] MustacheContext and fixed partials resolving 1. Partials are now resolved with the 'path' package 2. MustacheContext is an object that holds all folder contextual information for views and partials, and cleanly isolates all file resolving in one location --- lib/angel_mustache.dart | 20 ++++++++-------- lib/src/cache.dart | 43 +++++++++++++---------------------- lib/src/mustache_context.dart | 22 ++++++++++++++++++ 3 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 lib/src/mustache_context.dart diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index d26d8db1..18b5c486 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -4,27 +4,29 @@ import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:mustache4dart/mustache4dart.dart' show render; import 'package:angel_mustache/src/cache.dart'; +import 'package:angel_mustache/src/mustache_context.dart'; +import 'package:path/path.dart' as path; mustache(Directory viewsDirectory, {String fileExtension: '.mustache', String partialsPath: './partials'}) { + Directory partialsDirectory = + new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath)); - Directory partialsDirectory = new Directory.fromUri( - viewsDirectory.uri.resolve(partialsPath)); - - MustacheCacheController cache = new MustacheCacheController( - viewsDirectory, partialsDirectory, fileExtension); + MustacheContext context = + new MustacheContext(viewsDirectory, partialsDirectory, fileExtension); + + MustacheCacheController cache = new MustacheCacheController(context); return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { var partialsProvider; partialsProvider = (String name) { - String template = cache.get_partial(name, app); - return render(template, data ?? {}, - partial: partialsProvider); + String template = cache.get_partial(name, app); + return render(template, data ?? {}, partial: partialsProvider); }; String viewTemplate = await cache.get_view(name, app); return render(viewTemplate, data ?? {}, partial: partialsProvider); }; }; -} \ No newline at end of file +} diff --git a/lib/src/cache.dart b/lib/src/cache.dart index 7d58336d..debafc81 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -1,29 +1,22 @@ import 'dart:io'; import 'dart:collection'; +import 'package:path/path.dart' as path; import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mustache/src/mustache_context.dart'; class MustacheCacheController { + + /** + * The context for which views and partials are + * served from. + */ + MustacheContext context; + HashMap viewCache = new HashMap(); HashMap partialCache = new HashMap(); - /** - * The directory of the mustache views - */ - Directory viewDirectory; - - /** - * The directory of mustache partials - */ - Directory partialsDirectory; - - /** - * Default file extension associated with a view file - */ - String fileExtension; - - MustacheCacheController( - [this.viewDirectory, this.partialsDirectory, this.fileExtension]); + MustacheCacheController([this.context]); get_view(String viewName, Angel app) async { if (app.isProduction) { @@ -32,9 +25,7 @@ class MustacheCacheController { } } - String viewPath = viewName + this.fileExtension; - File viewFile = - new File.fromUri(this.viewDirectory.absolute.uri.resolve(viewPath)); + File viewFile = context.resolveView(viewName); if (await viewFile.exists()) { String viewTemplate = await viewFile.readAsString(); @@ -44,7 +35,7 @@ class MustacheCacheController { return viewTemplate; } else throw new FileSystemException( - 'View "$viewName" was not found.', viewPath); + 'View "$viewName" was not found.', viewFile.path); } get_partial(String partialName, Angel app) { @@ -54,18 +45,16 @@ class MustacheCacheController { } } - String viewPath = partialName + this.fileExtension; - File viewFile = - new File.fromUri(partialsDirectory.absolute.uri.resolve(viewPath)); + File partialFile = context.resolvePartial(partialName); - if (viewFile.existsSync()) { - String partialTemplate = viewFile.readAsStringSync(); + if (partialFile.existsSync()) { + String partialTemplate = partialFile.readAsStringSync(); if (app.isProduction) { this.partialCache[partialName] = partialTemplate; } return partialTemplate; } else throw new FileSystemException( - 'View "$partialName" was not found.', viewPath); + 'View "$partialName" was not found.', partialFile.path); } } diff --git a/lib/src/mustache_context.dart b/lib/src/mustache_context.dart new file mode 100644 index 00000000..d872e858 --- /dev/null +++ b/lib/src/mustache_context.dart @@ -0,0 +1,22 @@ +import 'dart:io'; +import 'package:path/path.dart' as path; + +class MustacheContext { + Directory viewDirectory; + + Directory partialDirectory; + + String extension; + + MustacheContext([this.viewDirectory, this.partialDirectory, this.extension]); + + File resolveView(String viewName) { + return new File.fromUri( + viewDirectory.uri.resolve('${viewName}${extension}')); + } + + File resolvePartial(String partialName) { + return new File.fromUri( + partialDirectory.uri.resolve('${partialName}${extension}')); + } +} From 9f5a2afecd476e4755009660da3de5feea92d1d5 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:41:09 -0400 Subject: [PATCH 12/22] Create .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..3939d628 --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: dart From 573fa9a969d94c1e2bcb3c888b684a709150728d Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:41:40 -0400 Subject: [PATCH 13/22] Rename all_tests.dart to all_test.dart --- test/{all_tests.dart => all_test.dart} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/{all_tests.dart => all_test.dart} (99%) diff --git a/test/all_tests.dart b/test/all_test.dart similarity index 99% rename from test/all_tests.dart rename to test/all_test.dart index f5f77a35..532c70cd 100644 --- a/test/all_tests.dart +++ b/test/all_test.dart @@ -29,4 +29,4 @@ main() async { var withPartial = await angel.viewGenerator('with-partial'); expect(withPartial, equals("Hello, world!")); }); -} \ No newline at end of file +} From 45bc3aba22f433b739a46e0e16cb684ea455f82a Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:43:15 -0400 Subject: [PATCH 14/22] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 8a7bf00b..4291bef6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,12 @@ # mustache +[![version 1.0.1](https://img.shields.io/badge/pub-1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mustache) +[![build status](https://travis-ci.org/angel-dart/mustache.svg?branch=master)](https://travis-ci.org/angel-dart/mustache) + Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel) web server framework. +Thanks so much @c4ward for his help with bringing this project to life! + # Installation In `pubspec.yaml`: From 15e7fa7eef41a86958a28b5a4fedee68b16b53c0 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:43:38 -0400 Subject: [PATCH 15/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4291bef6..4068115c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel) web server framework. -Thanks so much @c4ward for his help with bringing this project to life! +Thanks so much @c4wrd for his help with bringing this project to life! # Installation In `pubspec.yaml`: From 64ef2ad81a6b8718bf20f522481612a09118333f Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:43:49 -0400 Subject: [PATCH 16/22] Update pubspec.yaml --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 3f0ad6fe..e2f3c084 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,10 +2,10 @@ name: angel_mustache description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache -version: 1.0.2 +version: 1.0.3 dependencies: angel_framework: ">=1.0.0-dev < 2.0.0" mustache4dart: ">= 1.0.0 < 2.0.0" dev_dependencies: http: ">= 0.11.3 < 0.12.0" - test: ">= 0.12.13 < 0.13.0" \ No newline at end of file + test: ">= 0.12.13 < 0.13.0" From 23711fc1e4bd112aed51e2b9214ac0053fb93b60 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:44:02 -0400 Subject: [PATCH 17/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4068115c..489bc49d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # mustache -[![version 1.0.1](https://img.shields.io/badge/pub-1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mustache) +[![version 1.0.3](https://img.shields.io/badge/pub-1.0.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mustache) [![build status](https://travis-ci.org/angel-dart/mustache.svg?branch=master)](https://travis-ci.org/angel-dart/mustache) Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel) From 09107d9e5b7261eece882b283fa7d5bdf7604b66 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 5 Apr 2017 18:46:38 -0400 Subject: [PATCH 18/22] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 489bc49d..a8ac83b5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # mustache +[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/angel_dart/discussion) [![version 1.0.3](https://img.shields.io/badge/pub-1.0.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mustache) [![build status](https://travis-ci.org/angel-dart/mustache.svg?branch=master)](https://travis-ci.org/angel-dart/mustache) From a672d0df55ec95a98336fa4ed98f1cca9ceda34d Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 18 Nov 2017 13:39:10 -0500 Subject: [PATCH 19/22] 1.1.0 --- README.md | 19 ++++++++++--------- analysis_options.yaml | 2 ++ lib/angel_mustache.dart | 20 ++++++++++---------- lib/src/cache.dart | 15 +++++++-------- lib/src/mustache_context.dart | 8 +++----- pubspec.yaml | 5 +++-- test/all_test.dart | 14 ++++++-------- 7 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 analysis_options.yaml diff --git a/README.md b/README.md index a8ac83b5..afe4027b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # mustache [![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/angel_dart/discussion) -[![version 1.0.3](https://img.shields.io/badge/pub-1.0.3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_mustache) +[![version](https://img.shields.io/pub/v/angel_mustache.svg)](https://pub.dartlang.org/packages/angel_mustache) [![build status](https://travis-ci.org/angel-dart/mustache.svg?branch=master)](https://travis-ci.org/angel-dart/mustache) Mustache (Handlebars) view generator for the [Angel](https://github.com/angel-dart/angel) @@ -13,19 +13,20 @@ In `pubspec.yaml`: ```yaml dependencies: - angel_mustache: ^1.0.0 + angel_mustache: ^1.1.0 ``` -If your project imports [`package:angel_common`](https://github.com/angel-dart/common), -then this is already installed. - # Usage ```dart -// Run the plug-in -await app.configure(mustache(new Directory('views'))); +const FileSystem fs = const LocalFileSystem(); -// Render `hello.mustache` -await res.render('hello', {'name': 'world'}); +configureServer(Angel app) async { + // Run the plug-in + await app.configure(mustache(fs.directory('views'))); + + // Render `hello.mustache` + await res.render('hello', {'name': 'world'}); +} ``` # Options diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..518eb901 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,2 @@ +analyzer: + strong-mode: true \ No newline at end of file diff --git a/lib/angel_mustache.dart b/lib/angel_mustache.dart index 18b5c486..ce758fa2 100644 --- a/lib/angel_mustache.dart +++ b/lib/angel_mustache.dart @@ -1,32 +1,32 @@ library angel_mustache; -import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; +import 'package:file/file.dart'; import 'package:mustache4dart/mustache4dart.dart' show render; -import 'package:angel_mustache/src/cache.dart'; -import 'package:angel_mustache/src/mustache_context.dart'; -import 'package:path/path.dart' as path; +import 'package:path/path.dart' as p; +import 'src/cache.dart'; +import 'src/mustache_context.dart'; mustache(Directory viewsDirectory, {String fileExtension: '.mustache', String partialsPath: './partials'}) { - Directory partialsDirectory = - new Directory(path.join(path.fromUri(viewsDirectory.uri), partialsPath)); + Directory partialsDirectory = viewsDirectory.fileSystem + .directory(p.join(p.fromUri(viewsDirectory.uri), partialsPath)); MustacheContext context = new MustacheContext(viewsDirectory, partialsDirectory, fileExtension); - MustacheCacheController cache = new MustacheCacheController(context); + MustacheViewCache cache = new MustacheViewCache(context); return (Angel app) async { app.viewGenerator = (String name, [Map data]) async { var partialsProvider; partialsProvider = (String name) { - String template = cache.get_partial(name, app); + String template = cache.getPartialSync(name, app); return render(template, data ?? {}, partial: partialsProvider); }; - String viewTemplate = await cache.get_view(name, app); - return render(viewTemplate, data ?? {}, partial: partialsProvider); + String viewTemplate = await cache.getView(name, app); + return await render(viewTemplate, data ?? {}, partial: partialsProvider); }; }; } diff --git a/lib/src/cache.dart b/lib/src/cache.dart index debafc81..6c8f92bd 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -1,12 +1,11 @@ -import 'dart:io'; +import 'dart:async'; import 'dart:collection'; -import 'package:path/path.dart' as path; +import 'package:file/file.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_mustache/src/mustache_context.dart'; -class MustacheCacheController { - +class MustacheViewCache { /** * The context for which views and partials are * served from. @@ -16,9 +15,9 @@ class MustacheCacheController { HashMap viewCache = new HashMap(); HashMap partialCache = new HashMap(); - MustacheCacheController([this.context]); + MustacheViewCache([this.context]); - get_view(String viewName, Angel app) async { + Future getView(String viewName, Angel app) async { if (app.isProduction) { if (viewCache.containsKey(viewName)) { return viewCache[viewName]; @@ -27,7 +26,7 @@ class MustacheCacheController { File viewFile = context.resolveView(viewName); - if (await viewFile.exists()) { + if (viewFile.existsSync()) { String viewTemplate = await viewFile.readAsString(); if (app.isProduction) { this.viewCache[viewName] = viewTemplate; @@ -38,7 +37,7 @@ class MustacheCacheController { 'View "$viewName" was not found.', viewFile.path); } - get_partial(String partialName, Angel app) { + String getPartialSync(String partialName, Angel app) { if (app.isProduction) { if (partialCache.containsKey(partialName)) { return partialCache[partialName]; diff --git a/lib/src/mustache_context.dart b/lib/src/mustache_context.dart index d872e858..9461f82e 100644 --- a/lib/src/mustache_context.dart +++ b/lib/src/mustache_context.dart @@ -1,4 +1,4 @@ -import 'dart:io'; +import 'package:file/file.dart'; import 'package:path/path.dart' as path; class MustacheContext { @@ -11,12 +11,10 @@ class MustacheContext { MustacheContext([this.viewDirectory, this.partialDirectory, this.extension]); File resolveView(String viewName) { - return new File.fromUri( - viewDirectory.uri.resolve('${viewName}${extension}')); + return viewDirectory.childFile('${viewName}${extension}'); } File resolvePartial(String partialName) { - return new File.fromUri( - partialDirectory.uri.resolve('${partialName}${extension}')); + return partialDirectory.childFile('${partialName}${extension}'); } } diff --git a/pubspec.yaml b/pubspec.yaml index e2f3c084..9ea234a2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,9 +2,10 @@ name: angel_mustache description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache -version: 1.0.3 +version: 1.1.0 dependencies: - angel_framework: ">=1.0.0-dev < 2.0.0" + angel_framework: "^1.1.0-alpha" + file: ^2.0.0 mustache4dart: ">= 1.0.0 < 2.0.0" dev_dependencies: http: ">= 0.11.3 < 0.12.0" diff --git a/test/all_test.dart b/test/all_test.dart index 532c70cd..cc5c859f 100644 --- a/test/all_test.dart +++ b/test/all_test.dart @@ -1,12 +1,12 @@ import 'dart:async'; -import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_mustache/angel_mustache.dart'; +import 'package:file/local.dart'; import 'package:test/test.dart'; main() async { Angel angel = new Angel(); - await angel.configure(mustache(new Directory('./test'))); + await angel.configure(mustache(const LocalFileSystem().directory('./test'))); test('can render templates', () async { var hello = await angel.viewGenerator('hello', {'name': 'world'}); @@ -17,12 +17,10 @@ main() async { }); test('throws if view is not found', () { - expect( - new Future(() async { - var fails = await angel.viewGenerator( - 'fail', {'this_should': 'fail'}); - print(fails); - }), throws); + expect(new Future(() async { + var fails = await angel.viewGenerator('fail', {'this_should': 'fail'}); + print(fails); + }), throws); }); test("partials", () async { From 4bc0e7c9cd3e883c929c57a589fb63f45c3a3dcc Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 18 Nov 2017 13:39:52 -0500 Subject: [PATCH 20/22] env constraint --- mustache.iml | 14 ++++++++++++++ pubspec.yaml | 2 ++ 2 files changed, 16 insertions(+) create mode 100644 mustache.iml diff --git a/mustache.iml b/mustache.iml new file mode 100644 index 00000000..5a5ced28 --- /dev/null +++ b/mustache.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 9ea234a2..c4b417d7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,6 +3,8 @@ description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache version: 1.1.0 +environment: + sdk: ">=1.19.0" dependencies: angel_framework: "^1.1.0-alpha" file: ^2.0.0 From b5d83e0a218e08b03246616acd967b317b274df0 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 8 Nov 2018 10:56:00 -0500 Subject: [PATCH 21/22] example --- .gitignore | 3 ++- CHANGELOG.md | 2 ++ README.md | 2 +- example/main.dart | 16 ++++++++++++++++ pubspec.yaml | 14 +++++++------- 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 example/main.dart diff --git a/.gitignore b/.gitignore index f47e7629..670f6708 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ doc/api/ # (Library packages only! Remove pattern if developing an application package) pubspec.lock -.idea \ No newline at end of file +.idea +.dart_tool \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..cde505dc --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# 2.0.0 +* Angel 2 and Dart 2 support. \ No newline at end of file diff --git a/README.md b/README.md index afe4027b..8cb38563 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ In `pubspec.yaml`: ```yaml dependencies: - angel_mustache: ^1.1.0 + angel_mustache: ^2.0.0 ``` # Usage diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 00000000..30467e60 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,16 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mustache/angel_mustache.dart'; +import 'package:file/file.dart'; +import 'package:file/local.dart'; + +const FileSystem fs = const LocalFileSystem(); + +configureServer(Angel app) async { + // Run the plug-in + await app.configure(mustache(fs.directory('views'))); + + // Render `hello.mustache` + app.get('/', (req, res) async { + await res.render('hello', {'name': 'world'}); + }); +} diff --git a/pubspec.yaml b/pubspec.yaml index c4b417d7..45c95a93 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,13 +2,13 @@ name: angel_mustache description: Mustache view generator for Angel. author: thosakwe homepage: https://github.com/angel-dart/angel_mustache -version: 1.1.0 +version: 2.0.0 environment: - sdk: ">=1.19.0" + sdk: ">=2.0.0-dev <3.0.0" dependencies: - angel_framework: "^1.1.0-alpha" - file: ^2.0.0 - mustache4dart: ">= 1.0.0 < 2.0.0" + angel_framework: ^2.0.0-alpha + file: ^5.0.0 + mustache4dart: ^3.0.0-dev dev_dependencies: - http: ">= 0.11.3 < 0.12.0" - test: ">= 0.12.13 < 0.13.0" + http: + test: From d0335a925d7199f3a5988ca10da36fee38ce0c43 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 8 Nov 2018 10:56:40 -0500 Subject: [PATCH 22/22] deps --- pubspec.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/pubspec.yaml b/pubspec.yaml index 45c95a93..430cb43e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,6 +9,7 @@ dependencies: angel_framework: ^2.0.0-alpha file: ^5.0.0 mustache4dart: ^3.0.0-dev + path: ^1.0.0 dev_dependencies: http: test: