1<?php
2
3/* This file contains common logic needed in the php scripts */
4
5define('PROJECT_PATH', realpath(dirname(__FILE__)) . '/../');
6define('START_TIME', microtime(true));
7
8if (file_exists(realpath(dirname(__FILE__)) . '/customConfig.php'))
9	require_once(realpath(dirname(__FILE__)) . '/customConfig.php');
10
11if (!defined('MANDELBULBER_EXEC_PATH')) define('MANDELBULBER_EXEC_PATH', 'mandelbulber2');
12if (!defined('CLANG_FORMAT_EXEC_PATH')) define('CLANG_FORMAT_EXEC_PATH', 'clang-format');
13if(!defined('RUN_CLANG_TIDY_EXEC_PATH')) define('RUN_CLANG_TIDY_EXEC_PATH', '/usr/bin/run-clang-tidy-6.0.py');
14if(!defined('CLANG_APPLY_BINARY_PATH')) define('CLANG_APPLY_BINARY_PATH', '/usr/bin/clang-apply-replacements-6.0');
15
16checkArguments();
17
18function printStart()
19{
20	echo 'Processing...' . PHP_EOL;
21}
22
23function printFinish()
24{
25	echo "\33[2K\r";
26	$secString = noticeString(number_format(microtime(true) - START_TIME, 2) . ' seconds');
27	echo 'script took ' . $secString . ' to complete.' . PHP_EOL;
28	if (isDryRun()) {
29		echo 'This is a dry run.' . PHP_EOL;
30		echo 'If you want to apply the changes, execute with argument "nondry"' . PHP_EOL;
31	} else {
32		echo 'Changes applied' . PHP_EOL;
33	}
34}
35
36function printResultLine($name, $success, $status, $progress = -1)
37{
38	$out = "\33[2K\r";
39	$itemLine = str_pad(' ├── ' . $name, 50);
40	if (!($success && !isVerbose() && count($status) == 0)) {
41		if ($success) {
42			$out .= $itemLine . successString(' [ All OK ]') . PHP_EOL;
43		} else {
44			$out .= $itemLine . errorString(' [ ERROR  ]') . PHP_EOL;
45		}
46		if (count($status) > 0) {
47			foreach ($status as $i => $s) {
48				$treeStr = ($i == count($status) - 1) ? ' │   ╰── ' : ' │   ├── ';
49				$out .= $treeStr . $s . PHP_EOL;
50			}
51		}
52	}
53	$out .= printProgress($progress) . " handled: $name";
54	echo $out;
55}
56
57function printProgress($percent)
58{
59	if ($percent < 0) return '';
60	$freeWidth = 20;
61	$intProgress = floor($freeWidth * $percent);
62
63	$text = noticeString('[');
64	$text .= progressString(str_repeat('#', $intProgress));
65	$text .= str_repeat(' ', $freeWidth - $intProgress);
66	$text .= '/ ' . progressString(str_pad(number_format($percent * 100, 1), 4, ' ', STR_PAD_LEFT) . '%');
67	$text .= noticeString(']');
68	return $text;
69}
70
71function printStartGroup($title)
72{
73	echo PHP_EOL . "\033[1m\033[44m" . ' ' . str_pad($title, 54) . ' ' . "\033[0m" . PHP_EOL;
74}
75
76function printEndGroup()
77{
78	echo "\33[2K\r";
79	echo ' ╹' . PHP_EOL;
80}
81
82function errorString($s)
83{
84	return "\033[1;31m" . $s . "\033[0m";
85}
86
87function successString($s)
88{
89	return "\033[1;32m" . $s . "\033[0m";
90}
91
92function noticeString($s)
93{
94	return "\033[1;34m" . $s . "\033[0m";
95}
96
97function progressString($s)
98{
99	return "\033[1;35m" . $s . "\033[0m";
100}
101
102function warningString($s)
103{
104	return "\033[1;33m" . $s . "\033[0m";
105}
106
107function isDryRun()
108{
109	return !argumentContains('nondry');
110}
111
112function isVerbose()
113{
114	return argumentContains('verbose');
115}
116
117function isWarning()
118{
119	return argumentContains('verbose') || argumentContains('warning');
120}
121
122function argumentContains($needle)
123{
124	global $argv;
125	if (count($argv) > 1 && in_array($needle, $argv)) {
126		return true;
127	}
128	return false;
129}
130
131function checkArguments()
132{
133	global $argv;
134	foreach ($argv as $i => $arg) {
135		if ($i == 0) continue;
136		if (!in_array($arg, array('nondry', 'verbose', 'warning', 'checkCl', 'checkTidy'))){
137			//if($argv[0] != 'portMandelbulb3dFormula.php')
138			//	die('Unknown argument: ' . $arg . PHP_EOL);
139		}
140	}
141}
142
143function getModificationIntervals()
144{
145	$modificationFileInfo = array();
146	$cmd = "git log --format='COMMIT_SEPARATOR_1____________%adCOMMIT_SEPARATOR_2____________%s' --name-only";
147	$commitStringRaw = trim(shell_exec($cmd));
148	$commitStrings = explode('COMMIT_SEPARATOR_1____________', $commitStringRaw);
149	foreach ($commitStrings as $commitString) {
150		if (empty($commitString)) continue;
151		$commitLines = explode(PHP_EOL, $commitString);
152		$meta = explode('COMMIT_SEPARATOR_2____________', $commitLines[0]);
153		$date = substr($meta[0], -10, 4);
154		$title = $meta[1];
155
156		// these commits contain only auto formatting code changes
157		// and should not be counted for the modification invertal
158		if (strpos($title, 'source code') !== false) continue;
159		if (strpos($title, 'nullptr') !== false) continue;
160		if (strpos($title, 'remove src dir') !== false) continue;
161
162		foreach ($commitLines as $commitLine) {
163			if (!empty($commitLine) && strpos($commitLine, 'COMMIT_SEPARATOR_2____________') === false) {
164				$modificationFileInfo[$commitLine]['start'] =
165					min(empty($modificationFileInfo[$commitLine]['start']) ? 10000 : $modificationFileInfo[$commitLine]['start'], $date);
166				$modificationFileInfo[$commitLine]['end'] = max(@$modificationFileInfo[$commitLine]['end'], $date);
167			}
168		}
169	}
170	return $modificationFileInfo;
171}
172
173function getModificationInterval($filePath, $onlyEndYear = false)
174{
175	global $modificationIntervals;
176	if (empty($modificationIntervals)) $modificationIntervals = getModificationIntervals();
177
178	$filePathRelative = str_replace(PROJECT_PATH, 'mandelbulber2/', $filePath);
179	if (array_key_exists($filePathRelative, $modificationIntervals)) {
180		$yearStart = $modificationIntervals[$filePathRelative]['start'];
181		$yearEnd = $modificationIntervals[$filePathRelative]['end'];
182	}
183	if (empty($yearStart)) return date('Y');
184	if (empty($yearEnd) || $yearStart == $yearEnd) {
185		return $yearStart;
186	}
187	if ($onlyEndYear) return $yearEnd;
188	return $yearStart . '-' . substr($yearEnd, 2, 2);
189}
190
191
192?>
193