• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2022 Huawei Technologies Co., Ltd
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# get os name
17uname_s=$(uname -s)
18os_name=${uname_s:0:5}
19
20function install_windows_codecheck_tools() {
21    # markdownlint
22    echo "[INFO] prepare to install markdownlint"
23    which gem || (echo "[WARMING] you must install 'ruby' before install 'markdownlint'" && return 1)
24    if [ $? -eq "1" ]; then
25        return
26    fi
27    gem sources --add https://gems.ruby-china.com/
28    gem install chef-utils -v 16.6.14 && gem install mdl
29}
30
31function install_Linux_codecheck_tool() {
32    name_release=$(cat </etc/os-release | awk -F'=' '/^NAME/{print $2}')
33    if [ "$name_release" == '"Ubuntu"' ] || [ "$name_release" == '"Debian GNU/Linux"' ]; then
34        # clang-format
35        echo "[INFO] prepare to install clang-format"
36        sudo apt-get install clang-format-9
37        # check exist rubygems
38        echo "[INFO] prepare to install markdownlint"
39        gem -v || (sudo apt-get install -y rubygems)
40        gem sources --add https://gems.ruby-china.com/
41        sudo gem install chef-utils -v 16.6.14 && sudo gem install mdl
42        # install shellcheck
43        echo "[INFO] prepare to install shellcheck"
44        sudo apt-get install shellcheck
45    elif [ "$name_release" == '"CentOS Linux"' ] || [ "$name_release" == '"openEuler"' ] || [ "$name_release" == '"Red Hat Enterprise Linux"' ]; then
46        echo "[WARMING] This script is calling sudo in order to install tools on your system, please make sure you have sudo privilege and input your password in the following prompt!"
47        # clang-format
48        echo "[INFO] prepare to install clang-format"
49        if [ "$name_release" == '"Red Hat Enterprise Linux"' ] || [ "$name_release" == '"openEuler"' ]; then
50            sudo yum install -y git-clang-format.x86_64
51        else
52            sudo yum install -y centos-release-scl-rh
53            sudo yum install -y llvm-toolset-7-git-clang-format
54            llvm_path=$(sudo find / -name *clang-format* | grep -E "/clang-format$")
55            llvm_home=${llvm_path%/*}
56            sudo chmod 666 /etc/profile
57            echo "export LLVM_HOME=$llvm_home" >>/etc/profile
58            echo "export PATH=\$PATH:\$LLVM_HOME" >>/etc/profile
59            sudo chmod 644 /etc/profile
60            source /etc/profile
61        fi
62        # check rubygems exist and version, install markdownlint
63        echo "[INFO] prepare to install markdownlint"
64        gem -v || (sudo yum install -y rubygems)
65        if [ $? -eq "0" ]; then
66            gem_version_head=$(gem -v | awk -F'.' '{print $1}')
67            gem_version_next=$(gem -v | awk -F'.' '{print $2}')
68            if [ "$gem_version_head" -lt 2 ] || [ "$gem_version_head" -eq 2 ] && [ "$gem_version_next" -lt 3 ]; then
69                echo "[WARMING] gem version is less then 2.3 to install markdownlint, please upgrade gem"
70            else
71                gem sources --add https://gems.ruby-china.com/
72                sudo gem install chef-utils -v 16.6.14 && sudo gem install mdl
73            fi
74        fi
75        # install shellcheck
76        if [ "$name_release" == '"CentOS Linux"' ]; then
77            echo "[INFO] prepare to install shellcheck"
78            sudo yum install -y epel-release
79            sudo yum install -y ShellCheck
80        fi
81    fi
82}
83
84function install_Mac_codecheck_tools() {
85    # clang-format
86    echo "[INFO] prepare to install clang-format"
87    brew install clang-format
88    # markdownlint
89    echo "[INFO] prepare to install markdownlint"
90    brew install ruby
91    sudo gem sources --add https://gems.ruby-china.com/
92    sudo gem install chef-utils -v 16.6.14 && sudo gem install mdl
93    # install shellcheck
94    echo "[INFO] prepare to install shellcheck"
95    brew install shellcheck
96    brew link --overwrite shellcheck
97}
98if [ "$os_name" == "MINGW" ]; then # Windows
99    echo "Windows, git bash"
100    install_windows_codecheck_tools
101elif [ "$os_name" == "Linux" ]; then # Linux
102    echo "GNU/Linux"
103    install_Linux_codecheck_tool
104elif [ "$os_name" == "Darwi" ]; then # Darwin
105    echo "Mac OS X"
106    install_Mac_codecheck_tools
107else
108    echo "unknown os"
109    exit 1
110fi
111# check clang-format version
112echo "[INFO] check clang-format version"
113if [ "$os_name" == "Linux" ]; then
114    name_release=$(cat </etc/os-release | awk -F'=' '/^NAME/{print $2}')
115    if [ "$name_release" == '"Ubuntu"' ]; then
116        clang-format-9 --version || echo "[WARMING] clang-format not installed!"
117    else
118        clang-format --version || echo "[WARMING] clang-format not installed!"
119    fi
120else
121    clang-format --version || echo "[WARMING] clang-format not installed!"
122fi
123# mdl version
124echo "[INFO] check markdownlint version"
125mdl --version || echo "[WARMING] markdownlint not installed!"
126# version of shellcheck
127echo "[INFO] check shellcheck version"
128shellcheck --version
129if [ $? -eq "0" ]; then
130    shellcheck_version_first=$(shellcheck -V | awk -F' ' '/^version/{print $2}' | awk -F'.' '{print $1}')
131    shellcheck_version_second=$(shellcheck -V | awk -F' ' '/^version/{print $2}' | awk -F'.' '{print $2}')
132    shellcheck_version_third=$(shellcheck -V | awk -F' ' '/^version/{print $2}' | awk -F'.' '{print $3}')
133    if [ "$shellcheck_version_first" -le 0 ]; then
134        if [ "$shellcheck_version_second" -lt 7 ]; then
135            echo "[WARMING] shellcheck version less then 0.7.1, please upgrade shellcheck manually"
136        elif [ "$shellcheck_version_second" -eq 7 ] && [ "$shellcheck_version_third" -lt 1 ]; then
137            echo "[WARMING] shellcheck version less then 0.7.1, please upgrade shellcheck manually"
138        fi
139    fi
140else
141    echo "[WARMING] shellcheck not installed!"
142fi
143