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

[ocamlformat]: Version detection #41

Merged
merged 5 commits into from Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 33 additions & 4 deletions programs/ocamlformat.nix
Expand Up @@ -4,17 +4,46 @@
, ...
}:
let
l = lib // builtins;
cfg = config.programs.ocamlformat;

detectVersion = configFile: pkgSet:
let
optionValue = list:
assert l.assertMsg (list != [ ]) "treefmt-nix: Unable to detect ocamlformat version from file";
l.elemAt list (l.length list - 1);
in
l.getAttr "ocamlformat_${
l.replaceStrings ["."] ["_"]
(optionValue (l.findFirst (option: l.head option == "version") []
(l.map (n: l.splitString "=" n) (l.splitString "\n" (l.readFile configFile)))))
}"
pkgSet;
in
{
options.programs.ocamlformat = {
enable = lib.mkEnableOption "ocamlformat";
package = lib.mkPackageOption pkgs "ocamlformat" { };
enable = l.mkEnableOption "ocamlformat";
package = l.mkPackageOption pkgs "ocamlformat" { };

pkgs = l.mkOption {
type = l.types.lazyAttrsOf l.types.raw;
description = "The package set used to get the ocamlformat package at a specific version. defaults to nixpkgs.";
default = pkgs;
};

configFile = l.mkOption {
type = l.types.nullOr l.types.path;
description = "Path to the .ocamlformat file. Used to pick the right version of ocamlformat if passed.";
default = null;
};
};

config = lib.mkIf cfg.enable {
config = l.mkIf cfg.enable {
settings.formatter.ocamlformat = {
command = cfg.package;
command =
if l.isNull cfg.configFile
then cfg.package
else detectVersion cfg.configFile cfg.pkgs;
options = [ "-i" ];
includes = [ "*.ml" "*.mli" ];
};
Expand Down