Skip to content

Commit

Permalink
chore(aft): Clean up
Browse files Browse the repository at this point in the history
commit-id:de10a06e
  • Loading branch information
Dillon Nys committed Aug 27, 2022
1 parent 55e00ba commit 2854886
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 195 deletions.
2 changes: 1 addition & 1 deletion aft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ branches:
stable: stable
prerelease: next

# Strongly connected components which should have minor/major version bumps happen
# Strongly connected components which should have major version bumps happen
# in unison, i.e. a version bump to one package cascades to all.
components:
amplify:
Expand Down
8 changes: 4 additions & 4 deletions packages/aft/lib/src/changelog/commit_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ enum CommitTypeGroup {
features('Features'),
other('Other Changes');

final String header;

const CommitTypeGroup(this.header);

final String header;
}

enum CommitType {
Expand All @@ -49,11 +49,11 @@ enum CommitType {
style.other(),
test.other();

final CommitTypeGroup group;

const CommitType.fixes() : group = CommitTypeGroup.fixes;
const CommitType.features() : group = CommitTypeGroup.features;
const CommitType.other() : group = CommitTypeGroup.other;

final CommitTypeGroup group;
}

/// {@template aft.changelog.commit_message}
Expand Down
1 change: 0 additions & 1 deletion packages/aft/lib/src/commands/pub_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import 'dart:io';
import 'package:aft/aft.dart';
import 'package:async/async.dart';
import 'package:aws_common/aws_common.dart';
import 'package:cli_util/cli_logging.dart';
import 'package:http/http.dart' as http;
import 'package:pub/src/http.dart' as pub_http;

Expand Down
31 changes: 2 additions & 29 deletions packages/aft/lib/src/commands/publish_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import 'dart:io';
import 'package:aft/aft.dart';
import 'package:aft/src/util.dart';
import 'package:aws_common/aws_common.dart';
import 'package:cli_util/cli_logging.dart';
import 'package:graphs/graphs.dart';
import 'package:pubspec_parse/pubspec_parse.dart';

/// Command to publish all Dart/Flutter packages in the repo.
class PublishCommand extends AmplifyCommand {
Expand Down Expand Up @@ -146,7 +144,7 @@ class PublishCommand extends AmplifyCommand {
@override
Future<void> run() async {
// Gather packages which can be published.
var publishablePackages = (await Future.wait([
final publishablePackages = (await Future.wait([
for (final package in allPackages.values) _checkPublishable(package),
]))
.whereType<PackageInfo>()
Expand All @@ -165,7 +163,7 @@ class PublishCommand extends AmplifyCommand {
}

try {
publishablePackages = sortPackagesTopologically<PackageInfo>(
sortPackagesTopologically<PackageInfo>(
publishablePackages,
(pkg) => pkg.pubspecInfo.pubspec,
);
Expand Down Expand Up @@ -254,28 +252,3 @@ Future<void> runBuildRunner(
exit(1);
}
}

/// Sorts packages in topological order so they may be published in the order
/// they're sorted.
///
/// Packages with inter-dependencies cannot be topologically sorted and will
/// throw a [CycleException].
List<T> sortPackagesTopologically<T>(
Iterable<T> packages,
Pubspec Function(T) getPubspec,
) {
final pubspecs = packages.map(getPubspec);
final packageNames = pubspecs.map((el) => el.name).toList();
final graph = <String, Iterable<String>>{
for (var package in pubspecs)
package.name: package.dependencies.keys.where(packageNames.contains),
};
final ordered = topologicalSort(graph.keys, (key) => graph[key]!);
return packages.toList()
..sort((a, b) {
// `ordered` is in reverse ordering to our desired publish precedence.
return ordered
.indexOf(getPubspec(b).name)
.compareTo(ordered.indexOf(getPubspec(a).name));
});
}
145 changes: 2 additions & 143 deletions packages/aft/lib/src/commands/version_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@
import 'dart:io';

import 'package:aft/aft.dart';
import 'package:aft/src/changelog/changelog.dart';
import 'package:aft/src/changelog/commit_message.dart';
import 'package:aft/src/options/git_ref_options.dart';
import 'package:aft/src/options/glob_options.dart';
import 'package:aft/src/repo.dart';
import 'package:aft/src/util.dart';
import 'package:aws_common/aws_common.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';

/// Command for bumping package versions across the repo.
class VersionCommand extends AmplifyCommand {
Expand Down Expand Up @@ -62,11 +56,10 @@ abstract class _VersionBaseCommand extends AmplifyCommand
}

Future<void> _updateVersions({required bool preview}) async {
final updates = await bumpVersions(
repo: repo,
repo.bumpAllVersions(
changesForPackage: _changesForPackage,
);
final changelogUpdates = updates.updatedChangelogs;
final changelogUpdates = repo.changelogUpdates;

for (final package in allPackages.values) {
final edits = package.pubspecInfo.pubspecYamlEditor.edits;
Expand Down Expand Up @@ -123,137 +116,3 @@ class _VersionUpdateCommand extends _VersionBaseCommand {
return _updateVersions(preview: false);
}
}

Future<VersionChanges> bumpVersions({
required Repo repo,
required GitChanges Function(PackageInfo) changesForPackage,
}) async {
// Version updates, by component.
final versionUpdates = <String, Version>{};

// Changelog updates. by package.
final changelogUpdates = <PackageInfo, ChangelogUpdate>{};

/// Bumps the dependency for [package] in [dependent].
void bumpDependency(PackageInfo package, PackageInfo dependent) {
final component = repo.aftConfig.componentForPackage(package.name);
final newVersion = versionUpdates[component] ?? package.version;
if (dependent.pubspecInfo.pubspec.dependencies.containsKey(package.name)) {
dependent.pubspecInfo.pubspecYamlEditor.update(
['dependencies', package.name],
newVersion.amplifyConstraint(minVersion: newVersion),
);
}
}

/// Bumps the version and changelog in [package] using [commit].
///
/// Returns the new version.
Version bumpVersion(
PackageInfo package, {
bool propogate = false,
CommitMessage? commit,
}) {
final component = repo.aftConfig.componentForPackage(package.name);
final currentVersion = package.version;
final currentProposedVersion = versionUpdates[component] ?? currentVersion;
final isBreakingChange = commit?.isBreakingChange ?? false;
final newProposedVersion = currentVersion.nextAmplifyVersion(
isBreakingChange: isBreakingChange,
);
final newVersion = versionUpdates[component] = maxBy(
[currentProposedVersion, newProposedVersion],
(version) => version,
)!;

if (newVersion > currentVersion) {
repo.logger.debug(
'Bumping ${package.name} from $currentProposedVersion to $newVersion: '
'${commit?.summary}',
);
package.pubspecInfo.pubspecYamlEditor.update(
['version'],
newVersion.toString(),
);
final currentChangelogUpdate = changelogUpdates[package];
changelogUpdates[package] = package.changelog.update(
commits: {
...?currentChangelogUpdate?.commits,
if (commit != null) commit,
},
version: newVersion,
);

if (propogate) {
// Propogate to all dependent packages.
dfs<PackageInfo>(
repo.reversedPackageGraph,
root: package,
(dependent) {
if (!dependent.isExample) {
bumpVersion(dependent, commit: commit);
bumpDependency(package, dependent);
}
},
);

// Propogate to all component packages.
repo.components[component]?.forEach((componentPackage) {
bumpVersion(componentPackage, commit: commit);
dfs<PackageInfo>(
repo.reversedPackageGraph,
root: componentPackage,
(dependent) {
bumpDependency(componentPackage, dependent);
},
);
});
}
}

return newVersion;
}

final sortedPackages = repo.publishablePackages;
sortPackagesTopologically(
sortedPackages,
(PackageInfo pkg) => pkg.pubspecInfo.pubspec,
);
for (final package in sortedPackages) {
final changes = changesForPackage(package);
final commits = changes.commitsByPackage[package]?.toSet() ?? const {};
for (final commit in commits) {
// TODO(dnys1): Define full set of commit types which should be ignored
// when considering version changes.
if (commit.isVersionBump ||
commit.type == CommitType.merge && commit.taggedPr == null) {
continue;
}
bumpVersion(
package,
commit: commit,
propogate: commit.isBreakingChange,
);
// Propogate the version change to all packages affected by the same
// commit.
changes.packagesByCommit[commit]?.forEach((commitPackage) {
bumpDependency(package, commitPackage);
});
}
}

return VersionChanges(
updatedChangelogs: changelogUpdates,
updatedVersions: versionUpdates,
);
}

class VersionChanges {
const VersionChanges({
required this.updatedChangelogs,
required this.updatedVersions,
});

final Map<String, Version> updatedVersions;
final Map<PackageInfo, ChangelogUpdate> updatedChangelogs;
}
5 changes: 0 additions & 5 deletions packages/aft/lib/src/options/git_ref_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:convert';
import 'dart:io';

import 'package:aft/aft.dart';
import 'package:aft/src/changelog/changelog.dart';
import 'package:aft/src/changelog/commit_message.dart';
import 'package:aft/src/changelog/printer.dart';
import 'package:pub_semver/pub_semver.dart';

/// Adds git ref options and functionality to a command.
mixin GitRefOptions on AmplifyCommand {
Expand Down
Loading

0 comments on commit 2854886

Please sign in to comment.