Skip to content

Commit

Permalink
feat(dynamite): generate library for every spec
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Dec 21, 2023
1 parent e929e32 commit c329949
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 212 deletions.
62 changes: 31 additions & 31 deletions packages/dynamite/dynamite/lib/src/builder/imports.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/builder/state.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:path/path.dart' as p;

Iterable<Spec> generateImports(final AssetId outputId, final State state) sync* {
final analyzerIgnores = state.buildConfig.analyzerIgnores;
if (analyzerIgnores != null) {
for (final rule in analyzerIgnores) {
yield Code('// ignore_for_file: $rule');
}
}
Spec generateLibrary(final AssetId outputId, final State state, final openapi.OpenAPI spec) => Library((final b) {
b
..name = spec.info.title
..directives.addAll([
Directive.import('dart:convert'),
Directive.import('dart:typed_data'),
Directive.import('package:built_collection/built_collection.dart'),
Directive.import('package:built_value/built_value.dart'),
Directive.import('package:built_value/json_object.dart'),
Directive.import('package:built_value/serializer.dart'),
Directive.import('package:built_value/standard_json_plugin.dart'),
Directive.import('package:collection/collection.dart'),
Directive.import('package:dynamite_runtime/built_value.dart'),
Directive.import('package:dynamite_runtime/http_client.dart'),
Directive.import('package:dynamite_runtime/models.dart'),
Directive.import('package:dynamite_runtime/utils.dart', as: 'dynamite_utils'),
Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'),
Directive.import('package:uri/uri.dart'),
]);

yield* [
Directive.import('dart:convert'),
Directive.import('dart:typed_data'),
const Code(''),
Directive.import('package:built_collection/built_collection.dart'),
Directive.import('package:built_value/built_value.dart'),
Directive.import('package:built_value/json_object.dart'),
Directive.import('package:built_value/serializer.dart'),
Directive.import('package:built_value/standard_json_plugin.dart'),
Directive.import('package:collection/collection.dart'),
Directive.import('package:dynamite_runtime/built_value.dart'),
Directive.import('package:dynamite_runtime/http_client.dart'),
Directive.import('package:dynamite_runtime/models.dart'),
Directive.import('package:dynamite_runtime/utils.dart', as: 'dynamite_utils'),
Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'),
Directive.import('package:uri/uri.dart'),
const Code(''),
];
if (state.hasResolvedBuiltTypes) {
b.directives.add(
Directive.part(p.basename(outputId.changeExtension('.g.dart').path)),
);
}

if (state.hasResolvedBuiltTypes) {
yield Directive.part(p.basename(outputId.changeExtension('.g.dart').path));
yield const Code('');
}
}
final analyzerIgnores = state.buildConfig.analyzerIgnores;
if (analyzerIgnores != null) {
b.ignoreForFile.addAll(analyzerIgnores);
}
});
55 changes: 40 additions & 15 deletions packages/dynamite/dynamite/lib/src/builder/serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,44 @@ import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/builder/state.dart';

List<Spec> buildSerializer(final State state) => [
const Code('// coverage:ignore-start'),
const Code('@visibleForTesting'),
const Code('final Serializers serializers = (Serializers().toBuilder()'),
...state.resolvedTypes
.map((final type) => type.serializers)
.expand((final element) => element)
.toSet()
.map(Code.new),
const Code(').build();'),
const Code(''),
const Code('@visibleForTesting'),
const Code(
'final Serializers jsonSerializers = (serializers.toBuilder()..add(DynamiteDoubleSerializer())..addPlugin(StandardJsonPlugin())..addPlugin(const HeaderPlugin())..addPlugin(const ContentStringPlugin())).build();',
),
const Code('// coverage:ignore-end'),
const Code('// coverage:ignore-start\n'),
Field((final b) {
b
..annotations.add(refer('visibleForTesting'))
..modifier = FieldModifier.final$
..type = refer('Serializers')
..name = 'serializers';

final serializers =
state.resolvedTypes.map((final type) => type.serializers).expand((final element) => element).toSet();

final bodyBuilder = StringBuffer()
..writeln('(Serializers().toBuilder()')
..writeAll(serializers, '\n')
..writeln(').build()');

b.assignment = Code(bodyBuilder.toString());
}),
Field((final b) {
b
..annotations.add(refer('visibleForTesting'))
..modifier = FieldModifier.final$
..type = refer('Serializers')
..name = 'jsonSerializers';

const serializers = [
'..add(DynamiteDoubleSerializer())',
'..addPlugin(StandardJsonPlugin())',
'..addPlugin(const HeaderPlugin())',
'..addPlugin(const ContentStringPlugin())',
];

final bodyBuilder = StringBuffer()
..writeln('(serializers.toBuilder()')
..writeAll(serializers, '\n')
..writeln(').build()');

b.assignment = Code(bodyBuilder.toString());
}),
const Code('// coverage:ignore-end\n'),
];
50 changes: 39 additions & 11 deletions packages/dynamite/dynamite/lib/src/openapi_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import 'dart:async';
import 'dart:convert';

