about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-09-03 09:27:40 +0000
committerEric Wong <mwrap-public@80x24.org>2022-09-03 09:53:12 +0000
commit3d210d3fffafca348945500a53fba1d3a2072327 (patch)
treefdd4707276cd9c6568dfc77f00c92220564bff54
parent7524237b2de98f7e407ea227fb6ca6904153c0a5 (diff)
downloadmwrap-3d210d3fffafca348945500a53fba1d3a2072327.tar.gz
These may make things easier for new users, and we'll also with
help text if given no args.

We'll programmatically generate version based on `git describe',
but fallback to a hardcoded version if outside of git.  We'll
also start appending `-dirty' to the version string to match
git.git conventions.
-rw-r--r--MANIFEST3
-rwxr-xr-xVERSION-GEN36
-rwxr-xr-xbin/mwrap19
-rw-r--r--lib/mwrap/.gitignore1
-rw-r--r--mwrap.gemspec9
5 files changed, 65 insertions, 3 deletions
diff --git a/MANIFEST b/MANIFEST
index e6d8964..e8ace8b 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -5,10 +5,13 @@ COPYING
 MANIFEST
 README
 Rakefile
+VERSION-GEN
 bin/mwrap
 ext/mwrap/extconf.rb
 ext/mwrap/jhash.h
 ext/mwrap/mwrap.c
+lib/mwrap/.gitignore
 lib/mwrap_rack.rb
 mwrap.gemspec
 test/test_mwrap.rb
+lib/mwrap/version.rb
diff --git a/VERSION-GEN b/VERSION-GEN
new file mode 100755
index 0000000..161a04f
--- /dev/null
+++ b/VERSION-GEN
@@ -0,0 +1,36 @@
+#!/bin/sh
+VF=lib/mwrap/version.rb
+DEF_VER=v2.2.0
+VN=$(git describe HEAD 2>/dev/null)
+if test $? -eq 0
+then
+        case "$VN" in
+        v[0-9]*)
+                set -e
+                git update-index -q --refresh
+                set +e
+                git diff-index --quiet HEAD -- || VN="$VN-dirty"
+                set -e
+                VN=$(echo $VN | tr '-' '.')
+                ;;
+        esac
+fi
+set -e
+
+case $VN in
+'') VN="$DEF_VER" ;;
+esac
+
+VN=$(expr "$VN" : v*'\(.*\)')
+VC=unset
+if test -r $VF
+then
+        VC="$(cat $VF)"
+fi
+
+new="module Mwrap; VERSION = '$VN'.freeze; end"
+if test x"$new" != x"$VC"
+then
+        echo "$new" >$VF
+fi
+echo $VN
diff --git a/bin/mwrap b/bin/mwrap
index 212078c..054b3a3 100755
--- a/bin/mwrap
+++ b/bin/mwrap
@@ -2,6 +2,25 @@
 # frozen_string_literal: true
 # Copyright (C) mwrap hackers <mwrap-public@80x24.org>
 # License: GPL-2.0+ <https://www.gnu.org/licenses/gpl-2.0.txt>
+help = <<EOM
+usage: mwrap COMMAND [ARGS]
+see https://80x24.org/mwrap/README.html for more info
+EOM
+ARGV.empty? and abort help
+ARGV.each do |x|
+  case x
+  when '--version', '-v'
+    require 'mwrap/version'
+    puts "mwrap #{Mwrap::VERSION} - #{RUBY_DESCRIPTION}"
+    exit 0
+  when '--help', '-h'
+    puts help
+    exit 0
+  else # don't intercept --version/--help intended for commands we wrap
+    break
+  end
+end
+
 require 'mwrap'
 mwrap_so = $".grep(%r{/mwrap\.so\z})[0] or abort "mwrap.so not loaded"
 cur = ENV['LD_PRELOAD']
diff --git a/lib/mwrap/.gitignore b/lib/mwrap/.gitignore
new file mode 100644
index 0000000..07c0394
--- /dev/null
+++ b/lib/mwrap/.gitignore
@@ -0,0 +1 @@
+version.rb
diff --git a/mwrap.gemspec b/mwrap.gemspec
index 48a32b2..cb541e3 100644
--- a/mwrap.gemspec
+++ b/mwrap.gemspec
@@ -1,18 +1,21 @@
 git_manifest = `git ls-files 2>/dev/null`.split("\n")
+git_ok = $?.success?
+git_manifest << 'lib/mwrap/version.rb'.freeze # generated by ./VERSION-GEN
 manifest = File.exist?('MANIFEST') ?
   File.readlines('MANIFEST').map!(&:chomp).delete_if(&:empty?) : git_manifest
-if git_manifest[0] && manifest != git_manifest
+if git_ok && manifest != git_manifest
   tmp = "MANIFEST.#$$.tmp"
   File.open(tmp, 'w') { |fp| fp.puts(git_manifest.join("\n")) }
   File.rename(tmp, 'MANIFEST')
   system('git add MANIFEST')
 end
 
-desc = `git describe --abbrev=4 HEAD`.strip.tr('-', '.').delete_prefix('v')
+version = `./VERSION-GEN`.chomp
+$?.success? or abort './VERSION-GEN failed'
 
 Gem::Specification.new do |s|
   s.name = 'mwrap'
-  s.version = desc.empty? ? '2.2.0' : desc
+  s.version = version
   s.homepage = 'https://80x24.org/mwrap/'
   s.authors = ["mwrap hackers"]
   s.summary = 'LD_PRELOAD malloc wrapper for Ruby'