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 30, 2022
1 parent 55e00ba commit 048f836
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 325 deletions.
7 changes: 1 addition & 6 deletions aft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ dependencies:
ignore:
- synthetic_package

# Branch names which map to pub.dev stable and prerelease tracks.
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
4 changes: 2 additions & 2 deletions packages/aft/lib/src/changelog/changelog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import 'dart:convert';

import 'package:aft/src/changelog/commit_message.dart';
import 'package:aft/src/changelog/printer.dart';
import 'package:aws_common/aws_common.dart';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:cli_util/cli_logging.dart';
import 'package:collection/collection.dart';
import 'package:markdown/markdown.dart';
import 'package:pub_semver/pub_semver.dart';
Expand Down Expand Up @@ -46,7 +46,7 @@ abstract class Changelog implements Built<Changelog, ChangelogBuilder> {
/// changelog.
factory Changelog.parse(
String changelogMd, {
Logger? logger,
AWSLogger? logger,
}) {
final parser = Document();
final lines = LineSplitter.split(changelogMd).toList();
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
4 changes: 2 additions & 2 deletions packages/aft/lib/src/changelog/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final RegExp semverRegex = RegExp(r'\d+\.\d+\.\d+[\d\w\.\+\-]*');
class _ChangelogParser implements NodeVisitor {
_ChangelogParser(this.logger);

final Logger? logger;
final AWSLogger? logger;

final builder = ChangelogBuilder();

Expand All @@ -40,7 +40,7 @@ class _ChangelogParser implements NodeVisitor {
}
final versionMatch = semverRegex.firstMatch(versionText)?.group(0);
if (versionMatch == null) {
logger?.trace('Could not parse version: $versionText');
logger?.debug('Could not parse version: $versionText');
break;
}
_currentVersion = Version.parse(versionMatch);
Expand Down
1 change: 1 addition & 0 deletions packages/aft/lib/src/changelog/printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import 'package:aft/src/changelog/changelog.dart';
import 'package:markdown/markdown.dart';

/// Renders the [markdown] AST as a string.
String render(Iterable<Node> markdown) {
final renderer = _MarkdownRenderer();
for (final node in markdown) {
Expand Down
56 changes: 53 additions & 3 deletions packages/aft/lib/src/commands/amplify_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:collection';
import 'dart:io';

import 'package:aft/aft.dart';
import 'package:aft/src/repo.dart';
import 'package:args/command_runner.dart';
import 'package:aws_common/aws_common.dart';
import 'package:checked_yaml/checked_yaml.dart';
import 'package:git/git.dart' as git;
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
Expand Down Expand Up @@ -105,10 +108,57 @@ abstract class AmplifyCommand extends Command<void>
);
}();

late final Repo repo = Repo(rootDir, logger: logger);
/// All packages in the Amplify Flutter repo.
late final Map<String, PackageInfo> allPackages = () {
final allDirs = rootDir
.listSync(recursive: true, followLinks: false)
.whereType<Directory>();
final allPackages = <PackageInfo>[];
for (final dir in allDirs) {
final pubspecInfo = dir.pubspec;
if (pubspecInfo == null) {
continue;
}
final pubspec = pubspecInfo.pubspec;
if (aftConfig.ignore.contains(pubspec.name)) {
continue;
}
allPackages.add(
PackageInfo(
name: pubspec.name,
path: dir.path,
usesMonoRepo: dir.usesMonoRepo,
pubspecInfo: pubspecInfo,
flavor: pubspec.flavor,
),
);
}
return UnmodifiableMapView({
for (final package in allPackages..sort()) package.name: package,
});
}();

/// The global `aft` configuration for the repo.
late final AftConfig aftConfig = () {
final configFile = File(p.join(rootDir.path, 'aft.yaml'));
assert(configFile.existsSync(), 'Could not find aft.yaml');
final configYaml = configFile.readAsStringSync();
return checkedYamlDecode(configYaml, AftConfig.fromJson);
}();

Map<String, PackageInfo> get allPackages => repo.allPackages;
AftConfig get aftConfig => repo.aftConfig;
late final Repo repo = Repo(
rootDir,
allPackages: allPackages,
aftConfig: aftConfig,
logger: logger,
);

/// Runs `git` with the given [args] from the repo's root directory.
Future<void> runGit(List<String> args) => git.runGit(
args,
processWorkingDir: rootDir.path,
throwOnError: true,
);

/// A command runner for `pub`.
PubCommandRunner createPubRunner() => PubCommandRunner(
Expand Down
33 changes: 27 additions & 6 deletions packages/aft/lib/src/commands/changelog_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,29 @@ class ChangelogCommand extends AmplifyCommand {

abstract class _ChangelogBaseCommand extends AmplifyCommand
with GitRefOptions, GlobOptions {
_ChangelogBaseCommand() {
argParser.addFlag(
'yes',
abbr: 'y',
help: 'Responds "yes" to all prompts',
defaultsTo: false,
negatable: false,
);
}

late final bool yes = argResults!['yes'] as bool;

Future<void> _updateChangelogs({required bool preview}) async {
for (final package in repo.publishablePackages) {
final baseRef = this.baseRef ??
repo.latestTag(package.name) ??
repo.latestTag('amplify_flutter')!;
final baseRef = this.baseRef ?? repo.latestTag(package.name);
if (baseRef == null) {
exitError(
'No tag exists for package (${package.name}). '
'Supply a base ref manually using --base-ref',
);
}
final changes = repo.changes(baseRef, headRef);
final commits =
changes.commitsByPackage[package.name]?.toSet() ?? const {};
final commits = changes.commitsByPackage[package]?.toSet() ?? const {};
final changelogUpdate = package.changelog.update(commits: commits);
if (preview) {
if (changelogUpdate.hasUpdate) {
Expand Down Expand Up @@ -83,6 +98,12 @@ class _ChangelogUpdateCommand extends _ChangelogBaseCommand {

@override
Future<void> run() async {
return _updateChangelogs(preview: false);
await _updateChangelogs(preview: false);

logger.info('Changelogs successfully updated');
if (yes || prompt('Commit changes? (y/N) ').toLowerCase() == 'y') {
await runGit(['add', '.']);
await runGit(['commit', '-m', 'chore(version): Update changelogs']);
}
}
}
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));
});
}
Loading

0 comments on commit 048f836

Please sign in to comment.