Skip to content

Commit

Permalink
add scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
anilcse committed Jan 24, 2022
1 parent 5e94c35 commit 4df4f59
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ sim_log_file

# IDE
.vscode
.idea
176 changes: 92 additions & 84 deletions contrib/devtools/Makefile
Original file line number Diff line number Diff line change
@@ -1,84 +1,92 @@
# ###
# # Find OS and Go environment
# # GO contains the Go binary
# # FS contains the OS file separator
# ###
# ifeq ($(OS),Windows_NT)
# GO := $(shell where go.exe 2> NUL)
# FS := "\\"
# else
# GO := $(shell command -v go 2> /dev/null)
# FS := "/"
# endif

# ifeq ($(GO),)
# $(error could not find go. Is it in PATH? $(GO))
# endif

# ###############################################################################
# ### Functions ###
# ###############################################################################

# go_get = $(if $(findstring Windows_NT,$(OS)),\
# IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS) ( mkdir $(GITHUBDIR)$(FS)$(1) ) else (cd .) &\
# IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS)$(2)$(FS) ( cd $(GITHUBDIR)$(FS)$(1) && git clone https://github.com/$(1)/$(2) ) else (cd .) &\
# ,\
# mkdir -p $(GITHUBDIR)$(FS)$(1) &&\
# (test ! -d $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && cd $(GITHUBDIR)$(FS)$(1) && git clone https://github.com/$(1)/$(2)) || true &&\
# )\
# cd $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && git fetch origin && git checkout -q $(3)

# mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
# mkfile_dir := $(shell cd $(shell dirname $(mkfile_path)); pwd)


# ###############################################################################
# ### Tools ###
# ###############################################################################

# PREFIX ?= /usr/local
# BIN ?= $(PREFIX)/bin
# UNAME_S ?= $(shell uname -s)
# UNAME_M ?= $(shell uname -m)

# GOPATH ?= $(shell $(GO) env GOPATH)
# GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com

# BUF_VERSION ?= 0.11.0

# TOOLS_DESTDIR ?= $(GOPATH)/bin
# STATIK = $(TOOLS_DESTDIR)/statik
# RUNSIM = $(TOOLS_DESTDIR)/runsim

# tools: tools-stamp
# tools-stamp: statik runsim
# # Create dummy file to satisfy dependency and avoid
# # rebuilding when this Makefile target is hit twice
# # in a row.
# touch $@

# # Install the runsim binary with a temporary workaround of entering an outside
# # directory as the "go get" command ignores the -mod option and will polute the
# # go.{mod, sum} files.
# #
# # ref: https://github.com/golang/go/issues/30515
# statik: $(STATIK)
# $(STATIK):
# @echo "Installing statik..."
# @(cd /tmp && go get github.com/rakyll/[email protected])

# # Install the runsim binary with a temporary workaround of entering an outside
# # directory as the "go get" command ignores the -mod option and will polute the
# # go.{mod, sum} files.
# #
# # ref: https://github.com/golang/go/issues/30515
# runsim: $(RUNSIM)
# $(RUNSIM):
# @echo "Installing runsim..."
# @(cd /tmp && go get github.com/cosmos/tools/cmd/[email protected])

# tools-clean:
# rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM)
# rm -f tools-stamp

# .PHONY: tools-clean statik runsim
###
# Find OS and Go environment
# GO contains the Go binary
# FS contains the OS file separator
###
ifeq ($(OS),Windows_NT)
GO := $(shell where go.exe 2> NUL)
FS := "\\"
else
GO := $(shell command -v go 2> /dev/null)
FS := "/"
endif

ifeq ($(GO),)
$(error could not find go. Is it in PATH? $(GO))
endif

###############################################################################
### Functions ###
###############################################################################

