Skip to content

Commit

Permalink
feat(masamune_builder): Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Sep 12, 2022
1 parent 2a93023 commit f5f2699
Show file tree
Hide file tree
Showing 14 changed files with 958 additions and 15 deletions.
Empty file.
24 changes: 24 additions & 0 deletions packages/masamune_builder/LICENSE
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.
31 changes: 31 additions & 0 deletions packages/masamune_builder/README.md
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)
20 changes: 20 additions & 0 deletions packages/masamune_builder/build.yaml
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/**/*
22 changes: 22 additions & 0 deletions packages/masamune_builder/lib/masamune_builder.dart
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';
94 changes: 94 additions & 0 deletions packages/masamune_builder/lib/src/builder.dart
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(",");
}
}
Loading

0 comments on commit f5f2699

Please sign in to comment.