Skip to content

feat: Turn Core←→Plug-ins #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This code is provided by github.com/bool64/dev.
name: test
on:
push:
branches:
- master
pull_request:
# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
test:
strategy:
matrix:
go-version: [1.17.x]
runs-on: ubuntu-latest
steps:
- name: Install Go stable
if: matrix.go-version != 'tip'
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
- name: Go cache
uses: actions/cache@v3
with:
# In order:
# * Module download cache
# * Build cache (Linux)
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-cache
- name: Test
id: test
run: |
make test
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-include makefiles/help.mk

.PHONY: test

## Run tests
test:
@echo "Running tests"
@bash test.sh
105 changes: 92 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Add `Makefile` to your module with includes standard targets.
```Makefile
#GOLANGCI_LINT_VERSION := "v1.61.0" # Optional configuration to pinpoint golangci-lint version.

# MODULES is a list of dev modules (mk) to be included in the project.
MODULES := \
DEVGO_PATH=github.com/bool64/dev

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
export GO111MODULE = on
Expand All @@ -36,24 +40,31 @@ ifneq "$(GOFLAGS)" ""
$(info GOFLAGS: ${GOFLAGS})
endif

ifneq "$(wildcard ./vendor )" ""
$(info Using vendor)
modVendor = -mod=vendor
# Use vendored dependencies if available.
ifneq ($(wildcard ./vendor),)
modVendor := -mod=vendor
ifeq (,$(findstring -mod,$(GOFLAGS)))
export GOFLAGS := ${GOFLAGS} ${modVendor}
endif
ifneq "$(wildcard ./vendor/github.com/bool64/dev)" ""
DEVGO_PATH := ./vendor/github.com/bool64/dev
endif
endif

ifeq ($(DEVGO_PATH),)
DEVGO_PATH := $(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m github.com/bool64/dev)
ifeq ($(DEVGO_PATH),)
$(info Module github.com/bool64/dev not found, downloading.)
DEVGO_PATH := $(shell export GO111MODULE=on && $(GO) get github.com/bool64/dev && $(GO) list -f '{{.Dir}}' -m github.com/bool64/dev)
endif
endif
# Set dev module paths or download them.
$(foreach module,$(MODULES), \
$(eval key=$(word 1,$(subst =, ,$(module)))); \
$(eval value=$(word 2,$(subst =, ,$(module)))); \
\
$(if $(wildcard ./vendor/$(value)), \
$(eval export $(key)=./vendor/$(value)); \
) \
\
$(if $(strip $($(key))), , \
$(eval export $(key)=$(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m $(value))); \
$(if $(strip $($(key))), \
$(info Module $(value) not found, downloading.); \
$(eval export $(key)=$(shell export GO111MODULE=on && $(GO) get $(value) && $(GO) list -f '{{.Dir}}' -m $(value))); \
) \
) \
)

-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
Expand Down Expand Up @@ -87,3 +98,71 @@ Usage
You can include `$(DEVGO_PATH)/makefiles/build.mk` to add automated versioning of build artifacts. Make will
configure `ldflags` to set up [version info](./version/info.go), then you can access it in runtime with `version.Info()`
or expose with [HTTP handler](./version/handler.go). Version information also includes versions of dependencies.

## Create Plugins

You can add plugins for your project and include them in the Makefile. For example, you can add a plugin
to run `protoc` targets which is place it in `github/<owner>/<repo>/makefiles/protoc.mk`.

Then include it in the Makefile:

```Makefile
# MODULES is a list of dev modules (mk) to be included in the project.
MODULES := \
DEVGO_PATH=github.com/bool64/dev \
DEVGRPCGO_PATH=github/<owner>/<repo>/makefiles/protoc.mk

...

-include $(DEVGO_PATH)/makefiles/bench.mk
-include $(DEVGO_PATH)/makefiles/reset-ci.mk

-include $(DEVGRPCGO_PATH)/makefiles/protoc.mk
```

Then add `github/<owner>/<repo>` to your module with unused import defined in `dev_test.go`:

```go
package mymodule_test

