aboutsummaryrefslogtreecommitdiffhomepage
path: root/build_tools/style.fish
blob: 76cf920d5a6964d5423df1a1f560d8c313746550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env fish
#
# This is meant to be run by "make style" or "make style-all". It is not meant to
# be run directly from a shell prompt although it can be.
#
# This runs C++ files and fish scripts (*.fish) through their respective code
# formatting programs.
#
set c_files
set f_files
set all no

if test "$argv[1]" = "--all"
    set all yes
    set -e argv[1]
end

if set -q argv[1]
    echo "Unexpected arguments: '$argv'"
    exit 1
end

if test $all = yes
    set c_files src/*.h src/*.cpp
    set f_files ***.fish
else
    # We haven't been asked to reformat all the source. If there are uncommitted
    # changes reformat those, else reformat the files in the most recent commit.
    set pending (git status --porcelain --short --untracked-files=all | sed -e 's/^ *//')
    if count $pending > /dev/null
        # There are pending changes so lint those files.
        for arg in $pending
            set files $files (string split -m 1 ' ' $arg)[2]
        end
    else
        # No pending changes so lint the files in the most recent commit.
        set files (git show --name-only --pretty=oneline head | tail --lines=+2)
    end

    # Extract just the C/C++ files.
    set c_files (string match -r '^.*\.(?:c|cpp|h)$' -- $files)
    # Extract just the fish files.
    set f_files (string match -r '^.*\.fish$' -- $files)
end

# Run the C++ reformatter if we have any C++ files.
if set -q c_files[1]
    if type -q clang-format
        echo
        echo ========================================
        echo Running clang-format
        echo ========================================
        for file in $c_files
            clang-format $file > $file.new
            if cmp --quiet $file $file.new
                echo $file was correctly formatted
                rm $file.new
            else
                echo $file was NOT correctly formatted
                mv $file.new $file
            end
        end
    else
        echo
        echo 'WARNING: Cannot find clang-format command'
        echo
    end
end

# Run the fish reformatter if we have any fish files.
if set -q f_files[1]
    if not type -q fish_indent
        make fish_indent
        set PATH . $PATH
    end
    echo
    echo ========================================
    echo Running fish_indent
    echo ========================================
    for file in $f_files
        fish_indent < $file > $file.new
        if cmp --quiet $file $file.new
            echo $file was correctly formatted
            rm $file.new
        else
            echo $file was NOT correctly formatted
            mv $file.new $file
        end
    end
end