[fractal/fractal-next] ci: Make CI use same checks as pre-commit hook



commit 7f790a854f94574561d508acb1424fddbfe448d4
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Fri Jan 14 23:20:55 2022 +0100

    ci: Make CI use same checks as pre-commit hook

 .gitlab-ci.yml    | 12 +++-----
 scripts/checks.sh | 91 ++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 72 insertions(+), 31 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 2a587ddf..291a55dd 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -32,15 +32,13 @@ flatpak:
       - 'logs'
     expire_in: 14 days
 
-# Configure and run rustfmt
-# Exits and builds fails if on bad format
-rustfmt:
+# Configure and run code checks
+# Exits and fails if an error is encountered
+checks:
   image: "rust:slim"
+  stage: check
   script:
-    - rustup component add rustfmt
-    - rustc -Vv && cargo -Vv
-    - cargo fmt --version
-    - cargo fmt --all -- --color=always --check
+    - scripts/checks.sh --verbose --force-install
 
 cargo-clippy:
   image: 'registry.gitlab.gnome.org/gnome/gnome-runtime-images/rust_bundle:master'
diff --git a/scripts/checks.sh b/scripts/checks.sh
index 4996edf8..b7482b3f 100755
--- a/scripts/checks.sh
+++ b/scripts/checks.sh
@@ -1,6 +1,47 @@
-#!/bin/sh
+#!/bin/bash
 # Source: https://gitlab.gnome.org/GNOME/fractal/blob/master/hooks/pre-commit.hook
 
+# Usage info
+show_help() {
+cat << EOF
+Run conformity checks on the current Rust project.
+
+If a dependency is not found, helps the user to install it.
+
+USAGE: ${0##*/} [OPTIONS]
+
+OPTIONS:
+    -f, --force-install     Install missing dependencies without asking
+    -v, --verbose           Use verbose output
+    -h, --help              Display this help and exit
+
+ERROR CODES:
+    1                       Check failed
+    2                       Missing dependency
+EOF
+}
+
+# Initialize variables
+verbose=0
+force_install=0
+
+# Check arguments
+while [[ "$1" ]]; do case $1 in
+    -f | --force-install )
+        force_install=1
+        ;;
+    -v | --verbose )
+        verbose=1
+        ;;
+    -h | --help )
+        show_help
+        exit 0
+        ;;
+    *)
+        show_help >&2
+        exit 1
+esac; shift; done
+
 install_rustfmt() {
     if ! which rustup &> /dev/null; then
         curl https://sh.rustup.rs -sSf  | sh -s -- -y
@@ -11,40 +52,42 @@ install_rustfmt() {
         fi
     fi
 
-    if ! rustup component list|grep rustfmt &> /dev/null; then
-        echo "Installing rustfmt…"
-        rustup component add rustfmt
-    fi
+    echo "Installing rustfmt…"
+    rustup component add rustfmt
 }
 
 if ! which cargo >/dev/null 2>&1 || ! cargo fmt --help >/dev/null 2>&1; then
     echo "Unable to check Fractal’s code style, because rustfmt could not be run."
 
-    if [ ! -t 1 ]; then
+    if [[ $force_install -eq 1 ]]; then
+        install_rustfmt
+    elif [ ! -t 1 ]; then
         # No input is possible
-        echo "Performing commit."
-        exit 0
+        exit 2
+    else
+        echo ""
+        echo "y: Install rustfmt via rustup"
+        echo "N: Don't install rustfmt"
+        echo ""
+        while true
+        do
+            echo -n "Install rustfmt? [y/N]: "; read yn < /dev/tty
+            case $yn in
+                [Yy]* ) install_rustfmt; break;;
+                [Nn]* | "" ) exit 2 >/dev/null 2>&1;;
+                * ) echo "Invalid input";;
+            esac
+        done
     fi
+fi
 
-    echo ""
-    echo "y: Install rustfmt via rustup"
-    echo "N: Don't install rustfmt"
-
-    echo ""
-    while true
-    do
-        echo -n "Install rustfmt? [y/N]: "; read yn < /dev/tty
-        case $yn in
-            [Yy]* ) install_rustfmt; break;;
-            [Nn]* | "" ) exit 2 >/dev/null 2>&1;;
-            * ) echo "Invalid input";;
-        esac
-    done
-
+if [[ $verbose -eq 1 ]]; then
+    rustc -Vv && cargo -Vv
+    cargo fmt --version
 fi
 
 echo "--Checking style--"
-cargo fmt --all -- --check
+cargo fmt --all -- --color=always --check
 if test $? != 0; then
     echo "--Checking style fail--"
     echo "Please fix the above issues, either manually or by running: cargo fmt --all"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]