From 93c49f181bcac36ec21e1dd20a97d33ef366d93b Mon Sep 17 00:00:00 2001
From: Oleg Utkin <oleg.utkin@uber.com>
Date: Wed, 15 Feb 2023 14:09:30 -0800
Subject: [PATCH] feat: improve install experience

---
 .envrc                               |  3 +-
 .gitignore                           |  2 +-
 .goreleaser.yaml                     | 28 +++++++++++++++++++
 Makefile                             | 21 ++++++++++++--
 cmd/goprompt/cmdInstall.go           | 41 ++++++++++++++++++++++++++++
 go.mod                               |  2 +-
 plugin/zsh/prompt_asynczle_setup.zsh |  2 ++
 7 files changed, 93 insertions(+), 6 deletions(-)
 create mode 100644 .goreleaser.yaml
 create mode 100644 cmd/goprompt/cmdInstall.go

diff --git a/.envrc b/.envrc
index f8edd57..aa94036 100644
--- a/.envrc
+++ b/.envrc
@@ -1 +1,2 @@
-eval $(gimme 1.18.3)
+eval $(gimme 1.20)
+PATH_add dist
diff --git a/.gitignore b/.gitignore
index 69555c1..bb3fabb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,3 @@
 .idea
 .vscode
-goprompt
+dist
diff --git a/.goreleaser.yaml b/.goreleaser.yaml
new file mode 100644
index 0000000..49d38ce
--- /dev/null
+++ b/.goreleaser.yaml
@@ -0,0 +1,28 @@
+before:
+  hooks:
+    - go mod tidy
+builds:
+  - main: ./cmd/goprompt
+    binary: frk
+    env:
+      - CGO_ENABLED=0
+    goos:
+      - linux
+      - darwin
+archives:
+  - replacements:
+      darwin: Darwin
+      linux: Linux
+      windows: Windows
+      386: i386
+      amd64: x86_64
+    format: binary
+    wrap_in_directory: false
+
+checksum:
+  name_template: 'checksums.txt'
+snapshot:
+  name_template: "{{ incpatch .Version }}-next"
+
+# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
+# vim: set ts=2 sw=2 tw=0 fo=cnqoj
diff --git a/Makefile b/Makefile
index e19e242..259ba40 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,6 @@
+export prefix?=$(HOME)/.local
+export bindir?=$(prefix)/bin
+
 MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
 CURRENT_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
 
@@ -6,13 +9,22 @@ ZSH_PROMPT_SETUP_SCRIPT := $(CURRENT_DIR)/plugin/zsh/prompt_asynczle_setup.zsh
 USR_BIN_DIR := $(HOME)/bin
 USR_ZSH_DIR := $(HOME)/.local/share/zsh-funcs
 
+.PHONY: publish
+publish:
+	goreleaser release --rm-dist
+
+.PHONY: release
+release:
+	goreleaser release --rm-dist --snapshot --skip-publish
+
+.PHONY: build
 build:
-	go build -o "goprompt" ./cmd/goprompt 
+	goreleaser build --rm-dist --snapshot --single-target --output dist/goprompt
 
 .PHONY: install
-install:
+install: build
 	mkdir -p "$(USR_BIN_DIR)"
-	go build -o "$(USR_BIN_DIR)/goprompt" ./cmd/goprompt 
+	cp dist/goprompt "$(USR_BIN_DIR)/goprompt"
 	mkdir -p "$(USR_ZSH_DIR)"
 	cp "$(ZSH_PROMPT_SETUP_SCRIPT)" "$(USR_ZSH_DIR)/prompt_asynczle_setup"
 	$(MAKE) setup
@@ -42,3 +54,6 @@ try: install
 	@echo '>> FOR DEVELOPMENT ONLY <<'
 	ZSH_DISABLE_PROMPT=Y ZSH_EXTRA_SOURCE="$(ZSH_PROMPT_SETUP_SCRIPT)" zsh
 
+.PHONY: clean
+clean:
+	rm -rf dist
diff --git a/cmd/goprompt/cmdInstall.go b/cmd/goprompt/cmdInstall.go
new file mode 100644
index 0000000..0f7695a
--- /dev/null
+++ b/cmd/goprompt/cmdInstall.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+	"fmt"
+	"github.com/spf13/cobra"
+)
+
+var (
+	cmdInstall = &cobra.Command{
+		Use:   "install",
+		Short: "install the integration",
+	}
+)
+
+func init() {
+	cmdInstall.RunE = cmdInstallRun
+}
+
+// TODO: bundle in the plugin directory, and provide a way to extract it into users directory of choice.
+
+func cmdInstallRun(command *cobra.Command, args []string) error {
+	fmt.Println(`
+# SETUP:
+# ------------------------------------------------------------------------------
+# Assuming GoPrompt is installed in $(USR_BIN_DIR)
+# and zsh func in $(USR_ZSH_DIR)
+# ------------------------------------------------------------------------------
+# $$ make setup >> ~/.zshrc"
+# ------------------------------------------------------------------------------
+# Add this to your ~/.zshenv
+# ------------------------------------------------------------------------------
+
+# PROMPT_ASYNC_ZLE: ------------------------------------------------------------
+path+=( "$(USR_BIN_DIR)" )
+fpath+=( "$(USR_ZSH_DIR)" )
+autoload -Uz promptinit
+promptinit && prompt_asynczle_setup
+# ------------------------------------------------------------------------------
+	`)
+	return nil
+}
diff --git a/go.mod b/go.mod
index dab575f..ff612a2 100644
--- a/go.mod
+++ b/go.mod
@@ -1,6 +1,6 @@
 module github.com/NonLogicalDev/shell.async-goprompt
 
-go 1.18
+go 1.20
 
 require (
 	github.com/gookit/color v1.5.1
diff --git a/plugin/zsh/prompt_asynczle_setup.zsh b/plugin/zsh/prompt_asynczle_setup.zsh
index 0c80746..ccf4f2f 100755
--- a/plugin/zsh/prompt_asynczle_setup.zsh
+++ b/plugin/zsh/prompt_asynczle_setup.zsh
@@ -126,6 +126,8 @@ __zle_async_dispatch() {
     __zle_async_detach "$OLD_ZLE_FD" 2>/dev/null
   fi
 
+  local ZLE_FD
+
   # Create File Descriptor and attach to async command
   exec {ZLE_FD}< <( "${command[@]}" )