Skip to content

Commit

Permalink
✨ add pre-commit + makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
ptavares committed Oct 8, 2021
1 parent cdd3047 commit 4d1484d
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: requirements-txt-fixer

- repo: https://github.com/frnmst/md-toc
rev: 8.0.0
hooks:
- id: md-toc
language_version: python3
106 changes: 106 additions & 0 deletions .release/make-release-support
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/bash
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
RELEASE=.release/release

function hasChanges() {
test -n "$(git status -s .)"
}

function getRelease() {
awk -F= '/^release=/{print $2}' ${RELEASE}
}

function getBaseTag() {
sed -n -e "s/^tag=\(.*\)$(getRelease)\$/\1/p" ${RELEASE}
}

function getTag() {
if [ -z "$1" ] ; then
awk -F= '/^tag/{print $2}' ${RELEASE}
else
echo "$(getBaseTag)$1"
fi
}

function setRelease() {
if [ -n "$1" ] ; then
sed -i.x -e "s/^tag=.*/tag=$(getTag $1)/" ${RELEASE}
sed -i.x -e "s/^release=.*/release=$1/g" ${RELEASE}
rm -f ${RELEASE}.x
runPreTagCommand "$1"
else
echo "ERROR: missing release version parameter " >&2
return 1
fi
}

function runPreTagCommand() {
if [ -n "$1" ] ; then
COMMAND=$(sed -n -e "s/@@RELEASE@@/$1/g" -e 's/^pre_tag_command=\(.*\)/\1/p' ${RELEASE})
if [ -n "$COMMAND" ] ; then
if ! OUTPUT=$(bash -c "$COMMAND" 2>&1) ; then echo $OUTPUT >&2 && exit 1 ; fi
fi
else
echo "ERROR: missing release version parameter " >&2
return 1
fi
}

function tagExists() {
tag=${1:-$(getTag)}
test -n "$tag" && test -n "$(git tag | grep "^$tag\$")"
}

function differsFromRelease() {
tag=$(getTag)
! tagExists $tag || test -n "$(git diff --shortstat -r $tag .)"
}

function getVersion() {
result=$(getRelease)

if differsFromRelease; then
result="$result-$(git log -n 1 --format=%h .)"
fi

if hasChanges ; then
result="$result-dirty"
fi
echo $result
}

function nextPatchLevel() {
version=${1:-$(getRelease)}
major_and_minor=$(echo $version | cut -d. -f1,2)
patch=$(echo $version | cut -d. -f3)
version=$(printf "%s.%d" $major_and_minor $(($patch + 1)))
echo $version
}

function nextMinorLevel() {
version=${1:-$(getRelease)}
major=$(echo $version | cut -d. -f1);
minor=$(echo $version | cut -d. -f2);
version=$(printf "%d.%d.0" $major $(($minor + 1))) ;
echo $version
}

function nextMajorLevel() {
version=${1:-$(getRelease)}
major=$(echo $version | cut -d. -f1);
version=$(printf "%d.0.0" $(($major + 1)))
echo $version
}
2 changes: 2 additions & 0 deletions .release/release
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release=0.0.0
tag=0.0.0
Empty file added CHANGELOG.md
Empty file.
110 changes: 110 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Contributing

:+1: :tada: First off, thanks for taking the time to contribute! :tada: :+1:

The following is a set of guidelines for contributing to **zsh-exa**. These are mostly guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request.

## Table of Content

<!--TOC-->

