|
| 1 | +{ |
| 2 | + pkgs, |
| 3 | + haskellPackages, |
| 4 | + ghcVersion, |
| 5 | +}: let |
| 6 | + # Apply coverage overlay to get packages with coverage enabled |
| 7 | + hpkgs = haskellPackages.extend (import ../overlays/haskell/coverage.nix pkgs); |
| 8 | + |
| 9 | + # The main package we're generating coverage for |
| 10 | + sdkPackage = hpkgs.temporal-sdk; |
| 11 | + |
| 12 | + # Tool to convert .tix files to markdown |
| 13 | + tixToMarkdown = hpkgs.tix-to-markdown; |
| 14 | +in |
| 15 | + pkgs.stdenv.mkDerivation { |
| 16 | + name = "temporal-sdk-coverage-${ghcVersion}"; |
| 17 | + |
| 18 | + buildInputs = [ |
| 19 | + hpkgs.ghc |
| 20 | + pkgs.haskellPackages.hpc |
| 21 | + tixToMarkdown |
| 22 | + ]; |
| 23 | + |
| 24 | + phases = ["buildPhase" "installPhase"]; |
| 25 | + |
| 26 | + buildPhase = '' |
| 27 | + echo "Building coverage report for temporal-sdk..." |
| 28 | + echo "SDK package: ${sdkPackage}" |
| 29 | +
|
| 30 | + # Create directories |
| 31 | + mkdir -p coverage |
| 32 | +
|
| 33 | + # The doCoverage function already generates HTML coverage reports |
| 34 | + # We just need to copy them from the package |
| 35 | + if [ -d "${sdkPackage}/share/hpc/vanilla/html" ]; then |
| 36 | + echo "Found pre-generated HPC HTML report" |
| 37 | + cp -rv "${sdkPackage}/share/hpc/vanilla/html" coverage/ |
| 38 | +
|
| 39 | + # Make the directory writable so we can create symlinks |
| 40 | + chmod -R u+w coverage/html |
| 41 | +
|
| 42 | + # Create index.html symlink to hpc_index.html for easier access |
| 43 | + if [ -f "coverage/html/hpc_index.html" ]; then |
| 44 | + ln -sf hpc_index.html coverage/html/index.html |
| 45 | + fi |
| 46 | +
|
| 47 | + echo "Coverage HTML report copied successfully!" |
| 48 | + else |
| 49 | + echo "Warning: No coverage HTML found in ${sdkPackage}" |
| 50 | + mkdir -p coverage/html |
| 51 | + cat > coverage/html/index.html <<EOF |
| 52 | + <!DOCTYPE html> |
| 53 | + <html> |
| 54 | + <head><title>No Coverage Data</title></head> |
| 55 | + <body> |
| 56 | + <h1>No Coverage Data Available</h1> |
| 57 | + <p>No coverage report was found in the package.</p> |
| 58 | + <p>Package checked: ${sdkPackage.pname}</p> |
| 59 | + </body> |
| 60 | + </html> |
| 61 | + EOF |
| 62 | + fi |
| 63 | +
|
| 64 | + # Also copy .tix files for reference |
| 65 | + if [ -d "${sdkPackage}/share/hpc/vanilla/tix" ]; then |
| 66 | + echo "Copying .tix files..." |
| 67 | + mkdir -p coverage/tix |
| 68 | + cp -v "${sdkPackage}"/share/hpc/vanilla/tix/*.tix coverage/tix/ 2>&1 || true |
| 69 | + fi |
| 70 | +
|
| 71 | + # Find the mix directory - it's in a versioned library path |
| 72 | + tix_file="${sdkPackage}/share/hpc/vanilla/tix/temporal-sdk-tests.tix" |
| 73 | + mix_dir=$(find "${sdkPackage}/lib" -type d -name "mix" -path "*/extra-compilation-artifacts/hpc/*/mix" | head -1) |
| 74 | +
|
| 75 | + if [ -f "$tix_file" ] && [ -d "$mix_dir" ]; then |
| 76 | + echo "Found .tix file: $tix_file" |
| 77 | + echo "Found .mix directory: $mix_dir" |
| 78 | +
|
| 79 | + echo "Generating text coverage summary..." |
| 80 | + ${hpkgs.ghc}/bin/hpc report \ |
| 81 | + --hpcdir="$mix_dir" \ |
| 82 | + "$tix_file" > coverage/report.txt 2>&1 || echo "Note: Could not generate text report" |
| 83 | +
|
| 84 | + if [ -f coverage/report.txt ] && [ -s coverage/report.txt ]; then |
| 85 | + echo "Coverage summary:" |
| 86 | + cat coverage/report.txt |
| 87 | + fi |
| 88 | +
|
| 89 | + # Generate markdown coverage report using tix-to-markdown |
| 90 | + # Set locale to UTF-8 to support emoji characters |
| 91 | + echo "Generating markdown coverage report..." |
| 92 | + export LC_ALL=C.UTF-8 |
| 93 | + ${tixToMarkdown}/bin/tix-to-markdown --hpcdir "$mix_dir" "$tix_file" > coverage/coverage.md 2>&1 || echo "Note: Could not generate markdown report" |
| 94 | +
|
| 95 | + if [ -f coverage/coverage.md ] && [ -s coverage/coverage.md ]; then |
| 96 | + echo "Markdown coverage report generated successfully!" |
| 97 | + echo "---" |
| 98 | + cat coverage/coverage.md |
| 99 | + echo "---" |
| 100 | + fi |
| 101 | + else |
| 102 | + echo "Warning: Could not find .tix file or .mix directory" |
| 103 | + echo " .tix file: $tix_file (exists: $([ -f "$tix_file" ] && echo "yes" || echo "no"))" |
| 104 | + echo " .mix dir: $mix_dir (exists: $([ -d "$mix_dir" ] && echo "yes" || echo "no"))" |
| 105 | + fi |
| 106 | + ''; |
| 107 | + |
| 108 | + installPhase = '' |
| 109 | + mkdir -p $out/share/hpc |
| 110 | + mkdir -p $out/bin |
| 111 | +
|
| 112 | + # Copy coverage data |
| 113 | + cp -r coverage/html $out/share/hpc/ |
| 114 | + [ -f coverage/report.txt ] && cp coverage/report.txt $out/share/hpc/ || true |
| 115 | + [ -f coverage/coverage.md ] && cp coverage/coverage.md $out/share/hpc/ || true |
| 116 | + [ -d coverage/tix ] && cp -r coverage/tix $out/share/hpc/ || true |
| 117 | +
|
| 118 | + # Create a convenience script to view the report |
| 119 | + cat > $out/bin/view-coverage <<EOF |
| 120 | + #!/usr/bin/env bash |
| 121 | + set -e |
| 122 | +
|
| 123 | + report_path="$out/share/hpc/html/index.html" |
| 124 | +
|
| 125 | + if [ ! -f "\$report_path" ]; then |
| 126 | + echo "Error: Coverage report not found at \$report_path" |
| 127 | + exit 1 |
| 128 | + fi |
| 129 | +
|
| 130 | + echo "Opening coverage report..." |
| 131 | + echo "Report location: \$report_path" |
| 132 | +
|
| 133 | + # Try to open in browser (works on macOS and most Linux) |
| 134 | + if command -v open >/dev/null 2>&1; then |
| 135 | + open "\$report_path" |
| 136 | + elif command -v xdg-open >/dev/null 2>&1; then |
| 137 | + xdg-open "\$report_path" |
| 138 | + else |
| 139 | + echo "Please open the following file in your browser:" |
| 140 | + echo "\$report_path" |
| 141 | + fi |
| 142 | + EOF |
| 143 | + chmod +x $out/bin/view-coverage |
| 144 | +
|
| 145 | + # Create a script to show the text report |
| 146 | + cat > $out/bin/show-coverage-report <<EOF |
| 147 | + #!/usr/bin/env bash |
| 148 | + if [ -f "$out/share/hpc/report.txt" ]; then |
| 149 | + cat "$out/share/hpc/report.txt" |
| 150 | + else |
| 151 | + echo "No coverage report found." |
| 152 | + fi |
| 153 | + EOF |
| 154 | + chmod +x $out/bin/show-coverage-report |
| 155 | +
|
| 156 | + echo "Coverage report installed to $out/share/hpc/html/index.html" |
| 157 | + ''; |
| 158 | + |
| 159 | + meta = with pkgs.lib; { |
| 160 | + description = "Code coverage report for temporal-sdk Haskell packages"; |
| 161 | + platforms = platforms.unix; |
| 162 | + }; |
| 163 | + } |
0 commit comments