Skip to content

Commit 81a6e4f

Browse files
committed
build: add script and autofix job step to add trailing newlines
1 parent b0e93c7 commit 81a6e4f

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

.github/workflows/lint_autofix.yml

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ jobs:
142142
files="${{ steps.changed-files.outputs.files }}"
143143
FIX=1 . "$GITHUB_WORKSPACE/.github/workflows/scripts/lint_javascript_files" "$files"
144144
145+
# Add missing trailing newlines:
146+
- name: 'Add missing trailing newlines'
147+
id: add-trailing-newlines
148+
run: |
149+
files="${{ steps.changed-files.outputs.files }}"
150+
. "$GITHUB_WORKSPACE/.github/workflows/scripts/add_trailing_newlines" "$files"
151+
145152
# Disable Git hooks:
146153
- name: 'Disable Git hooks'
147154
run: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env bash
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2024 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# Script to add trailing newlines to files if they don't already have one.
20+
#
21+
# Usage: add_trailing_newlines file1 [file2 file3 ...]
22+
#
23+
# Arguments:
24+
#
25+
# file1 File path.
26+
# file2 File path.
27+
# file3 File path.
28+
29+
# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails:
30+
set -o pipefail
31+
32+
# VARIABLES #
33+
34+
# List of file extensions to process:
35+
extensions=("js" "mjs" "cjs" "json" "ts" "py" "jl" "R" "c" "h" "cpp" "hpp" "f" "sh" "awk" "html" "xml" "css" "md" "yml" "gypi" "bib" "tex")
36+
37+
# FUNCTIONS #
38+
39+
# Convert Windows CRLF to Unix LF line endings.
40+
convert_line_endings() {
41+
if [[ "$OSTYPE" == "darwin"* ]]; then
42+
# macOS requires an empty string argument with -i to edit in-place without backup...
43+
sed -i '' 's/\r$//' "$1"
44+
else
45+
# Linux and other Unix-like systems...
46+
sed -i 's/\r$//' "$1"
47+
fi
48+
}
49+
50+
# Prints a success message.
51+
print_success() {
52+
echo 'Success!' >&2
53+
}
54+
55+
# Main execution sequence.
56+
main() {
57+
# Assuming all arguments are passed as one string and split by spaces:
58+
IFS=' ' read -r -a files <<< "$*"
59+
60+
for file in "${files[@]}"; do
61+
echo "Processing file: $file"
62+
if [[ ! -f "$file" ]]; then
63+
echo "File not found: $file"
64+
continue
65+
fi
66+
67+
# Convert line endings from CRLF to LF:
68+
convert_line_endings "$file"
69+
70+
extension="${file##*.}"
71+
local match_found=false
72+
for ext in "${extensions[@]}"; do
73+
if [[ "$ext" == "$extension" ]]; then
74+
match_found=true
75+
break
76+
fi
77+
done
78+
if [[ "$match_found" == true ]]; then
79+
if [[ "$(tail -c1 "$file"; echo x)" != $'\nx' ]]; then
80+
echo "Adding newline to: $file"
81+
echo >> "$file"
82+
fi
83+
else
84+
echo "Skipping file (unsupported extension): $file"
85+
fi
86+
done
87+
88+
print_success
89+
exit 0
90+
}
91+
92+
# Call main with all command-line arguments:
93+
main "$@"

0 commit comments

Comments
 (0)