-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaydn.php
182 lines (165 loc) · 5.21 KB
/
haydn.php
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env php
<?php
/**
* This little app is designed to selectively override the settings in a
* composer.json file with ones from an accompanying haydn.json file. It then
* runs composer against the (temporary) result, allowing all command-line
* arguments to pass through.
*
* It overrides package-versions and repositories, so that a developer on a
* local machine can easily develop against local branches in local git
* repositories.
*
* Note: This has not been tested or developed for Windows systems.
* @author Darien Hager
*/
define("SRC_CONF", "composer.json");
define("MOD_CONF", "haydn.json");
define("SAME_BRANCH", "@samebranch");
function loadJson($path){
if(!is_readable($path)){
throw new Exception("File $path cannot be read",1);
}
$str = file_get_contents($path);
try{
$obj = decodeJson($str);
}catch(Exception $e){
$msg = jsonErrorMessage($e->getCode());
throw new Exception("JSON error in $path: $msg",2,$e);
}
return $obj;
}
function decodeJson($str){
$in_obj = @json_decode($str);
if($in_obj === null){
$lastErr = json_last_error();
if($lastErr !== JSON_ERROR_NONE){
throw new Exception("JSON error", $lastErr);
}
}
return $in_obj;
}
function jsonErrorMessage($code){
// For now lets be lazy and just expose the PHP constant name
$constants = array(
'JSON_ERROR_DEPTH',
'JSON_ERROR_STATE_MISMATCH',
'JSON_ERROR_CTRL_CHAR',
'JSON_ERROR_SYNTAX',
'JSON_ERROR_UTF8',
'JSON_ERROR_RECURSION',
'JSON_ERROR_INF_OR_NAN',
'JSON_ERROR_UNSUPPORTED_TYPE'
);
foreach($constants as $cname){
$val = constant($cname);
if($val !== null && $val == $code){
return $cname;
}
}
return "UNKNOWN";
}
function getCurrentBranch(){
$output = array();
exec('git symbolic-ref --short HEAD',$output);
return trim(join(" ",$output));
}
/**
* @param object $base Decoded composer.json
* @param object $modConf Decoded haydn.json
*/
function override($base,$modConf){
$currBranch = getCurrentBranch();
/**
* Add/replace packages with alternate versions, unless the version is blank in
* which case the package is removed.
*/
if(isset($modConf->{'override-require'}) && isset($base->{'require'})){
foreach($modConf->{'override-require'} as $packageName => $version){
if($version == SAME_BRANCH){
$version = "dev-$currBranch";
}
if($version != ""){
$base->{'require'}->{$packageName} = $version;
}else{
unset($base->{'require'}->{$packageName});
}
}
}
if(isset($modConf->{'override-require-dev'}) && isset($base->{'require-dev'})){
foreach($modConf->{'override-require-dev'} as $packageName => $version){
if($version != ""){
$base->{'require-dev'}->{$packageName} = $version;
}else{
unset($base->{'require-dev'}->{$packageName});
}
}
}
/**
* Replace repositories with the same URL, otherwise add new ones
*/
if(isset($modConf->{'override-repositories'}) && isset($base->{'repositories'})){
foreach($modConf->{'override-repositories'} as $url => $newRepo){
$replaced = false;
foreach($base->{'repositories'} as $index => $repo){
if(!isset($repo->url)){
continue;
}
if($repo->url == $url){
$base->{'repositories'}[$index] = $newRepo;
$replaced = true;
break;
}
}
if(!$replaced){
// Add instead
$base->{'repositories'}[] = $newRepo;
}
}
}
}
function writeTemp($conf){
$tempStr = json_encode($conf,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$tempStr = str_replace('"_empty_"','""',$tempStr); // Shitty workaround for issue #2
$tempPath = tempnam(sys_get_temp_dir(),"haydn_");
file_put_contents($tempPath,$tempStr);
return $tempPath;
}
function launchComposer($newConf,$argv){
/*
* We assume that the `which` command will work.
*/
$commandAlts = array(
"composer",
"composer.phar",
);
$cmd = null;
foreach($commandAlts as $alt){
if(commandExists($alt)){
$cmd = $alt;
}
}
if($cmd === null){
throw new Exception("Unable to determine composer command, tried [".join(", ",$commandAlts)."]",3);
}
array_shift($argv); // Remove haydn.php command
putenv("COMPOSER=$newConf");
passthru("$cmd ". join(" ",$argv), $retVal);
return $retVal;
}
function commandExists($cmd){
exec("which $cmd",$output,$retVal);
return $retVal === 0;
}
////////////////////////////////////////////////////////////////////////////////
try{
$conf = loadJson(SRC_CONF);
$modifier = loadJson(MOD_CONF);
override($conf, $modifier);
$tempPath = writeTemp($conf);
$retVal = launchComposer($tempPath,$argv);
exit($retVal);
}catch(Exception $e){
fwrite(STDERR,$e->getMessage()."\n");
exit(100 + $e->getCode());
}