update-copyright.sh 2.1 KB

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