-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bf33eb1
Showing
14 changed files
with
728 additions
and
0 deletions.
There are no files selected for viewing
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,11 @@ | ||
# Files and directories created by pub | ||
.dart_tool/ | ||
.packages | ||
# Remove the following pattern if you wish to check in your lock file | ||
pubspec.lock | ||
|
||
# Conventional directory for build outputs | ||
build/ | ||
|
||
# Directory created by dartdoc | ||
doc/api/ |
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,26 @@ | ||
Copyright 2021, the Dart project authors. | ||
|
||
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 Google LLC 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 THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS 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,25 @@ | ||
# Description | ||
|
||
This is a basic prototype for macros using package:build. | ||
|
||
## Setup | ||
|
||
To use it you will need to build a custom SDK that allows patch files, which | ||
exists here https://github.com/jakemac53/sdk/tree/allow-arbitrary-patches. | ||
|
||
## Writing a macro | ||
|
||
To create a new macro see the example one at `lib/src/json.dart`. You will need | ||
to update the `createBuilder` method in `macro_builder.dart`, to pass in your | ||
new macro to the constructor. | ||
|
||
Note that only Phase 3 macros are supported, at least for now. | ||
|
||
## Running macros | ||
|
||
You will use the build_runner package to run builds, which you can run with the | ||
`pub run build_runner build` command. There is also a `watch` command you may | ||
want to use to get fast rebuilds. | ||
|
||
If you want to build and run an app in a single command, you can use the `run` | ||
command: `pub run build_runner run example/main.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,15 @@ | ||
targets: | ||
$default: | ||
builders: | ||
macro_builder:macro_builder: | ||
enabled: true | ||
generate_for: | ||
- example/user.dart | ||
|
||
builders: | ||
macro_builder: | ||
import: "package:macro_builder/macro_builder.dart" | ||
builder_factories: ["createBuilder"] | ||
build_extensions: {".dart": [".dart.patch"]} | ||
auto_apply: dependents | ||
build_to: source |
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,6 @@ | ||
import 'user.dart'; | ||
|
||
void main() { | ||
var user = User(name: 'jake'); | ||
print(user.toJson()); | ||
} |
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,10 @@ | ||
import 'package:macro_builder/src/json.dart'; | ||
|
||
@toJson | ||
class User { | ||
final String name; | ||
|
||
User({required this.name}); | ||
|
||
external Map<String, Object?> toJson(); | ||
} |
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,9 @@ | ||
import 'package:macro_builder/patch.dart'; | ||
|
||
@patch | ||
class User { | ||
@patch | ||
Map<String, Object?> toJson() => <String, Object?>{ | ||
"name": name, | ||
}; | ||
} |
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,11 @@ | ||
// TODO: the actual code class with identifiers and actual ast representations. | ||
class Code { | ||
final String code; | ||
|
||
Code(this.code); | ||
|
||
String toString() => code; | ||
} | ||
|
||
// TODO: something meaningful :P | ||
abstract class Scope {} |
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,196 @@ | ||
import 'code.dart'; | ||
|
||
abstract class Macro {} | ||
|
||
abstract class ClassTypeMacro implements Macro { | ||
void type(TargetClassType type); | ||
} | ||
|
||
abstract class ClassDeclarationMacro implements Macro { | ||
void declare(TargetClassDeclaration declaration); | ||
} | ||
|
||
abstract class ClassDefinitionMacro implements Macro { | ||
void define(TargetClassDefinition definition); | ||
} | ||
|
||
abstract class FieldTypeMacro implements Macro { | ||
void type(TargetFieldType type); | ||
} | ||
|
||
abstract class FieldDeclarationMacro implements Macro { | ||
void declare(TargetFieldDeclaration declaration); | ||
} | ||
|
||
abstract class FieldDefinitionMacro implements Macro { | ||
void define(TargetFieldDefinition definition); | ||
} | ||
|
||
abstract class MethodTypeMacro implements Macro { | ||
void type(TargetMethodType type); | ||
} | ||
|
||
abstract class MethodDeclarationMacro implements Macro { | ||
void declare(TargetMethodDeclaration declaration); | ||
} | ||
|
||
abstract class MethodDefinitionMacro implements Macro { | ||
void define(TargetMethodDefinition definition); | ||
} | ||
|
||
abstract class TypeReference { | ||
String get name; | ||
|
||
bool get isNullable; | ||
|
||
// The scope where the type reference should be resolved from. | ||
Scope get scope; | ||
|
||
Iterable<TypeReference> get typeArguments; | ||
|
||
Iterable<TypeParameterType> get typeParameters; | ||
} | ||
|
||
abstract class TypeDeclaration implements TypeReference { | ||
Iterable<TypeDeclaration> get typeArguments; | ||
|
||
Iterable<TypeParameterDeclaration> get typeParameters; | ||
|
||
bool isSubtype(TypeDeclaration other); | ||
} | ||
|
||
abstract class TypeDefinition implements TypeDeclaration { | ||
Iterable<TypeDefinition> get superinterfaces; | ||
|
||
Iterable<MethodDefinition> get methods; | ||
|
||
Iterable<FieldDefinition> get fields; | ||
} | ||
|
||
abstract class TargetClassType implements TypeReference { | ||
void addTypeToLibary(Code declaration); | ||
} | ||
|
||
abstract class TargetClassDeclaration implements TypeDeclaration { | ||
Iterable<TargetMethodDeclaration> get methods; | ||
|
||
Iterable<TargetFieldDeclaration> get fields; | ||
|
||
void addToClass(Code declaration); | ||
|
||
void addToLibrary(Code declaration); | ||
} | ||
|
||
abstract class TargetClassDefinition implements TypeDefinition { | ||
Iterable<TargetMethodDefinition> get methods; | ||
|
||
Iterable<TargetFieldDefinition> get fields; | ||
} | ||
|
||
abstract class MethodType { | ||
String get name; | ||
|
||
TypeReference get returnType; | ||
|
||
Iterable<ParameterType> get positionalParameters; | ||
|
||
Map<String, ParameterType> get namedParameters; | ||
|
||
Iterable<TypeParameterType> get typeParameters; | ||
} | ||
|
||
abstract class MethodDeclaration implements MethodType { | ||
TypeDeclaration get returnType; | ||
|
||
Iterable<ParameterDeclaration> get positionalParameters; | ||
|
||
Map<String, ParameterDeclaration> get namedParameters; | ||
|
||
Iterable<TypeParameterDeclaration> get typeParameters; | ||
} | ||
|
||
abstract class MethodDefinition implements MethodDeclaration { | ||
String get name; | ||
|
||
TypeDefinition get returnType; | ||
|
||
Iterable<ParameterDefinition> get positionalParameters; | ||
|
||
Map<String, ParameterDefinition> get namedParameters; | ||
|
||
Iterable<TypeParameterDefinition> get typeParameters; | ||
} | ||
|
||
abstract class TargetMethodType implements MethodType { | ||
void addTypeToLibary(Code declaration); | ||
} | ||
|
||
abstract class TargetMethodDeclaration implements MethodDeclaration { | ||
void addToClass(Code declaration); | ||
} | ||
|
||
abstract class TargetMethodDefinition implements MethodDefinition { | ||
void implement(Code body); | ||
} | ||
|
||
abstract class FieldType { | ||
String get name; | ||
|
||
TypeReference get type; | ||
} | ||
|
||
abstract class FieldDeclaration implements FieldType { | ||
String get name; | ||
|
||
TypeDeclaration get type; | ||
} | ||
|
||
abstract class FieldDefinition implements FieldDeclaration { | ||
String get name; | ||
|
||
TypeDefinition get type; | ||
} | ||
|
||
abstract class TargetFieldType implements FieldType { | ||
void addTypeToLibary(Code declaration); | ||
} | ||
|
||
abstract class TargetFieldDeclaration implements FieldDeclaration { | ||
void addToClass(Code declaration); | ||
} | ||
|
||
abstract class TargetFieldDefinition implements FieldDefinition { | ||
/// Implement this as a normal field and supply an initializer. | ||
void withInitializer(Code body); | ||
|
||
/// Implement this as a getter/setter pair, with an optional new backing | ||
/// field. | ||
void withGetterSetterPair(Code getter, Code setter, {Code? privateField}); | ||
} | ||
|
||
abstract class ParameterType { | ||
TypeReference get type; | ||
String get name; | ||
bool get required; | ||
} | ||
|
||
abstract class ParameterDeclaration implements ParameterType { | ||
TypeDeclaration get type; | ||
} | ||
|
||
abstract class ParameterDefinition implements ParameterDeclaration { | ||
TypeDefinition get type; | ||
} | ||
|
||
abstract class TypeParameterType { | ||
String get name; | ||
TypeReference? get bounds; | ||
} | ||
|
||
abstract class TypeParameterDeclaration implements TypeParameterType { | ||
TypeDeclaration? get bounds; | ||
} | ||
|
||
abstract class TypeParameterDefinition implements TypeParameterDeclaration { | ||
TypeDefinition? get bounds; | ||
} |
Oops, something went wrong.