Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillon Nys committed Sep 27, 2023
1 parent e82db6b commit fc1e166
Show file tree
Hide file tree
Showing 9 changed files with 1,776 additions and 1,142 deletions.
2,294 changes: 1,346 additions & 948 deletions .github/composite_actions/deputy_scan/dist/main.cjs

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions .github/composite_actions/deputy_scan/dist/main.cjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Setup Dart
uses: dart-lang/setup-dart@e58aeb62aef51dcc4d0ba8eada7c08092aad5314 # main
with:
sdk: 3.2.0-171.0.dev
sdk: 3.2.0-191.0.dev

- name: Setup pnpm
uses: pnpm/action-setup@d882d12c64e032187b2edb46d3a0d003b7a43598 # 2.4.0
Expand All @@ -57,7 +57,7 @@ jobs:
- name: Setup Dart
uses: dart-lang/setup-dart@e58aeb62aef51dcc4d0ba8eada7c08092aad5314 # main
with:
sdk: 3.2.0-171.0.dev
sdk: 3.2.0-191.0.dev

- name: Get Packages
working-directory: actions
Expand Down
52 changes: 13 additions & 39 deletions actions/bin/deputy_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,12 @@ const List<String> _doNotUpdate = [
'uuid',
];

/// The groups of dependencies which should be updated together.
final List<DependencyUpdateGroup> updateGroups = [
// Drift
DependencyUpdateGroup.of(['drift', 'drift_dev']),

// Code generation
DependencyUpdateGroup.of([
'built_value',
'built_collection',
'built_value_generator',
'json_annotation',
'json_serializable',
'code_builder',
]),
];

