Skip to content

Commit c53d2a1

Browse files
authored
feat: updated common.mk to include setup and self-update (#15)
## What Closes #13 ## Why common.mk was only re-downloaded when it was missing or explicitly deleted. If dev-kit updated the file, consuming repos would silently continue using a stale version until someone manually removed it. This was especially fragile because the logic had to be duplicated in every consuming repo. ## Testing Manually triggered on solar and: confirmed common.mk is re-fetched when content differs and skipped when content is unchanged Confirmed the 1h rate-limit works: .common.mk-checked prevents redundant network calls within the window make update-common-mk-bootstrap ran successfully, rewrote the old two-line recipe in-place <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Build tooling now performs periodic self-checks and will refresh the cached build script when a new version is detected. * Added a bootstrap target that initializes the build setup by fetching and placing the required build script and recording its version. * **Chores** * Improved initialization stability and robust handling of local build script state. * **Documentation** * Updated README snippet showing the new bootstrap and version-tracking usage. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/opendefensecloud/dev-kit/pull/15?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents d3593ad + 8106bc3 commit c53d2a1

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ To include `common.mk` into your own `Makefile` use this snippet or copy the pro
4848
DEV_KIT_VERSION := v1.0.0
4949
-include common.mk
5050
common.mk:
51-
curl -sSL https://raw.githubusercontent.com/opendefensecloud/dev-kit/$(DEV_KIT_VERSION)/common.mk -o $@
51+
@[ -f .common.mk-download ] || \
52+
curl --fail -sSL https://raw.githubusercontent.com/opendefensecloud/dev-kit/$(DEV_KIT_VERSION)/common.mk \
53+
-o .common.mk-download
54+
mv .common.mk-download $@
55+
printf '%s' '$(DEV_KIT_VERSION)' > .common.mk-version
56+
touch .common.mk-checked
5257
```
5358

5459
Add your own targets in your project's `Makefile` like normal:

common.mk

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
SHELL = /usr/bin/env bash -o pipefail
44
.SHELLFLAGS = -ec
55

6+
# Capture the path to this file before any other includes can shift MAKEFILE_LIST.
7+
_COMMON_MK_PATH := $(lastword $(MAKEFILE_LIST))
8+
69
# Set MAKEFLAGS to suppress entering/leaving directory messages
710
MAKEFLAGS += --no-print-directory
811

@@ -228,3 +231,64 @@ $(LOCALGOBIN)/ocm: $(LOCALGOBIN) $(TOOL_LOCK)
228231
version=$$(cut -d@ -f2 <<< $$module); \
229232
test -s $@ && grep -q "$$version" $(LOCALGOBIN)/.ocm-version 2>/dev/null || \
230233
curl -s https://ocm.software/install.sh | VERSION_OCM=$$version bash -s -- $(LOCALGOBIN) && echo $$version > $(LOCALGOBIN)/.ocm-version
234+
235+
# Rewrites the common.mk: rule in the calling project's Makefile to the current bootstrap format.
236+
# Run once per repository to migrate from any older recipe shape.
237+
.PHONY: update-common-mk-bootstrap
238+
update-common-mk-bootstrap: ## Rewrite the common.mk: rule in Makefile to the current bootstrap format
239+
@tmp=Makefile.tmp; \
240+
awk ' \
241+
BEGIN { found = 0 } \
242+
/^common\.mk:$$/ { \
243+
found = 1; \
244+
print; \
245+
print "\t@[ -f .common.mk-download ] || \\"; \
246+
print "\t\tcurl --fail -sSL https://raw.githubusercontent.com/opendefensecloud/dev-kit/$$(DEV_KIT_VERSION)/common.mk \\"; \
247+
print "\t\t -o .common.mk-download"; \
248+
print "\tmv .common.mk-download $$@"; \
249+
print "\tprintf \047%s\047 \047$$(DEV_KIT_VERSION)\047 > .common.mk-version"; \
250+
print "\ttouch .common.mk-checked"; \
251+
skip = 1; next \
252+
} \
253+
skip && /^\t/ { next } \
254+
{ skip = 0; print } \
255+
END { exit(found ? 0 : 1) } \
256+
' Makefile > "$$tmp" && mv "$$tmp" Makefile || { rm -f "$$tmp"; echo "error: common.mk: rule not found in Makefile" >&2; exit 1; }
257+
@echo "Updated common.mk: bootstrap in Makefile"
258+
259+
# ── Self-update ────────────────────────────────────────────────────────────────
260+
# DEV_KIT_VERSION must be set by the including Makefile before -include common.mk.
261+
# This fallback is for environments where common.mk is used standalone.
262+
ifndef DEV_KIT_VERSION
263+
$(warning DEV_KIT_VERSION was not set, using default value "main". Please consider pinning the version to avoid unexpected upgrades.)
264+
DEV_KIT_VERSION := main
265+
endif
266+
267+
# Performs a content-based staleness check at most once per hour.
268+
# If the remote content differs from the local file, deletes this file so that
269+
# Make's include-file-remake mechanism triggers the project's common.mk: rule on
270+
# its next restart — picking up the pre-downloaded .common.mk-download file.
271+
_COMMON_MK_SELF_UPDATE := $(shell \
272+
hash_cmd=$$(command -v sha256sum >/dev/null 2>&1 && echo "sha256sum" || echo "shasum -a 256"); \
273+
stored=$$(cat .common.mk-version 2>/dev/null); \
274+
if [ "$$stored" != "$(DEV_KIT_VERSION)" ]; then \
275+
rm -f .common.mk-checked .common.mk-download; \
276+
elif find .common.mk-checked -mmin -60 2>/dev/null | grep -q .; then \
277+
exit 0; \
278+
fi; \
279+
if curl --fail -sSL \
280+
'https://raw.githubusercontent.com/opendefensecloud/dev-kit/$(DEV_KIT_VERSION)/common.mk' \
281+
-o .common.mk-download 2>/dev/null; then \
282+
remote=$$($$hash_cmd .common.mk-download | cut -d' ' -f1); \
283+
local_hash=$$($$hash_cmd '$(_COMMON_MK_PATH)' 2>/dev/null | cut -d' ' -f1); \
284+
printf '%s' '$(DEV_KIT_VERSION)' > .common.mk-version; \
285+
if [ "$$remote" = "$$local_hash" ]; then \
286+
rm -f .common.mk-download; \
287+
touch .common.mk-checked; \
288+
else \
289+
rm -f '$(_COMMON_MK_PATH)'; \
290+
fi; \
291+
else \
292+
echo >&2 'warning: could not fetch common.mk update, using cached version'; \
293+
touch .common.mk-checked; \
294+
fi)

0 commit comments

Comments
 (0)