| 1 | | import Foundation |
| 2 | | |
| 3 | | /// The site's front page: the run's headline and every module, each folding out its files. |
| 4 | | public struct IndexPage: Sendable { |
| 5 | | public static let sitePath = "index.html" |
| 6 | | private let title: String |
| 7 | | private let modules: [ModuleTotals] |
| 8 | | private let tests: TestRun? |
| 9 | | private let commit: String? |
| 10 | | private let comparison: BaselineComparison? |
| 11 | | private let hasBranches: Bool |
| 12 | | private let hasConditions: Bool |
| 13 | | |
| 14 | | /// - Parameters: |
| 15 | | /// - title: the site's name, shown as given in the page's title and heading. |
| 16 | | /// - tests: the run's tests; nil leaves them out of the headline. With results, the |
| 17 | | /// headline links the test results page beside this one. |
| 18 | | /// - commit: the commit the report was built from, shown as given. |
| 19 | | /// - baseline: an earlier run to compare with; with it, the table gets a change |
| 20 | | /// column. |
| 21 | | public init( |
| 22 | | title: String, files: [FileTotals], tests: TestRun?, commit: String?, |
| 23 | | baseline: RunTotals? = nil |
| 24 | 37 | ) { |
| 25 | 37 | self.comparison = baseline.map(BaselineComparison.init) |
| 26 | 37 | self.title = title |
| 27 | 37 | self.modules = ModuleTotals.grouping(files) |
| 28 | 109 | self.hasBranches = files.contains { $0.branches != nil } |
| 29 | 111 | self.hasConditions = files.contains { $0.conditions != nil } |
| 30 | 37 | self.tests = tests |
| 31 | 37 | self.commit = commit |
| 32 | 37 | } |
| 33 | | |
| 34 | 37 | public var html: String { |
| 35 | 37 | """ |
| 36 | 37 | <!doctype html> |
| 37 | 37 | <html lang="en"> |
| 38 | 37 | <head> |
| 39 | 37 | <meta charset="utf-8"> |
| 40 | 37 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 41 | 37 | <title>\(escapingHTML(title))</title> |
| 42 | 37 | <link rel="stylesheet" href="\(SiteStylesheet.fileName)"> |
| 43 | 37 | </head> |
| 44 | 37 | <body class="\(bodyClasses)"> |
| 45 | 37 | <header class="page-header"> |
| 46 | 37 | \(SiteHeader(title: title, currentTab: .coverage, hasTestsPage: hasTestsPage, rootPath: "").html) |
| 47 | 37 | \(headlineMarkup) |
| 48 | 37 | </header> |
| 49 | 37 | <main> |
| 50 | 37 | <p class="legend"><span class="key high"></span>90% and up \ |
| 51 | 37 | <span class="key medium"></span>75% and up <span class="key low"></span>below 75%</p> |
| 52 | 37 | <div class="toolbar" data-needs-script hidden> |
| 53 | 37 | <input id="filter" type="search" placeholder="Filter files" aria-label="Filter files"> |
| 54 | 37 | </div> |
| 55 | 37 | <div class="row columns" data-needs-script hidden>\ |
| 56 | 37 | <button type="button" data-sort="name" data-first="ascending">File</button><span class="bar"></span>\ |
| 57 | 37 | <button type="button" data-sort="percent" data-first="ascending">Lines</button>\ |
| 58 | 37 | \(comparison == nil ? "" : #"<span class="change">Change</span>"#)\ |
| 59 | 37 | <button type="button" data-sort="missed" data-first="descending">Missed</button>\ |
| 60 | 37 | <button type="button" data-sort="lines" data-first="descending">Total</button>\ |
| 61 | 37 | \(hasBranches ? #"<span class="branches">Branches</span>"# : "")\ |
| 62 | 37 | \(hasConditions ? #"<span class="conditions">Conditions</span>"# : "")</div> |
| 63 | 37 | <div class="modules"> |
| 64 | 106 | \(modules.map(moduleMarkup).joined(separator: "\n")) |
| 65 | 37 | </div> |
| 66 | 37 | </main> |
| 67 | 37 | <script>\(Self.sortAndFilterScript)</script> |
| 68 | 37 | </body> |
| 69 | 37 | </html> |
| 70 | 37 | |
| 71 | 37 | """ |
| 72 | 37 | } |
| 73 | | |
| 74 | | /// The classes the stylesheet keys its extra columns on. |
| 75 | 37 | private var bodyClasses: String { |
| 76 | 37 | var classes = ["index-page"] |
| 77 | 37 | if comparison != nil { classes.append("has-baseline") } |
| 78 | 37 | if hasBranches { classes.append("has-branches") } |
| 79 | 37 | if hasConditions { classes.append("has-conditions") } |
| 80 | 37 | return classes.joined(separator: " ") |
| 81 | 37 | } |
| 82 | | |
| 83 | 37 | private var hasTestsPage: Bool { |
| 84 | 37 | if case .results = tests { true } else { false } |
| 85 | 37 | } |
| 86 | | |
| 87 | 37 | private var headlineMarkup: String { |
| 88 | 69 | let total = LineSummary(adding: modules.map(\.lines)) |
| 89 | 37 | let totalChange = |
| 90 | 37 | comparison.map { Self.changeSpan($0.changeOfTotal(now: total)) + " " } ?? "" |
| 91 | 37 | var parts = [ |
| 92 | 37 | #"<span class="percent \#(total.tier.rawValue)">\#(total.percentText)</span> "# |
| 93 | 37 | + totalChange |
| 94 | 37 | + "\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines" |
| 95 | 37 | ] |
| 96 | 37 | if let tests { |
| 97 | 7 | let counts = tests.counts |
| 98 | 7 | parts.append( |
| 99 | 7 | "\(counts.totalText): \(groupingDigits(counts.passed)) passed, " |
| 100 | 7 | + "\(groupingDigits(counts.failed)) failed, \(groupingDigits(counts.skipped)) skipped" |
| 101 | 7 | ) |
| 102 | 7 | if case .results = tests { |
| 103 | 4 | parts.append(#"<a href="\#(TestsPage.sitePath)">Test results</a>"#) |
| 104 | 4 | } |
| 105 | 7 | } |
| 106 | 37 | if let commit { |
| 107 | 13 | parts.append("commit \(escapingHTML(commit))") |
| 108 | 13 | } |
| 109 | 37 | return #"<p class="totals">"# + parts.joined(separator: " · ") + "</p>" |
| 110 | 37 | } |
| 111 | | |
| 112 | | /// Shows the controls, sorts modules and the files inside each by the clicked column |
| 113 | | /// (a second click reverses it), and filters files by path, opening the modules |
| 114 | | /// that still have matches. Without scripts the modules still fold out by hand. |
| 115 | | private static let sortAndFilterScript = """ |
| 116 | | (() => { |
| 117 | | const list = document.querySelector(".modules"); |
| 118 | | const modules = [...list.querySelectorAll("details.module")]; |
| 119 | | const buttons = [...document.querySelectorAll("button[data-sort]")]; |
| 120 | | const filter = document.getElementById("filter"); |
| 121 | | document.querySelectorAll("[data-needs-script]").forEach(element => element.hidden = false); |
| 122 | | let key = "name"; |
| 123 | | let ascending = true; |
| 124 | | const value = row => key === "name" ? row.dataset.name.toLowerCase() : parseFloat(row.dataset[key]); |
| 125 | | const order = (a, b) => { |
| 126 | | const [x, y] = [value(a), value(b)]; |
| 127 | | return (x < y ? -1 : x > y ? 1 : 0) * (ascending ? 1 : -1); |
| 128 | | }; |
| 129 | | const sort = () => { |
| 130 | | for (const module of modules.sort(order)) { |
| 131 | | module.append(...[...module.querySelectorAll("a.file")].sort(order)); |
| 132 | | list.append(module); |
| 133 | | } |
| 134 | | for (const button of buttons) { |
| 135 | | button.dataset.direction = button.dataset.sort === key ? (ascending ? "ascending" : "descending") : ""; |
| 136 | | } |
| 137 | | }; |
| 138 | | for (const button of buttons) { |
| 139 | | button.addEventListener("click", () => { |
| 140 | | ascending = button.dataset.sort === key ? !ascending : button.dataset.first === "ascending"; |
| 141 | | key = button.dataset.sort; |
| 142 | | sort(); |
| 143 | | }); |
| 144 | | } |
| 145 | | filter.addEventListener("input", () => { |
| 146 | | const query = filter.value.trim().toLowerCase(); |
| 147 | | for (const module of modules) { |
| 148 | | let hasMatch = false; |
| 149 | | for (const file of module.querySelectorAll("a.file")) { |
| 150 | | const path = (module.dataset.name + "/" + file.dataset.name).toLowerCase(); |
| 151 | | file.hidden = query !== "" && !path.includes(query); |
| 152 | | hasMatch = hasMatch || !file.hidden; |
| 153 | | } |
| 154 | | module.hidden = !hasMatch; |
| 155 | | module.open = query !== "" && hasMatch; |
| 156 | | } |
| 157 | | }); |
| 158 | | sort(); |
| 159 | | })(); |
| 160 | | """ |
| 161 | | |
| 162 | | /// A `<details>` per module, so its files fold out without any script. |
| 163 | 69 | private func moduleMarkup(_ module: ModuleTotals) -> String { |
| 164 | 111 | let files = module.files.map { file in |
| 165 | 111 | let name = String(file.relativePath.dropFirst(module.name.count + 1)) |
| 166 | 111 | let fileChange = comparison?.change(ofFile: file.relativePath, now: file.lines) |
| 167 | 111 | return |
| 168 | 111 | #"<a class="row file" href="\#(escapingHTML(SourcePage.sitePath(for: file.relativePath)))""# |
| 169 | 111 | + "\(Self.sortKeys(name: name, lines: file.lines))>" |
| 170 | 111 | + "\(cells(name: name, lines: file.lines, change: fileChange, branches: file.branches, conditions: file.conditions))</a>" |
| 171 | 111 | } |
| 172 | 69 | let moduleChange = comparison?.change(ofModule: module.name, now: module.lines) |
| 173 | 69 | return """ |
| 174 | 69 | <details class="module"\(Self.sortKeys(name: module.name, lines: module.lines))> |
| 175 | 69 | <summary class="row">\(cells(name: module.name, lines: module.lines, change: moduleChange, branches: module.branches, conditions: module.conditions))</summary> |
| 176 | 69 | \(files.joined(separator: "\n")) |
| 177 | 69 | </details> |
| 178 | 69 | """ |
| 179 | 69 | } |
| 180 | | |
| 181 | | /// The values the index's sort script orders rows by, unformatted, so it compares |
| 182 | | /// numbers rather than the text shown. |
| 183 | 180 | private static func sortKeys(name: String, lines: LineSummary) -> String { |
| 184 | 180 | #" data-name="\#(escapingHTML(name))""# |
| 185 | 180 | + #" data-percent="\#(String(format: "%.2f", lines.percent))""# |
| 186 | 180 | + #" data-missed="\#(lines.count - lines.covered)" data-lines="\#(lines.count)""# |
| 187 | 180 | } |
| 188 | | |
| 189 | 33 | private static func changeSpan(_ change: LineChange) -> String { |
| 190 | 33 | #"<span class="change \#(change.styleName)">\#(change.text)</span>"# |
| 191 | 33 | } |
| 192 | | |
| 193 | | private func cells( |
| 194 | | name: String, lines: LineSummary, change: LineChange?, branches: CoverageCount?, |
| 195 | | conditions: CoverageCount? |
| 196 | 180 | ) -> String { |
| 197 | 180 | let tier = lines.tier.rawValue |
| 198 | 180 | let changeCell = change.map(Self.changeSpan) ?? "" |
| 199 | 180 | let branchCell = hasBranches ? Self.countCell(branches, className: "branches") : "" |
| 200 | 180 | let conditionCell = hasConditions ? Self.countCell(conditions, className: "conditions") : "" |
| 201 | 180 | return #"<span class="name">\#(escapingHTML(name))</span>"# |
| 202 | 180 | + #"<span class="bar"><span class="fill \#(tier)" style="width: \#(lines.percentText)"></span></span>"# |
| 203 | 180 | + #"<span class="percent \#(tier)">\#(lines.percentText)</span>"# + changeCell |
| 204 | 180 | + #"<span class="missed">\#(groupingDigits(lines.count - lines.covered))</span>"# |
| 205 | 180 | + #"<span class="lines">\#(groupingDigits(lines.count))</span>"# |
| 206 | 180 | + branchCell + conditionCell |
| 207 | 180 | } |
| 208 | | |
| 209 | | /// The share in its tier colour and the counts it comes from; a dash where this |
| 210 | | /// file or module has no data while others do. |
| 211 | 12 | private static func countCell(_ count: CoverageCount?, className: String) -> String { |
| 212 | 12 | guard let count else { return #"<span class="\#(className)">–</span>"# } |
| 213 | 11 | return #"<span class="\#(className)"><span class="percent \#(count.tier.rawValue)">"# |
| 214 | 11 | + "\(count.percentText)</span> \(groupingDigits(count.covered))/" |
| 215 | 11 | + "\(groupingDigits(count.total))</span>" |
| 216 | 12 | } |
| 217 | | } |