xref: /aosp_15_r20/external/libpng/ci/ci_lint.sh (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1#!/usr/bin/env bash
2set -o errexit -o pipefail -o posix
3
4# Copyright (c) 2019-2024 Cosmin Truta.
5#
6# Use, modification and distribution are subject to the MIT License.
7# Please see the accompanying file LICENSE_MIT.txt
8#
9# SPDX-License-Identifier: MIT
10
11# shellcheck source=ci/lib/ci.lib.sh
12source "$(dirname "$0")/lib/ci.lib.sh"
13cd "$CI_TOPLEVEL_DIR"
14
15# Initialize the global constants CI_{...}{CHECK,CHECKER,LINT}.
16CI_SHELLCHECK="${CI_SHELLCHECK:-shellcheck}"
17CI_EDITORCONFIG_CHECKER="${CI_EDITORCONFIG_CHECKER:-editorconfig-checker}"
18CI_YAMLLINT="${CI_YAMLLINT:-yamllint}"
19
20# Initialize the global lint status.
21CI_LINT_STATUS=0
22
23function ci_init_lint {
24    ci_info "## START OF LINTING ##"
25    local my_program
26    # Complete the initialization of CI_SHELLCHECK.
27    # Set it to the empty string if the shellcheck program is unavailable.
28    my_program="$(command -v "$CI_SHELLCHECK")" || {
29        ci_warn "program not found: '$CI_SHELLCHECK'"
30    }
31    CI_SHELLCHECK="$my_program"
32    # Complete the initialization of CI_EDITORCONFIG_CHECKER.
33    # Set it to the empty string if the editorconfig-checker program is unavailable.
34    my_program="$(command -v "$CI_EDITORCONFIG_CHECKER")" || {
35        ci_warn "program not found: '$CI_EDITORCONFIG_CHECKER'"
36    }
37    CI_EDITORCONFIG_CHECKER="$my_program"
38    # Complete the initialization of CI_YAMLLINT.
39    # Set it to the empty string if the yamllint program is unavailable.
40    my_program="$(command -v "$CI_YAMLLINT")" || {
41        ci_warn "program not found: '$CI_YAMLLINT'"
42    }
43    CI_YAMLLINT="$my_program"
44}
45
46function ci_finish_lint {
47    ci_info "## END OF LINTING ##"
48    if [[ $CI_LINT_STATUS -eq 0 ]]
49    then
50        ci_info "## SUCCESS ##"
51    else
52        ci_info "linting failed"
53    fi
54    return "$CI_LINT_STATUS"
55}
56
57function ci_lint_ci_scripts {
58    [[ -x $CI_SHELLCHECK ]] || {
59        ci_warn "## NOT LINTING: CI scripts ##"
60        return 0
61    }
62    ci_info "## LINTING: CI scripts ##"
63    ci_spawn "$CI_SHELLCHECK" --version
64    find ./ci -maxdepth 1 -name "*.sh" | {
65        local my_file
66        while IFS="" read -r my_file
67        do
68            ci_spawn "$CI_SHELLCHECK" -x "$my_file" || {
69                # Linting failed.
70                return 1
71            }
72        done
73    }
74}
75
76function ci_lint_text_files {
77    [[ -x $CI_EDITORCONFIG_CHECKER ]] || {
78        ci_warn "## NOT LINTING: text files ##"
79        return 0
80    }
81    ci_info "## LINTING: text files ##"
82    ci_spawn "$CI_EDITORCONFIG_CHECKER" --version
83    ci_spawn "$CI_EDITORCONFIG_CHECKER" || {
84        # Linting failed.
85        return 1
86    }
87}
88
89function ci_lint_yaml_files {
90    [[ -x $CI_YAMLLINT ]] || {
91        ci_warn "## NOT LINTING: YAML files ##"
92        return 0
93    }
94    ci_info "## LINTING: YAML files ##"
95    ci_spawn "$CI_YAMLLINT" --version
96    find . \( -iname "*.yml" -o -iname "*.yaml" \) -not -path "./out/*" | {
97        local my_file
98        while IFS="" read -r my_file
99        do
100            ci_spawn "$CI_YAMLLINT" --strict "$my_file" || {
101                # Linting failed.
102                return 1
103            }
104        done
105    }
106}
107
108function ci_lint {
109    ci_init_lint
110    ci_lint_ci_scripts || CI_LINT_STATUS=1
111    ci_lint_text_files || CI_LINT_STATUS=1
112    ci_lint_yaml_files || CI_LINT_STATUS=1
113    # TODO: ci_lint_png_files, etc.
114    ci_finish_lint
115}
116
117function usage {
118    echo "usage: $CI_SCRIPT_NAME [<options>]"
119    echo "options: -?|-h|--help"
120    exit "${@:-0}"
121}
122
123function main {
124    local opt
125    while getopts ":" opt
126    do
127        # This ain't a while-loop. It only pretends to be.
128        [[ $1 == -[?h]* || $1 == --help || $1 == --help=* ]] && usage 0
129        ci_err "unknown option: '$1'"
130    done
131    shift $((OPTIND - 1))
132    [[ $# -eq 0 ]] || {
133        echo >&2 "error: unexpected argument: '$1'"
134        usage 2
135    }
136    # And... go!
137    ci_lint
138}
139
140main "$@"
141