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

Commit

Permalink
Merge branch 'main' into alestiago/remove-generator-script
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Feb 23, 2024
2 parents cd5ad17 + be6a6b5 commit 575ba00
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/sync_labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: ♻️ Sync Labels

on:
push:
paths:
- .github/labels.yml
branches:
- main
workflow_dispatch:

jobs:
labels:
name: ♻️ Sync labels
runs-on: ubuntu-20.04
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v4

- name: 🚀 Run Label Sync
uses: srealmoreno/label-sync-action@v1
with:
config-file: https://raw.githubusercontent.com/VeryGoodOpenSource/.github/main/.github/labels.yml
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?
86 changes: 85 additions & 1 deletion brick/hooks/post_gen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +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;
}

Future<void> run(HookContext context) async {}
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

0 comments on commit 575ba00

Please sign in to comment.