Skip to content

Commit

Permalink
Added codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeiffer committed Jul 4, 2023
1 parent bf2f289 commit 1a478b2
Show file tree
Hide file tree
Showing 23 changed files with 352 additions and 11 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/publish_annotation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Annotation Publisher

on:
workflow_dispatch:
push:
branches: [main]
paths:
- annotation/pubspec.yaml

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Validate
uses: peiffer-innovations/actions-flutter-validate@v1
with:
path: annotation

- name: Publish
uses: peiffer-innovations/actions-pub-publish@v1.0.2
with:
credentials: ${{ secrets.CREDENTIAL_JSON }}
path: annotation
26 changes: 26 additions & 0 deletions .github/workflows/publish_codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Codegen Publisher

on:
workflow_dispatch:
push:
branches: [main]
paths:
- codegen/pubspec.yaml

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Validate
uses: peiffer-innovations/actions-flutter-validate@v1
with:
path: codegen

- name: Publish
uses: peiffer-innovations/actions-pub-publish@v1.0.2
with:
credentials: ${{ secrets.CREDENTIAL_JSON }}
path: codegen
5 changes: 5 additions & 0 deletions .github/workflows/publisher.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Flutter Publisher

on:
workflow_dispatch:
push:
branches: [main]
paths:
Expand Down Expand Up @@ -41,6 +42,10 @@ jobs:
git push origin HEAD:gh-pages
cd ..
- name: Run Codegen
run: |
dart run build_runner build --delete-conflicting-outputs -v
- name: Publish
uses: peiffer-innovations/actions-pub-publish@v1.0.2
with:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/update_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,35 @@ on:
- cron: '0 1 * * 2'

jobs:
update_dependencies_annotation:
runs-on: ubuntu-latest

steps:
- name: Dependencies
uses: peiffer-innovations/actions-dart-dependency-updater@v1.0.18
with:
merge: true
paths: annotation
pull_request: true
token: ${{ secrets.REPO_TOKEN }}

update_dependencies_codegen:
needs:
- update_dependencies_annotation
runs-on: ubuntu-latest

steps:
- name: Dependencies
uses: peiffer-innovations/actions-dart-dependency-updater@v1.0.18
with:
merge: true
paths: .,example
pull_request: true
token: ${{ secrets.REPO_TOKEN }}

update_dependencies:
needs:
- update_dependencies_codegen
runs-on: ubuntu-latest

steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ coverage/
coverage_badge.svg
test/.test_coverage.dart
output/
*.g.dart

# IntelliJ related
*.iml
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [6.0.2] - July 4th, 2023

* Added a code generator to automatically generate the existing decoders into a map.


## [6.0.1+2] - July 4, 2023

* Automated dependency updates
Expand Down
3 changes: 3 additions & 0 deletions annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [1.0.0+1] - July 4th, 2023

* Initial Release
21 changes: 21 additions & 0 deletions annotation/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Peiffer Innovations, LLC

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.
4 changes: 4 additions & 0 deletions annotation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# json_theme_annotation

