Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(aft): Use public models #3727

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion .github/workflows/smoke_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ permissions: read-all
jobs:
smoke_test:
name: Smoke Test
runs-on: ubuntu-latest
runs-on:
labels: amplify-flutter_ubuntu-latest_4-core
steps:
- name: Git Checkout
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # 3.6.0
Expand Down
79 changes: 31 additions & 48 deletions packages/aft/lib/src/commands/generate/generate_sdk_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:async/async.dart';
import 'package:aws_common/aws_common.dart';
import 'package:checked_yaml/checked_yaml.dart';
import 'package:code_builder/code_builder.dart';
import 'package:collection/collection.dart';
import 'package:git/git.dart';
import 'package:path/path.dart' as p;
import 'package:pubspec_parse/pubspec_parse.dart';
Expand All @@ -26,7 +25,7 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
..addOption(
'models',
abbr: 'm',
help: 'The path to the AWS SDK models',
help: 'The path to the AWS JS V3 repo',
)
..addOption(
'output',
Expand Down Expand Up @@ -75,7 +74,9 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
await runGit(
[
'clone',
'https://github.com/aws/aws-models.git',
// https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/
'--filter=tree:0',
'https://github.com/aws/aws-sdk-js-v3.git',
cloneDir.path,
],
echoOutput: verbose,
Expand All @@ -102,30 +103,6 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
return worktreeDir;
}

/// Organizes model files from [baseDir] into a new temporary directory.
///
/// Returns the new directory.
Future<Directory> _organizeModels(Directory baseDir) async {
final modelsDir = await Directory.systemTemp.createTemp('models');
logger.debug('Organizing models in ${modelsDir.path}');
final services = baseDir.list(followLinks: false).whereType<Directory>();
await for (final serviceDir in services) {
final serviceName = p.basename(serviceDir.path);
final artifacts = await serviceDir.list().whereType<Directory>().toList();
final smithyDir = artifacts.firstWhereOrNull(
(dir) => p.basename(dir.path) == 'smithy',
);
if (smithyDir == null) {
continue;
}
final smithyModel = File(p.join(smithyDir.path, 'model.json'));
final copyToPath = p.join(modelsDir.path, '$serviceName.json');
logger.verbose('Copying $serviceName.json to $copyToPath');
await smithyModel.copy(copyToPath);
}
return modelsDir;
}

Future<Directory> _modelsDirForRef(String ref) async {
if (_modelsCache[ref] case final cachedDir?) {
return cachedDir;
Expand All @@ -137,19 +114,20 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
exitError('Model directory ($modelsDir) does not exist');
}
// Checkout models if modelsPath is a git repository.
if (File(p.join(modelsPath, '.git')).existsSync()) {
if (Directory(p.join(modelsPath, '.git')).existsSync()) {
modelsDir = await _checkoutModelsRef(modelsDir, ref);
} else {
logger.warn(
'Unable to check out $ref since $modelsDir is not a git repo',
);
}
modelsDir = await _organizeModels(modelsDir);
} else {
modelsDir = await _downloadModels();
modelsDir = await _checkoutModelsRef(modelsDir, ref);
modelsDir = await _organizeModels(modelsDir);
}
modelsDir = Directory.fromUri(
modelsDir.uri.resolve('codegen/sdk-codegen/aws-models'),
);
return _modelsCache[ref] = modelsDir;
}

Expand Down Expand Up @@ -180,13 +158,13 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
final modelsDir = await _modelsDirForRef(config.ref);
final models = modelsDir.list().whereType<File>();
await for (final model in models) {
final serviceName = p.basenameWithoutExtension(model.path);
if (!config.apis.keys.contains(serviceName)) {
continue;
}
final astJson = await model.readAsString();
final ast = parseAstJson(astJson);

final serviceNamespace =
ast.shapes.values.whereType<ServiceShape>().single.shapeId.namespace;
if (!config.apis.containsKey(serviceNamespace)) {
continue;
}
smithyModel.shapes!.addAll(ast.shapes);
}

Expand All @@ -201,21 +179,26 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
}

