#!/usr/bin/env php
<?php
/**
 * This file is part of the Tea programming language project
 *
 * @author 		Benny <benny@meetdreams.com>
 * @copyright 	(c)2019 YJ Technology Ltd. [http://tealang.org]
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

/// A script use to generate the loading file for PHP Unit

namespace Tea;

define('BASE_PATH', dirname(__DIR__) . '/');
define('LOG_PATH', BASE_PATH . 'log/');

const DEBUG = false;
const OPTION_KEYS = ['init'];
const USAGE = 'Usage: genphploader tea/tests/PHPDemoUnit';

require BASE_PATH . 'compiler/__unit.php';

try {
	$opts = process_cli_options($argv, OPTION_KEYS);

	$dir = $opts[0] ?? null;
	if ($dir === null) {
		error("Missed target dir.");
		halt(USAGE);
	}

	$realpath = realpath($dir);
	if ($realpath === false) {
		error("Target dir '{$dir}' not found.");
		halt(USAGE);
	}

	$realpath = FileHelper::normalize_path($realpath . DS);
	$loader_file = $realpath . PHPLoaderMaker::LOADER_FILE;
	if (!file_exists($loader_file)) {
		halt(sprintf("The '%s' not found in '%s'.", PHPLoaderMaker::LOADER_FILE, $realpath));
	}

	$scanner = new PHPUnitScanner();
	$class_map = $scanner->scan($realpath);

	PHPLoaderMaker::generate_loader_file($realpath, $class_map);

	$count = count($class_map);
	halt("$count classes found.");
}
catch (Exception $e) {
	halt("Error: {$e->getMessage()}");
}

// end
