From 693dc48e546d967d0d1ced56892ff2f52e76d053 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 5 Mar 2020 21:15:54 +0100 Subject: [PATCH] Add GitHub Action to validate image requirements --- .github/workflows/validate.yml | 19 +++++ .github/workflows/validate/Dockerfile | 4 ++ .github/workflows/validate/action.yml | 4 ++ .github/workflows/validate/validate.sh | 96 ++++++++++++++++++++++++++ scripts/validate.sh | 1 + 5 files changed, 124 insertions(+) create mode 100644 .github/workflows/validate.yml create mode 100644 .github/workflows/validate/Dockerfile create mode 100644 .github/workflows/validate/action.yml create mode 100755 .github/workflows/validate/validate.sh create mode 120000 scripts/validate.sh diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml new file mode 100644 index 00000000..d779f977 --- /dev/null +++ b/.github/workflows/validate.yml @@ -0,0 +1,19 @@ +name: Validate + +on: + push: + branches: + - master + pull_request: + paths: + - "src/**" + +jobs: + validate: + name: Images + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Validate images + uses: ./.github/workflows/validate diff --git a/.github/workflows/validate/Dockerfile b/.github/workflows/validate/Dockerfile new file mode 100644 index 00000000..3ad06a09 --- /dev/null +++ b/.github/workflows/validate/Dockerfile @@ -0,0 +1,4 @@ +FROM alpine +RUN apk add --no-cache imagemagick bash +COPY validate.sh /validate.sh +ENTRYPOINT ["/validate.sh"] diff --git a/.github/workflows/validate/action.yml b/.github/workflows/validate/action.yml new file mode 100644 index 00000000..bad4bf80 --- /dev/null +++ b/.github/workflows/validate/action.yml @@ -0,0 +1,4 @@ +name: "validate" +runs: + using: "docker" + image: "Dockerfile" diff --git a/.github/workflows/validate/validate.sh b/.github/workflows/validate/validate.sh new file mode 100755 index 00000000..af4a6080 --- /dev/null +++ b/.github/workflows/validate/validate.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +# Small variable to determine exit code at the end +ERRORS=0 +IMAGES=0 + +# Small error handling method, that works with GitHub Actions +function error() { + local file=${1} + local message=${2} + ((ERRORS++)) + + if [[ ! -z "${GITHUB_ACTIONS}" ]]; then + echo "::error file=${file}::${message}: ${file}" + else + echo "${message}: ${file}" + fi +} + +# Find all files in the src folder (should contain only images) +while read image; do + # Read properties from image + properties=($(identify -format "%w %h %m %[colorspace]" "${image}")) + if [[ "$?" -ne 0 ]]; then + error "${image}" "Could not read image file" + continue + fi + + # Extract properties into variables + filename=$(basename "${image}") + width="${properties[0]}" + height="${properties[1]}" + type="${properties[2]}" + colorspace="${properties[3]}" + + # Ensure file is actually a PNG file + [[ "${type}" != "PNG" ]] \ + && error "${image}" "Invalid file type '${type}' for file" + + # Ensure color space is sRGB + [[ "${colorspace}" != "sRGB" ]] \ + && error "${image}" "Invalid color space '${colorspace}' for file" + + # Validate image dimensions + if [[ "${filename}" == "icon.png" ]]; then + # icon dimension + [[ "${width}" -ne 256 || "${height}" -ne 256 ]] \ + && error "${image}" "Invalid icon size! Size is ${width}x${height}px, must be 256x256px" + + elif [[ "${filename}" == "icon@2x.png" ]]; then + # hDPI icon dimension + [[ "${width}" -ne 512 || "${height}" -ne 512 ]] \ + && error "${image}" "Invalid hDPI icon size! Size is ${width}x${height}px, must be 512x512px" + + elif [[ "${filename}" == "logo.png" ]]; then + # Minimal shortest side + if [[ "${width}" -le "${height}" && "${width}" -lt 128 ]]; then + error "${image}" "Invalid logo size! Size is ${width}x${height}px, shortest side must be at least 128px" + elif [[ "${width}" -ge "${height}" && "${height}" -lt 128 ]]; then + error "${image}" "Invalid logo size! Size is ${width}x${height}px, shortest side must be at least 128px" + fi + + # Maximal shortest size + if [[ "${width}" -le "${height}" && "${width}" -gt 256 ]]; then + error "${image}" "Invalid logo size! Size is ${width}x${height}px, shortest side must not exceed 256px" + elif [[ "${width}" -ge "${height}" && "${height}" -gt 256 ]]; then + error "${image}" "Invalid logo size! Size is ${width}x${height}px, shortest side must not exceed 256px" + fi + + elif [[ "${filename}" == "logo@2x.png" ]]; then + # Minimal shortest side + if [[ "${width}" -le "${height}" && "${width}" -lt 256 ]]; then + error "${image}" "Invalid hDPI logo size! Size is ${width}x${height}px, shortest side must be at least 256px" + elif [[ "${width}" -ge "${height}" && "${height}" -lt 256 ]]; then + error "${image}" "Invalid hDPI logo size! Size is ${width}x${height}px, shortest side must be at least 256px" + fi + + # Maximal shortest side + if [[ "${width}" -le "${height}" && "${width}" -gt 512 ]]; then + error "${image}" "Invalid hDPI logo size! Size is ${width}x${height}px, shortest side must not exceed 512px" + elif [[ "${width}" -ge "${height}" && "${height}" -gt 512 ]]; then + error "${image}" "Invalid hDPI logo size! Size is ${width}x${height}px, shortest side must not exceed 512px" + fi + + else + # Unexpected file + error "${image}" "Unknown and invalid filename" + fi + + ((IMAGES++)) +done <<< $(find src -type f) + +echo "" +echo "Total of ${IMAGES} images checked, found ${ERRORS} issues." + +[[ "${ERRORS}" -ne 0 ]] && exit 1 || exit 0 diff --git a/scripts/validate.sh b/scripts/validate.sh new file mode 120000 index 00000000..df169d3e --- /dev/null +++ b/scripts/validate.sh @@ -0,0 +1 @@ +../.github/workflows/validate/validate.sh \ No newline at end of file