From 11c014f13769d9c065d571aae8a2df74e211c7a5 Mon Sep 17 00:00:00 2001 From: Diego Restrepo Date: Sat, 30 Oct 2021 08:04:32 -0500 Subject: [PATCH] feat(build): #753 add scala formatter - i decided not to use nixpkgs.scalafmt because it uses an old version (v2.6.4) - nixpkgs.scalafmt compiles on site, this makes longer dependencies - i download the binary and java-jdk is not required --- .mailmap | 1 + src/evaluator/modules/default.nix | 1 + .../modules/format-scala/default.nix | 53 +++++++++++++++++++ .../modules/format-scala/entrypoint.sh | 11 ++++ 4 files changed, 66 insertions(+) create mode 100644 src/evaluator/modules/format-scala/default.nix create mode 100644 src/evaluator/modules/format-scala/entrypoint.sh diff --git a/.mailmap b/.mailmap index 56064160..5016fc74 100644 --- a/.mailmap +++ b/.mailmap @@ -8,6 +8,7 @@ Diego Restrepo Diego Restrepo Mesa <36453706+drest Fluid Attacks Fluid Attacks Github Dependabot <49699333+dependabot[bot]@users.noreply.github.com> dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Github Octocat GitHub +John Perez John Perez <34050007+jpverde@users.noreply.github.com> Kevin Amado Kevin Amado Luis Saavedra Luis David Saavedra <40694133+ludsrill@users.noreply.github.com> Luis Saavedra Luis Saavedra diff --git a/src/evaluator/modules/default.nix b/src/evaluator/modules/default.nix index 1dafd17a..d0d9c034 100644 --- a/src/evaluator/modules/default.nix +++ b/src/evaluator/modules/default.nix @@ -21,6 +21,7 @@ (import ./format-markdown/default.nix args) (import ./format-nix/default.nix args) (import ./format-python/default.nix args) + (import ./format-scala/default.nix args) (import ./format-javascript/default.nix args) (import ./format-terraform/default.nix args) (import ./hello-world/default.nix args) diff --git a/src/evaluator/modules/format-scala/default.nix b/src/evaluator/modules/format-scala/default.nix new file mode 100644 index 00000000..c75f409b --- /dev/null +++ b/src/evaluator/modules/format-scala/default.nix @@ -0,0 +1,53 @@ +{ __nixpkgs__ +, toBashArray +, makeDerivation +, makeScript +, isDarwin +, fetchUrl +, ... +}: +{ config +, lib +, ... +}: +let + chmodX = name: envSrc: makeDerivation { + env = { inherit envSrc; }; + builder = "cp $envSrc $out && chmod +x $out"; + inherit name; + }; + scalafmt_version = "3.0.8"; + binary_name = if isDarwin then "scalafmt-macos" else "scalafmt-linux-musl"; + binary_file = fetchUrl { + url = "https://github.com/scalameta/scalafmt/releases/download/v${scalafmt_version}/${binary_name}"; + sha256 = "0nxpny86qdbgsmxc9qzsv8f5qb21y25iyr8h2wg61kgiaw8988g0"; + }; +in +{ + options = { + formatScala = { + enable = lib.mkOption { + default = false; + type = lib.types.bool; + }; + targets = lib.mkOption { + default = [ "/" ]; + type = lib.types.listOf lib.types.str; + }; + }; + }; + config = { + outputs = { + "/formatScala" = lib.mkIf config.formatScala.enable + (makeScript { + replace = { + __argTargets__ = toBashArray + (builtins.map (rel: "." + rel) config.formatJavaScript.targets); + __argScalaFmtBinary__ = chmodX "scalafmt" binary_file; + }; + name = "format-scala"; + entrypoint = ./entrypoint.sh; + }); + }; + }; +} diff --git a/src/evaluator/modules/format-scala/entrypoint.sh b/src/evaluator/modules/format-scala/entrypoint.sh new file mode 100644 index 00000000..9a78e90f --- /dev/null +++ b/src/evaluator/modules/format-scala/entrypoint.sh @@ -0,0 +1,11 @@ +# shellcheck shell=bash + +function main { + source __argTargets__/template local targets + + for target in "${targets[@]}"; do + '__argScalaFmtBinary__' "${target}" + done +} + +main "${@}"