go_get = $(if $(findstring Windows_NT,$(OS)),\
IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS) ( mkdir $(GITHUBDIR)$(FS)$(1) ) else (cd .) &\
IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS)$(2)$(FS) ( cd $(GITHUBDIR)$(FS)$(1) && git clone https://github.com/$(1)/$(2) ) else (cd .) &\
,\
mkdir -p $(GITHUBDIR)$(FS)$(1) &&\
(test ! -d $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && cd $(GITHUBDIR)$(FS)$(1) && git clone https://github.com/$(1)/$(2)) || true &&\
)\
cd $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && git fetch origin && git checkout -q $(3)

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(shell cd $(shell dirname $(mkfile_path)); pwd)


###############################################################################
### Tools ###
###############################################################################

PREFIX ?= /usr/local
BIN ?= $(PREFIX)/bin
UNAME_S ?= $(shell uname -s)
UNAME_M ?= $(shell uname -m)

GOPATH ?= $(shell $(GO) env GOPATH)
GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com

BUF_VERSION ?= 0.11.0

TOOLS_DESTDIR ?= $(GOPATH)/bin
STATIK = $(TOOLS_DESTDIR)/statik
RUNSIM = $(TOOLS_DESTDIR)/runsim

tools: tools-stamp
tools-stamp: statik runsim
# Create dummy file to satisfy dependency and avoid
# rebuilding when this Makefile target is hit twice
# in a row.
touch $@

proto-tools: proto-tools-stamp
proto-tools-stamp:
bash $(mkfile_dir)/proto-tools-installer.sh
# Create dummy file to satisfy dependency and avoid
# rebuilding when this Makefile target is hit twice
# in a row.
touch $@

# Install the runsim binary with a temporary workaround of entering an outside
# directory as the "go get" command ignores the -mod option and will polute the
# go.{mod, sum} files.
#
# ref: https://github.com/golang/go/issues/30515
statik: $(STATIK)
$(STATIK):
@echo "Installing statik..."
@(cd /tmp && go get github.com/rakyll/[email protected])

# Install the runsim binary with a temporary workaround of entering an outside
# directory as the "go get" command ignores the -mod option and will polute the
# go.{mod, sum} files.
#
# ref: https://github.com/golang/go/issues/30515
runsim: $(RUNSIM)
$(RUNSIM):
@echo "Installing runsim..."
@(cd /tmp && go get github.com/cosmos/tools/cmd/[email protected])

tools-clean:
rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM)
rm -f proto-tools-stamp tools-stamp

.PHONY: tools-clean statik runsim
155 changes: 155 additions & 0 deletions contrib/devtools/proto-tools-installer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/bin/bash

set -ue

DESTDIR=${DESTDIR:-}
PREFIX=${PREFIX:-/usr/local}
UNAME_S="$(uname -s 2>/dev/null)"
UNAME_M="$(uname -m 2>/dev/null)"
BUF_VERSION=0.30.1
PROTOC_VERSION=3.13.0
PROTOC_GRPC_GATEWAY_VERSION=1.14.7

f_abort() {
local l_rc=$1
shift

echo $@ >&2
exit ${l_rc}
}

case "${UNAME_S}" in
Linux)
PROTOC_ZIP="protoc-${PROTOC_VERSION}-linux-x86_64.zip"
PROTOC_GRPC_GATEWAY_BIN="protoc-gen-grpc-gateway-v${PROTOC_GRPC_GATEWAY_VERSION}-linux-x86_64"
;;
Darwin)
PROTOC_ZIP="protoc-${PROTOC_VERSION}-osx-x86_64.zip"
PROTOC_GRPC_GATEWAY_BIN="protoc-gen-grpc-gateway-v${PROTOC_GRPC_GATEWAY_VERSION}-darwin-x86_64"
;;
*)
f_abort 1 "Unknown kernel name. Exiting."
esac

TEMPDIR="$(mktemp -d)"

trap "rm -rvf ${TEMPDIR}" EXIT

f_print_installing_with_padding() {
printf "Installing %30s ..." "$1" >&2
}

f_print_done() {
echo -e "\tDONE" >&2
}

f_ensure_tools() {
! which curl &>/dev/null && f_abort 2 "couldn't find curl, aborting" || true
}

f_ensure_dirs() {
mkdir -p "${DESTDIR}/${PREFIX}/bin"
mkdir -p "${DESTDIR}/${PREFIX}/include"
}

f_needs_install() {
if [ -x $1 ]; then
echo -e "\talready installed. Skipping." >&2
return 1
fi

return 0
}

