back to unicode
[MPC.git] / modules / Version.pm
bloba038748d26330e661d36215c667b65026e8221dd
1 package Version;
3 # ************************************************************
4 # Description : Central location for the MPC version.
5 # Author : Chad Elliott
6 # Create Date : 1/5/2003
7 # ************************************************************
9 # ************************************************************
10 # Pragmas
11 # ************************************************************
13 use strict;
14 use File::Spec;
16 # ************************************************************
17 # Data Section
18 # ************************************************************
20 ## This is the starting major and minor version
21 my $version = '5.1';
22 my $once = 1;
23 my $cache = 'modules/.version';
25 # ************************************************************
26 # Subroutine Section
27 # ************************************************************
29 sub get {
30 if ($once) {
31 ## We only need to do this once
32 $once = 0;
34 ## Attempt to dynamically determine the revision part of the version
35 ## string every time the version number is requested. This only happens
36 ## if the --version option is used, an invalid option is used, and when
37 ## the process starts up and the version hasn't been cached yet.
38 my $rev = '?';
39 my $cwd = Cwd::getcwd();
40 if (chdir(::getBasePath())) {
41 ## Get the git revision for the final part of the version string.
42 my $nul = File::Spec->devnull();
43 my $r = _readVersion("git rev-parse --short HEAD 2> $nul |");
44 if (defined $r) {
45 ## Store the version for later use, in the event that the git
46 ## revision isn't available in the future.
47 if (open(CLH, ">$cache")) {
48 print CLH "$r\n";
49 close(CLH);
52 else {
53 ## See if we can load in the previously stored version string.
54 $r = _readVersion($cache);
57 ## Set the revision string if we were able to read one.
58 $rev = $r if (defined $r);
60 chdir($cwd);
63 ## We then append the revision to the version string.
64 $version .= ".$rev";
67 return $version;
70 sub cache {
71 ## Attempt to cache the revision if the cache file does not exist.
72 ## This will allow the revision to be obtained in the event that git
73 ## cannot return the revision information at a later time.
74 get() if (!-e ::getBasePath() . '/' . $cache);
79 sub _readVersion {
80 my $file = shift;
81 my $rev;
82 if (open(CLH, $file)) {
83 while(<CLH>) {
84 if (/^(\w+)$/) {
85 $rev = $1;
86 last;
89 close(CLH);
91 return $rev;