1#!/usr/bin/env bash
2
3RENODE_ROOT_PATH="$( cd -- "$( dirname -- "$( dirname -- "${BASH_SOURCE[0]}" )" )" &> /dev/null && pwd -P )"
4
5function print_help() {
6  echo "Usage: $0 [action] <files> [-h] [-r report-file.json]"
7  echo
8  echo "Actions"
9  echo "format                            format files"
10  echo "lint                              lint files"
11  echo
12  echo "Arguments"
13  echo "<files>                           space separated list of files to format / lint,. If empty, all files will be included"
14  echo
15  echo "Options"
16  echo "-h                                show this message"
17  echo "-r                                generate report and save it to report-file.json"
18}
19
20function renode_format() {
21  unset ACTION
22  unset REPORT_FILE
23  unset FILES
24  unset COMMAND
25
26  # Check if command is valid
27  if [ -z ${1+x} ]
28  then
29    echo "Missing command"
30    print_help
31    return 1
32  fi
33  if [[ "$1" != "format" && "$1" != "lint" ]]
34  then
35    echo "Unrecognized command"
36    print_help
37    return 1
38  fi
39  ACTION=$1
40  shift
41
42  # Set files to format
43  while [[ ! -z ${1+x} && $1 !=  -* ]]
44  do
45    FILES="${FILES+} $1"
46    shift
47  done
48
49  # Parse options
50  while getopts "hr:" opt
51  do
52    case $opt in
53      h)
54        print_help
55        ;;
56      r)
57        REPORT_FILE="$OPTARG"
58        ;;
59    esac
60  done
61
62  # We don't want to format external libs, dotnet fromat expects relative paths
63  LIB_RELATIVE_PATH="$(realpath -s --relative-to=$(pwd -P) "$RENODE_ROOT_PATH/lib")"
64
65  COMMAND="dotnet format $RENODE_ROOT_PATH/Renode_NET.sln --exclude $LIB_RELATIVE_PATH"
66
67  if [[ "$ACTION" == "lint" ]]
68  then
69    COMMAND="$COMMAND --verify-no-changes"
70  fi
71
72  if [[ ! -z ${REPORT_FILE+x} ]]
73  then
74    COMMAND="$COMMAND --report $REPORT_FILE"
75  fi
76
77  if [[ ! -z ${FILES+x} ]]
78  then
79    COMMAND="$COMMAND --include $FILES"
80  fi
81
82  return $(eval "$COMMAND")
83}
84
85# Execute formatting command only if file is not sourced
86if [[ "${BASH_SOURCE[0]}" == "$0" ]]
87then
88    renode_format $@
89fi
90
91