-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(masamune_builder): Initial commit.
- Loading branch information
Showing
14 changed files
with
958 additions
and
15 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Copyright (c) 2021, mathru (https://mathru.net) | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
* Neither the name of the <organization> nor the names of its contributors | ||
may be used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Katana Module | ||
|
||
[![Version](https://img.shields.io/badge/version-0.9.0-blue.svg)](https://mathru.net) | ||
[![Language](https://img.shields.io/badge/language-dart-blue.svg)](https://dart.dev/) | ||
[![License: BSD](https://img.shields.io/badge/license-BSD-purple.svg)](https://opensource.org/licenses/BSD-3-Clause) | ||
|
||
--------------------------------------- | ||
|
||
"Katana" is a package of utilities for Flutter. | ||
It defines several extension methods and global functions. | ||
|
||
- Module building system | ||
|
||
## Getting Started | ||
|
||
Add this to your package's pubspec.yaml file: | ||
```yaml | ||
dev_dependencies: | ||
katana_module_builder: ^0.9.0 | ||
``` | ||
You should then run `flutter pub upgrade`. | ||
|
||
## Usage | ||
|
||
```yaml | ||
import 'package:katana_module_builder/katana_module_builder.dart'; | ||
``` | ||
|
||
## License | ||
|
||
[BSD](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
builders: | ||
masamune_builder: | ||
import: 'package:masamune_builder/masamune_builder.dart' | ||
builder_factories: ['masamuneBuilderFactory'] | ||
build_extensions: {'.dart': ['.m.dart']} | ||
auto_apply: dependents | ||
build_to: source | ||
applies_builders: ["source_gen|combining_builder"] | ||
targets: | ||
$default: | ||
builders: | ||
masamune_builder: | ||
enabled: true | ||
generate_for: | ||
exclude: | ||
- test | ||
- example | ||
include: | ||
- test/integration/* | ||
- test/integration/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2022 mathru. All rights reserved. | ||
|
||
/// Building system for masamune packages. Automatic Model creation and Page route generation. | ||
/// | ||
/// To use, import `package:masamune_builder/masamune_builder.dart`. | ||
/// | ||
/// [mathru.net]: https://mathru.net | ||
/// [YouTube]: https://www.youtube.com/c/mathrunetchannel | ||
library masamune_builder; | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:build/build.dart'; | ||
import 'package:code_builder/code_builder.dart'; | ||
import 'package:dart_style/dart_style.dart'; | ||
import 'package:katana_module_builder_annotation/katana_module_builder_annotation.dart'; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
part 'src/builder.dart'; | ||
part 'src/extensions.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
part of masamune_builder; | ||
|
||
Builder masamuneBuilderFactory(BuilderOptions options) { | ||
return PartBuilder([MasamuneGenerator()], ".m.dart"); | ||
} | ||
|
||
class MasamuneGenerator extends GeneratorForAnnotation<MasamuneModule> { | ||
@override | ||
FutureOr<String> generateForAnnotatedElement( | ||
Element element, | ||
ConstantReader annotation, | ||
BuildStep buildStep, | ||
) { | ||
if (!element.library!.isNonNullableByDefault) { | ||
throw InvalidGenerationSourceError( | ||
'Generator cannot target libraries that have not been migrated to ' | ||
'null-safety.', | ||
element: element, | ||
); | ||
} | ||
|
||
if (element is! ClassElement) { | ||
throw InvalidGenerationSourceError( | ||
'`@module` can only be used on classes.', | ||
element: element, | ||
); | ||
} | ||
|
||
final mixin = Library( | ||
(b) => b | ||
..body.addAll( | ||
[ | ||
Method( | ||
(m) => m | ||
..name = "_\$${element.name}FromMap" | ||
..requiredParameters.addAll([ | ||
Parameter( | ||
(b) => b | ||
..name = "map" | ||
..type = const Reference("DynamicMap"), | ||
), | ||
Parameter( | ||
(b) => b | ||
..name = "ref" | ||
..type = Reference(element.name), | ||
) | ||
]) | ||
..returns = refer("${element.name}?") | ||
..body = Code( | ||
"if(map.isEmpty || map.get(\"type\", \"\") != ref.type){ return null; } return ${element.name}(${_generateFromMapCode(element)});"), | ||
), | ||
Method( | ||
(m) => m | ||
..name = "_\$${element.name}ToMap" | ||
..requiredParameters.addAll([ | ||
Parameter( | ||
(b) => b | ||
..name = "ref" | ||
..type = Reference(element.name), | ||
) | ||
]) | ||
..returns = | ||
refer("DynamicMap", "package:masamune/masamune.dart") | ||
..body = Code( | ||
"return <String, dynamic>{${_generateToMapCode(element)}};"), | ||
), | ||
], | ||
), | ||
); | ||
final emitter = DartEmitter(); | ||
return DartFormatter().format('${mixin.accept(emitter)}'); | ||
} | ||
|
||
String _generateFromMapCode(ClassElement element) { | ||
return [ | ||
...element.constructors.first.parameters | ||
.map( | ||
(e) => e.codeFromMap, | ||
) | ||
.where((e) => e.isNotEmpty), | ||
].join(","); | ||
} | ||
|
||
String _generateToMapCode(ClassElement element) { | ||
return [ | ||
"\"type\": ref.type", | ||
...element.constructors.first.parameters | ||
.map( | ||
(e) => e.codeToMap, | ||
) | ||
.where((e) => e.isNotEmpty), | ||
].join(","); | ||
} | ||
} |
Oops, something went wrong.