final includeShapes = <ShapeId>[];
for (final shapeIdEntries in config.apis.entries) {
final shapeIds = shapeIdEntries.value;
if (shapeIds != null && shapeIds.isNotEmpty) {
includeShapes.addAll(shapeIds);
continue;
}

final apiName = shapeIdEntries.key;
for (final MapEntry(key: namespace, value: operations)
in config.apis.entries) {
final serviceShape = smithyModel.shapes!.values
.whereType<ServiceShape>()
.singleWhere(
(service) =>
service.expectTrait<ServiceTrait>().endpointPrefix == apiName,
.singleWhere((service) => service.shapeId.namespace == namespace);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool function, didn't know this existed.

final allOperations = serviceShape.operations.map((op) => op.target);
final includeOperations = switch (operations) {
List<String> _ when operations.isNotEmpty => operations.map(
(op) => ShapeId(namespace: namespace, shape: op),
),
_ => allOperations,
};
for (final operation in includeOperations) {
if (!allOperations.contains(operation)) {
throw Exception(
'Invalid operation specified for $namespace: $operation',
);
includeShapes.addAll(serviceShape.operations.map((op) => op.target));
}
}
includeShapes.addAll(includeOperations);
Comment on lines +194 to +201
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could flatten this logic a little bit.

final isMissingOp = includeOperations.every((e) => !allOperations.contains(operation));
if (isMissingOp) {
   throw  Exception(
     'Invalid operation specified for $namespace: $operation',
    );
}
includeShapes.addAll(includeOperations);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, started with something like that. This turned out to be cleaner - the problem is getting the element which didn't pass the condition, i.e. operation in your snippet.

}

// Generate SDK for combined operations
Expand Down Expand Up @@ -326,7 +309,7 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
),
]);

final modelsDir = await _modelsDirForRef('master');
final modelsDir = await _modelsDirForRef('main');
final models = modelsDir.list().whereType<File>();
final services = <Field>[];
await for (final model in models) {
Expand Down
6 changes: 3 additions & 3 deletions packages/aft/lib/src/config/raw_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class SdkConfig
AWSSerializable<Map<String, Object?>>,
AWSDebuggable {
const SdkConfig({
this.ref = 'master',
this.ref = 'main',
required this.apis,
this.plugins = const [],
});
Expand All @@ -412,9 +412,9 @@ class SdkConfig

/// The `aws-models` ref to pull.
///
/// Defaults to `master`.
/// Defaults to `main`.
final String ref;
final Map<String, List<ShapeId>?> apis;
final Map<String, List<String>?> apis;
final List<String> plugins;

@override
Expand Down
8 changes: 3 additions & 5 deletions packages/aft/lib/src/config/raw_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 6 additions & 14 deletions packages/analytics/amplify_analytics_pinpoint_dart/sdk.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
apis:
pinpoint:
- com.amazonaws.pinpoint#GetEndpoint
- com.amazonaws.pinpoint#GetInAppMessages
- com.amazonaws.pinpoint#PutEvents
- com.amazonaws.pinpoint#UpdateEndpoint
- com.amazonaws.pinpoint#UpdateEndpointsBatch
- com.amazonaws.pinpoint#GetUserProfile
- com.amazonaws.pinpoint#UpdateUserProfile
- com.amazonaws.pinpoint#DeleteUserProfile
- com.amazonaws.pinpoint#GetUserProfilesBatch
- com.amazonaws.pinpoint#UpdateUserProfilesBatch
- com.amazonaws.pinpoint#PutUserEvents
# patches:
# - tool/patch/user-models.patch
com.amazonaws.pinpoint:
- GetEndpoint
- GetInAppMessages
- PutEvents
- UpdateEndpoint
- UpdateEndpointsBatch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading