• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2025 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16# Fix code formatting issues.
17
18source "$(dirname "$0")/common_lib.sh"
19init_py_env
20
21# --- Argument Parsing ---
22run_python=false
23run_cpp=false
24run_arkts=false
25run_all=true
26
27if [ "$#" -gt 0 ]; then
28    run_all=false
29    for arg in "$@"; do
30        case "$arg" in
31            --python)
32                run_python=true
33                ;;
34            --cpp)
35                run_cpp=true
36                ;;
37            --arkts)
38                run_arkts=true
39                ;;
40            *)
41                echo "Error: Unknown argument '$arg'"
42                echo "Usage: $0 [--python] [--cpp] [--arkts]"
43                exit 1
44                ;;
45        esac
46    done
47fi
48
49# If no specific arguments were given, run all
50if "$run_all"; then
51    run_python=true
52    run_cpp=true
53    run_arkts=true
54fi
55
56# --- Formatting Steps (Conditional) ---
57
58# Python
59if "$run_python"; then
60    log_stage "Formatting Python code"
61    removestar -i taihe tests || true
62    black taihe tests
63    ruff check taihe tests --fix
64fi
65
66# Cpp, runtime and tests
67if "$run_cpp"; then
68    log_stage "Formatting C++ code"
69    # Cpp, runtime
70    find ../runtime/include ../runtime/src -type f |
71        clang-format-19 --verbose -i @/dev/stdin
72
73    # Cpp, tests
74    find ../test ../cookbook \
75         \( -name '*.c' -or -name '*.cpp' -or -name '*.h' -or -name '*.hpp' \) \
76         -not -path '*generated*' -type f |
77        clang-format-19 --verbose -i @/dev/stdin
78fi
79
80# ArkTS
81if "$run_arkts"; then
82    log_stage "Formatting ArkTS code"
83    find ../test ../cookbook -name '*.ets' -type f -print0 | while IFS= read -r -d $'\0' file; do
84        log_verbose "Formatting $file"
85        clang-format-19 --assume-filename main.js <"$file" >"${file}.formatted" && mv "${file}.formatted" "$file"
86    done
87fi
88