Skip to content

Commit

Permalink
build: refactor makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
o1egl committed Dec 20, 2021
1 parent b1e0d5b commit f81857a
Show file tree
Hide file tree
Showing 15 changed files with 3,333 additions and 87 deletions.
28 changes: 21 additions & 7 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
pull_request:

jobs:
# linters
lint-frontend:
runs-on: ubuntu-latest
steps:
Expand All @@ -34,24 +35,37 @@ jobs:
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm i -g commitlint
- run: make lint-commits
lint:
runs-on: ubuntu-latest
needs: [lint-frontend, lint-backend, lint-commints]
steps:
- run: echo "passed"
test:
- run: echo "done"

# tests
test-frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.17
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: make test
- run: make test-frontend
test-backend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.17
- run: make test-backend
test:
runs-on: ubuntu-latest
needs: [test-frontend, test-backend]
steps:
- run: echo "done"

# release
release:
runs-on: ubuntu-latest
needs: [lint, test]
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
*.db
*.lock
*.bak
_old
rice-box.go
Expand Down
104 changes: 37 additions & 67 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,98 +1,68 @@
SHELL := /bin/bash
BASE_PATH := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
VERSION ?= $(shell git describe --tags --always --match=v* 2> /dev/null || \
cat $(CURDIR)/.version 2> /dev/null || echo v0)
VERSION_HASH = $(shell git rev-parse HEAD)

BIN = $(BASE_PATH)/bin
PATH := $(BIN):$(PATH)
export PATH

# printing
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m▶\033[0m")

GO = GOGC=off go
# go module
MODULE = $(shell env GO111MODULE=on $(GO) list -m)

DATE ?= $(shell date +%FT%T%z)
VERSION ?= $(shell git describe --tags --always --match=v* 2> /dev/null || \
cat $(CURDIR)/.version 2> /dev/null || echo v0)
VERSION_HASH = $(shell git rev-parse --short HEAD)
BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
include common.mk
include tools.mk

LDFLAGS += -X "$(MODULE)/version.Version=$(VERSION)" -X "$(MODULE)/version.CommitSHA=$(VERSION_HASH)"

# tools
$(BIN):
@mkdir -p $@
$(BIN)/%: | $(BIN) ; $(info $(M) installing $(PACKAGE)…)
$Q env GOBIN=$(BIN) $(GO) install $(PACKAGE)
## Build:

GOLANGCI_LINT = $(BIN)/golangci-lint
$(BIN)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint@v1.41.1

GOIMPORTS = $(BIN)/goimports
$(BIN)/goimports: PACKAGE=golang.org/x/tools/cmd/goimports@v0.1.8

## build: Build
.PHONY: build
build: | build-frontend build-backend ; $(info $(M) building…)
build: | build-frontend build-backend ## Build binary

## build-frontend: Build frontend
.PHONY: build-frontend
build-frontend: | ; $(info $(M) building frontend…)
build-frontend: ## Build frontend
$Q cd frontend && npm ci && npm run build

## build-backend: Build backend
.PHONY: build-backend
build-backend: | ; $(info $(M) building backend…)
$Q $(GO) build -ldflags '$(LDFLAGS)' -o .
build-backend: ## Build backend
$Q $(go) build -ldflags '$(LDFLAGS)' -o .

## test: Run all tests
.PHONY: test
test: | test-frontend test-backend ; $(info $(M) running tests…)
test: | test-frontend test-backend ## Run all tests

## test-frontend: Run frontend tests
.PHONY: test-frontend
test-frontend: | ; $(info $(M) running frontend tests…)
test-frontend: ## Run frontend tests

## test-backend: Run backend tests
.PHONY: test-backend
test-backend: | ; $(info $(M) running backend tests…)
$Q $(GO) test -v ./...
test-backend: ## Run backend tests
$Q $(go) test -v ./...

## lint: Lint
.PHONY: lint
lint: lint-frontend lint-backend lint-commits | ; $(info $(M) running all linters…)
lint: lint-frontend lint-backend lint-commits ## Run all linters

## lint-frontend: Lint frontend
.PHONY: lint-frontend
lint-frontend: | ; $(info $(M) running frontend linters…)
lint-frontend: ## Run frontend linters
$Q cd frontend && npm ci && npm run lint

## lint-backend: Lint backend
.PHONY: lint-backend
lint-backend: | $(GOLANGCI_LINT) ; $(info $(M) running backend linters…)
$Q $(GOLANGCI_LINT) run -v
lint-backend: | $(golangci-lint) ## Run backend linters
$Q $(golangci-lint) run -v

## lint-commits: Lint commits
.PHONY: lint-commits
lint-commits: | ; $(info $(M) running commitlint…)
lint-commits: $(commitlint) ## Run commit linters
$Q ./scripts/commitlint.sh

## fmt: Format source files
fmt: $(GOIMPORTS)
$Q $(GOIMPORTS) -local $(MODULE) -w $$(find . -type f -name '*.go' -not -path "./vendor/*")
fmt: $(goimports) ## Format source files
$Q $(goimports) -local $(MODULE) -w $$(find . -type f -name '*.go' -not -path "./vendor/*")

clean: clean-tools ## Clean

## Release:

## bump-version: Bump app version
.PHONY: bump-version
bump-version: | ; $(info $(M) creating a new release…)
bump-version: $(standard-version) ## Bump app version
$Q ./scripts/bump_version.sh

## help: Show this help
.PHONY: help
help:
@sed -n 's/^## //p' $(MAKEFILE_LIST) | column -t -s ':' | sed -e 's/^/ /' | sort
## Help:
help: ## Show this help
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target> [options]${RESET}'
@echo ''
@echo 'Options:'
@$(call global_option, "V [0|1]", "enable verbose mode (default:0)")
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"crypto/tls"
"errors"
"io"
"io/fs"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -299,7 +299,7 @@ func setupLog(logMethod string) {
case "stderr":
log.SetOutput(os.Stderr)
case "":
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
default:
log.SetOutput(&lumberjack.Logger{
Filename: logMethod,
Expand Down
28 changes: 28 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SHELL := /bin/bash
DATE ?= $(shell date +%FT%T%z)
BASE_PATH := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
VERSION ?= $(shell git describe --tags --always --match=v* 2> /dev/null || \
cat $(CURDIR)/.version 2> /dev/null || echo v0)
VERSION_HASH = $(shell git rev-parse HEAD)
BRANCH = $(shell git rev-parse --abbrev-ref HEAD)

go = GOGC=off go
MODULE = $(shell env GO111MODULE=on go list -m)

# printing
# $Q (quiet) is used in the targets as a replacer for @.
# This macro helps to print the command for debugging by setting V to 1. Example `make test-unit V=1`
V = 0
Q = $(if $(filter 1,$V),,@)
# $M is a macro to print a colored ▶ character. Example `$(info $(M) running coverage tests…)` will print "▶ running coverage tests…"
M = $(shell printf "\033[34;1m▶\033[0m")

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)

define global_option
printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n" $(1) $(2)
endef
4 changes: 2 additions & 2 deletions diskcache/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (f *FileCache) Load(ctx context.Context, key string) (value []byte, exist b
}
defer r.Close()

value, err = ioutil.ReadAll(r)
value, err = io.ReadAll(r)
if err != nil {
return nil, false, err
}
Expand Down
11 changes: 6 additions & 5 deletions http/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ func handle(fn handleFunc, prefix string, store *storage.Storage, server *settin
server: server,
})

if status != 0 {
txt := http.StatusText(status)
http.Error(w, strconv.Itoa(status)+" "+txt, status)
}

if status >= 400 || err != nil {
clientIP := realip.FromRequest(r)
log.Printf("%s: %v %s %v", r.URL.Path, status, clientIP, err)
}

if status != 0 {
txt := http.StatusText(status)
http.Error(w, strconv.Itoa(status)+" "+txt, status)
return
}
})

