swiftlint: convert to finalAttrs, add linux support, tests and update script (#510095)

This commit is contained in:
nixpkgs-ci[bot]
2026-04-15 08:13:07 +00:00
committed by GitHub
3 changed files with 106 additions and 13 deletions
+41 -13
View File
@@ -4,16 +4,22 @@
fetchurl,
unzip,
installShellFiles,
nix-update-script,
versionCheckHook,
runCommand,
}:
stdenvNoCC.mkDerivation rec {
let
sources = lib.importJSON ./sources.json;
platform =
sources.platforms.${stdenvNoCC.hostPlatform.system}
or (throw "Unsupported platform: ${stdenvNoCC.hostPlatform.system}");
in
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "swiftlint";
version = "0.63.2";
inherit (sources) version;
src = fetchurl {
url = "https://github.com/realm/SwiftLint/releases/download/${version}/portable_swiftlint.zip";
hash = "sha256-xZpAXIX5W5LO1nelAIBOCBWWpMrkpqSFr3YGVVfW7Sk=";
url = "https://github.com/realm/SwiftLint/releases/download/${finalAttrs.version}/${platform.filename}";
inherit (platform) hash;
};
dontPatch = true;
@@ -27,11 +33,15 @@ stdenvNoCC.mkDerivation rec {
sourceRoot = ".";
installPhase = ''
runHook preInstall
install -Dm755 swiftlint $out/bin/swiftlint
runHook postInstall
'';
installPhase =
let
binary = if stdenvNoCC.hostPlatform.isLinux then "swiftlint-static" else "swiftlint";
in
''
runHook preInstall
install -Dm755 ${binary} $out/bin/swiftlint
runHook postInstall
'';
postInstall = lib.optionalString (stdenvNoCC.buildPlatform.canExecute stdenvNoCC.hostPlatform) ''
installShellCompletion --cmd swiftlint \
@@ -43,7 +53,25 @@ stdenvNoCC.mkDerivation rec {
doInstallCheck = true;
nativeInstallCheckInputs = [ versionCheckHook ];
passthru.updateScript = nix-update-script { };
passthru = {
updateScript = ./update.sh;
tests = {
lint =
runCommand "swiftlint-test-lint"
{
nativeBuildInputs = [ finalAttrs.finalPackage ];
}
''
printf "class test{}\n\nvar a = 1" > test.swift
swiftlint lint ${lib.optionalString stdenvNoCC.hostPlatform.isDarwin "--disable-sourcekit"} test.swift > output.txt 2>&1 || true
grep -q "identifier_name" output.txt
grep -q "opening_brace" output.txt
grep -q "trailing_newline" output.txt
grep -q "type_name" output.txt
touch $out
'';
};
};
meta = {
description = "Tool to enforce Swift style and conventions";
@@ -54,7 +82,7 @@ stdenvNoCC.mkDerivation rec {
matteopacini
DimitarNestorov
];
platforms = lib.platforms.darwin;
platforms = lib.attrNames sources.platforms;
sourceProvenance = [ lib.sourceTypes.binaryNativeCode ];
};
}
})
+21
View File
@@ -0,0 +1,21 @@
{
"version": "0.63.2",
"platforms": {
"x86_64-linux": {
"filename": "swiftlint_linux_amd64.zip",
"hash": "sha256-3RAXz9IKFFfyZFkLy1h1pu4GzXW5qdT3fNQ6VSSZFDs="
},
"aarch64-linux": {
"filename": "swiftlint_linux_arm64.zip",
"hash": "sha256-EE3t/3YhV/XP93UvHMKiibYPPqZ35y1lHG86Mof92Ug="
},
"x86_64-darwin": {
"filename": "portable_swiftlint.zip",
"hash": "sha256-xZpAXIX5W5LO1nelAIBOCBWWpMrkpqSFr3YGVVfW7Sk="
},
"aarch64-darwin": {
"filename": "portable_swiftlint.zip",
"hash": "sha256-xZpAXIX5W5LO1nelAIBOCBWWpMrkpqSFr3YGVVfW7Sk="
}
}
}
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq nix
set -euo pipefail
cd "$(dirname "$0")"
old_version=$(jq -r ".version" sources.json)
version=$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} \
"https://api.github.com/repos/realm/SwiftLint/releases/latest" | jq -r ".tag_name")
if [[ "$old_version" == "$version" ]]; then
echo "swiftlint is already up to date at $version"
exit 0
fi
echo "Updating swiftlint from $old_version to $version"
declare -A platforms=(
[x86_64-linux]="swiftlint_linux_amd64.zip"
[aarch64-linux]="swiftlint_linux_arm64.zip"
[x86_64-darwin]="portable_swiftlint.zip"
[aarch64-darwin]="portable_swiftlint.zip"
)
sources_tmp="$(mktemp)"
jq -n --arg v "$version" '{version: $v, platforms: {}}' > "$sources_tmp"
for platform in "${!platforms[@]}"; do
filename="${platforms[$platform]}"
url="https://github.com/realm/SwiftLint/releases/download/${version}/${filename}"
echo "Fetching hash for $platform ($filename)..."
sha256hash="$(nix-prefetch-url --type sha256 "$url")"
hash="$(nix hash convert --to sri --hash-algo sha256 "$sha256hash")"
jq --arg platform "$platform" \
--arg filename "$filename" \
--arg hash "$hash" \
'.platforms += {($platform): {filename: $filename, hash: $hash}}' \
"$sources_tmp" > "${sources_tmp}.tmp" && mv "${sources_tmp}.tmp" "$sources_tmp"
done
mv "$sources_tmp" sources.json
echo "Updated swiftlint to $version"