mwrap user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-public@80x24.org
Cc: Sam Saffron <sam.saffron@gmail.com>
Subject: [PATCH 2/3] add --version and --help args
Date: Sat,  3 Sep 2022 09:27:40 +0000	[thread overview]
Message-ID: <20220903092741.2284559-3-e@80x24.org> (raw)
In-Reply-To: <20220903092741.2284559-1-e@80x24.org>

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.
---
 MANIFEST             |  3 +++
 VERSION-GEN          | 36 ++++++++++++++++++++++++++++++++++++
 bin/mwrap            | 19 +++++++++++++++++++
 lib/mwrap/.gitignore |  1 +
 mwrap.gemspec        |  9 ++++++---
 5 files changed, 65 insertions(+), 3 deletions(-)
 create mode 100755 VERSION-GEN
 create mode 100644 lib/mwrap/.gitignore

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'

  parent reply	other threads:[~2022-09-03  9:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-03  9:27 [PATCH 0/3] mwrap odds and ends before 2.3 Eric Wong
2022-09-03  9:27 ` [PATCH 1/3] extconf.rb: avoid RDoc errors during gem install Eric Wong
2022-09-03  9:27 ` Eric Wong [this message]
2022-09-03  9:27 ` [PATCH 3/3] cleanup some FreeBSD-related workarounds Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://80x24.org/mwrap/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220903092741.2284559-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=mwrap-public@80x24.org \
    --cc=sam.saffron@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).