Skip to content

Commit

Permalink
Added version information to built binary, updated Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Baldin committed Dec 21, 2024
1 parent 803f94c commit 4934ce2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false

[{Makefile}]
[Makefile]
indent_style = tab

[*.{html,js,json,css,yml}]
Expand Down
57 changes: 38 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,65 @@
SRC:=config.go ipc.go options.go $(wildcard cmd/open-in-mpv/*)
EXT_SRC:=$(wildcard extension/Chrome/*) extension/Firefox/manifest.json
SCRIPTS_DIR:=scripts
BUILD_DIR:=build

all: $(BUILD_DIR)/open-in-mpv.tar.gz $(BUILD_DIR)/linux.tar $(BUILD_DIR)/mac.tar $(BUILD_DIR)/windows.tar $(BUILD_DIR)/firefox.zip

builddir:
SRC := config.go ipc.go options.go $(wildcard cmd/open-in-mpv/*)
EXT_SRC := $(wildcard extension/Chrome/*) extension/Firefox/manifest.json
SCRIPTS_DIR := scripts

TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1)
TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true)

This comment has been minimized.

Copy link
@vitaly-zdanevich

This comment has been minimized.

Copy link
@vitaly-zdanevich

vitaly-zdanevich Dec 21, 2024

And please consider adding simple make build command - because on Gentoo package manager my script will install by another way (it cannot copy to /usr/bin during Makefile execution)

COMMIT := $(shell git rev-parse --short HEAD)
DATE_PRETTY := $(shell env TZ=UTC0 git log -1 --format=%cd --date=format:"%Y-%m-%d")
DATE := $(shell env TZ=UTC0 git log -1 --format=%cd --date=format:"%Y%m%d")
VERSION := $(TAG:v%=%)
ifneq ($(COMMIT), $(TAG_COMMIT))
VERSION := $(VERSION)-next-$(COMMIT)-$(DATE)
endif
ifeq ($(VERSION),)
VERSION := $(COMMIT)-$(DATA)
endif
ifneq ($(shell git status --porcelain),)
VERSION := $(VERSION)-dirty
endif
BUILD_DIR := build/$(VERSION)

LDFLAGS := -s -w -X main.Version=$(TAG) -X main.BuildDate=$(DATE_PRETTY) -X main.Commit=$(COMMIT)


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

$(BUILD_DIR):
@mkdir -p $(BUILD_DIR)/linux $(BUILD_DIR)/windows $(BUILD_DIR)/mac

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

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

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

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

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

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

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

clean:
rm -rf $(BUILD_DIR)/*
rm -rf $(dir $(BUILD_DIR))*

test:
go test ./...

.PHONY: all builddir install install-protocol uninstall clean test
.PHONY: all install install-protocol uninstall clean test
28 changes: 26 additions & 2 deletions cmd/open-in-mpv/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"log"
"os"
Expand All @@ -9,6 +10,12 @@ import (
oim "github.com/Baldomo/open-in-mpv"
)

var (
Version = "dev"
Commit string
BuildDate string
)

func must(err error) {
if err == nil {
return
Expand All @@ -18,8 +25,25 @@ func must(err error) {
}

func main() {
if len(os.Args) < 2 {
fmt.Println("This program is not supposed to be called from the command line!")
flag.Usage = func() {
fmt.Printf("Usage: open-in-mpv [OPTIONS] <URL>\n")
fmt.Printf("Flags:\n")
flag.PrintDefaults()
}

showVersion := flag.Bool("v", false, "show version information")
flag.Parse()

if *showVersion {
fmt.Printf("open-in-mpv %s\n", Version)
fmt.Printf(" Commit: %s\n", Commit)
fmt.Printf(" Built on: %s\n", BuildDate)
os.Exit(0)
}

if flag.NArg() == 0 {
fmt.Println("No arguments supplied!")
flag.Usage()
os.Exit(1)
}

Expand Down

0 comments on commit 4934ce2

Please sign in to comment.