-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboFile.php
More file actions
153 lines (136 loc) · 4.62 KB
/
RoboFile.php
File metadata and controls
153 lines (136 loc) · 4.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
use Robo\Tasks;
use Symfony\Component\Finder\Finder;
class RoboFile extends \Robo\Tasks {
/**
* Bumps the version based on the specified level (major, minor, patch, dev, beta, rc).
*
* @param string $level The level to increment (major, minor, patch, dev, beta, rc). Default: patch
*/
public function bumpVersion( $level = 'patch' ) {
$versionFile = '.version';
$currentVersion = file_exists( $versionFile ) ? file_get_contents( $versionFile ) : '0.0.0';
$nextVersion = $this->incrementVersion( $currentVersion, $level );
file_put_contents( $versionFile, $nextVersion );
$phpFiles = $this->getPhpFilesWithPackage('project-donations-wc'); // Replace 'your-package-name' with the desired package value
$pattern = '\d+\.\d+\.\d+(-[A-Za-z]+(-[a-zA-Z0-9\.-]+)?)?';
$this->replaceInFiles( $phpFiles, '/@version\s+' . $pattern . '/', "@version $nextVersion" );
$this->replaceInFile( 'README.md', '/Version ' . $pattern . '/', "Version $nextVersion" );
$this->replaceInFile( 'README.md', '/Version\/' . $pattern . '\//', "Version/$nextVersion/" );
$packageJsonPath = 'package.json';
$packageJson = json_decode(file_get_contents($packageJsonPath), true);
$packageJson['version'] = $nextVersion;
file_put_contents($packageJsonPath, json_encode($packageJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->say( "Version bumped to: $nextVersion" );
}
/**
* Increments the version based on the specified level (major, minor, patch, dev, beta, rc).
*
* @param string $version The current version.
* @param string $level The level to increment (major, minor, patch, dev, beta, rc).
* @return string The incremented version.
*/
private function incrementVersion( $version, $level ) {
$parts = explode( '.', $version );
$major = (int) $parts[0];
$minor = (int) $parts[1];
$patch_parts = explode( '-', $parts[2] );
$patch = $patch_parts[0];
$cur_suffix = isset( $patch_parts[1] ) ? $patch_parts[1] : null;
$suffix_num = isset( $patch_parts[2] ) ? (int) $patch_parts[2] : 1;
$suffix = '';
switch ( $level ) {
case 'major':
$major++;
$minor = 0;
$patch = 0;
break;
case 'minor':
$minor++;
$patch = 0;
break;
case 'patch':
if ( ! in_array( $cur_suffix, array( 'dev', 'beta', 'rc' ) ) ) {
error_log( '$cur_suffix ' . $cur_suffix );
$patch++;
}
break;
case 'dev':
$suffix = "-$level";
if ( $cur_suffix === $level ) {
$suffix = "-$level-" . ( $suffix_num + 1 );
} else {
$patch++;
}
break;
case 'beta':
$suffix = "-$level";
if ( $cur_suffix === $level ) {
$suffix = "-$level-" . ( $suffix_num + 1 );
} elseif ( $cur_suffix !== 'dev' ) {
$patch++;
}
break;
case 'rc':
$suffix = "-$level";
if ( $cur_suffix === $level ) {
$suffix = "-$level-" . ( $suffix_num + 1 );
} elseif ( ! in_array( $cur_suffix, array( 'dev', 'beta' ) ) ) {
$patch++;
}
break;
default:
break;
}
return "$major.$minor.$patch$suffix";
}
/**
* Replaces the given pattern with the replacement string in the specified files.
*
* @param array $files The files to perform the replacement on.
* @param string $pattern The pattern to search for.
* @param string $replacement The replacement string.
*/
private function replaceInFiles( $files, $pattern, $replacement ) {
foreach ( $files as $file ) {
$contents = file_get_contents( $file );
$contents = preg_replace( $pattern, $replacement, $contents );
file_put_contents( $file, $contents );
}
}
/**
* Replaces the given pattern with the replacement string in the specified file.
*
* @param string $file The file to perform the replacement on.
* @param string $pattern The pattern to search for.
* @param string $replacement The replacement string.
*/
private function replaceInFile( $file, $pattern, $replacement ) {
$contents = file_get_contents( $file );
$contents = preg_replace( $pattern, $replacement, $contents );
file_put_contents( $file, $contents );
}
/**
* Returns an array of PHP file paths with the specified @package value in the docblocks.
*
* @param string $package The package value to match.
* @return array The PHP file paths.
*/
private function getPhpFilesWithPackage($package)
{
$finder = new Finder();
$finder
->files()
->in('./')
->name('*.php')
->exclude(['vendor', 'node_modules'])
->ignoreVCS(true)
->ignoreDotFiles(true)
->contains("@package $package"); // Add the condition to match @package value
$phpFiles = [];
foreach ($finder as $file) {
$phpFiles[] = $file->getRealPath();
}
return $phpFiles;
}
}