|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | +SCRIPT_NAME=$(basename $0) |
| 5 | + |
| 6 | +# Arg1: OLD_YEAR |
| 7 | +# Arg2: NEW_YEAR |
| 8 | +# Arg3: File Path |
| 9 | +replace_copyright() { |
| 10 | + sed -i '' -e "s|Copyright (c) 2010-${1}, Deusty, LLC|Copyright (c) 2010-${2}, Deusty, LLC|g" "$3" |
| 11 | + return $? |
| 12 | +} |
| 13 | + |
| 14 | +current_year() { |
| 15 | + date '+%Y' |
| 16 | + return $? |
| 17 | +} |
| 18 | + |
| 19 | +last_year() { |
| 20 | + date -v'-1y' '+%Y' |
| 21 | + return $? |
| 22 | +} |
| 23 | + |
| 24 | +# Arg1: Mode (full, usage_only). Defaults to 'full'. |
| 25 | +print_usage() { |
| 26 | + echo "Usage: ${SCRIPT_NAME} [OLD_YEAR NEW_YEAR]" |
| 27 | + if [[ "${1:-full}" == "full" ]]; then |
| 28 | + echo "" |
| 29 | + echo "If called with OLD_YEAR and NEW_YEAR arguments, updates the copyright years from OLD_YEAR to NEW_YEAR." |
| 30 | + echo "If called with no arguments but OLD_YEAR and NEW_YEAR environment variables defined, updates from OLD_YEAR to NEW_YEAR." |
| 31 | + echo "If called with no arguments and OLD_YEAR and NEW_YEAR not being defined, updates from last year to the current year." |
| 32 | + echo "" |
| 33 | + echo "Examples:" |
| 34 | + echo "$ ${SCRIPT_NAME} 2016 2017 # Updates from 2016 to 2017." |
| 35 | + echo "$ OLD_YEAR=2017 NEW_YEAR=2018 ${SCRIPT_NAME} # Updates from 2017 to 2018." |
| 36 | + echo "$ ${SCRIPT_NAME} # Updates from $(last_year) to $(current_year)." |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +OLD_YEAR=${OLD_YEAR:-$(last_year)} |
| 41 | +NEW_YEAR=${NEW_YEAR:-$(current_year)} |
| 42 | +if [[ $# -gt 0 ]]; then |
| 43 | + if [[ $# -eq 2 ]]; then |
| 44 | + OLD_YEAR="$1" |
| 45 | + NEW_YEAR="$2" |
| 46 | + elif [[ $# -eq 1 ]] && [[ $1 == "--help" ]] || [[ $1 == "-h" ]]; then |
| 47 | + print_usage 'full' |
| 48 | + exit 0 |
| 49 | + else |
| 50 | + echo "Specifying years via command line arguments requires two arguments (OLD_YEAR and NEW_YEAR)!" |
| 51 | + echo "For more information use --help." |
| 52 | + echo "" |
| 53 | + print_usage 'usage_only' |
| 54 | + exit -1 |
| 55 | + fi |
| 56 | +fi |
| 57 | + |
| 58 | +# We need to export the function so that bash can call it from the find exec argument. |
| 59 | +export -f replace_copyright |
| 60 | + |
| 61 | +pushd "$(dirname $0)/../" > /dev/null |
| 62 | +find -E . -regex ".*\.([hm]|swift|pch)" -exec bash -c "replace_copyright \"${OLD_YEAR}\" \"${NEW_YEAR}\" \"{}\"" \; |
| 63 | +replace_copyright "${OLD_YEAR}" "${NEW_YEAR}" "./LICENSE" |
| 64 | +replace_copyright "${OLD_YEAR}" "${NEW_YEAR}" "./Dangerfile" |
| 65 | +popd > /dev/null |
| 66 | + |
| 67 | +# Delete the function again |
| 68 | +unset -f replace_copyright |
0 commit comments