summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/sw/swiftformat/package.nix
blob: f1fafb8043c9e40bf30647dd164c0f40ff23282b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
  lib,
  fetchFromGitHub,
  runCommand,
  swift,
  swiftpm,
  versionCheckHook,
  nix-update-script,
}:

swift.stdenv.mkDerivation (finalAttrs: {
  pname = "swiftformat";
  version = "0.63.0";

  src = fetchFromGitHub {
    owner = "nicklockwood";
    repo = "SwiftFormat";
    rev = finalAttrs.version;
    sha256 = "sha256-fFvF52MIX3cl1+LVbdUjASQ/kw77d48vOewAfu4VgNA=";
  };

  nativeBuildInputs = [
    swift
    swiftpm
  ];

  installPhase = ''
    install -D "$(swiftpmBinPath)/swiftformat" $out/bin/swiftformat
  '';

  nativeInstallCheckInputs = [
    versionCheckHook
  ];
  doInstallCheck = true;

  passthru = {
    updateScript = nix-update-script { };

    tests.format =
      runCommand "swiftformat-test-format"
        {
          nativeBuildInputs = [ finalAttrs.finalPackage ];
        }
        ''
          export CACHE_DIR=$(mktemp -d)
          printf "class Test{\nvar a:Int=1;;\n}" > test.swift
          swiftformat --cache $CACHE_DIR --swiftversion 5.8 --indent 2 test.swift 2> stderr.txt

          grep -Fxq "Running SwiftFormat..." stderr.txt
          grep -Fxq "1/1 files formatted." stderr.txt

          cat > expected.swift <<'EOF'
          class Test {
            var a: Int = 1
          }
          EOF

          cmp expected.swift test.swift

          swiftformat --cache $CACHE_DIR --swiftversion 5.8 --indent 2 test.swift 2> stderr.txt

          grep -Fxq "Running SwiftFormat..." stderr.txt
          grep -Fxq "0/1 files formatted." stderr.txt

          cmp expected.swift test.swift

          touch $out
        '';
  };

  meta = {
    description = "Code formatting and linting tool for Swift";
    homepage = "https://github.com/nicklockwood/SwiftFormat";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [
      bdesham
      DimitarNestorov
    ];
    platforms = lib.platforms.linux ++ lib.platforms.darwin;
  };
})