|
| 1 | +const core = require('@actions/core') |
| 2 | +const exec = require('@actions/exec') |
| 3 | +const io = require('@actions/io') |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + * @param {string} executable |
| 8 | + * @param {string[]} args |
| 9 | + * @async |
| 10 | + * @returns {Promise<string[]>} |
| 11 | + */ |
| 12 | +async function execAndGetStdout(executable, args) { |
| 13 | + /** @type {string} */ |
| 14 | + let output = '' |
| 15 | + |
| 16 | + /** @type {exec.ExecOptions} */ |
| 17 | + const options = { |
| 18 | + failOnStdErr: true, |
| 19 | + ignoreReturnCode: false, |
| 20 | + listeners: { |
| 21 | + stdout: (data) => { |
| 22 | + output += data.toString() |
| 23 | + }, |
| 24 | + }, |
| 25 | + } |
| 26 | + |
| 27 | + await io.which(executable, true) |
| 28 | + |
| 29 | + /** @type {number} */ |
| 30 | + const exitCode = await exec.exec(executable, args, options) |
| 31 | + |
| 32 | + if (exitCode != 0) { |
| 33 | + throw new Error(`${executable} exited with exit code ${exitCode}`) |
| 34 | + } |
| 35 | + |
| 36 | + /** @type {string[]} */ |
| 37 | + const result = output === '' ? [] : output.split('\n') |
| 38 | + |
| 39 | + return result |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * @param {boolean} onlyGitFiles |
| 44 | + * @async |
| 45 | + * @returns {Promise<string[]>} |
| 46 | + */ |
| 47 | +async function getMesonFiles(onlyGitFiles) { |
| 48 | + /** @type {string[]} */ |
| 49 | + let files |
| 50 | + |
| 51 | + /** @type {string[]} */ |
| 52 | + const mesonFiles = ['meson.build', 'meson.options', 'meson_options.txt'] |
| 53 | + |
| 54 | + if (onlyGitFiles) { |
| 55 | + /** @type {string[]} */ |
| 56 | + const gitFiles = mesonFiles.flatMap((file) => ['--exclude', file]) |
| 57 | + |
| 58 | + files = await execAndGetStdout('git', [ |
| 59 | + 'ls-files', |
| 60 | + ...gitFiles, |
| 61 | + '--ignored', |
| 62 | + '-c', |
| 63 | + ]) |
| 64 | + } else { |
| 65 | + /** @type {string[][]} */ |
| 66 | + const findFiles = mesonFiles.map((file) => ['-name', file]) |
| 67 | + |
| 68 | + /** @type {string[]} */ |
| 69 | + const finalFindFiles = findFiles.reduce((acc, elem, index) => { |
| 70 | + if (index != 0) { |
| 71 | + acc.push('-o') |
| 72 | + } |
| 73 | + |
| 74 | + acc.push(...elem) |
| 75 | + |
| 76 | + return acc |
| 77 | + }, []) |
| 78 | + |
| 79 | + files = await execAndGetStdout('find', [ |
| 80 | + '.', |
| 81 | + '(', |
| 82 | + ...finalFindFiles, |
| 83 | + ')', |
| 84 | + ]) |
| 85 | + } |
| 86 | + |
| 87 | + return files |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * |
| 92 | + * @param {string} file |
| 93 | + * @param {string} formatFile |
| 94 | + * @async |
| 95 | + * @returns {Promise<boolean>} |
| 96 | + */ |
| 97 | +async function checkFile(file, formatFile) { |
| 98 | + /** @type {exec.ExecOptions} */ |
| 99 | + const options = { |
| 100 | + ignoreReturnCode: true, |
| 101 | + } |
| 102 | + |
| 103 | + /** @type {string[]} */ |
| 104 | + const additionalArgs = formatFile === '' ? [] : ['-c', formatFile] |
| 105 | + |
| 106 | + /** @type {number} */ |
| 107 | + const exitCode = await exec.exec( |
| 108 | + 'meson', |
| 109 | + ['format', '--check-only', ...additionalArgs, file], |
| 110 | + options |
| 111 | + ) |
| 112 | + |
| 113 | + return exitCode === 0 |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * |
| 118 | + * @param {string[]} items |
| 119 | + * @param {boolean} ordered |
| 120 | + * @returns {string} |
| 121 | + */ |
| 122 | +function getMarkdownListOf(items, ordered) { |
| 123 | + /** @type {string[]} */ |
| 124 | + const itemsContent = items.map((item) => `<li>${item}</li>`) |
| 125 | + |
| 126 | + /** @type {string} */ |
| 127 | + const listType = ordered ? 'ol' : 'ul' |
| 128 | + |
| 129 | + return `<${listType}>${itemsContent}</${listType}>` |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * @async |
| 134 | + * @returns {Promise<void>} |
| 135 | + */ |
| 136 | +async function main() { |
| 137 | + try { |
| 138 | + /** @type {string} */ |
| 139 | + const os = core.platform.platform |
| 140 | + |
| 141 | + if (os != 'linux') { |
| 142 | + throw new Error( |
| 143 | + `Action atm only supported on linux: but are on: ${os}` |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + /** @type {string} */ |
| 148 | + const formatFile = core.getInput('format-file', { required: false }) |
| 149 | + |
| 150 | + /** @type {boolean} */ |
| 151 | + const onlyGitFiles = core.getBooleanInput('only-git-files', { |
| 152 | + required: false, |
| 153 | + }) |
| 154 | + |
| 155 | + /** @type {string[]} */ |
| 156 | + const files = await getMesonFiles(onlyGitFiles) |
| 157 | + |
| 158 | + await io.which('meson', true) |
| 159 | + |
| 160 | + /** @type {string[]} */ |
| 161 | + const notFormattedFiles = [] |
| 162 | + |
| 163 | + core.startGroup('Check all files') |
| 164 | + |
| 165 | + //TODO: maybe parallelize this |
| 166 | + for (const file of files) { |
| 167 | + core.info(`Checking file: '${file}'`) |
| 168 | + |
| 169 | + /** @type {boolean} */ |
| 170 | + const result = await checkFile(file, formatFile) |
| 171 | + |
| 172 | + core.info( |
| 173 | + result |
| 174 | + ? 'File is formatted correctly' |
| 175 | + : 'File has formatting errors' |
| 176 | + ) |
| 177 | + core.info('') |
| 178 | + |
| 179 | + if (!result) { |
| 180 | + notFormattedFiles.push(file) |
| 181 | + |
| 182 | + /** @type {core.AnnotationProperties} */ |
| 183 | + const properties = { |
| 184 | + file, |
| 185 | + title: 'File not formatted correctly', |
| 186 | + } |
| 187 | + |
| 188 | + core.error('File not formatted correctly', properties) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + core.endGroup() |
| 193 | + |
| 194 | + if (notFormattedFiles.length === 0) { |
| 195 | + core.summary.clear() |
| 196 | + |
| 197 | + core.summary.addHeading('Result', 1) |
| 198 | + core.summary.addRaw( |
| 199 | + ':white_check_mark: All files are correctly formatted', |
| 200 | + true |
| 201 | + ) |
| 202 | + |
| 203 | + core.summary.write({ overwrite: true }) |
| 204 | + return |
| 205 | + } |
| 206 | + |
| 207 | + core.summary.clear() |
| 208 | + |
| 209 | + core.summary.addHeading('Result', 1) |
| 210 | + core.summary.addRaw(':x: Some files are not formatted correctly', true) |
| 211 | + core.summary.addBreak() |
| 212 | + |
| 213 | + /** @type {string} */ |
| 214 | + const fileList = getMarkdownListOf(notFormattedFiles, false) |
| 215 | + |
| 216 | + core.summary.addDetails('Affected Files', fileList) |
| 217 | + core.summary.addSeparator() |
| 218 | + |
| 219 | + core.summary.addRaw( |
| 220 | + 'To format the files run the following command', |
| 221 | + true |
| 222 | + ) |
| 223 | + core.summary.addBreak() |
| 224 | + |
| 225 | + /** @type {string} */ |
| 226 | + const additionalArgs = formatFile === '' ? '' : `-c "${formatFile}"` |
| 227 | + |
| 228 | + /** @type {string} */ |
| 229 | + const finalFileList = notFormattedFiles |
| 230 | + .map((file) => `"${file}"`) |
| 231 | + .join(' ') |
| 232 | + |
| 233 | + core.summary.addCodeBlock( |
| 234 | + `meson format ${additionalArgs} -i ${finalFileList}`, |
| 235 | + 'bash' |
| 236 | + ) |
| 237 | + |
| 238 | + core.summary.write({ overwrite: true }) |
| 239 | + |
| 240 | + throw new Error('Some files are not formatted correctly') |
| 241 | + } catch (error) { |
| 242 | + if (error instanceof Error) { |
| 243 | + core.setFailed(error) |
| 244 | + } else { |
| 245 | + core.setFailed(`Invalid error thrown: ${error}`) |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +main() |
0 commit comments