import (
_ "github.com/bool64/dev" // Include development helpers to project.
_ "github.com/<owner>/<repo>" // Include custom plugins to project.
)
```

`DEVGRPCGO_PATH` is a path to the plugin module. It will be the path to the remote repository.

### Plugin Structure

The plugin should have the following structure:

```makefile
GO ?= go

PWD ?= $(shell pwd)

DEVGRPCGO_PATH ?= $(PWD)/vendor/github.com/<owner>/<repo>
DEVGRPCGO_SCRIPTS ?= $(DEVGRPCGO_PATH)/scripts # In case there is a scripts directory.

## Check/install protoc tool
protoc-cli:
@bash $(DEVGRPC_SCRIPTS)/protoc-gen-cli.sh


.PHONY: protoc-cli
```

Then `make` will have these targets:

```
Usage
test: Run tests
test-unit: Run unit tests
test-unit-multi: Run unit tests multiple times
lint: Check with golangci-lint
fix-lint: Apply goimports and gofmt
bench: Run benchmark, iterations count controlled by BENCH_COUNT, default 5.
github-actions: Replace GitHub Actions from template
protoc-cli: Check/install protoc tool
```
37 changes: 24 additions & 13 deletions makefiles/base.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#GOLANGCI_LINT_VERSION := "v1.61.0" # Optional configuration to pinpoint golangci-lint version.

# MODULES is a list of dev modules (mk) to be included in the project.
MODULES := \
DEVGO_PATH=github.com/bool64/dev

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
export GO111MODULE = on
Expand All @@ -8,24 +12,31 @@ ifneq "$(GOFLAGS)" ""
$(info GOFLAGS: ${GOFLAGS})
endif

ifneq "$(wildcard ./vendor )" ""
$(info Using vendor)
modVendor = -mod=vendor
# Use vendored dependencies if available.
ifneq ($(wildcard ./vendor),)
modVendor := -mod=vendor
ifeq (,$(findstring -mod,$(GOFLAGS)))
export GOFLAGS := ${GOFLAGS} ${modVendor}
endif
ifneq "$(wildcard ./vendor/github.com/bool64/dev)" ""
DEVGO_PATH := ./vendor/github.com/bool64/dev
endif
endif

ifeq ($(DEVGO_PATH),)
DEVGO_PATH := $(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m github.com/bool64/dev)
ifeq ($(DEVGO_PATH),)
$(info Module github.com/bool64/dev not found, downloading.)
DEVGO_PATH := $(shell export GO111MODULE=on && $(GO) get github.com/bool64/dev && $(GO) list -f '{{.Dir}}' -m github.com/bool64/dev)
endif
endif
# Set dev module paths or download them.
$(foreach module,$(MODULES), \
$(eval key=$(word 1,$(subst =, ,$(module)))); \
$(eval value=$(word 2,$(subst =, ,$(module)))); \
\
$(if $(wildcard ./vendor/$(value)), \
$(eval export $(key)=./vendor/$(value)); \
) \
\
$(if $(strip $($(key))), , \
$(eval export $(key)=$(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m $(value))); \
$(if $(strip $($(key))), \
$(info Module $(value) not found, downloading.); \
$(eval export $(key)=$(shell export GO111MODULE=on && $(GO) get $(value) && $(GO) list -f '{{.Dir}}' -m $(value))); \
) \
) \
)

-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
Expand Down
62 changes: 62 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

PWD=$(pwd)

TESTDATA_PATH="$PWD/testdata"
OUTPUT="$PWD/testdata/make.output"
TEST_OUTPUT="make.out"

# tmake is the base command to run make
# Every timme the command runs, it runs in a new shell with the local env
# avoiding to use the env from the upstream runner
tmake="make"


strip_output() {
# Regular expression to match both error message formats and extract "Error 1"
error_pattern='make(\\[[0-9]+\\])?:.*Error 1'

cat "$TEST_OUTPUT" | \
grep -v 'Entering directory' | \
grep -v 'Leaving directory' | \
grep -v 'awk: warning: command line argument .* is a directory: skipped' | \
awk -v pattern="$error_pattern" '{ while (match($0, pattern)) { $0 = substr($0, 1, RSTART-1) "Error 1" substr($0, RSTART+RLENGTH); } } 1' \
> "$TEST_OUTPUT.tmp" && mv "$TEST_OUTPUT.tmp" "$TEST_OUTPUT"
}

check_output() {
# cat "$1" > "$2"
# Checking the output
diff "$1" "$2"
if [ $? -ne 0 ]; then
if [ -n "$TEST_FILE" ]; then
echo "Error in $TEST_FILE:${BASH_LINENO[0]}: make output is not the same"
fi
exit 1
fi
}

# Record the start time
start_time=$(date +%s)

# Running test
printf "Test make -> "
cd "$TESTDATA_PATH" && PWD="$TESTDATA_PATH"
$tmake > "$TEST_OUTPUT" 2>/dev/null
if [ $? -ne 0 ]; then
echo "make failed"
exit 1
fi

# Removing the lines that are not part of the output but are appended by github actions
strip_output
# Checking the output
check_output "$TEST_OUTPUT" "$OUTPUT"

# Record the end time
end_time=$(date +%s.%N)

# Calculate the elapsed time with two decimal places
elapsed_time=$(echo "$end_time - $start_time" | bc -l | xargs printf "%.2f\n")

echo "[OK] ${elapsed_time}s"
51 changes: 51 additions & 0 deletions testdata/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#GOLANGCI_LINT_VERSION := "v1.61.0" # Optional configuration to pinpoint golangci-lint version.

# MODULES is a list of dev modules (mk) to be included in the project.
MODULES := \
DEVGO_PATH=github.com/bool64/dev


# The head of Makefile determines the location of dev-go to include standard targets.
GO ?= go
export GO111MODULE = on

ifneq "$(GOFLAGS)" ""
$(info GOFLAGS: ${GOFLAGS})
endif

# Use vendored dependencies if available.
ifneq ($(wildcard ./vendor),)
modVendor := -mod=vendor
ifeq (,$(findstring -mod,$(GOFLAGS)))
export GOFLAGS := ${GOFLAGS} ${modVendor}
endif
endif

# Set dev module paths or download them.
$(foreach module,$(MODULES), \
$(eval key=$(word 1,$(subst =, ,$(module)))); \
$(eval value=$(word 2,$(subst =, ,$(module)))); \
\
$(if $(wildcard ./vendor/$(value)), \
$(eval export $(key)=./vendor/$(value)); \
) \
\
$(if $(strip $($(key))), , \
$(eval export $(key)=$(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m $(value))); \
$(if $(strip $($(key))), \
$(info Module $(value) not found, downloading.); \
$(eval export $(key)=$(shell export GO111MODULE=on && $(GO) get $(value) && $(GO) list -f '{{.Dir}}' -m $(value))); \
) \
) \
)

-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
-include $(DEVGO_PATH)/makefiles/test-unit.mk
-include $(DEVGO_PATH)/makefiles/bench.mk
-include $(DEVGO_PATH)/makefiles/reset-ci.mk

# Add your custom targets here.

## Run tests
test: test-unit
6 changes: 6 additions & 0 deletions testdata/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build never
// +build never

package noprune

import _ "github.com/bool64/dev" // Include CI/Dev scripts to project.
7 changes: 7 additions & 0 deletions testdata/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module testenv

go 1.17

replace github.com/bool64/dev v0.2.36 => ./../

require github.com/bool64/dev v0.2.36 // indirect
13 changes: 13 additions & 0 deletions testdata/make.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Module github.com/bool64/dev not found, downloading.
Usage
test:  Run tests
lint:  Check with golangci-lint
fix-lint:  Apply goimports and gofmt
test-unit:  Run unit tests
test-unit-multi:  Run unit tests multiple times, use `UNIT_TEST_COUNT=10 make test-unit-multi` to control count
bench:  Run benchmark and show result stats, iterations count controlled by BENCH_COUNT, default 5.
bench-run:  Run benchmark, iterations count controlled by BENCH_COUNT, default 5.
bench-stat-diff:  Show benchmark comparison with base branch.
bench-stat:  Show result of benchmark.
reset-ci:  Reset CI files from bool64/dev templates, make sure to review changes before committing.
github-actions:  Replace GitHub Actions from template
Loading