Simple package containing the annotation classes needed for the code generation
tools within the [json_theme](https://pub.dev/packages/json_theme) package.
5 changes: 5 additions & 0 deletions annotation/lib/json_theme_annotation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const jsonThemeDecoder = JsonThemeDecoder();

class JsonThemeDecoder {
const JsonThemeDecoder();
}
41 changes: 41 additions & 0 deletions annotation/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: 'json_theme_annotation'
description: 'Simple package holding the annotations needed by the JsonTheme code generator.'
homepage: 'https://github.com/peiffer-innovations/json_theme'
version: '1.0.0+1'

environment:
sdk: '>=3.0.0 <4.0.0'

analyzer:
exclude:
- 'lib/generated/**'
- 'lib/**/*.g.dart'

dev_dependencies:
test: '^1.24.3'

ignore_updates:
- 'archive'
- 'async'
- 'boolean_selector'
- 'characters'
- 'charcode'
- 'collection'
- 'clock'
- 'crypto'
- 'fake_async'
- 'file'
- 'matcher'
- 'material_color_utilities'
- 'meta'
- 'path'
- 'source_span'
- 'stack_trace'
- 'stream_channel'
- 'string_scanner'
- 'sync_http'
- 'term_glyph'
- 'test_api'
- 'typed_data'
- 'vector_math'
- 'webdriver'
5 changes: 5 additions & 0 deletions annotation/test/no_op_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:test/test.dart';

void main() {
test('no-op', () {});
}
3 changes: 3 additions & 0 deletions codegen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [1.0.0] - July 4th, 2023

* Initial Release
21 changes: 21 additions & 0 deletions codegen/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Peiffer Innovations, LLC

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.
8 changes: 8 additions & 0 deletions codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# json_theme_codegen

Code generator for the [json_theme] package. This is intended to be used as an
internal tool on the [json_theme] package itself rather than for external
consumption.

<!-- Links -->
[json_theme]: https://pub.dev/packages/json_theme
8 changes: 8 additions & 0 deletions codegen/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
builders:
decoder_builder:
import: 'package:json_theme_codegen/builder.dart'
builder_factories: ['decoderLibrary']
build_extensions: { '.dart': ['decoder.g.part'] }
auto_apply: dependents
build_to: cache
applies_builders: ['source_gen|combining_builder']
10 changes: 10 additions & 0 deletions codegen/lib/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:build/build.dart';
import 'package:json_theme_codegen/json_theme_codegen.dart';
import 'package:source_gen/source_gen.dart';

Builder decoderLibrary(BuilderOptions options) => SharedPartBuilder(
[
DecoderLibraryBuilder(),
],
'decoder',
);
1 change: 1 addition & 0 deletions codegen/lib/json_theme_codegen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/decoder_library_builder.dart';
50 changes: 50 additions & 0 deletions codegen/lib/src/decoder_library_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ignore_for_file: avoid_print

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:json_theme_annotation/json_theme_annotation.dart';
import 'package:source_gen/source_gen.dart';

class DecoderLibraryBuilder extends GeneratorForAnnotation<JsonThemeDecoder> {
@override
String generateForAnnotatedElement(
Element element,
ConstantReader annotation,
BuildStep buildStep,
) {
const kDecode = 'decode';
final name = element.name;
if (name == null) {
throw Exception(
'Annotation found on unnamed location, cannot continue.',
);
}

if (element is! ClassElement) {
throw Exception('Annotation found but is ');
}
final methods = <String, String>{};
for (var m in element.methods) {
if (m.isStatic && m.name.startsWith(kDecode)) {
final mName = m.name;
methods[mName.substring(kDecode.length)] = '$name.$mName';
print(mName);
}
}

final buf = StringBuffer();

buf.write('''
typedef Decoder = dynamic Function(dynamic map, {bool validate});
const k${name}s = <String, Decoder>{
''');

for (var entry in methods.entries) {
buf.write("'${entry.key}': ${entry.value},");
}

buf.write('};');
return buf.toString();
}
}
55 changes: 55 additions & 0 deletions codegen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: 'json_theme_codegen'
description: 'A library to generate the mapping of decoders to the class names they decode'
homepage: 'https://github.com/peiffer-innovations/json_theme'
version: '1.0.0'

environment:
sdk: '>=3.0.0 <4.0.0'

analyzer:
exclude:
- 'lib/generated/**'
- 'lib/**/*.g.dart'

dependencies:
analyzer: '^5.13.0'
build: '^2.4.0'
build_runner: '^2.4.5'
code_builder: '^4.4.0'
json_class: '^2.2.1+3'
json_theme_annotation: '^1.0.0+1'
recase: '^4.1.0'
source_gen: '^1.2.7'

dev_dependencies:
test: '^1.24.3'

# dependency_overrides:
# json_theme_annotation:
# path: ../annotation

ignore_updates:
- 'archive'
- 'async'
- 'boolean_selector'
- 'characters'
- 'charcode'
- 'collection'
- 'clock'
- 'crypto'
- 'fake_async'
- 'file'
- 'matcher'
- 'material_color_utilities'
- 'meta'
- 'path'
- 'source_span'
- 'stack_trace'
- 'stream_channel'
- 'string_scanner'
- 'sync_http'
- 'term_glyph'
- 'test_api'
- 'typed_data'
- 'vector_math'
- 'webdriver'
5 changes: 5 additions & 0 deletions codegen/test/no_op_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:test/test.dart';

void main() {
test('no-op', () {});
}
Loading

0 comments on commit 1a478b2

Please sign in to comment.