forked from bovigo/vfsStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release
executable file
·163 lines (149 loc) · 3.97 KB
/
release
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
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env php
<?php
echo "Composer package release script\n";
echo " (c) 2012 Frank Kleine\n";
echo " This software is Drinkware. If you happen to meet the author spend him a drink.\n\n";
$pwd = getcwd();
if (false === $pwd) {
error("Can't retrieve current directory - please check file permissions.");
exit(1);
}
$output = execute('git status 2> /dev/null | tail -n1', 'Failure while checking git status', 2);
if ('nothing to commit (working directory clean)' !== $output[0]) {
error("Can't create release, working directory not clean.\n");
passthru('git status');
exit(3);
}
if (!file_exists($pwd . DIRECTORY_SEPARATOR . 'composer.json')) {
error("No composer.json found - are you sure this is a composer package?");
exit(4);
}
$composerData = new ComposerData($pwd . DIRECTORY_SEPARATOR . 'composer.json');
if (!$composerData->hasVersion()) {
error("No version found in composer.json. Can't create release without version.");
exit(5);
}
echo "Current package version: " . $composerData->getVersion() . "\n";
echo "Please name the version to release (empty to keep current version): ";
$newVersion = trim(rtrim(fgets(STDIN), "\r\n"));
if (empty($newVersion)) {
$newVersion = $composerData->getVersion();
echo "Using current package version " . $newVersion . " for release\n";
} else {
echo "Updating package version in composer.json to " . $newVersion . "\n";
$composerData->updateVersion($newVersion);
passthrough('git add composer.json', 6);
passthrough('git commit -m "raise version for release"', 7);
passthrough('git push origin master', 8);
}
passthrough('git tag -a v' . $newVersion . ' -m "tag release ' . $newVersion . '"', 9);
passthrough('git push --tags', 10);
echo "Successfully created release v" . $newVersion . "\n";
exit(0);
class ComposerData
{
/**
* path to composer file
*
* @type
*/
private $composerFile;
/**
* raw composer json string
*
* @type string
*/
private $json;
/**
* package data
*
* @type stdClass
*/
private $data;
/**
* constructor
*
* @param string $composerFile path to composer file
*/
public function __construct($composerFile)
{
$this->composerFile = $composerFile;
$this->json = file_get_contents($this->composerFile);
$this->data = json_decode($this->json);
}
/**
* checks if package has a version number
*
* @return bool
*/
public function hasVersion()
{
return isset($this->data->version);
}
/**
* returns package version
*
* @return string
*/
public function getVersion()
{
return trim($this->data->version);
}
/**
* updates package version
*
* @param string $newVersion
*/
public function updateVersion($newVersion)
{
file_put_contents($this->composerFile, str_replace($this->data->version, $newVersion, $this->json));
}
}
/**
* calls passthru() and exits with given exit code if return code is not 0
*
* @param string $command
* @param int $exitCode
*/
function passthrough($command, $exitCode)
{
$return = 0;
passthru($command, $return);
if (0 !== $return) {
exit($exitCode);
}
}
/**
* execute given command
*
* If the command fails it prints the given error message and calls exit() with
* the given exit code.
*
* Returns the output of the command if successful.
*
* @param string $command
* @param string $errorMsg
* @param int $exitCode
* @return string[]
*/
function execute($command, $errorMsg, $exitCode)
{
$output = array();
$return = 0;
exec($command, $output, $return);
if (0 !== $return) {
error($errorMsg . ": " . join("\n", $output));
exit($exitCode);
}
return $output;
}
/**
* prints out given error message
*
* @param string $message
*/
function error($message)
{
echo 'ERROR: ' . $message . "\n";
}
?>