nitc :: AAnnotation :: as_version
version(1, 5, git_revision)
.The user can enter as many fields as needed. The call to git_revision
will be replaced by the short
revision number. If the working tree is dirty, it will append another field with "d" for dirty.
# Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
#
# The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
# revision number. If the working tree is dirty, it will append another field with "d" for dirty.
private fun as_version(modelbuilder: ModelBuilder): String
do
var version_fields = new Array[Object]
var args = n_args
if args.length < 1 then
modelbuilder.error(self, "Syntax Error: `{name}` expects at least one argument.")
return ""
else
for arg in args do
var value
value = arg.as_int
if value != null then
version_fields.add value
continue
end
value = arg.as_string
if value != null then
version_fields.add value
end
value = arg.as_id
if value == "git_revision" then
# Get Git short revision
var proc = new ProcessReader("git", "rev-parse", "--short", "HEAD")
proc.wait
if proc.status != 0 then
# Fallback if this is not a git repository or git bins are missing
version_fields.add "0"
modelbuilder.warning(self, "git_revision", "Warning: `git_revision` used outside of a git repository or git binaries not available")
continue
end
var lines = proc.read_all
var revision = lines.split("\n").first
# Is it dirty?
# If not, the return of `git diff --shortstat` is an empty line
proc = new ProcessReader("git", "diff-index", "--quiet", "HEAD")
proc.wait
var dirty = proc.status != 0
if dirty then revision += ".d"
version_fields.add revision
continue
end
var format_error = "Syntax Error: `{name}` expects its arguments to be of type Int or a call to `git_revision`."
modelbuilder.error(self, format_error)
return ""
end
end
return version_fields.join(".")
end
src/platform/app_annotations.nit:83,2--142,4