Created
April 20, 2014 02:04
-
-
Save tony19/11103027 to your computer and use it in GitHub Desktop.
Inserts copyright text into files if needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash -eu | |
################################################################################ | |
# | |
# Copyright (c) 2014 Anthony Trinh. | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to | |
# deal in the Software without restriction, including without limitation the | |
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
# sell copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
# DEALINGS IN THE SOFTWARE. | |
# | |
################################################################################ | |
copyright="copyright.txt" | |
dir="." | |
includes=(-name "*.sh") | |
excludes=(-not -path "*test/.util/*") | |
comment="#" | |
verbose=false | |
#---------------------------------------------------------------------- | |
# Prints this program's help text | |
#---------------------------------------------------------------------- | |
usage() { | |
name=$(basename "$0") | |
echo "Inserts copyright text into files if needed | |
Usage: ./$name [OPTIONS] | |
OPTIONS: | |
-c <FILE> Copyright template to insert into files that are missing | |
the copyright notice. This file is copied verbatim into files | |
after the shebang line. | |
(default: ./$copyright) | |
-d <DIR> Directory to recursively scan for files. | |
(default: $dir) | |
-i <PATT> Pattern of filenames to include in scan. | |
(default: ${includes[1]}) | |
-x <PATT> Pattern of filenames to exclude from scan. | |
(default: ${excludes[2]}) | |
-m <CHAR> Line-prefix character(s), used to find existing copyright. | |
(default: $comment) | |
-v|--verbose Show debug messages | |
EXAMPLES: | |
./$name | |
./$name -d my_files | |
./$name -c copyleft.txt | |
./$name -c copyleft.txt -d my_files -n \"*.txt\" | |
./$name -i \"*.sh\" -x \"foo/sh/*\" -x \"bar/txt/*\"" | |
} | |
#---------------------------------------------------------------------- | |
# Loads command-line arguments | |
#---------------------------------------------------------------------- | |
args_load() { | |
while [[ $# > 0 ]]; do | |
key="$1" | |
shift | |
case "$key" in | |
-c) | |
copyright="$1" | |
shift;; | |
-d) | |
dir="$1" | |
shift;; | |
-i) | |
includes=(-name "$1") | |
shift;; | |
-m) | |
comment="$1" | |
shift;; | |
-x) | |
excludes+=(-not -path "$1") | |
shift;; | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-v|--verbose) | |
verbose=true | |
;; | |
*) | |
fail unknown argument: "$key" | |
;; | |
esac | |
done | |
} | |
#---------------------------------------------------------------------- | |
# Validates the loaded command-line arguments | |
#---------------------------------------------------------------------- | |
args_validate() { | |
[[ ! -d "$dir" ]] && fail "directory not found: $dir" | |
[[ ! -f "$copyright" ]] && fail "file not found: $copyright" | |
: | |
} | |
#---------------------------------------------------------------------- | |
# Adds copyright text to a file, while preserving the shebang line | |
#---------------------------------------------------------------------- | |
copyright_file() { | |
file=${1?} | |
_echo scanning $file | |
has_copyright=$(grep "$comment\s*Copyright" "$file" || :) | |
if [ ! -z "$has_copyright" ]; then | |
_echo already copyrighted | |
else | |
echo adding copyright to "$file" | |
buf= | |
first_line=$(head -1 "$file") | |
has_shebang=$(echo "$first_line" | grep "#\!" || :) | |
if [ ! -z "$has_shebang" ]; then | |
buf="$first_line" | |
buf+="$(cat "$copyright")" | |
buf+=$'\n' | |
buf+="$(tail -n +2 "$file")" | |
else | |
buf="$(cat "$copyright")" | |
buf+=$'\n' | |
buf+="$(cat "$file")" | |
fi | |
echo -e "$buf" >"$file" | |
fi | |
} | |
#---------------------------------------------------------------------- | |
# Adds copyright text to all shell files in a directory | |
#---------------------------------------------------------------------- | |
copyright_all_files() { | |
while read -r file | |
do | |
[ -z "$file" ] && continue | |
copyright_file "$file" | |
done < <(find "$dir" -type f "${includes[@]}" "${excludes[@]}") | |
} | |
#---------------------------------------------------------------------- | |
# Prints a message only if $verbose is true | |
#---------------------------------------------------------------------- | |
_echo() { | |
if [[ "$verbose" == true ]]; then | |
echo "$@" | |
fi | |
} | |
#---------------------------------------------------------------------- | |
# Prints an error message and then exits with code 1 | |
#---------------------------------------------------------------------- | |
fail() { | |
echo error: "$@" >&2 | |
exit 1 | |
} | |
#---------------------------------------------------------------------- | |
# Main | |
#---------------------------------------------------------------------- | |
args_load "$@" | |
args_validate | |
copyright_all_files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment