Skip to content

Commit 4934ce2

Browse files
committed
Added version information to built binary, updated Makefile
1 parent 803f94c commit 4934ce2

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ charset = utf-8
88
trim_trailing_whitespace = false
99
insert_final_newline = false
1010

11-
[{Makefile}]
11+
[Makefile]
1212
indent_style = tab
1313

1414
[*.{html,js,json,css,yml}]

Makefile

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
1-
SRC:=config.go ipc.go options.go $(wildcard cmd/open-in-mpv/*)
2-
EXT_SRC:=$(wildcard extension/Chrome/*) extension/Firefox/manifest.json
3-
SCRIPTS_DIR:=scripts
4-
BUILD_DIR:=build
5-
6-
all: $(BUILD_DIR)/open-in-mpv.tar.gz $(BUILD_DIR)/linux.tar $(BUILD_DIR)/mac.tar $(BUILD_DIR)/windows.tar $(BUILD_DIR)/firefox.zip
7-
8-
builddir:
1+
SRC := config.go ipc.go options.go $(wildcard cmd/open-in-mpv/*)
2+
EXT_SRC := $(wildcard extension/Chrome/*) extension/Firefox/manifest.json
3+
SCRIPTS_DIR := scripts
4+
5+
TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1)
6+
TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true)
7+
COMMIT := $(shell git rev-parse --short HEAD)
8+
DATE_PRETTY := $(shell env TZ=UTC0 git log -1 --format=%cd --date=format:"%Y-%m-%d")
9+
DATE := $(shell env TZ=UTC0 git log -1 --format=%cd --date=format:"%Y%m%d")
10+
VERSION := $(TAG:v%=%)
11+
ifneq ($(COMMIT), $(TAG_COMMIT))
12+
VERSION := $(VERSION)-next-$(COMMIT)-$(DATE)
13+
endif
14+
ifeq ($(VERSION),)
15+
VERSION := $(COMMIT)-$(DATA)
16+
endif
17+
ifneq ($(shell git status --porcelain),)
18+
VERSION := $(VERSION)-dirty
19+
endif
20+
BUILD_DIR := build/$(VERSION)
21+
22+
LDFLAGS := -s -w -X main.Version=$(TAG) -X main.BuildDate=$(DATE_PRETTY) -X main.Commit=$(COMMIT)
23+
24+
25+
all: $(BUILD_DIR)/open-in-mpv_$(VERSION).tar.gz $(BUILD_DIR)/linux.tar $(BUILD_DIR)/mac.tar $(BUILD_DIR)/windows.tar $(BUILD_DIR)/firefox.zip
26+
27+
$(BUILD_DIR):
928
@mkdir -p $(BUILD_DIR)/linux $(BUILD_DIR)/windows $(BUILD_DIR)/mac
1029

11-
$(BUILD_DIR)/open-in-mpv.tar.gz: $(SRC)
30+
$(BUILD_DIR)/open-in-mpv_$(VERSION).tar.gz: $(SRC)
1231
@echo -e "\n# Creating tarball"
1332
go mod vendor
14-
tar czf $(BUILD_DIR)/open-in-mpv.tar.gz --transform "s,^,open-in-mpv/," --exclude $(BUILD_DIR) *
33+
tar czf $(BUILD_DIR)/open-in-mpv_$(VERSION).tar.gz --transform "s,^,open-in-mpv_$(VERSION)/," --exclude $(BUILD_DIR) *
1534
rm -rf vendor
1635

17-
$(BUILD_DIR)/linux/open-in-mpv: $(SRC) builddir
36+
$(BUILD_DIR)/linux/open-in-mpv: $(SRC) $(BUILD_DIR)
1837
@echo -e "\n# Building for Linux"
19-
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o $@ ./cmd/open-in-mpv
38+
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o $@ ./cmd/open-in-mpv
2039
cp $(SCRIPTS_DIR)/install-protocol.sh $(dir $@)
2140
cp $(SCRIPTS_DIR)/open-in-mpv.desktop $(dir $@)
2241

2342
$(BUILD_DIR)/linux.tar: $(BUILD_DIR)/linux/open-in-mpv
2443
tar cf $@ -C $(dir $@)linux $(notdir $(wildcard $(BUILD_DIR)/linux/*))
2544

26-
$(BUILD_DIR)/mac/open-in-mpv.app: $(SRC) scripts/Info.plist builddir
45+
$(BUILD_DIR)/mac/open-in-mpv.app: $(SRC) scripts/Info.plist $(BUILD_DIR)
2746
@# See https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html
2847
@# and https://apple.stackexchange.com/questions/253184/associating-protocol-handler-in-mac-os-x
2948
@echo -e "\n# Building MacOS app bundle"
3049
@mkdir -p $@/Contents/MacOS
3150
go install github.com/randall77/makefat@latest
32-
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o $(dir $@)open-in-mpv.amd64 ./cmd/open-in-mpv
33-
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o $(dir $@)open-in-mpv.arm64 ./cmd/open-in-mpv
51+
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o $(dir $@)open-in-mpv.amd64 ./cmd/open-in-mpv
52+
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$(LDFLAGS)" -o $(dir $@)open-in-mpv.arm64 ./cmd/open-in-mpv
3453
$(GOBIN)/makefat $@/Contents/MacOS/open-in-mpv $(dir $@)open-in-mpv.amd64 $(dir $@)open-in-mpv.arm64
3554
cp config.yml $@/Contents/MacOS/
3655
cp $(SCRIPTS_DIR)/Info.plist $@/Contents
3756

3857
$(BUILD_DIR)/mac.tar: $(BUILD_DIR)/mac/open-in-mpv.app
3958
tar cf $@ -C $(dir $@)/mac open-in-mpv.app
4059

41-
$(BUILD_DIR)/windows/open-in-mpv.exe: $(SRC) builddir
60+
$(BUILD_DIR)/windows/open-in-mpv.exe: $(SRC) $(BUILD_DIR)
4261
@echo -e "\n# Building for Windows"
43-
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -H windowsgui" -o $@ ./cmd/open-in-mpv
62+
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="$(LDFLAGS) -H windowsgui" -o $@ ./cmd/open-in-mpv
4463
cp $(SCRIPTS_DIR)/install-protocol.reg $(dir $@)
4564

4665
$(BUILD_DIR)/windows.tar: $(BUILD_DIR)/windows/open-in-mpv.exe
@@ -62,9 +81,9 @@ uninstall:
6281
rm /usr/bin/open-in-mpv
6382

6483
clean:
65-
rm -rf $(BUILD_DIR)/*
84+
rm -rf $(dir $(BUILD_DIR))*
6685

6786
test:
6887
go test ./...
6988

70-
.PHONY: all builddir install install-protocol uninstall clean test
89+
.PHONY: all install install-protocol uninstall clean test

cmd/open-in-mpv/main.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"log"
67
"os"
@@ -9,6 +10,12 @@ import (
910
oim "github.com/Baldomo/open-in-mpv"
1011
)
1112

13+
var (
14+
Version = "dev"
15+
Commit string
16+
BuildDate string
17+
)
18+
1219
func must(err error) {
1320
if err == nil {
1421
return
@@ -18,8 +25,25 @@ func must(err error) {
1825
}
1926

2027
func main() {
21-
if len(os.Args) < 2 {
22-
fmt.Println("This program is not supposed to be called from the command line!")
28+
flag.Usage = func() {
29+
fmt.Printf("Usage: open-in-mpv [OPTIONS] <URL>\n")
30+
fmt.Printf("Flags:\n")
31+
flag.PrintDefaults()
32+
}
33+
34+
showVersion := flag.Bool("v", false, "show version information")
35+
flag.Parse()
36+
37+
if *showVersion {
38+
fmt.Printf("open-in-mpv %s\n", Version)
39+
fmt.Printf(" Commit: %s\n", Commit)
40+
fmt.Printf(" Built on: %s\n", BuildDate)
41+
os.Exit(0)
42+
}
43+
44+
if flag.NArg() == 0 {
45+
fmt.Println("No arguments supplied!")
46+
flag.Usage()
2347
os.Exit(1)
2448
}
2549

0 commit comments

Comments
 (0)