Future<void> _deputyScan() async {
final logger = AWSLogger().createChild('Deputy')
..unregisterAllPlugins()
..registerPlugin(const NodeLoggerPlugin());
final deputy = await Deputy.create(
dependencyGroups: updateGroups,
dependencyGroups: DependencyUpdateGroup.all,
fileSystem: nodeFileSystem,
platform: nodePlatform,
processManager: nodeProcessManager,
Expand All @@ -69,16 +53,16 @@ Future<void> _deputyScan() async {
await allocateSwapSpace();

// Create a PR for each dependency group which does not already have a PR.
for (final MapEntry(key: dependencies, value: groupUpdates)
in updates.toMap().entries) {
final groupName = dependencies.join('+');
for (final MapEntry(key: groupName, value: group)
in updates.entries) {
// If the group updates all deps to a unique constraint, use that in messages.
final uniqueConstraint = groupUpdates
.map((update) => update.updatedConstraint)
final uniqueConstraint = group
.updatedConstraints
.values
.toSet()
.singleOrNull;
await core.withGroup('Create PR for group "$groupName"', () async {
if (dependencies.any(_doNotUpdate.contains)) {
if (group.dependencies.any(_doNotUpdate.contains)) {
core.info(
'Skipping "$groupName" since one of its dependencies are on the do-not-update list',
);
Expand Down Expand Up @@ -123,20 +107,11 @@ Future<void> _deputyScan() async {
);
final worktree = NodeGitDir(worktreeRepo.git);

for (final groupUpdate in groupUpdates) {
core.info('Updating pubspecs...');
await groupUpdate.updatePubspecs(worktreeRepo);
}
core.info('Updating pubspecs...');
await group.updatePubspecs(worktreeRepo);

core.info('Running post-update tasks...');
final updatedPackages = groupUpdates
.expand((groupUpdate) => groupUpdate.update.dependentPackages.keys)
.toList();
await PostUpdateTasks.runAll(
worktreeRepo,
dependencies,
updatedPackages,
);
await group.runPostUpdateTasks(worktreeRepo);

core.info('Diffing changes...');
await worktree.runCommand(['diff']);
Expand All @@ -153,11 +128,10 @@ Future<void> _deputyScan() async {

// Create a PR for the changes using the `gh` CLI.
core.info('Creating PR...');
final constraintUpdates = groupUpdates
final constraintUpdates = group.dependencies
.map(
(groupUpdate) =>
'- Updated `${groupUpdate.update.dependencyName}` to '
'`${groupUpdate.updatedConstraint}`',
(dependency) =>
'- Updated `$dependency` to `${group.updatedConstraints[dependency]}`',
)
.join('\n');
final prBody = '''
Expand Down
88 changes: 42 additions & 46 deletions actions/lib/src/deputy/post_update_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,40 @@ import 'package:aws_common/aws_common.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;

enum PostUpdateTasks {
builtValueGenerator(
/// The groups of dependencies which should be updated together.
enum DependencyUpdateGroup {
codeGeneration(
needsBuildRunner: true,
needsSmithy: true,
dependencies: [
'built_value',
'built_collection',
'built_value_generator',
'json_annotation',
'json_serializable',
'code_builder',
],
),
codeBuilder(
needsBuildRunner: true,
needsSmithy: true,
),
dartStyle(
needsBuildRunner: true,
needsSmithy: true,
),
driftDev(needsBuildRunner: true),
jsonSerializable(needsBuildRunner: true);
drift(needsBuildRunner: true, dependencies: ['drift', 'drift_dev']);

const PostUpdateTasks({
const DependencyUpdateGroup({
required this.dependencies,
this.needsBuildRunner = false,
this.needsSmithy = false,
});

final bool needsBuildRunner;
final bool needsSmithy;
final List<String> dependencies;

static PostUpdateTasks? of(String dependency) =>
values.firstWhereOrNull((el) => el.name.snakeCase == dependency);
static Map<String, List<String>> get all => {
for (final value in values) value.name.snakeCase: value.dependencies,
};

static Future<void> runAll(
Repo repo,
DependencyUpdateGroup group,
List<String> updatedPackages,
) async {
final tasksBuilders = [
for (final dependency in group) PostUpdateTasks.of(dependency),
].nonNulls;
if (tasksBuilders.isEmpty) {
core.info('No tasks to run.');
return;
}
final groupName = group.join('+');
core.info('Running post-update tasks for "$groupName"');
final tasks = tasksBuilders
.expand((builder) => builder.buildTasks(repo, updatedPackages))
.toSet();
for (final task in tasks) {
await task.run(repo);
}
}
static DependencyUpdateGroup? of(String dependency) =>
values.firstWhereOrNull((el) => el.name.snakeCase == dependency);

List<PostUpdateTask> buildTasks(Repo repo, List<String> updatedPackages) {
List<PostUpdateTask> buildTasks(Repo repo, Iterable<String> updatedPackages) {
return [
if (needsSmithy) ...[
const PostUpdateTask.aft(['generate', 'goldens']),
Expand Down Expand Up @@ -85,6 +69,25 @@ enum PostUpdateTasks {
}
}

extension GroupPostUpdateTasks on DependencyGroupUpdate {
/// Runs all post-update tasks for the group in [repo].
Future<void> runPostUpdateTasks(Repo repo) async {
final tasksBuilder = DependencyUpdateGroup.of(groupName);
if (tasksBuilder == null) {
core.info('No tasks to run.');
return;
}
core.info('Running post-update tasks for "$groupName"');
final updatedPackages = updates.values
.expand((update) => update.dependentPackages.keys)
.toSet();
final tasks = tasksBuilder.buildTasks(repo, updatedPackages);
for (final task in tasks) {
await task.run(repo);
}
}
}

/// A task to run once a dependency has been updated.
abstract base class PostUpdateTask {
const PostUpdateTask();
Expand Down Expand Up @@ -130,7 +133,7 @@ abstract base class PostUpdateTask {
}

/// Runs `aft` with the given [args].
final class _AftTask extends PostUpdateTask with AWSEquatable<_AftTask> {
final class _AftTask extends PostUpdateTask {
const _AftTask(this.args);

final List<String> args;
Expand All @@ -148,14 +151,10 @@ final class _AftTask extends PostUpdateTask with AWSEquatable<_AftTask> {
throw ProcessException('aft', args);
}
}

@override
List<Object?> get props => [args];
}

/// Runs `build_runner` in each of the [packages].
final class _BuildRunnerTask extends PostUpdateTask
with AWSEquatable<_BuildRunnerTask> {
final class _BuildRunnerTask extends PostUpdateTask {
const _BuildRunnerTask(this.packages);

final List<String> packages;
Expand Down Expand Up @@ -197,7 +196,4 @@ final class _BuildRunnerTask extends PostUpdateTask
}
}
}

@override
List<Object?> get props => [packages];
}
53 changes: 51 additions & 2 deletions packages/aft/common/lib/src/deputy/dependency_update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,60 @@ import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';

part 'dependency_update.g.dart';

typedef DependencyUpdateGroup = BuiltList<String>;
typedef DependencyGroup = BuiltSet<String>;
typedef DependencyGroups = BuiltSetMultimap<String, String>;

/// {@template aft_common.deputy.dependency_group_update}
/// A proposed set of [updates] to a dependency group.
///
/// Call [updatePubspecs] to write proposed changes to disk.
/// {@endtemplate}
abstract class DependencyGroupUpdate
implements Built<DependencyGroupUpdate, DependencyGroupUpdateBuilder> {
/// {@macro aft_common.deputy.dependency_group_update}
factory DependencyGroupUpdate([
void Function(DependencyGroupUpdateBuilder) updates,
]) = _$DependencyGroupUpdate;

const DependencyGroupUpdate._();

/// The name of the update group.
///
/// For unnamed groups, this is set to the sole dependency's name.
String get groupName;

/// The dependencies included in this group.
BuiltSet<String> get dependencies;

/// The updated version constraints, by dependency name.
BuiltMap<String, VersionConstraint> get updatedConstraints;

/// The change records, by dependency name.
BuiltMap<String, DependencyUpdate> get updates;

@protected
Deputy get deputy;

@protected
BuiltList<void Function(Repo)> get pubspecUpdates;

/// Updates all pubspecs in the group and writes the changes
/// to disk.
///
/// If [worktree] is specified, updates are applied in that repo.
/// Otherwise, they are applied to the current, active repo.
Future<void> updatePubspecs([Repo? worktree]) async {
for (final update in pubspecUpdates) {
update(worktree ?? deputy.repo);
}
await deputy.commitUpdates(worktree);
}
}

abstract class DependencyUpdate
with AWSSerializable
Expand All @@ -21,7 +70,7 @@ abstract class DependencyUpdate

const DependencyUpdate._();

static DependencyUpdate fromJson(Map<String, Object?> json) {
factory DependencyUpdate.fromJson(Map<String, Object?> json) {
return _serializers.deserializeWith(serializer, json)!;
}

Expand Down
Loading

0 comments on commit fc1e166

Please sign in to comment.