init 2.97 KB
Newer Older
1
#!/usr/bin/env php
2 3
<?php
$root = str_replace('\\', '/', __DIR__);
4
$envs = require("$root/environments/index.php");
5 6
$envNames = array_keys($envs);

Qiang Xue committed
7 8
echo "Yii Application Init Tool v1.0\n\n";
echo "Which environment do you want the application to be initialized in?\n\n";
9 10 11 12 13 14
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])) {
Qiang Xue committed
15
	echo "\n  Quit initialization.\n";
16 17 18 19
	return;
}

$env = $envs[$envNames[$answer]];
Qiang Xue committed
20
echo "\n  Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
21 22
$answer = trim(fgets(STDIN));
if (strncasecmp($answer, 'y', 1)) {
Qiang Xue committed
23
	echo "\n  Quit initialization.\n";
24 25 26
	return;
}

Qiang Xue committed
27
echo "\n  Start initialization ...\n\n";
28
$files = getFileList("$root/environments/{$env['path']}");
29 30
$all = false;
foreach ($files as $file) {
31
	if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all)) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
		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);
	}
}

Qiang Xue committed
50
echo "\n  ... initialization completed.\n\n";
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

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;
}