Skip to content

Commit

Permalink
Overhaul the build system (#375)
Browse files Browse the repository at this point in the history
== Build with Nix

Add all the build dependencies to CI with Nix. Nix is
not a requirement to build direnv but you need to do specify the
dependencies yourself otherwise.

== Allow to checkout direnv everywhere

Handle our own GOPATH in the Makefile. direnv is still buildable with
`go get github.com/direnv/direnv` but should also work if go is just
installed in the system and the user checks out direnv anywhere and runs
`make`

== Better Travis-CI testing

Test for more shells by default and also for formatting by default.

Avoid unnecessary regressions. Some shells like tcsh are
broken and will need to be fixed in the future.

Test on Darwin

== Format all the things

stdlib.sh is formatted with shfmt

== Misc

Makefile: use lower-case variables. When a variable is not exported and
not meant to be changed by the user, use lower-case variables

Rename Makefile to GNUmakefile as it wasn't tested with the other make
implementations.

Ensure that bash is being used with make

Replace usage of `which` with shell builtins when possible.

add BINDIR and MANDIR to the Makefile
  • Loading branch information
zimbatm committed Jul 8, 2018
1 parent 3462e72 commit a4a3c6a
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 186 deletions.
6 changes: 3 additions & 3 deletions .gitignore
@@ -1,9 +1,9 @@
*.sw?
.direnv
.gopath
/direnv
/direnv.test
/dist
/site
/test/allow
/test/direnv
/test/config/direnv/allow
*.sw?
/.idea
19 changes: 10 additions & 9 deletions .travis.yml
@@ -1,12 +1,14 @@
language: go
go:
- 1.8.x
- 1.9.x
- 1.10.x
sudo: false
script: make test
sudo: required
os:
- osx
- linux
language: nix

script: nix-shell --pure --run "make test"

# Deploy
before_deploy:
- PATH="$HOME/gopath/bin:$PATH" make dist
- nix shell --pure --run "make dist"
deploy:
provider: releases
api_key:
Expand All @@ -30,4 +32,3 @@ deploy:
skip_cleanup: true
on:
tags: true
condition: $TRAVIS_GO_VERSION =~ ^1\.10
163 changes: 163 additions & 0 deletions GNUmakefile
@@ -0,0 +1,163 @@
############################################################################
# Variables
############################################################################

# Set this to change the target installation path
DESTDIR ?= /usr/local
BINDIR = ${DESTDIR}/bin
MANDIR = ${DESTDIR}/share/man

# Override the go executable
GO = go

# Change if you want to fork direnv
PACKAGE = github.com/direnv/direnv

# BASH_PATH can also be passed to hard-code the path to bash at build time

SHELL = bash

############################################################################
# Common
############################################################################

.PHONY: all
all: fmt build man

export GOPATH = $(CURDIR)/.gopath
export GO15VENDOREXPERIMENT=1

# Creates the GOPATH for us
base = $(GOPATH)/src/$(PACKAGE)
$(base):
@mkdir -p "$(dir $@)"
@ln -sf "$(CURDIR)" "$@"

############################################################################
# Build
############################################################################

.PHONY: build
build: direnv

.PHONY: clean
clean:
rm -rf \
.gopath \
direnv

GO_LDFLAGS =

ifeq ($(shell uname), Darwin)
# Fixes DYLD_INSERT_LIBRARIES issues
# See https://github.com/direnv/direnv/issues/194
GO_LDFLAGS += -linkmode=external
endif

ifdef BASH_PATH
GO_LDFLAGS += -X main.bashPath=$(BASH_PATH)
endif

ifdef GO_LDFLAGS
GO_FLAGS += -ldflags '$(GO_LDFLAGS)'
endif

direnv: stdlib.go *.go | $(base)
$(GO) fmt
cd "$(base)" && $(GO) build $(GO_FLAGS) -o direnv

stdlib.go: stdlib.sh
cat $< | ./script/str2go main STDLIB $< > $@

version.go: version.txt
echo package main > $@
echo 'const VERSION = "$(shell cat $<)";' >> $@

############################################################################
# Format all the things
############################################################################
.PHONY: fmt fmt-go fmt-sh
fmt: fmt-go fmt-sh

fmt-go:
go fmt

fmt-sh:
shfmt -i 2 -w stdlib.sh

############################################################################
# Documentation
############################################################################

man_md = $(wildcard man/*.md)
roffs = $(man_md:.md=)

.PHONY: man
man: $(roffs)

%.1: %.1.md
@command -v go-md2man >/dev/null || (echo "Could not generate man page because go-md2man is missing. Run: go get -u github.com/cpuguy83/go-md2man"; false)
go-md2man -in $< -out $@

############################################################################
# Testing
############################################################################

tests = \
test-shellcheck \
test-go \
test-go-fmt \
test-bash \
test-elvish \
test-fish \
test-tcsh \
test-zsh

.PHONY: $(tests)
test: build $(tests)
@echo
@echo SUCCESS!

test-go: | $(base)
cd "$(base)" && $(GO) test -v ./...

test-go-fmt:
[ $$(go fmt | tee /dev/stderr | wc -l) = 0 ]

test-shellcheck:
shellcheck stdlib.sh

test-bash:
bash ./test/direnv-test.bash

# Needs elvish 0.12+
test-elvish:
elvish ./test/direnv-test.elv

test-fish:
fish ./test/direnv-test.fish

test-tcsh:
# Currently broken
#tcsh -e ./test/direnv-test.tcsh

test-zsh:
# Currently missing
#zsh ./test/direnv-test.zsh

############################################################################
# Installation
############################################################################

.PHONY: install
install: all
install -d $(BINDIR)
install direnv $(BINDIR)
install -d $(MANDIR)/man1
cp -R man/*.1 $(MANDIR)/man1

.PHONY: dist
dist: | $(base)
@command -v gox >/dev/null || (echo "Could not generate dist because gox is missing. Run: go get -u github.com/mitchellh/gox"; false)
cd "$(base)" && gox -output "dist/direnv.{{.OS}}-{{.Arch}}"

65 changes: 0 additions & 65 deletions Makefile

This file was deleted.

18 changes: 6 additions & 12 deletions README.md
@@ -1,6 +1,8 @@
direnv -- Unclutter your .profile
=================================

[![Built with Nix](https://builtwithnix.org/badge.svg)](https://builtwithnix.org)

`direnv` is an environment switcher for the shell. It knows how to hook into
bash, zsh, tcsh, fish shell and elvish to load or unload environment variables
depending on the current directory. This allows project-specific
Expand Down Expand Up @@ -71,21 +73,13 @@ Setup go environment https://golang.org/doc/install

> go >= 1.9 is required
For example using $HOME/go for your workspace

$ export GOPATH=$HOME/go

Create the directory:

$ mkdir -p $HOME/go/src/github.com/direnv

Clone project into that directory:
Clone project:

$ git clone git@github.com:direnv/direnv.git $HOME/go/src/github.com/direnv/direnv
$ git clone git@github.com:direnv/direnv.git

Build by just typing make:

$ cd $HOME/go/src/github.com/direnv/direnv
$ cd direnv
$ make

To install to /usr/local:
Expand Down Expand Up @@ -137,7 +131,7 @@ Add the following line at the end of the `~/.cshrc` file:
eval `direnv hook tcsh`
```

### Elvish
### Elvish (0.12+)

Run:

Expand Down
2 changes: 1 addition & 1 deletion cmd_stdlib.go
Expand Up @@ -15,7 +15,7 @@ var CmdStdlib = &Cmd{
return
}

fmt.Println(strings.Replace(STDLIB, "$(which direnv)", config.SelfPath, 1))
fmt.Println(strings.Replace(STDLIB, "$(command -v direnv)", config.SelfPath, 1))
return
},
}
30 changes: 24 additions & 6 deletions shell.nix
@@ -1,16 +1,34 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
elvish012 = elvish.overrideDerivation (self: {
name = "elvis-0.12-rc2";

src = fetchFromGitHub {
owner = "elves";
repo = "elvish";
rev = "0.12-rc2";
sha256 = "1nxijphdbqbv032s9mw7miypy92xdyzkql1bcqh0s1d0ghi66hvm";
};

excludedPackages = [ "github.com/elves/elvish/website" ];
});
in
mkShell {
buildInputs = [
gnumake
go
go-md2man
gox
shellcheck
shfmt
which

# Shells
bashInteractive
elvish
elvish012
fish
go
tcsh
zsh
];

shellHook = ''
unset GOPATH
'';
}

0 comments on commit a4a3c6a

Please sign in to comment.