diff --git a/.config/filegen-manifest.json b/.config/filegen-manifest.json index 4a6d1dd..6245d9d 100644 --- a/.config/filegen-manifest.json +++ b/.config/filegen-manifest.json @@ -5,7 +5,7 @@ "clobber": true, "deactivate": null, "ignore-modification": null, - "source": "/nix/store/7lmqw9hjcp0wrrcr1fn8vgyjcpsiryns-pre-commit-config.yaml", + "source": "/nix/store/yhbigqfbyr91f9si3b6y6ib5558ddzx3-pre-commit-config.yaml", "target": "./.pre-commit-config.yaml", "type": "copy" }, @@ -13,7 +13,15 @@ "clobber": true, "deactivate": null, "ignore-modification": null, - "source": "/nix/store/lai6c0kl9c0r0j1ld4j8ilrw53k3bxmq-rust-test.yml", + "source": "/nix/store/1i9xz4fis8a12qlv03ss9smrhhv8nicn-dart-test.yml", + "target": "./.github/workflows/dart-test.yml", + "type": "copy" + }, + { + "clobber": true, + "deactivate": null, + "ignore-modification": null, + "source": "/nix/store/59cxdgpnmw5y43fhn7q2bcvz0jv5v9b3-rust-test.yml", "target": "./.github/workflows/rust-test.yml", "type": "copy" }, @@ -21,7 +29,7 @@ "clobber": true, "deactivate": null, "ignore-modification": null, - "source": "/nix/store/glxwjx24212kdvwwwpzcqkhdzck96cvi-envrc", + "source": "/nix/store/qxl4d2ablir1lzshp9manp44nkl0328x-envrc", "target": "./.envrc", "type": "copy" } diff --git a/nix/dart/default.nix b/nix/dart/default.nix new file mode 100644 index 0000000..5cbecca --- /dev/null +++ b/nix/dart/default.nix @@ -0,0 +1,34 @@ +{ + flake-parts-lib, + lib, + importApply, + ... +}@args: +importingFlake: { + imports = [ + (importApply ./pre-commit-hooks.nix args) + + ./workflows/workflow.nix + ]; + + options.perSystem = flake-parts-lib.mkPerSystemOption ({ + options.famedly.standards.dart.projects = lib.mkOption { + description = '' + Dart projects in the repository that should be equipped with our + standards. + + This must be a relative path starting with `.`. Simply use `.` if the + whole project is a Dart project. + ''; + default = { }; + + example = '' + { + "." = { }; + } + ''; + + type = lib.types.attrsOf (lib.types.submodule { }); + }; + }); +} diff --git a/nix/dart/pre-commit-hooks.nix b/nix/dart/pre-commit-hooks.nix new file mode 100644 index 0000000..1378bdd --- /dev/null +++ b/nix/dart/pre-commit-hooks.nix @@ -0,0 +1,84 @@ +{ + flakeModules, + inputs, + flake-parts-lib, + lib, + ... +}: +importingFlake: { + config.perSystem = + { + pkgs, + config, + ... + }: + let + dart = lib.getExe pkgs.dart; + grep = lib.getExe pkgs.gnugrep; + + check-conventional-commits = pkgs.writeShellScript "check-conventional-commits" '' + set -eu + + commit_message_file="$1" + accepted_prefixes='ci|fix|feat|chore|test|perf|refactor|style|builds|docs|revert' + + if ! ${grep} -Eq "^($accepted_prefixes)(\([[:alnum:]_.-]+\))?!?: .+" "$commit_message_file"; then + echo "Commit message must start with one of: $accepted_prefixes" + exit 1 + fi + ''; + + dart-analyze = pkgs.writeShellScript "dart-analyze" '' + set -eu + + if [ -f pubspec.yaml ] && ${grep} -Eq '^flutter:' pubspec.yaml; then + command=flutter + else + command=${dart} + fi + + ${dart} format lib/ --set-exit-if-changed + "$command" pub get + "$command" analyze + + if command -v dart_code_metrics >/dev/null 2>&1; then + dart_code_metrics analyze lib || true + fi + ''; + in + { + prek-pre-commit.workspaces = lib.mapAttrs (name: _: { + default_language_version.dart = "system"; + + repos = [ + { + repo = "local"; + hooks = [ + { + id = "check-conventional-commits"; + name = "check-conventional-commits"; + description = "Check commit messages follow conventional commits"; + + entry = "${check-conventional-commits}"; + + language = "system"; + stages = [ "commit-msg" ]; + } + + { + id = "dart-analyze"; + name = "dart-analyze"; + description = "Run dart analyze on all targets"; + + entry = "${dart-analyze}"; + + language = "system"; + types = [ "dart" ]; + pass_filenames = false; + } + ]; + } + ]; + }) config.famedly.standards.dart.projects; + }; +} diff --git a/nix/dart/workflows/default.nix b/nix/dart/workflows/default.nix new file mode 100644 index 0000000..af7310a --- /dev/null +++ b/nix/dart/workflows/default.nix @@ -0,0 +1,34 @@ +{ + flake-parts-lib, + lib, + importApply, + ... +}@args: +importingFlake: { + imports = [ + (importApply ./pre-commit-hooks.nix args) + + ./workflow.nix + ]; + + options.perSystem = flake-parts-lib.mkPerSystemOption ({ + options.famedly.standards.dart.projects = lib.mkOption { + description = '' + Dart projects in the repository that should be equipped with our + standards. + + This must be a relative path starting with `.`. Simply use `.` if the + whole project is a Dart project. + ''; + default = { }; + + example = '' + { + "." = { }; + } + ''; + + type = lib.types.attrsOf (lib.types.submodule { }); + }; + }); +} diff --git a/nix/dart/workflows/workflow.nix b/nix/dart/workflows/workflow.nix new file mode 100644 index 0000000..f389cb0 --- /dev/null +++ b/nix/dart/workflows/workflow.nix @@ -0,0 +1,56 @@ +{ config, lib, ... }: +let + allowed-actions = config.famedly.standards.allowed-action-versions; +in +{ + perSystem = + { config, ... }: + let + run-pre-commit = project: '' + nix develop .#general --command sh -c ${lib.escapeShellArg "cd ${lib.escapeShellArg project} && prek run --all-files"} + ''; + dart-project-steps = lib.mapAttrsToList (project: _: { + name = "Run Dart pre-commit checks (${project})"; + run = run-pre-commit project; + }) config.famedly.standards.dart.projects; + in + { + githubActions.workflows.dart-test = { + name = "Dart test workflow"; + + on = { + push = { + branches = [ "main" ]; + tags = [ "*" ]; + }; + + pullRequest = { + branches = [ "*" ]; + types = [ + "opened" + "reopened" + "synchronize" + "ready_for_review" + ]; + }; + }; + + concurrency = { + group = "\${{ github.workflow }}-\${{ github.ref }}"; + cancelInProgress = true; + }; + + defaults.run.shell = "nu --no-config-file --no-history {0}"; + env.DART_TERM_COLOR = "always"; + + jobs.test = { + runsOn = "ubuntu-latest-4core"; + + steps = [ + { uses = allowed-actions."actions/checkout".uses; } + { uses = allowed-actions."cachix/install-nix-action".uses; } + ] ++ dart-project-steps; + }; + }; + }; +} diff --git a/nix/default.nix b/nix/default.nix index 502066c..ed7b994 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -11,6 +11,7 @@ importingFlake: { flakeModules.prek-pre-commit (importApply ./general args) + (importApply ./dart args) (importApply ./rust args) ]; }