Skip to content

Commit 6409650

Browse files
committed
Merge branch 'bump-script'
2 parents bea7b27 + 87f9a9e commit 6409650

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed

tools/bump-version.php

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
#!/usr/bin/php
2+
<?php
3+
/**
4+
* Helper script to increase Source Integration versions
5+
*
6+
* Command-line options:
7+
*
8+
* -v [version] Set Framework version (in Source/MantisSourceBase.class.php)
9+
* and generate a bump commit; if version is not specified,
10+
* or the framework is already at that version, the script will
11+
* just print the commit message.
12+
*
13+
* -t Create a signed tag for the new version (based on framework
14+
* version, or the version specified in -v option), listing all
15+
* included VCS-specific plugins names and versions.
16+
*/
17+
18+
// Path to MantisBT root, relative to the Source Integration Plugin's git root
19+
// Change this based on your dev environment's setting
20+
$g_mantis_root = '../../mantisbt';
21+
22+
23+
// ---------------------------------------------------------------------------
24+
// Main program
25+
//
26+
27+
// Process command-line options
28+
$t_options = getopt( 'v::th' );
29+
$t_bump_version = array_key_exists( 'v', $t_options );
30+
$t_create_tag = array_key_exists( 't', $t_options );
31+
if( array_key_exists( 'h', $t_options ) || !$t_bump_version && !$t_create_tag ) {
32+
print_help();
33+
exit(0);
34+
}
35+
36+
// Change to framework's root dir
37+
chdir( dirname( __DIR__ ) );
38+
39+
// Load plugins and get their version numbers
40+
foreach( new DirectoryIterator( getcwd() ) as $t_file ) {
41+
$t_name = $t_file->getFilename();
42+
if( $t_file->isDir() && strpos( $t_name, 'Source' ) === 0 ) {
43+
if( plugin_load_class( $t_name ) ) {
44+
$g_plugins[$t_name] = plugin_get_version( $t_name );
45+
} else {
46+
echo "ERROR: plugin '$t_name' could not be loaded";
47+
}
48+
}
49+
}
50+
ksort( $g_plugins );
51+
52+
$t_framework_version = array_shift( $g_plugins );
53+
54+
// Set version and create bump commit
55+
if( $t_bump_version ) {
56+
bump_version_and_commit( $t_options['v'], $t_framework_version );
57+
}
58+
59+
// Create Tag
60+
if( $t_create_tag ) {
61+
create_tag( $t_framework_version );
62+
}
63+
64+
65+
// ---------------------------------------------------------------------------
66+
// Helper functions
67+
//
68+
69+
/**
70+
* Prints command-line help
71+
*/
72+
function print_help() {
73+
echo basename( $argv[0] ) . "[-v version] [-t] [-h]\n\n", <<<EOF
74+
Helper script to increase Source Integration versions
75+
76+
-v [version] Set version if specified, and create bump commit
77+
If no version given, prints commit message
78+
-t Create signed tag
79+
-h Help
80+
81+
82+
EOF;
83+
}
84+
85+
/**
86+
* Load VCS plugin base
87+
* @param string $p_basename
88+
* @return bool
89+
*/
90+
function plugin_load_class( $p_basename ) {
91+
$t_path = $p_basename . '/' . $p_basename . '.php';
92+
93+
// Suppressing errors since we don't need to actually run the classes
94+
$t_result = @include_once $t_path;
95+
96+
if( !$t_result ) {
97+
echo "Failed to load $p_basename\n";
98+
return false;
99+
}
100+
return $t_result;
101+
}
102+
103+
/**
104+
* Returns the given plugin's version number
105+
* @param string $p_basename
106+
* @return string
107+
*/
108+
function plugin_get_version( $p_basename ) {
109+
$t_class = $p_basename . 'Plugin';
110+
return $t_class::PLUGIN_VERSION;
111+
}
112+
113+
/**
114+
* Set the Framework version in MantisSourceBase class and create bump commit.
115+
*
116+
* @param string $p_version
117+
* @param string $p_framework_version
118+
* @return void
119+
*/
120+
function bump_version_and_commit( $p_version, &$p_framework_version ) {
121+
global $g_plugins;
122+
$t_filename = 'Source/MantisSourceBase.class.php';
123+
124+
// Check if framework version needs to be updated
125+
if( $p_version ) {
126+
echo "New version '$p_version' specified; ";
127+
if( $p_version == $p_framework_version ) {
128+
echo "framework already at";
129+
$p_version = false;
130+
} else {
131+
echo "bump from";
132+
}
133+
} else {
134+
echo "Unspecified version bump; framework at";
135+
}
136+
echo " '$p_framework_version'\n";
137+
138+
// Update framework version
139+
if( $p_version ) {
140+
echo "Update version in $t_filename\n";
141+
exec( 'sed -r -i "s/(const FRAMEWORK_VERSION = \').*(\';)/\1'
142+
. $p_version . '\2/" ' . $t_filename
143+
);
144+
}
145+
146+
// Generate commit message
147+
$t_message = "Bump version to $p_version\n\n";
148+
$t_message .= "VCS plugins changes:\n";
149+
foreach( get_changed_plugins() as $t_plugin ) {
150+
$t_message .= "- $t_plugin " . $g_plugins[$t_plugin] . "\n";
151+
}
152+
153+
// Commit
154+
if( $p_version ) {
155+
echo "Committing version bump\n";
156+
exec( 'git add -- ' . $t_filename );
157+
exec( "git commit -m '$t_message' 2>&1", $t_output, $t_result );
158+
foreach( $t_output as $t_line ) {
159+
echo $t_line . "\n";
160+
}
161+
if( $t_result ) {
162+
echo "ERROR: Commit failed\n";
163+
exit( 1 );
164+
}
165+
$p_framework_version = $p_version;
166+
} else {
167+
echo "Commit message:\n";
168+
echo "------------------\n";
169+
echo $t_message . "\n";
170+
echo "------------------\n";
171+
}
172+
}
173+
174+
/**
175+
* Retrieves the list of changed plugins since previous tag
176+
* @return array
177+
*/
178+
function get_changed_plugins() {
179+
$t_previous_tag = exec( "git describe --abbrev=0" );
180+
181+
echo "Retrieving changed plugins since previous tag '$t_previous_tag'\n";
182+
183+
// List all changed VCS plugins (exclude Source framework)
184+
exec(
185+
"git diff --name-only $t_previous_tag | cut -d'/' -f1 | sort -u | grep '^Source.'",
186+
$t_changed_plugins
187+
);
188+
189+
return $t_changed_plugins;
190+
}
191+
192+
/**
193+
* Create a signed tag for the release
194+
* @param $p_framework_version
195+
* @return void
196+
*/
197+
function create_tag( $p_framework_version ) {
198+
global $g_plugins;
199+
200+
// Generate message
201+
$t_message = "Release $p_framework_version\n\n";
202+
$t_message .= "Includes the following VCS-specific plugins:\n";
203+
foreach( $g_plugins as $t_plugin => $t_version ) {
204+
$t_message .= "- $t_plugin $t_version\n";
205+
}
206+
207+
// Create Tag
208+
$t_tag = 'v' . $p_framework_version;
209+
exec( "git tag -s $t_tag -m '$t_message' 2>&1", $t_output, $t_result );
210+
if( $t_result ) {
211+
echo "ERROR: Tag creation failed\n";
212+
foreach( $t_output as $t_line ) {
213+
echo $t_line . "\n";
214+
}
215+
exit( 1 );
216+
}
217+
echo "Tag $t_tag created\n";
218+
}
219+
220+
/**
221+
* Return MantisBT root dir
222+
* @return string
223+
*/
224+
function get_mantis_root() {
225+
global $g_mantis_root;
226+
$g_mantis_root = rtrim( $g_mantis_root, '/' ) . '/';
227+
228+
if( file_exists( $g_mantis_root . 'core.php' ) ) {
229+
return $g_mantis_root;
230+
}
231+
232+
die( "ERROR: MantisBT not found in '$g_mantis_root'\n" );
233+
}
234+
235+
/**
236+
* Fake Mantis core functions.
237+
* - config_get()
238+
* - config_get_global()
239+
* - require_api()
240+
* - require_lib
241+
* These are required so that the Source Integration classes can be initialized
242+
* without actually loading the Mantis core.
243+
*/
244+
function config_get( $p_string ) {
245+
if( $p_string == 'plugin_path' ) {
246+
return getcwd() . '/';
247+
} else {
248+
return config_get_global( $p_string );
249+
}
250+
}
251+
252+
function config_get_global( $p_string ) {
253+
$t_root = get_mantis_root() . 'core/';
254+
255+
switch( $p_string ) {
256+
case 'core_path':
257+
return $t_root;
258+
case 'class_path':
259+
return $t_root . 'classes/';
260+
}
261+
return null;
262+
}
263+
264+
function require_api( $p_path ) {
265+
return;
266+
}
267+
268+
function require_lib( $p_path ) {
269+
return;
270+
}

0 commit comments

Comments
 (0)