-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathversion.php
More file actions
87 lines (77 loc) · 3.26 KB
/
version.php
File metadata and controls
87 lines (77 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Package version data for OpenEMR modules
*
* This file automatically determines version from git tags or uses the default version.
* Update the default version values when creating a new module from this template.
*
* @package OpenCoreEMR
* @link http://www.open-emr.org
* @author Your Name <your.email@opencoreemr.com>
* @copyright Copyright (c) 2026 OpenCoreEMR Inc
* @license GNU General Public License 3
*/
// Calculate and unpack version information into global variables that OpenEMR expects
[$v_major, $v_minor, $v_patch, $v_tag, $v_database] = (function (
string $default_major,
string $default_minor,
string $default_patch
): array {
/**
* Execute a git command in the module directory
*
* @param array<string> $gitArgs Git command arguments as array
* @return string Command output trimmed, or empty string on failure
*/
$executeGitCommand = function (array $gitArgs): string {
$output = [];
$return_code = 0;
// Build command with properly escaped arguments
$escapedArgs = array_map('escapeshellarg', $gitArgs);
$command = sprintf(
'git -C %s %s 2>/dev/null',
escapeshellarg(__DIR__),
implode(' ', $escapedArgs)
);
exec($command, $output, $return_code);
// Return empty string if git command failed
return $return_code === 0 ? trim(implode("\n", $output)) : '';
};
$git_dir = __DIR__ . '/.git';
if (is_dir($git_dir) || is_file($git_dir)) {
// We're in a git repository - use git describe for version
$git_describe = $executeGitCommand(['describe', '--tags', '--always', '--dirty']);
if (!empty($git_describe)) {
// Parse git describe output (e.g., "v1.0.0-5-gabc1234-dirty" or "abc1234-dirty")
// Format: [tag]-[commits since tag]-g[short hash][-dirty]
if (preg_match('/^v?(\d+)\.(\d+)\.(\d+)/', $git_describe, $matches)) {
// Has a version tag - use it via array destructuring (skip index 0)
[, $v_major, $v_minor, $v_patch] = $matches;
// Add commit count and hash if there are commits after the tag
if (preg_match('/-(\d+)-g([0-9a-f]+)(-dirty)?$/', $git_describe, $extra)) {
// Unpack with default for optional dirty flag
[, $commits, $hash, $dirty] = $extra + [3 => null];
$v_patch .= '-dev+' . $hash;
if ($dirty !== null) {
$v_patch .= '.dirty';
}
} elseif (str_ends_with($git_describe, '-dirty')) {
$v_patch .= '-dirty';
}
} else {
// No version tag - use commit hash
$branch = $executeGitCommand(['rev-parse', '--abbrev-ref', 'HEAD']) ?: 'unknown';
$v_major = $branch;
$v_minor = $git_describe;
$v_patch = '';
}
return [$v_major, $v_minor, $v_patch, '', 1];
}
}
// Not in a git repository or git command failed - use default version
return [$default_major, $default_minor, $default_patch, '', 1];
})(
default_major: '1',
default_minor: '0',
default_patch: '0'
);