import 'package:build/build.dart';
import 'package:built_collection/built_collection.dart';
import 'package:checked_yaml/checked_yaml.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
import 'package:dynamite/src/builder/client.dart';
import 'package:dynamite/src/builder/generate_ofs.dart';
import 'package:dynamite/src/builder/generate_schemas.dart';
import 'package:dynamite/src/builder/imports.dart';
import 'package:dynamite/src/builder/serializer.dart';
import 'package:dynamite/src/builder/state.dart';
import 'package:dynamite/src/helpers/version_checker.dart';
import 'package:dynamite/src/models/config.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:path/path.dart' as p;
import 'package:version/version.dart';

class OpenAPIBuilder implements Builder {
Expand Down Expand Up @@ -79,16 +78,45 @@ class OpenAPIBuilder implements Builder {
final config = buildConfig.configFor(inputId.path);
final state = State(config);

// Imports need to be generated after everything else so we know if we need the local part directive,
// but they need to be added to the beginning of the output.
final output = ListBuilder<Spec>()
..addAll(generateClients(spec, state))
..addAll(generateSchemas(spec, state))
..addAll(buildOfsExtensions(spec, state))
..addAll(buildSerializer(state))
..insertAll(0, generateImports(outputId, state));
final output = Library((final b) {
final analyzerIgnores = state.buildConfig.analyzerIgnores;
if (analyzerIgnores != null) {
b.ignoreForFile.addAll(analyzerIgnores);
}

b
..name = spec.info.title
..directives.addAll([
Directive.import('dart:convert'),
Directive.import('dart:typed_data'),
Directive.import('package:built_collection/built_collection.dart'),
Directive.import('package:built_value/built_value.dart'),
Directive.import('package:built_value/json_object.dart'),
Directive.import('package:built_value/serializer.dart'),
Directive.import('package:built_value/standard_json_plugin.dart'),
Directive.import('package:collection/collection.dart'),
Directive.import('package:dynamite_runtime/built_value.dart'),
Directive.import('package:dynamite_runtime/http_client.dart'),
Directive.import('package:dynamite_runtime/models.dart'),
Directive.import('package:dynamite_runtime/utils.dart', as: 'dynamite_utils'),
Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'),
Directive.import('package:uri/uri.dart'),
])
..body.addAll(generateClients(spec, state))
..body.addAll(generateSchemas(spec, state))
..body.addAll(buildOfsExtensions(spec, state))
..body.addAll(buildSerializer(state));

// Part directive needs to be generated after everything else so we know if we need it.
if (state.hasResolvedBuiltTypes) {
b.directives.add(
Directive.part(p.basename(outputId.changeExtension('.g.dart').path)),
);
}
});

var outputString = output.build().map((final e) => e.accept(emitter)).join('\n');
var outputString = output.accept(emitter).toString();

final coverageIgnores = state.buildConfig.coverageIgnores;
if (coverageIgnores != null) {
Expand Down
10 changes: 4 additions & 6 deletions packages/nextcloud/lib/src/api/comments.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library comments;

import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
Expand Down Expand Up @@ -68,7 +67,6 @@ final Serializers serializers = (Serializers().toBuilder()
..addBuilderFactory(const FullType(Capabilities_Files), Capabilities_FilesBuilder.new)
..add(Capabilities_Files.serializer))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
18 changes: 5 additions & 13 deletions packages/nextcloud/lib/src/api/core.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library core;

import 'dart:typed_data';

import 'package:built_collection/built_collection.dart';
Expand Down Expand Up @@ -10605,19 +10605,15 @@ abstract class WipeCheckWipeResponseApplicationJson
}

typedef AutocompleteResult_Status = ({AutocompleteResult_Status0? autocompleteResultStatus0, String? string});

typedef NavigationEntry_Order = ({int? $int, String? string});

typedef SharebymailCapabilities = ({
BuiltList<Never>? builtListNever,
SharebymailCapabilities0? sharebymailCapabilities0
});

typedef SpreedPublicCapabilities = ({
BuiltList<Never>? builtListNever,
SpreedPublicCapabilities0? spreedPublicCapabilities0
});

typedef OcsGetCapabilitiesResponseApplicationJson_Ocs_Data_Capabilities = ({
CommentsCapabilities? commentsCapabilities,
DavCapabilities? davCapabilities,
Expand All @@ -10634,11 +10630,8 @@ typedef OcsGetCapabilitiesResponseApplicationJson_Ocs_Data_Capabilities = ({
UserStatusCapabilities? userStatusCapabilities,
WeatherStatusCapabilities? weatherStatusCapabilities
});

typedef UnifiedSearchSearchCursor = ({int? $int, String? string});

typedef UnifiedSearchResult_Cursor = ({int? $int, String? string});

typedef $AutocompleteResultStatus0String = ({AutocompleteResult_Status0? autocompleteResultStatus0, String? string});

extension $AutocompleteResultStatus0StringExtension on $AutocompleteResultStatus0String {
Expand Down Expand Up @@ -11957,7 +11950,6 @@ final Serializers serializers = (Serializers().toBuilder()
)
..add(WipeCheckWipeResponseApplicationJson.serializer))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
10 changes: 4 additions & 6 deletions packages/nextcloud/lib/src/api/dashboard.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library dashboard;

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
Expand Down Expand Up @@ -767,7 +766,6 @@ final Serializers serializers = (Serializers().toBuilder()
MapBuilder<String, WidgetItems>.new,
))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
10 changes: 4 additions & 6 deletions packages/nextcloud/lib/src/api/dav.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library dav;

import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
Expand Down Expand Up @@ -340,7 +339,6 @@ final Serializers serializers = (Serializers().toBuilder()
..addBuilderFactory(const FullType(Capabilities_Dav), Capabilities_DavBuilder.new)
..add(Capabilities_Dav.serializer))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
11 changes: 5 additions & 6 deletions packages/nextcloud/lib/src/api/files.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library files;

import 'dart:typed_data';

import 'package:built_collection/built_collection.dart';
Expand Down Expand Up @@ -2863,7 +2863,6 @@ final Serializers serializers = (Serializers().toBuilder()
..addBuilderFactory(const FullType(Template), TemplateBuilder.new)
..add(Template.serializer))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
10 changes: 4 additions & 6 deletions packages/nextcloud/lib/src/api/files_external.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library files_external;

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
Expand Down Expand Up @@ -469,7 +468,6 @@ final Serializers serializers = (Serializers().toBuilder()
..add(StorageConfig_Type.serializer)
..addBuilderFactory(const FullType(BuiltList, [FullType(Mount)]), ListBuilder<Mount>.new))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
10 changes: 4 additions & 6 deletions packages/nextcloud/lib/src/api/files_reminders.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
// ignore_for_file: camel_case_extensions
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

library files_reminders;

import 'package:built_value/built_value.dart';
import 'package:built_value/json_object.dart';
Expand Down Expand Up @@ -630,7 +629,6 @@ final Serializers serializers = (Serializers().toBuilder()
)
..add(ApiRemoveResponseApplicationJson_Ocs.serializer))
.build();

@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
..add(DynamiteDoubleSerializer())
Expand Down
Loading

0 comments on commit c329949

Please sign in to comment.