-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42header_tool.sh
More file actions
executable file
·284 lines (255 loc) · 8.68 KB
/
Copy path42header_tool.sh
File metadata and controls
executable file
·284 lines (255 loc) · 8.68 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# 42header_tool.sh
# 42のヘッダーを削除または挿入するスクリプト
# 使用法: ./42header_tool.sh [オプション] [ディレクトリ]
# 色の定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
show_help() {
echo "42のソースコードファイルのヘッダーを削除または挿入します。"
echo ""
echo "usage: $0 [option] [directory]"
echo ""
echo "option:"
echo " -c, --check ヘッダーの存在を確認します"
echo " -d, --delete ヘッダーを削除します"
echo " -a, --add ヘッダーを追加します"
echo " -r, --replace ヘッダーを置き換えます"
echo " -h, --help このヘルプを表示します"
echo " -u, --username USER ユーザー名を指定します(デフォルト: 環境変数USERから取得)"
echo " -e, --email EMAIL メールアドレスを指定します(デフォルト: username@student.42tokyo.jp)"
echo ""
}
# デフォルト値
ACTION="check" # デフォルトはチェックモード
TARGET_DIR="."
TARGET_FILE=""
IS_FILE=0 # 0=ディレクトリ、1=ファイル
USERNAME="${USER}"
EMAIL=""
# 引数解析
while [[ $# -gt 0 ]]; do
case $1 in
-c|--check)
ACTION="check"
shift
;;
-d|--delete)
ACTION="delete"
shift
;;
-a|--add)
ACTION="add"
shift
;;
-r|--replace)
ACTION="replace"
shift
;;
-h|--help)
show_help
exit 0
;;
-u|--username)
USERNAME="$2"
shift 2
;;
-e|--email)
EMAIL="$2"
shift 2
;;
*)
if [[ -d "$1" ]]; then
# ディレクトリの場合
TARGET_DIR="$1"
IS_FILE=0
elif [[ -f "$1" ]]; then
# ファイルの場合
TARGET_FILE="$1"
IS_FILE=1
else
echo -e "${RED}error: invalid argument or non-existent file/directory: $1${NC}"
show_help
exit 1
fi
shift
;;
esac
done
# メールアドレスが指定されていない場合はデフォルト値を設定
if [[ -z "$EMAIL" ]]; then
EMAIL="${USERNAME}@student.42tokyo.jp"
fi
# ヘッダーが存在するか確認する関数
check_header() {
local file="$1"
# ファイルの先頭13行を取得
local header_lines=$(head -n 13 "$file" 2>/dev/null)
# 42ヘッダーの特徴をチェック
if echo "$header_lines" | grep -q "\* © \|By:\|Created:\|Updated:\|\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"; then
echo -e "${GREEN}header exists: $file${NC}"
return 0
else
echo -e "${YELLOW}header not found: $file${NC}"
return 1
fi
}
# ヘッダーを削除する関数
delete_header() {
local file="$1"
local temp_file=$(mktemp)
# check_header関数を使用してヘッダーの存在を確認
if check_header "$file" > /dev/null; then
# ヘッダー(13行)を削除
tail -n +13 "$file" > "$temp_file"
mv "$temp_file" "$file"
echo -e "${GREEN}header deleted: $file${NC}"
return 0
else
echo -e "${YELLOW}header not found: $file${NC}"
rm "$temp_file"
return 1
fi
}
# 1行を 80 文字に整形するヘルパ
# $1: 行頭固定部分 $2: 可変部分 $3: 行末固定部分
padline() {
local head="$1" body="$2" tail="$3"
local fill=$(( 80 - ${#head} - ${#body} - ${#tail} ))
(( fill < 0 )) && fill=0 # はみ出したら 0 に潰す
printf '%s%s%*s%s\n' "$head" "$body" "$fill" '' "$tail"
}
# ヘッダーを追加する関数
add_header() {
local file="$1"
local fname="$(basename "$file")"
local tmp="$(mktemp)"
local now="$(date '+%Y/%m/%d %H:%M:%S')"
# 既存チェック
if check_header "$file" >/dev/null; then
echo -e "${YELLOW}header already exists: $file${NC}"
rm "$tmp"; return 1
fi
# 各行生成 -------------------------------------------------------------
{
printf '/* ************************************************************************** */\n'
printf '/* */\n'
printf '/* ::: :::::::: */\n'
padline '/* ' "$fname" ':+: :+: :+: */'
printf '/* +:+ +:+ +:+ */\n'
padline '/* By: ' "${USERNAME} <${EMAIL}>" '+#+ +:+ +#+ */'
printf '/* +#+#+#+#+#+ +#+ */\n'
padline '/* Created: ' "$now by $USERNAME" '#+# #+# */'
padline '/* Updated: ' "$now by $USERNAME" '### ########.fr */'
printf '/* */\n'
printf '/* ************************************************************************** */\n\n'
} >"$tmp"
# 本文連結
cat "$file" >>"$tmp"
mv "$tmp" "$file"
echo -e "${GREEN}header added: $file${NC}"
}
replace_header() {
local file="$1"
# ヘッダーがあるかチェック
if check_header "$file" >/dev/null; then
# 既存を削除してから再挿入
if delete_header "$file" 1>/dev/null && add_header "$file" 1>/dev/null; then
echo -e "${GREEN}header replaced: $file${NC}"
return 0
else
echo -e "${RED}header replace failed: $file${NC}"
return 1
fi
else
# 無ければ単純に追加
if add_header "$file"; then
return 0
else
echo -e "${RED}header add failed: $file${NC}"
return 1
fi
fi
}
# 対象ファイルを検索して処理
process_files() {
local target="$1"
local action="$2"
local is_file="$3"
local count_success=0
local count_skipped=0
local count_total=0
local count_with_header=0
local count_without_header=0
local files=
if [[ "$is_file" -eq 1 ]]; then
# 単一ファイルの場合
files="$target"
else
# ディレクトリの場合、C/C++ソースファイルとヘッダーファイルを検索
files=$(find "$target" -type f \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) | sort)
fi
# 各ファイルを処理
while IFS= read -r file; do
# ファイルが空でない場合のみ処理
if [[ -n "$file" ]]; then
count_total=$((count_total + 1))
if [[ "$action" == "check" ]]; then
if check_header "$file"; then
count_with_header=$((count_with_header + 1))
else
count_without_header=$((count_without_header + 1))
fi
elif [[ "$action" == "delete" ]]; then
if delete_header "$file"; then
count_success=$((count_success + 1))
else
count_skipped=$((count_skipped + 1))
fi
elif [[ "$action" == "add" ]]; then
if add_header "$file"; then
count_success=$((count_success + 1))
else
count_skipped=$((count_skipped + 1))
fi
elif [[ "$action" == "replace" ]]; then
if replace_header "$file"; then
count_success=$((count_success + 1))
else
count_skipped=$((count_skipped + 1))
fi
fi
fi
done <<< "$files"
echo ""
echo -e "${BLUE}process completed:${NC}"
echo " processed files: $count_total"
if [[ "$action" == "check" ]]; then
echo " files with header: $count_with_header"
echo " files without header: $count_without_header"
else
echo " success: $count_success"
echo " skipped: $count_skipped"
fi
}
# メイン処理
echo -e "${BLUE}42header_tool${NC}"
if [[ "$IS_FILE" -eq 1 ]]; then
echo "file: $TARGET_FILE"
else
echo "directory: $TARGET_DIR"
fi
echo "username: $USERNAME"
echo "email: $EMAIL"
echo "action: $ACTION"
echo ""
# 処理実行
if [[ "$IS_FILE" -eq 1 ]]; then
process_files "$TARGET_FILE" "$ACTION" "$IS_FILE"
else
process_files "$TARGET_DIR" "$ACTION" "$IS_FILE"
fi
exit 0