Jeff Gaston | dcca534 | 2018-09-04 19:14:14 -0400 | [diff] [blame] | 1 | set -e |
| 2 | |
| 3 | # parse args |
| 4 | inputFile="$1" |
| 5 | oldVersion="$2" |
| 6 | newVersion="$3" |
| 7 | |
| 8 | function usage() { |
| 9 | echo "re-version-repo.sh takes a .zip of a Maven repo and rewrites the versions inside it to a new version |
| 10 | |
| 11 | Usage: re-version-repo.sh <input-zip> <old-version> <new-version> |
| 12 | " |
| 13 | exit 1 |
| 14 | } |
| 15 | |
| 16 | if [ "${inputFile}" == "" ]; then |
| 17 | usage |
| 18 | fi |
| 19 | if [ "${oldVersion}" == "" ]; then |
| 20 | usage |
| 21 | fi |
| 22 | if [ "${newVersion}" == "" ]; then |
| 23 | usage |
| 24 | fi |
| 25 | |
| 26 | # setup |
| 27 | if stat "${inputFile}" > /dev/null 2>/dev/null; then |
| 28 | echo |
| 29 | else |
| 30 | echo "Input file ${inputFile} does not exist" |
| 31 | exit 1 |
| 32 | fi |
| 33 | inputFile="$(readlink -f ${inputFile})" |
| 34 | |
| 35 | if echo "${inputFile}" | grep -v "${oldVersion}" >/dev/null; then |
| 36 | echo "The text '${oldVersion}' does not appear in the name of the file, ${inputFile}." |
| 37 | echo "This is required as a sanity check (and also facilitates computing the output file name)" |
| 38 | exit 1 |
| 39 | fi |
| 40 | outputFile="$(echo ${inputFile} | sed "s/${oldVersion}/${newVersion}/g")" |
| 41 | |
| 42 | tempDir="/tmp/repo" |
| 43 | rm "${tempDir}" -rf |
| 44 | mkdir -p "${tempDir}" |
| 45 | cd "${tempDir}" |
| 46 | |
| 47 | # unzip dir |
| 48 | echo |
| 49 | echo unzipping "${inputFile}" |
| 50 | unzippedDir="${tempDir}/unzipped" |
| 51 | unzip -q "${inputFile}" -d "${unzippedDir}" |
| 52 | cd "${unzippedDir}" |
| 53 | |
| 54 | # make new dirs for new files |
| 55 | echo |
| 56 | oldDirs="$(find -type d)" |
| 57 | newDirs="$(echo ${oldDirs} | sed "s/${oldVersion}/${newVersion}/g")" |
| 58 | echo "Making new dirs: ${newDirs}" |
| 59 | echo "${newDirs}" | xargs --no-run-if-empty mkdir -p |
| 60 | |
| 61 | # move every file |
| 62 | echo |
| 63 | echo moving files |
| 64 | oldFiles="$(find -type f)" |
| 65 | moveCommand="" |
| 66 | for oldFile in ${oldFiles}; do |
| 67 | if echo "${oldFile}" | grep "${oldVersion}">/dev/null; then |
| 68 | newFile="$(echo ${oldFile} | sed "s/${oldVersion}/${newVersion}/g")" |
| 69 | echo "moving ${oldFile} -> ${newFile}" |
| 70 | mv "${oldFile}" "${newFile}" |
| 71 | fi |
| 72 | done |
| 73 | |
| 74 | # remove old dirs |
| 75 | echo |
| 76 | obsoleteDirs="$(find -type d | grep ${oldVersion} | grep -v ${newVersion} | sort -r)" |
| 77 | echo "Removing dirs: ${obsoleteDirs}" |
| 78 | echo "${obsoleteDirs}" | xargs -n 1 --no-run-if-empty rmdir |
| 79 | |
| 80 | # rewrite .pom files |
| 81 | echo |
| 82 | echo rewriting poms |
| 83 | find -name "*.pom" | xargs sed -i "s/${oldVersion}/${newVersion}/g" |
| 84 | |
| 85 | # regenerate .md5 and .sha1 files |
| 86 | for f in $(find -type f | grep -v '\.sha1$' | grep -v '\.md5'); do |
| 87 | md5=$(md5sum $f | sed 's/ .*//') |
| 88 | sha1=$(sha1sum $f | sed 's/ .*//') |
| 89 | |
| 90 | echo -n $md5 > "${f}.md5" |
| 91 | echo -n $sha1 > "${f}.sha1" |
| 92 | done |
| 93 | |
| 94 | |
| 95 | echo |
| 96 | echo rezipping |
| 97 | rm -f "${outputFile}" |
| 98 | zip -qr "${outputFile}" . |
| 99 | |
| 100 | echo "Done transforming ${inputFile} (${oldVersion}) -> ${outputFile} (${newVersion})" |