-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathnew
executable file
·114 lines (103 loc) · 3.36 KB
/
new
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
#!/usr/bin/env php
<?php
define('SWOOLE_COLOR_RED', 1);
define('SWOOLE_COLOR_GREEN', 2);
define('SWOOLE_COLOR_YELLOW', 3);
define('SWOOLE_COLOR_BLUE', 4);
define('SWOOLE_COLOR_MAGENTA', 5);
define('SWOOLE_COLOR_CYAN', 6);
define('SWOOLE_COLOR_WHITE', 7);
function swoole_color(string $content, int $color): string
{
return "\033[3{$color}m{$content}\033[0m";
}
function fgetsin(string $tip = '', bool $accept_empty = true): string
{
do {
if ($tip) {
echo "$tip: ";
}
$in = trim(fgets(STDIN));
} while (!$accept_empty && $in === '');
return $in;
}
function yes(string $content = ''): bool
{
echo $content;
return fgetsin() === 'y';
}
if (empty($argv[1])) {
error_filename:
exit(
swoole_color("Please enter the correct file name or path! e.g.:", SWOOLE_COLOR_RED) .
swoole_color("\n\"./new ./swoole_coroutine\"\n", SWOOLE_COLOR_MAGENTA)
);
}
$filename = $argv[1];
if (!pathinfo($filename, PATHINFO_EXTENSION)) {
$path = ['dirname' => $filename];
} else {
$path = pathinfo($filename);
}
$path['dirname'] = trim($path['dirname'], './'); // i know arg2 is a list but it's no problem
$replacement = [];
$tip = swoole_color("[Test name]: ", SWOOLE_COLOR_BLUE);
$path['filename'] = fgetsin($tip, false);// use test name to be filename
$tip = swoole_color("[Test intro]: ", SWOOLE_COLOR_BLUE);
$replacement['test_intro'] = fgetsin($tip, false);
$this_dir_name = explode('/', $path['dirname']);
$replacement['test_name'] = end($this_dir_name); // use dir name to be test name
$filename = "{$path['dirname']}/{$path['filename']}.phpt";
//if dir not exist, create it
if (!is_dir(__DIR__ . "/{$path['dirname']}")) {
echo swoole_color(
"The dir [{$path['dirname']}] is not exist, if you want to create it? [y/n]: ",
SWOOLE_COLOR_YELLOW
);
if (yes()) {
mkdir($path['dirname'], 0755, true);
} else {
exit(swoole_color('Can\'t generate the test file in nonexistent dir!', SWOOLE_COLOR_RED));
}
} elseif (file_exists($filename)) {
echo swoole_color(
"The file [{$path['filename']}] is exist, if you want to overwrite it? [y/n]: ",
SWOOLE_COLOR_YELLOW
);
if (!yes()) {
exit(swoole_color('You should rename your test filename.', SWOOLE_COLOR_RED));
}
}
//calc dir deep
$deep = 0;
$temp = $filename;
while (($temp = dirname($temp)) !== '.') {
$deep++;
}
if ($deep < 1) {
goto error_filename;
}
$template = file_get_contents(__DIR__ . '/template');
$replacement['dir_deep'] = str_repeat('/..', $deep);
foreach ($replacement as $key => $value) {
$template = str_replace("{{{$key}}}", $value, $template);
}
if (file_put_contents($filename, $template)) {
echo swoole_color("Generate the test file successfully!\n", SWOOLE_COLOR_GREEN) .
"[" . __DIR__ . "/$filename]";
@shell_exec('/usr/bin/env git add ' . __DIR__ . "/$filename");
if (\stripos(PHP_OS, 'Darwin') !== false) {
//MacOS
$pstorm = '/usr/local/bin/pstorm';
if (file_exists($pstorm) || (
file_exists('/Applications/PhpStorm.app') &&
file_put_contents($pstorm, file_get_contents(__DIR__ . '/include/macos/phpstorm.py')) &&
chmod($pstorm, 0744)
)
) {
@shell_exec("/usr/local/bin/phpstorm {$filename}");
}
}
} else {
exit("\nGenerate the test file failed!");
}