|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +GO111MODULE=$(go env GO111MODULE) |
| 6 | + |
| 7 | +# move_vendor will copy the function's vendor folder, |
| 8 | +# if it exists. |
| 9 | +move_vendor() { |
| 10 | + if [ ! -d ./function/vendor ]; then |
| 11 | + echo "vendor not found" |
| 12 | + return |
| 13 | + fi |
| 14 | + |
| 15 | + echo "moving function vendor" |
| 16 | + mv -f ./function/vendor . |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +# cleanup_gomod will move the function's go module |
| 21 | +cleanup_gomod() { |
| 22 | + |
| 23 | + # Nothing to do when modules is explicitly off |
| 24 | + # the z prefix protects against any SH wonkiness |
| 25 | + # see https://stackoverflow.com/a/18264223 |
| 26 | + if [ "z$GO111MODULE" = "zoff" ]; then |
| 27 | + echo "modules disabled, skipping go.mod cleanup" |
| 28 | + return; |
| 29 | + fi |
| 30 | + |
| 31 | + if [ ! -f ./function/go.mod ]; then |
| 32 | + echo "module not initialized, skipping go.mod cleanup" |
| 33 | + return; |
| 34 | + fi |
| 35 | + |
| 36 | + echo "cleaning up go.mod" |
| 37 | + |
| 38 | + # Copy the user's go.mod |
| 39 | + mv -f ./function/go.mod . |
| 40 | + mv -f ./function/go.sum . |
| 41 | + |
| 42 | + # Clean up the go.mod |
| 43 | + |
| 44 | + # Cleanup any sub-module replacements. |
| 45 | + # This requires modifying any replace that points to "./*", |
| 46 | + # the user has will use this to reference sub-modules instead |
| 47 | + # of sub-packages, which we cleanup below. |
| 48 | + echo "cleanup local replace statements" |
| 49 | + # 1. Replace references to the local folder with `./function` |
| 50 | + sed -i 's/=> \.\//=> \.\/function\//' go.mod |
| 51 | + |
| 52 | + |
| 53 | + # Remove any references to the handler/function module. |
| 54 | + # It is ok to just remove it because we will replace it later. |
| 55 | + # |
| 56 | + # Note that these references may or may not exist. We expect the |
| 57 | + # go.mod to have a replace statement _if_ developer has subpackages |
| 58 | + # in their handler. In this case they will need a this replace statement |
| 59 | + # |
| 60 | + # replace handler/function => ./ |
| 61 | + # |
| 62 | + # `go mod` will then add a line that looks like |
| 63 | + # |
| 64 | + # handler/function v0.0.0-00010101000000-000000000000 |
| 65 | + # |
| 66 | + # both of these lines need to be replaced, this grep selects everything |
| 67 | + # _except_ those offending lines. |
| 68 | + grep -v "\shandler/function" go.mod > gomod2; mv gomod2 go.mod |
| 69 | + |
| 70 | + # Now update the go.mod |
| 71 | + # |
| 72 | + # 1. use replace so that imports of handler/function use the local code |
| 73 | + # 2. we need to rename the module to handler because our main.go assumes |
| 74 | + # this is the package name |
| 75 | + go mod edit \ |
| 76 | + -replace=handler/function=./function \ |
| 77 | + -module handler |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + if [ "$DEBUG" -eq 1 ]; then |
| 82 | + cat go.mod |
| 83 | + echo "" |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +# cleanup_vendor_modulestxt will cleanup the modules.txt file in the vendor folder |
| 89 | +# this file is needed when modules are enabled and it must be in sync with the |
| 90 | +# go.mod. To function correctly we need to modify the references to handler/function, |
| 91 | +# if they exist. |
| 92 | +cleanup_vendor_modulestxt() { |
| 93 | + if [ ! -d ./vendor ]; then |
| 94 | + echo "no vendor found, skipping modules.txt cleanup" |
| 95 | + return |
| 96 | + fi |
| 97 | + |
| 98 | + # Nothing to do when modules is explicitly off |
| 99 | + # the z prefix protects against any SH wonkiness |
| 100 | + # see https://stackoverflow.com/a/18264223 |
| 101 | + if [ "z$GO111MODULE" = "zoff" ]; then |
| 102 | + echo "modules disabled, skipping modules.txt cleanup" |
| 103 | + return; |
| 104 | + fi |
| 105 | + |
| 106 | + echo "cleanup vendor/modules.txt" |
| 107 | + |
| 108 | + # just in case |
| 109 | + touch "./vendor/modules.txt" |
| 110 | + |
| 111 | + # when vendored, we need to do similar edits to the vendor/modules.txt |
| 112 | + # as we did to the go.mod |
| 113 | + |
| 114 | + # 1. we need to replace any possible copy of the handler code |
| 115 | + rm -rf vendor/handler && \ |
| 116 | + |
| 117 | + # 2. in modules.txt, we remove existing references to the handler/function |
| 118 | + # we reconstruct these in the last step |
| 119 | + grep -v "\shandler/function" ./vendor/modules.txt> modulestext; mv modulestext ./vendor/modules.txt |
| 120 | + |
| 121 | + # 3. Handle any other local replacements. |
| 122 | + # any replace that points to `./**` needs to be udpat echo "cleanup local replace statements" |
| 123 | + sed -i 's/=> \.\//=> \.\/function\//' ./vendor/modules.txt |
| 124 | + |
| 125 | + # 4. To make the modules.txt consistent with the new go.mod, |
| 126 | + # we add the mising replace to the vendor/modules.txt |
| 127 | + echo "## explicit" >> ./vendor/modules.txt |
| 128 | + echo "# handler/function => ./function" >> ./vendor/modules.txt |
| 129 | + |
| 130 | + if [ "$DEBUG" -eq 1 ]; then |
| 131 | + cat ./vendor/modules.txt; |
| 132 | + echo "" |
| 133 | + fi |
| 134 | +} |
| 135 | + |
| 136 | +# has_local_replacement checks if the file contains local go module replacement |
| 137 | +has_local_replacement() { |
| 138 | + return "$(grep -E -c '=> \./\S+' "$1")" |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +################ |
| 143 | +# main |
| 144 | +################ |
| 145 | +move_vendor |
| 146 | + |
| 147 | +cleanup_gomod |
| 148 | + |
| 149 | +cleanup_vendor_modulestxt |
0 commit comments