Tweak Go plugin build script and targets.

This commit is contained in:
Jeffrey Regan
2019-12-12 10:24:45 -08:00
parent f5805c64b2
commit 400140a401
2 changed files with 49 additions and 44 deletions

View File

@@ -152,6 +152,14 @@ $(pGen)/%.go: $(MYGOBIN)/pluginator
.PHONY: generate-kustomize-builtin-plugins .PHONY: generate-kustomize-builtin-plugins
generate-kustomize-builtin-plugins: $(builtinplugins) generate-kustomize-builtin-plugins: $(builtinplugins)
.PHONY: kustomize-external-go-plugin-build
kustomize-external-go-plugin-build:
./hack/buildExternalGoPlugins.sh ./plugin
.PHONY: kustomize-external-go-plugin-clean
kustomize-external-go-plugin-clean:
./hack/buildExternalGoPlugins.sh ./plugin clean
### End kustomize plugin rules. ### End kustomize plugin rules.
.PHONY: lint-kustomize .PHONY: lint-kustomize
@@ -229,22 +237,8 @@ $(MYGOBIN)/helm:
rm -rf $$d \ rm -rf $$d \
) )
.PHONY: build-external-go-plugins
build-external-go-plugins:
( \
cd plugin; \
../hack/buildExternalGoPlugins.sh; \
)
.PHONY: clean-external-go-plugins
clean-external-go-plugins:
( \
cd plugin; \
../hack/buildExternalGoPlugins.sh clean; \
)
.PHONY: clean .PHONY: clean
clean: clean: kustomize-external-go-plugin-clean
go clean --cache go clean --cache
rm -f $(builtinplugins) rm -f $(builtinplugins)
rm -f $(MYGOBIN)/pluginator rm -f $(MYGOBIN)/pluginator

View File

@@ -1,40 +1,51 @@
#!/usr/bin/env sh #!/usr/bin/env sh
set -e set -e
# We assume one go file per plugin, and the desired name of the plugin .so # Builds or removes Go plugin object code.
# matches the basename of said go file.
# Example: ReplacementTransformer.go -> ReplacementTransformer.so
# #
# We also assume the script is run from a kustomize plugin directory. # Specify plugin root as first arg, e.g.
# Usually one of: #
# - ${HOME}/.config/kustomize/plugin # ./hack/buildExternalGoPlugins.sh $KUSTOMIZE_PLUGIN_HOME
# - ${kustomize_git_repo}/plugin # ./hack/buildExternalGoPlugins.sh $XDG_CONFIG_HOME/kustomize/plugin
plugins_dir="${PWD}" # ./hack/buildExternalGoPlugins.sh ${HOME}/.config/kustomize/plugin
command="${1}" # ./hack/buildExternalGoPlugins.sh ./plugin
#
# add 2nd arg "clean" to remove instead of build.
root=$1
if [ ! -d $root ]; then
echo "Don't see directory $root."
exit 1
fi
fn=buildPlugin
if [ "$2" == "clean" ]; then
fn=removePlugin
fi
function buildPlugin { function buildPlugin {
pushd "${1}" >& /dev/null echo "Building $1/$2.so"
plugin_src="$(find "${1}" -name '*.go'|grep -Ev '.*_test.go'|head -n1)" # Change dir so local go.mod applies
plugin_bin="$(echo "${plugin_src}"|sed 's/\(.*\).go/\1.so/')" pushd $1 >& /dev/null
if [ "${command}" = "clean" ]; then go build -buildmode plugin -o $2.so $2.go
echo "Deleting ${plugin_bin}"
rm -f "${plugin_bin}"
return
fi
echo "Building ${plugin_bin}"
go build -buildmode plugin -o "${plugin_bin}" "${plugin_src}"
popd >& /dev/null popd >& /dev/null
} }
for goMod in $(find "${plugins_dir}" -name 'go.mod'); do function removePlugin {
d=$(dirname "${goMod}") local f="$1/$2.so"
# Skip "builtin" plugins in kustomize repo. if [ -f "$f" ]; then
if [ ! -z "$(echo "${goMod}" | grep -F "/plugin/builtin/")" ]; then echo "Removing $f"
continue rm "$f"
fi fi
# Skip plugins with only test Go files. }
if [ -z "$(find "${d}" -maxdepth 1 -name '*.go' | grep -Ev ".*_test.go")" ]; then
continue goPlugins=$(
fi find $root -name "*.go" |
buildPlugin "${d}" grep -v builtin/ |
xargs grep -l "var KustomizePlugin")
for p in $goPlugins; do
d=$(dirname "$p")
n=$(basename "$p" | cut -f 1 -d '.')
$fn $d $n
done done