- [Contributing](#contributing)
- [Table of Content](#table-of-content)
- [Developers Information](#developers-information)
- [Tools used](#tools-used)
- [Pre-commit](#pre-commit)
- [Makefile Usage](#makefile-usage)
- [Styleguides](#styleguides)
- [Git Commit Messages](#git-commit-messages)
- [Documentation Styleguide](#documentation-styleguide)

<!--TOC-->

## Tools used

### Pre-commit

To facilitate testing before committing, you have the option of running automated tests with pre-commit. Each hook enables a test that we can customize.

#### The main hooks configured

##### Linting

###### md-toc

To ensure an automatic generation of markdown *Table of content*, the `md-toc` hook is used.

#### Install dependencies

* [`pre-commit`](https://pre-commit.com/#install)

#### Usage Pre-commit

To execution automated on commit you can install pre-commit on your repo folder

```bash
pre-commit install
```

To perform code test manually, you must run the pre-commit before pushing your code
like this:

```bash
pre-commit install
pre-commit run --all
```

### Makefile

#### Install dependencies

* [`docker`](https://docs.docker.com/get-docker/)

#### Usage

:warning: **Warning** : Don't tag manually.

```bash
> make help

default Default Task, build program with default values
version Get current version
check-status Check current git status
check-release Check release status
major-release Do a major-release, ie : bumped first digit X+1.y.z
minor-release Do a minor-release, ie : bumped second digit x.Y+1.z
patch-release Do a patch-release, ie : bumped third digit x.y.Z+1
precommit Execute some checks with pre-commit hooks
help Show this help (Run make <target> V=1 to enable verbose)
```

## Styleguides

### Git Commit Messages

* Use the present tense ("Add feature" not "Added feature")
* Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
* Limit commit title to 48 characters or less
* Reference issues and pull requests liberally in commit title/message
* Consider starting the commit message with an applicable [emoji](https://gitmoji.dev/) like :
* :sparkles: `:sparkles:` when introducing new features
* :recycle: `:recycle:` when refactoring code
* :art: `:art:` when improving the format/structure of the code
* :zap: `:zap:` when improving performance
* :memo: `:memo:` when writing docs
* :bug: `:bug:` when fixing a bug
* :fire: `:fire:` when removing code or files
* :green_heart: `:green_heart:` when fixing the CI build
* :pushpin: `:pushpin:` when pinning dependencies to specific version
* :arrow_up: `:arrow_up:` when upgrading dependencies
* :arrow_down: `:arrow_down:` when downgrading dependencies
* :rotating_light: `:rotating_light:` when removing linter warnings

### Changelog

Changelog is generated automatically (based on [gitmoji-changelog](https://github.com/frinyvonnick/gitmoji-changelog)) when building a new release.


### Documentation Styleguide

* Use [Markdown](https://daringfireball.net/projects/markdown).
135 changes: 135 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
###################
# RELEASE ENV
##################
RELEASE_SUPPORT=.release/make-release-support
RELEASE_FILE=.release/release
VERSION=$(shell . $(RELEASE_SUPPORT) ; getVersion)
TAG=$(shell . $(RELEASE_SUPPORT); getTag)
SHA=$(shell git show-ref -s $(TAG))

#####################
# COMMON VALUES
#####################
SHELL=/bin/bash
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m▶\033[0m")
MT = $(shell printf " \033[36;1m▶\033[0m")
MT2 = $(shell printf " \033[36;1m-\033[0m")

#####################
# TARGETS
#####################
default: .build version ; @ ## Default Task, build program with default values

version: .do-version ; @ ## Get current version

check-status: .do-check-status ; @ ## Check current git status

check-release: .do-check-release ; @ ## Check release status

major-release: .do-major-release ; @ ## Do a major-release, ie : bumped first digit X+1.y.z

minor-release: .do-minor-release ; @ ## Do a minor-release, ie : bumped second digit x.Y+1.z

patch-release: .do-patch-release ; @ ## Do a patch-release, ie : bumped third digit x.y.Z+1

precommit: .do-precommit ; @ ## Execute some checks with pre-commit hooks

help: .do-help ; @ ## Show this help (Run make <target> V=1 to enable verbose)

# =====================
# ==== BUILD =====
# =====================
.build: .build-info .pre-build .do-build .post-build
.build-info: ; $(info $(M) Building...)

.pre-build:

.do-build: ; $(info $(MT) Nothing to build here.)

.post-build:

# =====================
# ==== RELEASES =====
# =====================

# ===> Major
.do-major-release: .major-release-info .tag-major-release .do-release version
.major-release-info: ; $(info $(M) Do major release...)
.tag-major-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextMajorLevel)
.tag-major-release: .release .tag

# ===> Minor
.do-minor-release: .minor-release-info .tag-minor-release .do-release version
.minor-release-info: ; $(info $(M) Do minor release...)
.tag-minor-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextMinorLevel)
.tag-minor-release: .release .tag

# ===> Path
.do-patch-release: .patch-release-info .tag-patch-release .do-release version
.patch-release-info: ; $(info $(M) Do minor release...)
.tag-patch-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextPatchLevel)
.tag-patch-release: .release .tag


# ===> INIT RELEASE FILE
.release:
@echo "release=0.0.0" > $(RELEASE_FILE)
@echo "tag=0.0.0" >> $(RELEASE_FILE)
@echo INFO: $(RELEASE_FILE) created
@cat $(RELEASE_FILE)

# ===> DO RELEASE
.do-release: check-status check-release

# ===> Do TAG
.tag: TAG=$(shell . $(RELEASE_SUPPORT); getTag $(VERSION))
.tag: check-status
@. $(RELEASE_SUPPORT) ; ! tagExists $(TAG) || (echo "ERROR: tag $(TAG) for version $(VERSION) already tagged in git" >&2 && exit 1) ;
@. $(RELEASE_SUPPORT) ; setRelease $(VERSION)
sed -i -e "s/Release_version-.*-blue/Release_version-$(VERSION)-blue/g" README.md
sed -i -e "s/\"version\": \".*\"/\"version\": \"$(VERSION)\"/g" package.json
docker container run -it -v ${PWD}:/app --rm yvonnick/gitmoji-changelog:latest update $(VERSION)
git add --all
git commit -m ":bookmark: bumped to version $(VERSION)" ;
git tag $(TAG) ;
@ if [ -n "$(shell git remote -v)" ] ; then git push --tags ; else echo 'no remote to push tags to' ; fi
git push

# ===> CHECK RELEASE
.do-check-release: ; $(info $(M) Checking release...)
@. $(RELEASE_SUPPORT) ; tagExists $(TAG) || (echo "ERROR: version not yet tagged in git. make [minor,major,patch]-release." >&2 && exit 1) ;
@. $(RELEASE_SUPPORT) ; ! differsFromRelease $(TAG) || (echo "ERROR: current directory differs from tagged $(TAG). make [minor,major,patch]-release." ; exit 1)


# =======================
# === COMMONS =====
# =======================

# ===> Get current version
.do-version: ; $(info $(M) Current version)
$(info $(MT) $(VERSION))

# ===> Check current repository status
.do-check-status: ; $(info $(M) Checking git status...)
@. $(RELEASE_SUPPORT) ; ! hasChanges || (echo "ERROR: there are still outstanding changes" >&2 && exit 1) ;

# ===> Run pre-commit
.do-precommit: ; $(info $(M) Checking pre-commit hooks...)
pre-commit run -a

# ===> Help
.do-help:
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

# ===========================================================================================

BUILD_TARGETS := pre-build do-build post-build build
REALEASE_TARGETS := check-release major-release minor-release patch-release
INFO_TARGETS := version .do-version check-status .do-check-status
PRECOMMIT_TARGETS := precommit do-precommit
HELP_TARGETS := help .do-help

.PHONY: $(BUILD_TARGETS) $(RELEASE_TARGETS) $(INFO_TARGETS) $(PRECOMMIT_TARGETS) $(HELP_TARGETS)
Loading

0 comments on commit 4d1484d

Please sign in to comment.