Commit afd5807

mo khan <mo@mokhan.ca>
2021-02-06 00:21:54
feat: add shell script shim to change directory after jive command
1 parent ed288e9
exe/jive
@@ -1,4 +1,4 @@
-#!/usr/bin/env -S ruby --disable-gems
+#!/usr/bin/env ruby
 # frozen_string_literal: true
 
 require "jive/cli"
exe/jive.sh
@@ -1,41 +0,0 @@
-#!/bin/sh
-# shellcheck disable=1001,1012,2039,1090
-# 1001,1012 stop complaints about '\awk' syntax to bypass aliases.
-# 2039 stops complaints about array references not being POSIX.
-# 1090 stops complaints about sourcing non-constant paths.
-
-__shell="$(\ps -p $$ | \awk 'NR > 1 { sub(/^-/, "", $4); print $4 }')"
-__shellname="$(basename "${__shell}")"
-
-case "${__shellname}" in
-  bash)
-    __jive_source_dir="$(builtin cd "$(\dirname "${BASH_SOURCE[0]}")" && \pwd)"
-    ;;
-  zsh)
-    __jive_source_dir="$(\dirname "$0:A")"
-    ;;
-  *)
-    >&2 \echo "jive is not compatible with your shell (${__shell})"
-    \return 1
-    ;;
-esac
-
-__mtime_of_jive_script="$(\date -r "${__jive_source_dir}/jive.sh" +%s)"
-__jive_auto_reload() {
-  local current_mtime
-  current_mtime="$(\date -r "${__jive_source_dir}/jive.sh" +%s)"
-
-  if [[ "${current_mtime}" != "${__mtime_of_jive_script}" ]]; then
-    echo "Reloading... jive.sh"
-    . "${__jive_source_dir}/jive.sh"
-  fi
-}
-
-jive() {
-  __jive_auto_reload
-  __jive_exec "$@"
-}
-
-__jive_exec() {
-  echo "$@"
-}
lib/jive/cli.rb
@@ -2,7 +2,7 @@
 
 require "thor"
 require "jive"
-require 'pathname'
+require "pathname"
 
 module Jive
   module Cli
@@ -11,29 +11,31 @@ module Jive
         true
       end
 
-      desc "clone <org>/<project>", "Clones the project from GitHub to ~/src/github.com/<org>/<project>"
+      desc "clone <org>/<project>", "git clone to ~/src/github.com/<org>/<project>"
       def clone(slug)
-        org, project = slug.split('/')
         target_dir = Pathname.new(Dir.home).join("src/github.com/#{slug}")
         run_each([
           [:mkdir, "-p", target_dir.parent.to_s],
-          [:git, "clone", "git@github.com:#{slug}.git", target_dir],
-          [:cd, target_dir],
+          [:git, "clone", "git@github.com:#{slug}.git", target_dir]
+        ])
+        after_run([
+          ["cd", target_dir],
+          ["setenv", "JIVE_LAST_RUN=#{Time.now.to_i}"]
         ])
       end
 
       private
 
       COMMAND_MAP = {
-        cd: '/usr/bin/cd',
-        echo: '/usr/bin/echo',
-        git: '/usr/bin/git',
-        mkdir: '/usr/bin/mkdir',
+        cd: "/usr/bin/cd",
+        echo: "/usr/bin/echo",
+        git: "/usr/bin/git",
+        mkdir: "/usr/bin/mkdir"
       }.freeze
 
       def run_each(tasks)
         tasks.each do |task|
-          return unless execute(task)
+          break unless execute(task)
         end
       end
 
@@ -41,13 +43,17 @@ module Jive
         system(env, expand(command))
       end
 
-      private
+      def after_run(tasks)
+        finalizer_fd = 9
+        pipe = IO.new(finalizer_fd)
+        pipe.puts(tasks.map { |x| x.join(":") }.join("\n"))
+      end
 
       def expand(command)
         Array(command)
           .flatten
           .map { |x| COMMAND_MAP.fetch(x, x).to_s }
-          .join(' ')
+          .join(" ")
       end
     end
   end
.rubocop.yml
@@ -1,3 +1,7 @@
+require:
+  - rubocop-minitest
+  - rubocop-rake
+
 AllCops:
   NewCops: enable
   TargetRubyVersion: 2.5
@@ -5,6 +9,12 @@ AllCops:
 Layout/LineLength:
   Max: 120
 
+Layout/EndOfLine:
+  EnforcedStyle: lf
+
+Layout/FirstArrayElementIndentation:
+  EnforcedStyle: consistent
+
 Metrics/AbcSize:
   Exclude:
     - lib/jive/batch_runner.rb
jive.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+# shellcheck disable=1001,1012,2039,1090
+# 1001,1012 stop complaints about '\awk' syntax to bypass aliases.
+# 2039 stops complaints about array references not being POSIX.
+# 1090 stops complaints about sourcing non-constant paths.
+
+__shell="$(\ps -p $$ | \awk 'NR > 1 { sub(/^-/, "", $4); print $4 }')"
+__shellname="$(basename "${__shell}")"
+
+case "${__shellname}" in
+  bash)
+    __jive_root_dir="$(builtin cd "$(\dirname "${BASH_SOURCE[0]}")" && \pwd)"
+    ;;
+  zsh)
+    __jive_root_dir="$(\dirname "$0:A")"
+    ;;
+  *)
+    >&2 \echo "jive is not compatible with your shell (${__shell})"
+    \return 1
+    ;;
+esac
+
+__jive_exe_dir="${__jive_root_dir}/exe"
+__jive_lib_dir="${__jive_root_dir}/lib"
+__jive_script="${__jive_root_dir}/jive.sh"
+
+__mtime_of_jive_script="$(\date -r "${__jive_script}" +%s)"
+__jive_auto_reload() {
+  local current_mtime
+  current_mtime="$(\date -r "${__jive_script}" +%s)"
+
+  if [[ "${current_mtime}" != "${__mtime_of_jive_script}" ]]; then
+    echo "Reloading... ${__jive_script}"
+    . "${__jive_script}"
+  fi
+}
+
+jive() {
+  __jive_auto_reload
+  __jive_open_pipe
+  __jive_exec "$@"
+  __jive_read_and_close_pipe
+}
+
+__jive_exec() {
+  /usr/bin/env -S ruby -I "${__jive_lib_dir}" "${__jive_exe_dir}/jive" "$@"
+}
+
+__jive_open_pipe() {
+  local tmpfile
+  tmpfile="$(\mktemp -u)"
+
+  exec 9>"${tmpfile}" # Open the tempfile for writing on FD 9.
+  exec 8<"${tmpfile}" # Open the tempfile for reading on FD 8.
+  \rm -f "${tmpfile}" # Unlink the tempfile. (we've already opened it).
+}
+
+__jive_read_and_close_pipe() {
+  local tasks
+  tasks=()
+
+  local task
+  while \read -r task; do
+    tasks+=("${task}")
+  done <&8
+
+  __jive_close_pipe
+
+  for task in "${tasks[@]}"; do
+    case "${task}" in
+      cd:*)
+        # shellcheck disable=SC2164
+        cd "${task//cd:/}"
+        ;;
+      setenv:*)
+        export "${task//setenv:/}"
+        ;;
+      *)
+        echo "wtf is ${task} ?"
+        ;;
+    esac
+  done
+}
+
+__jive_close_pipe() {
+  exec 8<&- # close FD 8.
+  exec 9<&- # close FD 9.
+}
+