You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.0 KiB
113 lines
3.0 KiB
12 years ago
|
#!/usr/bin/env php
|
||
12 years ago
|
<?php
|
||
|
$root = str_replace('\\', '/', __DIR__);
|
||
12 years ago
|
$envs = require("$root/environments/index.php");
|
||
12 years ago
|
$envNames = array_keys($envs);
|
||
|
|
||
12 years ago
|
echo "Yii Application Init Tool v1.0\n\n";
|
||
|
echo "Which environment do you want the application to be initialized in?\n\n";
|
||
12 years ago
|
foreach ($envNames as $i => $name) {
|
||
|
echo " [$i] $name\n";
|
||
|
}
|
||
|
echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
|
||
|
$answer = trim(fgets(STDIN));
|
||
|
if (!ctype_digit($answer) || !isset($envNames[$answer])) {
|
||
12 years ago
|
echo "\n Quit initialization.\n";
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
$env = $envs[$envNames[$answer]];
|
||
12 years ago
|
echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
|
||
12 years ago
|
$answer = trim(fgets(STDIN));
|
||
|
if (strncasecmp($answer, 'y', 1)) {
|
||
12 years ago
|
echo "\n Quit initialization.\n";
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
echo "\n Start initialization ...\n\n";
|
||
12 years ago
|
$files = getFileList("$root/environments/{$env['path']}");
|
||
12 years ago
|
$all = false;
|
||
|
foreach ($files as $file) {
|
||
12 years ago
|
if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all)) {
|
||
12 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (isset($env['writable'])) {
|
||
|
foreach ($env['writable'] as $writable) {
|
||
|
echo " chmod 0777 $writable\n";
|
||
|
@chmod("$root/$writable", 0777);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (isset($env['executable'])) {
|
||
|
foreach ($env['executable'] as $executable) {
|
||
|
echo " chmod 0755 $executable\n";
|
||
|
@chmod("$root/$executable", 0755);
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
echo "\n ... initialization completed.\n\n";
|
||
12 years ago
|
|
||
|
function getFileList($root, $basePath = '')
|
||
|
{
|
||
|
$files = array();
|
||
|
$handle = opendir($root);
|
||
|
while (($path = readdir($handle)) !== false) {
|
||
|
if ($path === '.svn' || $path === '.' || $path === '..') {
|
||
|
continue;
|
||
|
}
|
||
|
$fullPath = "$root/$path";
|
||
|
$relativePath = $basePath === '' ? $path : "$basePath/$path";
|
||
|
if (is_dir($fullPath)) {
|
||
|
$files = array_merge($files, getFileList($fullPath, $relativePath));
|
||
|
} else {
|
||
|
$files[] = $relativePath;
|
||
|
}
|
||
|
}
|
||
|
closedir($handle);
|
||
|
return $files;
|
||
|
}
|
||
|
|
||
|
function copyFile($root, $source, $target, &$all)
|
||
|
{
|
||
|
if (!is_file($root . '/' . $source)) {
|
||
|
echo " skip $target ($source not exist)\n";
|
||
|
return true;
|
||
|
}
|
||
|
if (is_file($root . '/' . $target)) {
|
||
|
if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
|
||
|
echo " unchanged $target\n";
|
||
|
return true;
|
||
|
}
|
||
|
if ($all) {
|
||
|
echo " overwrite $target\n";
|
||
|
} else {
|
||
|
echo " exist $target\n";
|
||
|
echo " ...overwrite? [Yes|No|All|Quit] ";
|
||
|
$answer = trim(fgets(STDIN));
|
||
|
if (!strncasecmp($answer, 'q', 1)) {
|
||
|
return false;
|
||
|
} else {
|
||
|
if (!strncasecmp($answer, 'y', 1)) {
|
||
|
echo " overwrite $target\n";
|
||
|
} else {
|
||
|
if (!strncasecmp($answer, 'a', 1)) {
|
||
|
echo " overwrite $target\n";
|
||
|
$all = true;
|
||
|
} else {
|
||
|
echo " skip $target\n";
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
|
||
|
return true;
|
||
|
}
|
||
|
echo " generate $target\n";
|
||
|
@mkdir(dirname($root . '/' . $target), 0777, true);
|
||
|
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
|
||
|
return true;
|
||
|
}
|