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

Add reformat tweak #197

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/blast_repo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ available tweaks:
github-actions: ensure GitHub actions use the latest versions and are keyed by SHA
monorepo: regenerate the latest configuration files for package:mono_repo
no-response: configure a 'no response' bot to handle needs-info labels
reformat: file a PR for a run of `dart format`
```
2 changes: 2 additions & 0 deletions pkgs/blast_repo/lib/src/top_level.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'tweaks/dependabot_tweak.dart';
import 'tweaks/github_action_tweak.dart';
import 'tweaks/mono_repo_tweak.dart';
import 'tweaks/no_reponse_tweak.dart';
import 'tweaks/reformat_tweak.dart';
import 'utils.dart';

final allTweaks = Set<RepoTweak>.unmodifiable([
Expand All @@ -22,6 +23,7 @@ final allTweaks = Set<RepoTweak>.unmodifiable([
GitHubActionTweak(),
MonoRepoTweak(),
NoResponseTweak(),
ReformatTweak(),
]);

Future<void> runFix({
Expand Down
49 changes: 49 additions & 0 deletions pkgs/blast_repo/lib/src/tweaks/reformat_tweak.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:firehose/firehose.dart';
mosuem marked this conversation as resolved.
Show resolved Hide resolved

import '../repo_tweak.dart';
import '../utils.dart';

final _instance = ReformatTweak._();

/// Reformat the Dart files in all packages in a repo.
class ReformatTweak extends RepoTweak {
factory ReformatTweak() => _instance;

ReformatTweak._()
: super(
id: 'reformat',
description: 'file a PR for a run of `dart format`',
);

@override
bool shouldRunByDefault(Directory checkout, String repoSlug) => true;

@override
FutureOr<FixResult> fix(Directory checkout, String repoSlug) async {
var repo = Repository(checkout);
var packages = repo.locatePackages();

final fixes = <String>[];
for (var package in packages) {
var exitCode = await runProc(
'running dart format for ${package.name}',
Platform.resolvedExecutable,
['format', '.', '--set-exit-if-changed'],
workingDirectory: package.directory.path,
throwOnFailure: false,
);
if (exitCode == 1) {
mosuem marked this conversation as resolved.
Show resolved Hide resolved
fixes.add(package.name);
}
}

return fixes.isEmpty ? FixResult.noFixesMade : FixResult(fixes: fixes);
}
}
7 changes: 5 additions & 2 deletions pkgs/blast_repo/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ extension GitDirExtension on GitDir {
}
}

Future<void> runProc(
Future<int> runProc(
String description,
String proc,
List<String> args, {
required String workingDirectory,
bool throwOnFailure = true,
}) async {
printHeader(description);

Expand All @@ -64,9 +65,11 @@ Future<void> runProc(

final exitCode = await ghProc.exitCode;

if (exitCode != 0) {
if (throwOnFailure && exitCode != 0) {
throw ProcessException(proc, args, 'Process failed', exitCode);
}

return exitCode;
}

Future<void> withSystemTemp(
Expand Down
2 changes: 2 additions & 0 deletions pkgs/blast_repo/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ environment:
dependencies:
args: ^2.3.1
collection: ^1.17.0
firehose:
Copy link
Member

Choose a reason for hiding this comment

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

I would avoid this path-relative dep if possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought that blast_repo not being published means we can relax the principle of reducing the number of imports :) I am also fine with code duplication if that's the lesser evil.

Copy link
Member

Choose a reason for hiding this comment

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

It's not wrong - avoiding publishing another package is more important. Perhaps add a line of docs here - # Re-using the repo scanning logic from firehose or similar.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure I understand - does this mean you would rather want to copy the code or add the dep with a comment?

path: ../firehose
git: ^2.2.0
github: ^9.6.0
io: ^1.0.0
Expand Down
114 changes: 114 additions & 0 deletions pkgs/blast_repo/test/reformat_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io' as io;

import 'package:blast_repo/src/tweaks/reformat_tweak.dart';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

void main() {
late ReformatTweak tweak;
late io.Directory singleRepo;
late io.Directory multiRepo;
late io.Directory nochangeRepo;

setUp(() async {
tweak = ReformatTweak();

await d.dir('nochange', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]).create();
nochangeRepo = d.dir('single').io;

await d.dir('single', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', unformatted),
]).create();
singleRepo = d.dir('single').io;

await d.dir('multi', [
d.dir('package1', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', unformatted),
]),
d.dir('package2', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', unformatted),
]),
d.dir('package3', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]),
]).create();

nochangeRepo = d.dir('nochange').io;
singleRepo = d.dir('single').io;
multiRepo = d.dir('multi').io;
});
test('does nothing on formatted repo', () async {
var results = await tweak.fix(nochangeRepo, 'my_org/my_repo');
expect(results.fixes, isEmpty);

await d.dir('nochange', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]).validate();
});

test('formats single package repo', () async {
var results = await tweak.fix(singleRepo, 'my_org/my_repo');
expect(results.fixes, isNotEmpty);

await d.dir('single', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]).validate();
});

test('formats multi package repo', () async {
var results = await tweak.fix(multiRepo, 'my_org/my_repo');
expect(results.fixes, isNotEmpty);

await d.dir('multi', [
d.dir('package1', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]),
d.dir('package2', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]),
d.dir('package3', [
d.file('pubspec.yaml', pubspec),
d.file('something.dart', formatted),
]),
]).validate();
});
}

final unformatted = '''
void main() {
var i = 0;
}
''';

final formatted = '''
void main() {
var i = 0;
}
''';

final pubspec = '''
name: test_package
environment:
sdk: ^3.0.0

dependencies:
args: ^2.3.1

dev_dependencies:
test: ^1.22.0
''';
2 changes: 2 additions & 0 deletions pkgs/firehose/lib/firehose.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'src/pub.dart';
import 'src/repo.dart';
import 'src/utils.dart';

export 'src/repo.dart';

const String _botSuffix = '[bot]';

const String _githubActionsUser = 'github-actions[bot]';
Expand Down
1 change: 0 additions & 1 deletion pkgs/firehose/lib/src/health/health.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:pub_semver/pub_semver.dart';

import '../../firehose.dart';
import '../github.dart';
import '../repo.dart';
import '../utils.dart';
import 'changelog.dart';
import 'coverage.dart';
Expand Down