f_install_protoc() {
f_print_installing_with_padding proto_c
f_needs_install "${DESTDIR}/${PREFIX}/bin/protoc" || return 0

pushd "${TEMPDIR}" >/dev/null
curl -o "${PROTOC_ZIP}" -sSL "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/${PROTOC_ZIP}"
unzip -q -o ${PROTOC_ZIP} -d ${DESTDIR}/${PREFIX} bin/protoc; \
unzip -q -o ${PROTOC_ZIP} -d ${DESTDIR}/${PREFIX} 'include/*'; \
rm -f ${PROTOC_ZIP}
popd >/dev/null
f_print_done
}

f_install_buf() {
f_print_installing_with_padding buf
f_needs_install "${DESTDIR}/${PREFIX}/bin/buf" || return 0

curl -sSL "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/buf-${UNAME_S}-${UNAME_M}" -o "${DESTDIR}/${PREFIX}/bin/buf"
chmod +x "${DESTDIR}/${PREFIX}/bin/buf"
f_print_done
}

f_install_protoc_gen_gocosmos() {
f_print_installing_with_padding protoc-gen-gocosmos

if ! grep "github.com/gogo/protobuf => github.com/regen-network/protobuf" go.mod &>/dev/null ; then
echo -e "\tPlease run this command from somewhere inside the cosmos-sdk folder."
return 1
fi

go get github.com/regen-network/cosmos-proto/protoc-gen-gocosmos 2>/dev/null
f_print_done
}

f_install_protoc_gen_grpc_gateway() {
f_print_installing_with_padding protoc-gen-grpc-gateway
f_needs_install "${DESTDIR}/${PREFIX}/bin/protoc-gen-grpc-gateway" || return 0

curl -o "${DESTDIR}/${PREFIX}/bin/protoc-gen-grpc-gateway" -sSL "https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v${PROTOC_GRPC_GATEWAY_VERSION}/${PROTOC_GRPC_GATEWAY_BIN}"
f_print_done
}

f_install_protoc_gen_swagger() {
f_print_installing_with_padding protoc-gen-swagger
f_needs_install "${DESTDIR}/${PREFIX}/bin/protoc-gen-swagger" || return 0

if ! which npm &>/dev/null ; then
echo -e "\tNPM is not installed. Skipping."
return 0
fi

pushd "${TEMPDIR}" >/dev/null
go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
npm install -g swagger-combine
popd >/dev/null
f_print_done
}

f_install_clang_format() {
f_print_installing_with_padding clang-format

if which clang-format &>/dev/null ; then
echo -e "\talready installed. Skipping."
return 0
fi

case "${UNAME_S}" in
Linux)
if [ -e /etc/debian_version ]; then
echo -e "\tRun: sudo apt-get install clang-format" >&2
elif [ -e /etc/fedora-release ]; then
echo -e "\tRun: sudo dnf install clang" >&2
else
echo -e "\tRun (as root): subscription-manager repos --enable rhel-7-server-devtools-rpms ; yum install llvm-toolset-7" >&2
fi
;;
Darwin)
echo "\tRun: brew install clang-format" >&2
;;
*)
echo "\tunknown operating system. Skipping." >&2
esac
}

f_ensure_tools
f_ensure_dirs
f_install_protoc
f_install_buf
f_install_protoc_gen_gocosmos
f_install_protoc_gen_grpc_gateway
f_install_protoc_gen_swagger
f_install_clang_format
21 changes: 21 additions & 0 deletions contrib/githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Git hooks

Installation:

```
$ git config core.hooksPath contrib/githooks
```

## pre-commit

The hook automatically runs `gofmt`, `goimports`, and `misspell`
to correctly format the `.go` files included in the commit, provided
that all the aforementioned commands are installed and available
in the user's search `$PATH` environment variable:

```
$ go get golang.org/x/tools/cmd/goimports
$ go get github.com/golangci/misspell/cmd/misspell@master
```

It also runs `go mod tidy` and `golangci-lint` if available.
Loading

0 comments on commit 4df4f59

Please sign in to comment.