#!/usr/bin/env php $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); $replacement['test_name'] = fgetsin($tip, false); $tip = swoole_color("[Test intro]: ", SWOOLE_COLOR_BLUE); $replacement['test_intro'] = fgetsin($tip); if (empty($path['filename'])) { $path['filename'] = $replacement['test_name']; // use test name to be filename } $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.phpt'); $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!"); }