-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposer-binaries
More file actions
executable file
·139 lines (119 loc) · 3.65 KB
/
composer-binaries
File metadata and controls
executable file
·139 lines (119 loc) · 3.65 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
#!/usr/bin/env php
<?php
/**
* Searches the local directory for all executable files (other than itself)
* and updates the local composer.json file's config.bin array.
*/
/**
* Prints usage information and exits.
*
* @param string $script Typically set to basename($argv[0]) when the method is called.
* @param int $exitCode The numeric exit code to return. 0 = success. >0 = failure.
* @return void
*/
function usage($script = null, $exitCode = 0) {
$script = $script ?: basename(__FILE__);
$usage = <<<EOD
{$script}
A meta script for working with the CakePHP-Shell-Scripts repo itself.
Searches the directory this script lives in for other executable files.
Generates a list of all _other_ executable files (exculding itself) in
the same directory of this script and replaces the "bin" configuration
in the local composer.json with a properly formatted list using the
found file names.
In short, this script updates the composer.json file with a list of
executable scripts that should be symlinked into the "bin-dir" of
projects that require this package.
Usage:
./{$script}
EOD;
echo $usage;
exit($exitCode);
}
/**
* Extends the Directory iterator class to filter out the current script and
* any non-executables from the given directory.
*/
class ExeIterator extends FilterIterator {
/**
* Stores the name of the current script to strip out from output.
*
* @var string
*/
private $selfName = null;
/**
* Created a filtered iterator with an internal Directory iterator.
*
* @param string $path Path to the folder to scan for executables.
* @param string $selfName The basename() of the currently executing script (to exclude from the results).
* @return void
*/
public function __construct($path, $selfName) {
parent::__construct(new DirectoryIterator($path));
$this->selfName = $selfName;
}
/**
* Returns true for executable files in the current directory. Returns
* false for dot directories (. and ..), the currently executing script,
* and anything that is not set as executable.
*
* @return bool True if current file is executable and not ourself, false otherwise.
*/
public function accept() {
$f = parent::current();
return (
!$f->isDot()
&& !$f->isDir()
&& $f->isExecutable()
&& ($f->getBasename() !== $this->selfName)
);
}
/**
* Returns the basename for the current file, instead of the entire
* SplFileInfo object.
*
* @return string The basename() for the current file.
*/
public function current() {
$f = parent::current();
return $f->getBasename();
}
}
/**
* Takes a variable number of string arguments and checks to make sure they
* are available JSON_* constants (based on PHP version.) Returns the bitwise
* OR of all available options, suitable for passing as the second argument
* to `json_encode()`.
*
* @return int The bitwise OR-ing of all valid and available JSON_* options.
*/
function json_opts() {
$bitwise = 0;
foreach (func_get_args() as $arg) {
if (defined($arg)) {
$bitwise |= constant($arg);
}
}
return $bitwise;
}
/**
* main() ===================================================================
*/
$options = getopt('h', []);
$dir = dirname(__FILE__);
$composerFile = $dir . '/composer.json';
$selfName = basename($argv[0]);
if (isset($options['h'])) {
usage($selfName);
}
$executables = array_values(iterator_to_array(new ExeIterator($dir, $selfName)));
$composerJson = json_decode(file_get_contents($composerFile), $assoc = false);
$composerJson->bin = $executables;
file_put_contents(
$composerFile,
json_encode(
$composerJson,
json_opts('JSON_UNESCAPED_SLASHES', 'JSON_PRETTY_PRINT')
)
);
echo "{$composerFile} updated." . PHP_EOL . PHP_EOL;