Skip to content

Commit

Permalink
split hack/headers-strip
Browse files Browse the repository at this point in the history
  • Loading branch information
bpmooch committed Aug 1, 2024
1 parent 299a0f1 commit 7f6a59c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ fmt: headers ## Format the entire code base(s)
headers:
./hack/source-headers-write

# strip old headers
.PHONY: headers-strip
headers-strip: ; ./hack/source-headers-strip

.PHONY: check-deps
check-deps: musl $(GEN_TS) $(GEN_RS) ## Check if there are any unused dependencies in Cargo.toml
$(cargo) +nightly udeps --target $(uname_m)-unknown-linux-musl --package auraed
Expand Down
30 changes: 30 additions & 0 deletions hack/.header.script.strip
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ---------------------------------------------------------------------------- #
# Apache 2.0 License Copyright © 2022-2023 The Aurae Authors #
# #
# +--------------------------------------------+ #
# | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | #
# | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | #
# | ███████║██║ ██║██████╔╝███████║█████╗ | #
# | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | #
# | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | #
# | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | #
# +--------------------------------------------+ #
# #
# Distributed Systems Runtime #
# #
# ---------------------------------------------------------------------------- #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# ---------------------------------------------------------------------------- #

30 changes: 30 additions & 0 deletions hack/.header.source.strip
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* -------------------------------------------------------------------------- *\
* Apache 2.0 License Copyright © 2022-2023 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */

2 changes: 2 additions & 0 deletions hack/file-definitions
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ fi
# This script assumes running from the top level directory within a Makefile

EXPECTEDSCRIPT=$(cat hack/.header.script)
EXPECTEDSCRIPTSTRIP=$(cat hack/.header.script.strip)
EXPECTEDSOURCE=$(cat hack/.header.source)
EXPECTEDSOURCESTRIP=$(cat hack/.header.source.strip)

### Define Scripts
SCRIPTS=$(find . \( \
Expand Down
47 changes: 4 additions & 43 deletions hack/headers-strip → hack/script-headers-strip
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if [[ $D == *"hack"* ]]; then
exit 99
fi

# contains $SCRIPTS, $SOURCES, $EXPECTEDSOURCE, $EXPECTEDSCRIPT
# contains $SCRIPTS, $SOURCES, $EXPECTEDSOURCE, $EXPECTEDSCRIPTSTRIP
. hack/file-definitions

# strip a header from a script file. use `/hack/file-definitions` to understand what files can be
Expand Down Expand Up @@ -76,56 +76,17 @@ function strip_script_header() {
fi
}

# simpler method of stripping headers for source that doesn't need to worry about shebang
function strip_source_header() {
local file="$1"
local expected="$2"
# Bypass files here
if [ "$file" == "ignore.me" ]; then
return
fi

HEADER_LINES=29
FILE_HEADER=$(head -n "$HEADER_LINES" "$file")
if [ "$FILE_HEADER" = "$expected" ]; then
info " -> [MUTATING SOURCE FILE] Stripping header: $file"
# strip the header from the source
tail -n +$((HEADER_LINES + 1)) "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
else
info " -> [SKIPPING SOURCE FILE] $file"
if [ "${SHOW_DEBUG:-0}" -gt 0 ] ; then
info "File Header:"
echo "$FILE_HEADER"
info "Expected Header:"
echo "$expected"
fi
fi
}

FILE_ARG="$1"
if [ -f "$FILE_ARG" ] ; then
strip_script_header "$FILE_ARG"
exit "$?"
fi

EXPECTEDSCRIPT=$(echo "$EXPECTEDSCRIPT" | sed -E -e "s/Copyright © DATE/Copyright © $(date +%Y)/")
EXPECTEDSCRIPTSTRIP=$(echo "$EXPECTEDSCRIPTSTRIP" | sed -E -e "s/Copyright © DATE/Copyright © $(date +%Y)/")
echo ""
echo " [ Stripping Scripts ] "
echo ""
for FILE in $SCRIPTS; do
strip_script_header "$FILE" "$EXPECTEDSCRIPT"
strip_script_header "$FILE" "$EXPECTEDSCRIPTSTRIP"
done

echo ""
echo " [ Stripping Hack Scripts ] "
echo ""
for FILE in $HACKSCRIPTS; do
strip_script_header "$FILE" "$EXPECTEDSCRIPT"
done

echo ""
echo " [ Stripping Source ] "
echo ""
for FILE in $SOURCES; do
strip_source_header "$FILE" "$EXPECTEDSOURCE"
strip_script_header "$FILE" "$EXPECTEDSCRIPTSTRIP"
done
69 changes: 69 additions & 0 deletions hack/source-headers-strip
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# ---------------------------------------------------------------------------- #
# | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | #
# | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | #
# | ███████║██║ ██║██████╔╝███████║█████╗ | #
# | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | #
# | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | #
# | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | #
# +--------------------------------------------+ #
# #
# Distributed Systems Runtime #
# ---------------------------------------------------------------------------- #
# Copyright 2022 - 2024, the aurae contributors #
# SPDX-License-Identifier: Apache-2.0 #
# ---------------------------------------------------------------------------- #
#
# strips SPDX headers from source and scripts

function info() {
green=''
nc=''
printf "${green}%s${nc}\n" "$1"
}

# This script assumes running from the top level directory within a Makefile
D=$(pwd)
if [[ $D == *"hack"* ]]; then
echo ""
echo "/hack is a special directory. These scripts should only be executed from the Makefile."
echo "..or the directory above this one."
echo ""
exit 99
fi

# contains $SCRIPTS, $SOURCES, $EXPECTEDSOURCE, $EXPECTEDSCRIPT
. hack/file-definitions

# simpler method of stripping headers for source that doesn't need to worry about shebang
function strip_source_header() {
local file="$1"
local expected="$2"
# Bypass files here
if [ "$file" == "ignore.me" ]; then
return
fi

HEADER_LINES=29
FILE_HEADER=$(head -n "$HEADER_LINES" "$file")
if [ "$FILE_HEADER" = "$expected" ]; then
info " -> [MUTATING SOURCE FILE] Stripping header: $file"
# strip the header from the source
tail -n +$((HEADER_LINES + 1)) "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
else
info " -> [SKIPPING SOURCE FILE] $file"
if [ "${SHOW_DEBUG:-0}" -gt 0 ] ; then
info "File Header:"
echo "$FILE_HEADER"
info "Expected Header:"
echo "$expected"
fi
fi
}

echo ""
echo " [ Stripping Source ] "
echo ""
for FILE in $SOURCES; do
strip_source_header "$FILE" "$EXPECTEDSOURCESTRIP"
done
Empty file modified hack/source-headers-write
100644 → 100755
Empty file.

0 comments on commit 7f6a59c

Please sign in to comment.