From 08cad198e9ad26665e41016d97e1b566cddc6e24 Mon Sep 17 00:00:00 2001 From: timrijckaert Date: Thu, 28 Nov 2019 18:22:24 +0100 Subject: [PATCH 1/5] [sealed_class] Add better description [sealed_class_generator] Format some files with DartFMT --- sealed_class/pubspec.yaml | 2 +- sealed_class/test/sealed_class_test.dart | 6 ------ sealed_class_generator/lib/src/printer.dart | 4 ++-- sealed_class_generator/lib/src/transformer.dart | 5 +++-- sealed_class_generator/lib/src/visitor.dart | 2 +- 5 files changed, 7 insertions(+), 12 deletions(-) delete mode 100755 sealed_class/test/sealed_class_test.dart diff --git a/sealed_class/pubspec.yaml b/sealed_class/pubspec.yaml index c4d00dd..8d9491e 100755 --- a/sealed_class/pubspec.yaml +++ b/sealed_class/pubspec.yaml @@ -1,5 +1,5 @@ name: sealed_class -description: Make a sealed class hierarchy +description: The annotation necessary for a sealed class hierarchy version: 1.0.0 author: Tim Rijckaert homepage: https://github.com/vrtdev/sealed_class_generator diff --git a/sealed_class/test/sealed_class_test.dart b/sealed_class/test/sealed_class_test.dart deleted file mode 100755 index 64dfa6c..0000000 --- a/sealed_class/test/sealed_class_test.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:test/test.dart'; - -import 'package:sealed_class/sealed_class.dart'; - -void main() { -} diff --git a/sealed_class_generator/lib/src/printer.dart b/sealed_class_generator/lib/src/printer.dart index 8744e59..b4784ac 100644 --- a/sealed_class_generator/lib/src/printer.dart +++ b/sealed_class_generator/lib/src/printer.dart @@ -67,8 +67,8 @@ class Printer { .toString(); } - Iterable generateMixinDeclarations( - final String className, final Iterable parseData) sync* { + Iterable generateMixinDeclarations(final String className, + final Iterable parseData) sync* { for (final data in parseData) { final output = StringBuffer() ..writeln( diff --git a/sealed_class_generator/lib/src/transformer.dart b/sealed_class_generator/lib/src/transformer.dart index d7d8211..d7fa7bb 100644 --- a/sealed_class_generator/lib/src/transformer.dart +++ b/sealed_class_generator/lib/src/transformer.dart @@ -3,7 +3,7 @@ import 'common.dart'; class GeneratedCodeData { static const continuedMethodName = "continuation"; static const mapMethodName = "join"; - + final String generatedClassName; final String continuedFunction; @@ -25,6 +25,7 @@ class GeneratedCodeData { class TypeParameterTransformer { TypeParameterTransformer._(); - static List toGeneratedCodeData(final List typeParameters) => + static List toGeneratedCodeData( + final List typeParameters) => typeParameters.map((it) => GeneratedCodeData(it)).toList(growable: false); } diff --git a/sealed_class_generator/lib/src/visitor.dart b/sealed_class_generator/lib/src/visitor.dart index 145c314..f5b7d37 100644 --- a/sealed_class_generator/lib/src/visitor.dart +++ b/sealed_class_generator/lib/src/visitor.dart @@ -2,7 +2,7 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/visitor.dart'; class SealedClassVisitor extends SimpleElementVisitor { - String className = null; + String className; @override visitConstructorElement(ConstructorElement element) => From 2fadf66da7eaaab07fc9d204c7b14294fa88a4c7 Mon Sep 17 00:00:00 2001 From: timrijckaert Date: Thu, 28 Nov 2019 18:27:48 +0100 Subject: [PATCH 2/5] [sealed_class] Add better description --- sealed_class/pubspec.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sealed_class/pubspec.yaml b/sealed_class/pubspec.yaml index 8d9491e..864cffd 100755 --- a/sealed_class/pubspec.yaml +++ b/sealed_class/pubspec.yaml @@ -1,5 +1,7 @@ name: sealed_class -description: The annotation necessary for a sealed class hierarchy +description: > + Annotation needed to create a sealed hierarchy in Dart. + This library is the runtime dependency. version: 1.0.0 author: Tim Rijckaert homepage: https://github.com/vrtdev/sealed_class_generator From ce33872faadfabf22fe06586d7d4a77045868735 Mon Sep 17 00:00:00 2001 From: timrijckaert Date: Thu, 28 Nov 2019 18:32:13 +0100 Subject: [PATCH 3/5] Don't duplicate the README.md to each sub folder --- sealed_class/.gitignore | 3 + sealed_class/LICENSE | 21 ------ sealed_class/README.md | 114 ------------------------------ sealed_class_generator/.gitignore | 3 + sealed_class_generator/LICENSE | 21 ------ sealed_class_generator/README.md | 114 ------------------------------ 6 files changed, 6 insertions(+), 270 deletions(-) create mode 100644 sealed_class/.gitignore delete mode 100755 sealed_class/LICENSE delete mode 100755 sealed_class/README.md create mode 100644 sealed_class_generator/.gitignore delete mode 100755 sealed_class_generator/LICENSE delete mode 100755 sealed_class_generator/README.md diff --git a/sealed_class/.gitignore b/sealed_class/.gitignore new file mode 100644 index 0000000..38df3a7 --- /dev/null +++ b/sealed_class/.gitignore @@ -0,0 +1,3 @@ +LICENSE +CHANGELOG.md +README.md diff --git a/sealed_class/LICENSE b/sealed_class/LICENSE deleted file mode 100755 index 3a0714b..0000000 --- a/sealed_class/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2019 Tim Rijckaert - -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/sealed_class/README.md b/sealed_class/README.md deleted file mode 100755 index 948cdf1..0000000 --- a/sealed_class/README.md +++ /dev/null @@ -1,114 +0,0 @@ -# Sealed Class Generator - -sealed_class: [![pub package](https://img.shields.io/pub/v/sealed_class.svg)](https://pub.dartlang.org/packages/sealed_class) -sealed_class_generator: [![pub package](https://img.shields.io/pub/v/sealed_class_generator.svg)](https://pub.dartlang.org/packages/sealed_class_generator) - -Fix Dart's lack of a sealed class. -Inspired by [Kotlin's implementation](https://kotlinlang.org/docs/reference/sealed-classes.html) and by [Sealed Unions](https://pub.dev/packages/sealed_unions). -With this lib you can generate an unlimited amount of sealed subclasses. - -## Installation - -```yaml -dependencies: - sealed_class: 1.0.0 - -dev_dependencies: - sealed_class_generator: 1.0.0 - build_runner: 1.0.0 -``` - -## How to use: - -A complete example is found in the [example](https://github.com/timrijckaert/sealed_class_generator/tree/master/example). - -Define your `Sealed` class by annotating it with `@Sealed()` - -```dart -part 'yourfilename.g.dart'; - -@Sealed() -abstract class Result{} -``` - -Add the subclasses to the annotation. - -```dart -part 'yourfilename.g.dart'; - -@Sealed([Failure, Success]) -abstract class Result{} -``` - -Let your subclasses mixin the generated file. -Let your sealed class implement the generated mixin. - -```dart -@Sealed([Failure, Success]) -abstract class Result implements $Result {} - -class Failure with $Failure implements Result { - final String errorMessage; - - Failure(this.errorMessage); -} - -class Success with $Success implements Result { - final String data; - - Success(this.data); -} -``` - -The complete file should now look like this: - -````dart -import 'package:sealed_class/sealed_class.dart'; - -part 'yourfilename.g.dart'; - -@Sealed([Failure, Success]) -abstract class Result implements $Result {} - -class Failure with $Failure implements Result { - final String errorMessage; - - Failure(this.errorMessage); -} - -class Success with $Success implements Result { - final String data; - - Success(this.data); -} -```` - -To make it compile run the following command in the terminal: - -```shell script -flutter packages pub run build_runner build --delete-conflicting-outputs -``` - -You can do: - -```dart -void main() { - final someResult = Success("Woohoo Sealed classes in Dart"); - someResult.continued( - (failure) { - print("Oh no it failed with errorMessage: ${failure.errorMessage}"); - }, - (success) { - print(success.data); - }, - ); - - //Or if you want to reduce it to another value use `join` - final someOtherResult = Failure("Some Error Message"); - final mapResult = someOtherResult.join( - (failure) => failure.errorMessage, - (success) => success.data, - ); - print(mapResult); -} -``` diff --git a/sealed_class_generator/.gitignore b/sealed_class_generator/.gitignore new file mode 100644 index 0000000..38df3a7 --- /dev/null +++ b/sealed_class_generator/.gitignore @@ -0,0 +1,3 @@ +LICENSE +CHANGELOG.md +README.md diff --git a/sealed_class_generator/LICENSE b/sealed_class_generator/LICENSE deleted file mode 100755 index 3a0714b..0000000 --- a/sealed_class_generator/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2019 Tim Rijckaert - -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/sealed_class_generator/README.md b/sealed_class_generator/README.md deleted file mode 100755 index 948cdf1..0000000 --- a/sealed_class_generator/README.md +++ /dev/null @@ -1,114 +0,0 @@ -# Sealed Class Generator - -sealed_class: [![pub package](https://img.shields.io/pub/v/sealed_class.svg)](https://pub.dartlang.org/packages/sealed_class) -sealed_class_generator: [![pub package](https://img.shields.io/pub/v/sealed_class_generator.svg)](https://pub.dartlang.org/packages/sealed_class_generator) - -Fix Dart's lack of a sealed class. -Inspired by [Kotlin's implementation](https://kotlinlang.org/docs/reference/sealed-classes.html) and by [Sealed Unions](https://pub.dev/packages/sealed_unions). -With this lib you can generate an unlimited amount of sealed subclasses. - -## Installation - -```yaml -dependencies: - sealed_class: 1.0.0 - -dev_dependencies: - sealed_class_generator: 1.0.0 - build_runner: 1.0.0 -``` - -## How to use: - -A complete example is found in the [example](https://github.com/timrijckaert/sealed_class_generator/tree/master/example). - -Define your `Sealed` class by annotating it with `@Sealed()` - -```dart -part 'yourfilename.g.dart'; - -@Sealed() -abstract class Result{} -``` - -Add the subclasses to the annotation. - -```dart -part 'yourfilename.g.dart'; - -@Sealed([Failure, Success]) -abstract class Result{} -``` - -Let your subclasses mixin the generated file. -Let your sealed class implement the generated mixin. - -```dart -@Sealed([Failure, Success]) -abstract class Result implements $Result {} - -class Failure with $Failure implements Result { - final String errorMessage; - - Failure(this.errorMessage); -} - -class Success with $Success implements Result { - final String data; - - Success(this.data); -} -``` - -The complete file should now look like this: - -````dart -import 'package:sealed_class/sealed_class.dart'; - -part 'yourfilename.g.dart'; - -@Sealed([Failure, Success]) -abstract class Result implements $Result {} - -class Failure with $Failure implements Result { - final String errorMessage; - - Failure(this.errorMessage); -} - -class Success with $Success implements Result { - final String data; - - Success(this.data); -} -```` - -To make it compile run the following command in the terminal: - -```shell script -flutter packages pub run build_runner build --delete-conflicting-outputs -``` - -You can do: - -```dart -void main() { - final someResult = Success("Woohoo Sealed classes in Dart"); - someResult.continued( - (failure) { - print("Oh no it failed with errorMessage: ${failure.errorMessage}"); - }, - (success) { - print(success.data); - }, - ); - - //Or if you want to reduce it to another value use `join` - final someOtherResult = Failure("Some Error Message"); - final mapResult = someOtherResult.join( - (failure) => failure.errorMessage, - (success) => success.data, - ); - print(mapResult); -} -``` From 6f10650fad6db5af6f823bae0e46a84c7fbb8116 Mon Sep 17 00:00:00 2001 From: timrijckaert Date: Thu, 28 Nov 2019 18:38:52 +0100 Subject: [PATCH 4/5] Don't check in the example multiple times --- sealed_class/.gitignore | 1 + sealed_class_generator/.gitignore | 1 + 2 files changed, 2 insertions(+) diff --git a/sealed_class/.gitignore b/sealed_class/.gitignore index 38df3a7..a5653af 100644 --- a/sealed_class/.gitignore +++ b/sealed_class/.gitignore @@ -1,3 +1,4 @@ LICENSE CHANGELOG.md README.md +example/ diff --git a/sealed_class_generator/.gitignore b/sealed_class_generator/.gitignore index 38df3a7..a5653af 100644 --- a/sealed_class_generator/.gitignore +++ b/sealed_class_generator/.gitignore @@ -1,3 +1,4 @@ LICENSE CHANGELOG.md README.md +example/ From 917613e261504ed2461896288d9f4648dab77a83 Mon Sep 17 00:00:00 2001 From: timrijckaert Date: Thu, 28 Nov 2019 20:18:02 +0100 Subject: [PATCH 5/5] Add convenience script to up the dependency --- tool/prepare_push_release.dart | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tool/prepare_push_release.dart diff --git a/tool/prepare_push_release.dart b/tool/prepare_push_release.dart new file mode 100644 index 0000000..67e0be4 --- /dev/null +++ b/tool/prepare_push_release.dart @@ -0,0 +1,67 @@ +import 'dart:convert'; +import 'dart:io'; + +final versionRegex = RegExp(r"version: (.*)"); + +/// dart tool/prepare_push_release.dart +void main() { + print("Enter your new version:"); + final newVersion = stdin.readLineSync(encoding: Utf8Codec()); + print("Enter the new change log"); + final newChangeLog = + stdin.readLineSync(encoding: Utf8Codec(), retainNewlines: true); + + final currentVersion = _currentVersion(); + + _updateChangeLog(newVersion, newChangeLog); + _updateReadme(currentVersion, newVersion); + _updatePubspec(currentVersion, newVersion); +} + +void _updatePubspec(final String currentVersion, final String newVersion) { + final sealedClassPubSpec = File("sealed_class/pubspec.yaml"); + final sealedClassPubSpecContents = sealedClassPubSpec.readAsStringSync(); + + sealedClassPubSpec.writeAsStringSync( + sealedClassPubSpecContents.replaceAll( + currentVersion, + newVersion, + ), + ); + + final sealedClassGeneratorPubSpec = + File("sealed_class_generator/pubspec.yaml"); + final sealedClassGeneratorPubSpecContents = + sealedClassGeneratorPubSpec.readAsStringSync(); + + sealedClassGeneratorPubSpec.writeAsStringSync( + sealedClassGeneratorPubSpecContents.replaceAll( + currentVersion, + newVersion, + ), + ); +} + +String _currentVersion() { + final currentVersion = + File("sealed_class_generator/pubspec.yaml").readAsStringSync(); + return versionRegex.firstMatch(currentVersion).group(1); +} + +void _updateReadme(final String currentVersion, final String newVersion) { + final readme = File("README.md"); + final currentContents = readme.readAsStringSync(); + readme.writeAsStringSync( + currentContents.replaceAll(currentVersion, newVersion)); +} + +void _updateChangeLog(final String newVersion, final String newChangeLog) { + final file = File("CHANGELOG.md"); + final currentContents = file.readAsStringSync(); + file.writeAsStringSync((StringBuffer() + ..writeln("# $newVersion") + ..writeln("* ${newChangeLog}") + ..write(currentContents)) + .toString() + .trim()); +}