|
| 1 | +// Description: This script reads the matrix.json file and filters |
| 2 | +// the main entries for which we test the releases in a clean container. |
| 3 | +// The releases are used to generate the demos in multiple environments, |
| 4 | +// and one of these environments uploads the generated demos to the |
| 5 | +// mrdocs.com server. |
| 6 | + |
| 7 | +const fs = require('fs'); |
| 8 | +const core = require('@actions/core'); |
| 9 | +const {exec} = require('child_process'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Compares the priority of two compiler entries based on their operating system. |
| 13 | + * |
| 14 | + * @param {Object} entryA - The first entry to compare. |
| 15 | + * @param {string} entryA.os - The operating system of the first entry. |
| 16 | + * @param {string} entryA.compiler - The compiler of the first entry. |
| 17 | + * @param {Object} entryB - The second entry to compare. |
| 18 | + * @param {string} entryB.os - The operating system of the second entry. |
| 19 | + * @param {string} entryB.compiler - The compiler of the second entry. |
| 20 | + * @returns {number} - A negative number if entryA has higher priority, |
| 21 | + * a positive number if entryB has higher priority, |
| 22 | + * or zero if they have the same priority. |
| 23 | + */ |
| 24 | +function compareCompilerPriority(entryA, entryB) { |
| 25 | + // Define the compiler priority for each operating system |
| 26 | + const compilerPriority = { |
| 27 | + 'windows': ['msvc', 'clang', 'gcc'], |
| 28 | + 'macos': ['clang', 'gcc'], |
| 29 | + 'linux': ['gcc', 'clang'] |
| 30 | + }; |
| 31 | + |
| 32 | + // Retrieve the priority list for the OS of entryA |
| 33 | + const lcOs = entryA.os.toLowerCase(); |
| 34 | + if (!compilerPriority.hasOwnProperty(lcOs)) { |
| 35 | + throw new Error(`Unknown operating system: ${entryA.os}`) |
| 36 | + } |
| 37 | + const osPriority = compilerPriority[lcOs] |
| 38 | + |
| 39 | + // Get the index of the compiler for entryA and entryB in the priority list |
| 40 | + const aPriority = osPriority.indexOf(entryA.compiler) |
| 41 | + const bPriority = osPriority.indexOf(entryB.compiler) |
| 42 | + |
| 43 | + // If the compiler is not found in the list, assign it the lowest priority |
| 44 | + const aFinalPriority = aPriority === -1 ? osPriority.length : aPriority |
| 45 | + const bFinalPriority = bPriority === -1 ? osPriority.length : bPriority |
| 46 | + |
| 47 | + // Return the difference between the priorities of entryA and entryB |
| 48 | + return aFinalPriority - bFinalPriority; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Finds the highest priority entry among all entries that have the same specified value |
| 53 | + * for the `mrdocs-release-package-artifact`. |
| 54 | + * |
| 55 | + * @param {Array<Object>} entries - The array of entries to search. |
| 56 | + * @param {string} artifactName - The value of `mrdocs-release-package-artifact` to match. |
| 57 | + * @returns {Object|null} - The highest priority entry or null if no matching entry is found. |
| 58 | + */ |
| 59 | +function findHighestPriorityEntry(entries, artifactName) { |
| 60 | + /** @type {Object|null} */ |
| 61 | + let highestPriorityEntry = null; |
| 62 | + |
| 63 | + for (const entry of entries) { |
| 64 | + if (entry['is-main'] !== true) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + if (entry['mrdocs-release-package-artifact'] === artifactName) { |
| 68 | + if (highestPriorityEntry === null) { |
| 69 | + highestPriorityEntry = entry; |
| 70 | + } else { |
| 71 | + if (compareCompilerPriority(entry, highestPriorityEntry) < 0) { |
| 72 | + highestPriorityEntry = entry; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return highestPriorityEntry; |
| 79 | +} |
| 80 | + |
| 81 | +(async () => { |
| 82 | + // Read the JSON string from the file |
| 83 | + const matrixJson = fs.readFileSync('matrix.json', 'utf8'); |
| 84 | + |
| 85 | + // Parse the JSON string into an array of objects |
| 86 | + const matrixEntries = JSON.parse(matrixJson); |
| 87 | + |
| 88 | + // Create a new array to store unique entries based on llvm-archive-filename |
| 89 | + const seenArtifactNames = new Set(); |
| 90 | + const releaseMatrixEntries = []; |
| 91 | + |
| 92 | + for (const entry of matrixEntries) { |
| 93 | + if (entry['is-main'] !== true) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + const artifactName = entry['mrdocs-release-package-artifact']; |
| 97 | + if (!seenArtifactNames.has(artifactName)) { |
| 98 | + seenArtifactNames.add(artifactName); |
| 99 | + const highestPriorityEntry = findHighestPriorityEntry(matrixEntries, artifactName); |
| 100 | + if (highestPriorityEntry !== null) { |
| 101 | + releaseMatrixEntries.push(highestPriorityEntry); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Convert the new array back to a JSON string |
| 107 | + const uniqueMatrixJson = JSON.stringify(releaseMatrixEntries); |
| 108 | + |
| 109 | + // Output the filtered JSON string using core.setOutput |
| 110 | + core.setOutput('releases-matrix', uniqueMatrixJson); |
| 111 | + |
| 112 | + // Print matrix to console |
| 113 | + console.log(`Releases Matrix (${releaseMatrixEntries.length} entries):`) |
| 114 | + releaseMatrixEntries.forEach(obj => { |
| 115 | + console.log(`- ${obj.name}`) |
| 116 | + for (const key in obj) { |
| 117 | + if (key !== 'name') { |
| 118 | + console.log(` ${key}: ${JSON.stringify(obj[key])}`) |
| 119 | + } |
| 120 | + } |
| 121 | + }) |
| 122 | +})(); |
0 commit comments