refactor: add formatting script

This commit is contained in:
Jake Hamilton 2024-06-22 10:56:18 -07:00
parent 403420fd4b
commit 3479354ec9
Signed by: jakehamilton
GPG key ID: 9762169A1B35EA68
7 changed files with 105 additions and 121 deletions

4
format.sh Executable file
View file

@ -0,0 +1,4 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -I "nixpkgs=https://github.com/nixos/nixpkgs/archive/nixos-24.05.tar.gz" -p nixfmt-rfc-style
nixfmt ${1:-"--verify"} ./**/*.nix

View file

@ -1,14 +1,13 @@
{system ? builtins.currentSystem}: let {
system ? builtins.currentSystem,
}:
let
lib = import ./../lib; lib = import ./../lib;
modules = import ./src; modules = import ./src;
result = lib.modules.run { result = lib.modules.run {
modules = modules = (builtins.attrValues modules) ++ [ { config.aux.system = system; } ];
(builtins.attrValues modules)
++ [
{config.aux.system = system;}
];
}; };
in in
result.config.exports.resolved.packages result.config.exports.resolved.packages

View file

@ -7,34 +7,30 @@
}; };
}; };
outputs = inputs: let outputs =
inherit (inputs.lib) lib; inputs:
let
inherit (inputs.lib) lib;
modules = import ./src; modules = import ./src;
forEachSystem = lib.attrs.generate [ forEachSystem = lib.attrs.generate [ "i686-linux" ];
"i686-linux"
];
in {
extras = let
result = lib.modules.run {
modules =
builtins.attrValues modules;
};
in in
result.config.exports.resolved.extras; {
extras =
let
result = lib.modules.run { modules = builtins.attrValues modules; };
in
result.config.exports.resolved.extras;
packages = forEachSystem ( packages = forEachSystem (
system: let system:
result = lib.modules.run { let
modules = result = lib.modules.run {
(builtins.attrValues modules) modules = (builtins.attrValues modules) ++ [ { config.aux.system = system; } ];
++ [ };
{config.aux.system = system;} in
];
};
in
result.config.exports.resolved.packages result.config.exports.resolved.packages
); );
}; };
} }

View file

@ -25,54 +25,51 @@ let
./src/versions/default.test.nix ./src/versions/default.test.nix
]; ];
resolve = file: let resolve =
imported = import file; file:
value = let
if builtins.isFunction imported imported = import file;
then imported {inherit lib;} value = if builtins.isFunction imported then imported { inherit lib; } else imported;
else imported; relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file);
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file); in
in { {
inherit file value; inherit file value;
relative = relative = if lib.strings.hasPrefix "/" relative then "." + relative else relative;
if lib.strings.hasPrefix "/" relative namespace = getNamespace file;
then "." + relative };
else relative;
namespace = getNamespace file;
};
resolved = builtins.map resolve files; resolved = builtins.map resolve files;
getNamespace = path: let getNamespace =
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString path); path:
parts = lib.strings.split "/" relative; let
in relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString path);
if builtins.length parts > 2 parts = lib.strings.split "/" relative;
then builtins.elemAt parts 2
else relative;
results = let
getTests = file: prefix: suite: let
nested = lib.attrs.mapToList (name: value: getTests file (prefix ++ [name]) value) suite;
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file);
in in
if builtins.isAttrs suite if builtins.length parts > 2 then builtins.elemAt parts 2 else relative;
then builtins.concatLists nested
else [
{
inherit prefix file;
name = builtins.concatStringsSep " > " prefix;
value = suite;
relative =
if lib.strings.hasPrefix "/" relative
then "." + relative
else relative;
}
];
base = results =
builtins.map (entry: getTests entry.file [entry.namespace] entry.value) resolved; let
in getTests =
file: prefix: suite:
let
nested = lib.attrs.mapToList (name: value: getTests file (prefix ++ [ name ]) value) suite;
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file);
in
if builtins.isAttrs suite then
builtins.concatLists nested
else
[
{
inherit prefix file;
name = builtins.concatStringsSep " > " prefix;
value = suite;
relative = if lib.strings.hasPrefix "/" relative then "." + relative else relative;
}
];
base = builtins.map (entry: getTests entry.file [ entry.namespace ] entry.value) resolved;
in
builtins.concatLists base; builtins.concatLists base;
successes = builtins.filter (test: test.value) results; successes = builtins.filter (test: test.value) results;
@ -80,29 +77,23 @@ let
total = "${builtins.toString (builtins.length successes)} / ${builtins.toString (builtins.length results)}"; total = "${builtins.toString (builtins.length successes)} / ${builtins.toString (builtins.length results)}";
in in
if failures == [] if failures == [ ] then
then let let
message = message = lib.strings.concatMapSep "\n" (test: " ${test.name}") successes;
lib.strings.concatMapSep "\n" in
(test: " ${test.name}") ''
successes;
in ''
SUCCESS (${total}) SUCCESS (${total})
${message} ${message}
'' ''
else let else
successMessage = let
lib.strings.concatMapSep "\n" successMessage = lib.strings.concatMapSep "\n" (test: " ${test.name}") successes;
(test: " ${test.name}") failureMessage = lib.strings.concatMapSep "\n\n" (
successes; test: " ${test.name}\n" + " -> ${test.relative}"
failureMessage = ) failures;
lib.strings.concatMapSep "\n\n" in
(test: ''
" ${test.name}\n"
+ " -> ${test.relative}")
failures;
in ''
FAILURE (${total}) FAILURE (${total})
${failureMessage} ${failureMessage}

View file

@ -1,7 +1,5 @@
{ {
description = "A NixPkgs library replacement containing helper functions and a module system."; description = "A NixPkgs library replacement containing helper functions and a module system.";
outputs = _: { outputs = _: { lib = import ./src; };
lib = import ./src;
};
} }

View file

@ -1,27 +1,21 @@
{ {
lib ? import ./../lib, lib ? import ./../lib,
foundation ? import ./../foundation {system = "i686-linux";}, foundation ? import ./../foundation { system = "i686-linux"; },
}: let }:
let
modules = import ./src/modules.nix; modules = import ./src/modules.nix;
result = lib.modules.run { result = lib.modules.run {
modules = modules = (builtins.attrValues modules) ++ [
(builtins.attrValues modules) ./src/export.nix
++ [ {
./src/export.nix __file__ = ./default.nix;
{
__file__ = ./default.nix;
options.foundation = lib.options.create { options.foundation = lib.options.create { type = lib.types.attrs.of lib.types.derivation; };
type = lib.types.attrs.of lib.types.derivation;
};
config.foundation = foundation; config.foundation = foundation;
} }
]; ];
}; };
in in
result.config.exported result.config.exported // { inherit (result) config; }
// {
inherit (result) config;
}

View file

@ -9,11 +9,13 @@
}; };
}; };
outputs = inputs: let outputs =
exports = import ./default.nix { inputs:
lib = inputs.lib.lib; let
foundation = inputs.foundation.packages.i686-linux; exports = import ./default.nix {
}; lib = inputs.lib.lib;
in foundation = inputs.foundation.packages.i686-linux;
};
in
exports; exports;
} }