Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

feat: allow optionally fixing template output #151

Merged
merged 2 commits into from
Feb 9, 2024
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
6 changes: 6 additions & 0 deletions brick/brick.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ vars:
description: Whether the generated package is intended to be published.
default: false
prompt: Will the package be published?
dart_fix_output:
type: boolean
description: >-
Whether or not the generated Dart output files should be fixed and formatted.
default: false
prompt: Should the generated output fix and format Dart files?
87 changes: 87 additions & 0 deletions brick/hooks/post_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'dart:io';

import 'package:mason/mason.dart';
import 'package:meta/meta.dart';
import 'package:very_good_flutter_plugin_hooks/src/cli/cli.dart';

/// The key for the `dartFixOutput` context variable.
@visibleForTesting
const dartFixOutputVariableKey = 'dartFixOutput';

Future<void> run(
HookContext context, {
@visibleForTesting VeryGoodCli veryGoodCli = VeryGoodCli.instance,
@visibleForTesting DartCli dartCli = DartCli.instance,
}) async {
final dartFixOutput = context.vars.containsKey(dartFixOutputVariableKey) &&
context.vars[dartFixOutputVariableKey] as bool;
if (dartFixOutput) {
await _dartFixOutput(
logger: context.logger,
workingDirectory: Directory.current.path,
veryGoodCli: veryGoodCli,
dartCli: dartCli,
);
}
}

/// Attempts to `dart` fix and format the output.
///
/// Since the template includes Dart files with templated variables, generating
/// the template can cause the generated Dart files to be invalid. For example,
/// if the user inputs a long enough package name, the generated Dart files may
/// exceed the 80 character line limit enforced by the the Dart formatter.
/// Running `dart fix` and `dart format` on the generated output ensures that
/// the generated output is always valid.
///
/// Before we can run `dart fix` and `dart format`, we need to ensure that the
/// dependencies are installed. Doing so allows getting remote analysis options
/// and allows the Dart code to resolve the imports.
///
/// If the [DartCli] or [VeryGoodCli] is not installed, this function will log
/// a warning and return immediately.
Future<void> _dartFixOutput({
required Logger logger,
required String workingDirectory,
required VeryGoodCli veryGoodCli,
required DartCli dartCli,
}) async {
if (!await dartCli.isInstalled(logger: logger)) {
logger.warn(
'''Could not fix output because Dart CLI is not installed.''',
);
return;
}
if (!await veryGoodCli.isInstalled(logger: logger)) {
logger.warn(
'''Could not fix output because Very Good CLI is not installed.''',
);
return;
}

try {
await veryGoodCli.packagesGet(
logger: logger,
recursive: true,
cwd: workingDirectory,
);
await dartCli.fix(
logger: logger,
apply: true,
cwd: workingDirectory,
);
await dartCli.format(
logger: logger,
cwd: workingDirectory,
);
} on ProcessException catch (e) {
logger.err(
'''
Running process ${e.executable} with ${e.arguments} failed:
${e.message}
''',
);
} catch (e) {
logger.err('Unknown error occurred when fixing output: $e');
}
}
2 changes: 1 addition & 1 deletion brick/hooks/test/cli/dart_cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:io';
import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
import 'package:very_good_start_hooks/cli/cli.dart';
import 'package:very_good_flutter_plugin_hooks/src/cli/cli.dart';

class _TestProcess {
Future<ProcessResult> run(
Expand Down
Loading