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

sz CLI: Move logic out of package.dart. #618

Merged
merged 2 commits into from
Apr 7, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions tools/sz_repo_cli/lib/src/commands/src/analyze_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

import 'dart:async';

import 'package:sz_repo_cli/src/commands/src/format_command.dart';
import 'package:sz_repo_cli/src/common/common.dart';

import 'fix_comment_spacing_command.dart';
import 'pub_get_command.dart';

class AnalyzeCommand extends ConcurrentCommand {
AnalyzeCommand(SharezoneRepo repo) : super(repo);

Expand All @@ -24,7 +28,26 @@ class AnalyzeCommand extends ConcurrentCommand {
Duration get defaultPackageTimeout => Duration(minutes: 5);

@override
Future<void> runTaskForPackage(Package package) {
return package.analyzePackage();
Future<void> runTaskForPackage(Package package) => analyzePackage(package);
}

Future<void> analyzePackage(Package package) async {
await getPackage(package);
await _runDartAnalyze(package);
await formatCode(package, throwIfCodeChanged: true);
await _checkForCommentsWithBadSpacing(package);
}

Future<void> _runDartAnalyze(Package package) {
return runProcessSucessfullyOrThrow(
'fvm', ['dart', 'analyze', '--fatal-infos', '--fatal-warnings'],
workingDirectory: package.path);
}

Future<void> _checkForCommentsWithBadSpacing(Package package) async {
if (doesPackageIncludeFilesWithBadCommentSpacing(package.path)) {
throw Exception(
'Package ${package.name} has comments with bad spacing. Fix them by running the `sz fix-comment-spacing` command.');
}
return;
}
21 changes: 18 additions & 3 deletions tools/sz_repo_cli/lib/src/commands/src/format_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ class FormatCommand extends ConcurrentCommand {
Duration get defaultPackageTimeout => Duration(minutes: 3);

@override
Future<void> runTaskForPackage(Package package) {
return package.formatCode();
}
Future<void> runTaskForPackage(Package package) => formatCode(package);
}

Future<void> formatCode(
Package package, {
/// Throws if code is not already formatted properly.
/// Useful for code analysis in CI.
bool throwIfCodeChanged = false,
}) {
return runProcessSucessfullyOrThrow(
'fvm',
[
'dart',
'format',
if (throwIfCodeChanged) '--set-exit-if-changed',
'.',
],
workingDirectory: package.path);
}
23 changes: 21 additions & 2 deletions tools/sz_repo_cli/lib/src/commands/src/pub_get_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,26 @@ class PubGetCommand extends ConcurrentCommand {
Duration get defaultPackageTimeout => Duration(minutes: 5);

@override
Future<void> runTaskForPackage(Package package) {
return package.getPackages();
Future<void> runTaskForPackage(Package package) => getPackage(package);
}

Future<void> getPackage(Package package) async {
if (package.isFlutterPackage) {
await getPackagesFlutter(package);
} else {
await getPackagesDart(package);
}
}

Future<void> getPackagesDart(Package package) async {
await runProcessSucessfullyOrThrow('fvm', ['dart', 'pub', 'get'],
workingDirectory: package.path);
}

Future<void> getPackagesFlutter(Package package) async {
await runProcessSucessfullyOrThrow(
'fvm',
['flutter', 'pub', 'get'],
workingDirectory: package.path,
);
}
96 changes: 94 additions & 2 deletions tools/sz_repo_cli/lib/src/commands/src/test_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

import 'dart:async';

import 'package:meta/meta.dart';
import 'package:sz_repo_cli/src/common/common.dart';

import 'pub_get_command.dart';

class TestCommand extends ConcurrentCommand {
TestCommand(SharezoneRepo repo) : super(repo) {
argParser.addFlag(
Expand Down Expand Up @@ -44,7 +47,7 @@ class TestCommand extends ConcurrentCommand {
if (argResults['only-goldens'] as bool) {
return repo.streamPackages().where(
(package) =>
package is FlutterPackage && package.hasGoldenTestsDirectory,
package.isFlutterPackage && package.hasGoldenTestsDirectory,
);
}

Expand All @@ -53,9 +56,98 @@ class TestCommand extends ConcurrentCommand {

@override
Future<void> runTaskForPackage(Package package) {
return package.runTests(
return runTests(
package,
excludeGoldens: argResults['exclude-goldens'] as bool,
onlyGoldens: argResults['only-goldens'] as bool,
);
}
}

Future<void> runTests(
Package package, {
@required bool excludeGoldens,
@required bool onlyGoldens,
}) {
if (package.isFlutterPackage) {
return _runTestsFlutter(
package,
excludeGoldens: excludeGoldens,
onlyGoldens: onlyGoldens,
);
} else {
return _runTestsDart(
package,
excludeGoldens: excludeGoldens,
onlyGoldens: onlyGoldens,
);
}
}

Future<void> _runTestsDart(
Package package, {
// We can ignore the "excludeGoldens" parameter here because Dart packages
// don't have golden tests.
@required bool excludeGoldens,
@required bool onlyGoldens,
}) async {
if (onlyGoldens) {
// Golden tests are only run in the flutter package.
return;
}

await getPackage(package);

await runProcessSucessfullyOrThrow(
'fvm',
['dart', 'test'],
workingDirectory: package.path,
);
}

Future<void> _runTestsFlutter(
Package package, {
@required bool excludeGoldens,
@required bool onlyGoldens,
}) async {
if (onlyGoldens) {
if (!package.hasGoldenTestsDirectory) {
return;
}

await runProcessSucessfullyOrThrow(
'fvm',
['flutter', 'test', 'test_goldens'],
workingDirectory: package.path,
);
return;
}

// If the package has no golden tests, we need to use the normal test
// command. Otherwise the throws the Flutter tool throws an error that it
// couldn't find the "test_goldens" directory.
if (excludeGoldens || !package.hasGoldenTestsDirectory) {
await runProcessSucessfullyOrThrow(
'fvm',
['flutter', 'test'],
workingDirectory: package.path,
);
return;
}

/// Flutter test lässt automatisch flutter pub get laufen.
/// Deswegen muss nicht erst noch [getPackages] aufgerufen werden.

await runProcessSucessfullyOrThrow(
'fvm',
[
'flutter',
'test',
// Directory for golden tests.
'test_goldens',
// Directory for unit and widget tests.
'test',
],
workingDirectory: package.path,
);
}
Loading