return stripPrefix(prefix, handler)
Expand Down
2 changes: 1 addition & 1 deletion http/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestPublicShareHandlerAuthentication(t *testing.T) {

func newHTTPRequest(t *testing.T, requestModifiers ...func(*http.Request)) *http.Request {
t.Helper()
r, err := http.NewRequest(http.MethodGet, "h", nil)
r, err := http.NewRequest(http.MethodGet, "h", http.NoBody)
if err != nil {
t.Fatalf("failed to construct request: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions settings/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -50,8 +51,8 @@ func (s *Settings) MakeUserDir(username, userScope, serverRoot string) (string,
}

// Create default user dir
userHomeBase := s.Defaults.Scope + string(os.PathSeparator) + "users"
userHome := userHomeBase + string(os.PathSeparator) + username
userHomeBase := filepath.Join(s.Defaults.Scope, "users")
userHome := filepath.Join(userHomeBase, username)
err = fs.MkdirAll(userHome, os.ModePerm)
if err != nil {
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
Expand Down
37 changes: 37 additions & 0 deletions tools.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include common.mk

# tools
TOOLS_DIR := $(BASE_PATH)/tools
TOOLS_GO_DEPS := $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum
TOOLS_BIN := $(TOOLS_DIR)/bin
$(eval $(shell mkdir -p $(TOOLS_BIN)))
PATH := $(TOOLS_BIN):$(PATH)
export PATH

.PHONY: clean-tools
clean-tools:
$Q rm -rf $(TOOLS_BIN)

goimports=$(TOOLS_BIN)/goimports
$(goimports): $(TOOLS_GO_DEPS)
$Q cd $(TOOLS_DIR) && $(go) build -o $@ golang.org/x/tools/cmd/goimports

golangci-lint=$(TOOLS_BIN)/golangci-lint
$(golangci-lint): $(TOOLS_GO_DEPS)
$Q cd $(TOOLS_DIR) && $(go) build -o $@ github.com/golangci/golangci-lint/cmd/golangci-lint

# js tools
TOOLS_JS_DEPS=$(TOOLS_DIR)/node_modules/.modified
$(TOOLS_JS_DEPS): $(TOOLS_DIR)/package.json $(TOOLS_DIR)/yarn.lock
$Q cd ${TOOLS_DIR} && yarn install
$Q touch -am $@

standard-version=$(TOOLS_BIN)/standard-version
$(standard-version): $(TOOLS_JS_DEPS)
$Q ln -sf $(TOOLS_DIR)/node_modules/.bin/standard-version $@
$Q touch -am $@

commitlint=$(TOOLS_BIN)/commitlint
$(commitlint): $(TOOLS_JS_DEPS)
$Q ln -sf $(TOOLS_DIR)/node_modules/.bin/commitlint $@
$Q touch -am $@
Loading

0 comments on commit f81857a

Please sign in to comment.