tools: generate version number and give it to tools
authorJean Privat <jean@pryen.org>
Fri, 26 Jun 2009 04:55:52 +0000 (00:55 -0400)
committerJean Privat <jean@pryen.org>
Fri, 26 Jun 2009 05:06:51 +0000 (01:06 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

.gitignore
Makefile
src/abstracttool.nit
src/git-gen-version.sh [new file with mode: 0755]
src/mmloader.nit
src/nc

index 8b5a929..50ed558 100644 (file)
@@ -13,6 +13,7 @@ src/parser/.nit.sablecc3
 src/parser/.nit.sablecc3.dump
 src/parser/parser_abs.patch
 src/parser/parser_nodes.nit.rej
+src/nit_version.nit
 
 c_src/*.o
 c_src/*.cksum
index 6521d64..bb36721 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,12 +24,14 @@ bin/nitc: c_src/nitc src/parser/parser.nit
        @echo '***************************************************************'
        mkdir .nit_compile 2> /dev/null || true
        cp c_src/* .nit_compile
+       src/git-gen-version.sh
        c_src/nitc ${NITCOPT} -o bin/nitc -O src/nitc.nit
 
 bin/nitdoc: bin/nitc
        @echo '***************************************************************'
        @echo '* Compile nitdoc from NIT source files                        *'
        @echo '***************************************************************'
+       src/git-gen-version.sh
        bin/nitc ${NITCOPT} -o bin/nitdoc -O src/nitdoc.nit
 
 doc/stdlib/index.html: bin/nitdoc
index 756117a..b03c827 100644 (file)
@@ -21,6 +21,7 @@ package abstracttool
 
 import mmloader
 import syntax
+import nit_version
 
 class AbstractCompiler
 special ToolContext
@@ -40,6 +41,11 @@ special ToolContext
        do
                process_options
 
+               if opt_version.value then
+                       print "{tool_name} version {nit_version}"
+                       exit(0)
+               end
+
                if opt_help.value then
                        print "usage: {tool_name} [options] file..."
                        option_context.usage
diff --git a/src/git-gen-version.sh b/src/git-gen-version.sh
new file mode 100755 (executable)
index 0000000..601b2b2
--- /dev/null
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This program is used to generate version number from git refs.
+# The version number is stored in a dedicated Nit module.
+
+gen_version() {
+cat > nit_version.nit<<END
+# This file was generated by git-gen-version.sh
+package nit_version
+meth nit_version: String do return "$*"
+END
+echo "Version $*"
+}
+
+# Are we in the nitc.nit directory?
+if [ ! -f nitc.nit ]; then
+       if [ -f src/nitc.nit ]; then
+               cd src
+       else
+               echo "Error: no nitc.nit found." >&2
+               exit 1
+       fi
+fi
+
+VN=$(git describe --tags --always HEAD)
+if [ -z "$VN" ]; then
+       VN="undefined"
+fi
+if [ -n "$1" ]; then
+       VN="${VN}_$1"
+fi
+if [ -n "$(git diff HEAD)" ]; then
+       VN="${VN}_dirty"
+fi
+
+gen_version "$VN"
index 13adce2..6b44267 100644 (file)
@@ -76,10 +76,13 @@ special MMContext
        # Option --help
        readable attr _opt_help: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
 
+       # Option --version
+       readable attr _opt_version: OptionBool = new OptionBool("Show version and exit", "--version")
+
        init
        do
                super
-               option_context.add_option(opt_warn, opt_path, opt_log, opt_only_parse, opt_only_metamodel, opt_help)
+               option_context.add_option(opt_warn, opt_path, opt_log, opt_only_parse, opt_only_metamodel, opt_help, opt_version)
        end
 
        # Parse and process the options given on the command line
diff --git a/src/nc b/src/nc
index df75d19..8060070 100755 (executable)
--- a/src/nc
+++ b/src/nc
@@ -29,6 +29,8 @@ case $name in
        *) echo "unknown mode $name"; exit 1;;
 esac
 
+./git-gen-version.sh "$name"
+
 echo ${NITC} ${NITCOPTS} ${opts} "$@"
 ${NITC} ${NITCOPTS} ${opts} "$@"