2024.8.23
Loading...
Searching...
No Matches
class.tool.git.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class ToolGit
12 *
13 * This class represents the Git tool module in the Bearsampp application.
14 * It handles the configuration, initialization, and management of Git-related settings and repositories.
15 */
16class ToolGit extends Module
17{
18 const ROOT_CFG_VERSION = 'gitVersion';
19
20 const LOCAL_CFG_EXE = 'gitExe';
21 const LOCAL_CFG_BASH = 'gitBash';
22 const LOCAL_CFG_SCAN_STARTUP = 'gitScanStartup';
23
24 const REPOS_FILE = 'repos.dat';
25 const REPOS_CACHE_FILE = 'reposCache.dat';
26
27 private $reposFile;
29 private $repos;
30
31 private $exe;
32 private $bash;
33 private $scanStartup;
34
35 /**
36 * Constructs a ToolGit object and initializes the Git tool module.
37 *
38 * @param string $id The ID of the module.
39 * @param string $type The type of the module.
40 */
41 public function __construct($id, $type) {
42 Util::logInitClass($this);
43 $this->reload($id, $type);
44 }
45
46 /**
47 * Reloads the Git tool module configuration based on the provided ID and type.
48 *
49 * @param string|null $id The ID of the module. If null, the current ID is used.
50 * @param string|null $type The type of the module. If null, the current type is used.
51 */
52 public function reload($id = null, $type = null) {
55
56 $this->name = $bearsamppLang->getValue(Lang::GIT);
57 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
58 parent::reload($id, $type);
59
60 $this->reposFile = $this->symlinkPath . '/' . self::REPOS_FILE;
61 $this->reposCacheFile = $this->symlinkPath . '/' . self::REPOS_CACHE_FILE;
62
63 if ($this->bearsamppConfRaw !== false) {
64 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
65 $this->bash = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_BASH];
66 $this->scanStartup = $this->bearsamppConfRaw[self::LOCAL_CFG_SCAN_STARTUP];
67 }
68
69 if (!$this->enable) {
70 Util::logInfo($this->name . ' is not enabled!');
71 return;
72 }
73 if (!is_dir($this->currentPath)) {
74 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
75 }
76 if (!is_dir($this->symlinkPath)) {
77 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
78 return;
79 }
80 if (!is_file($this->bearsamppConf)) {
81 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
82 }
83 if (!is_file($this->reposFile)) {
84 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->reposFile));
85 }
86 if (!is_file($this->exe)) {
87 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
88 }
89 if (!is_file($this->bash)) {
90 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->bash));
91 }
92
93 if (is_file($this->reposFile)) {
94 $this->repos = explode(PHP_EOL, file_get_contents($this->reposFile));
95 $rebuildRepos = array();
96 foreach ($this->repos as $repo) {
97 $repo = trim($repo);
98 if (stripos($repo, ':') === false) {
99 $repo = $bearsamppRoot->getRootPath() . '/' . $repo;
100 }
101 if (is_dir($repo)) {
102 $rebuildRepos[] = Util::formatUnixPath($repo);
103 } else {
104 Util::logWarning($this->name . ' repository not found: ' . $repo);
105 }
106 }
107 $this->repos = $rebuildRepos;
108 }
109 }
110
111 /**
112 * Updates the Git tool module configuration with a specific version.
113 *
114 * @param string|null $version The version to update to. If null, the current version is used.
115 * @param int $sub The sub-level for logging indentation.
116 * @param bool $showWindow Whether to show a window during the update process.
117 * @return bool True if the update was successful, false otherwise.
118 */
119 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
120 global $bearsamppWinbinder;
121
122 if (!$this->enable) {
123 return true;
124 }
125
126 $version = $version == null ? $this->version : $version;
127 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
128
129 if (file_exists($this->getSymlinkPath() . '/post-install.bat')) {
130 $bearsamppWinbinder->exec($this->getBash(), '--no-needs-console --hide --no-cd --command=' . $this->getSymlinkPath() . '/post-install.bat', true);
131 }
132
133 $bearsamppWinbinder->exec($this->getExe(), 'config --global core.autocrlf false', true);
134 $bearsamppWinbinder->exec($this->getExe(), 'config --global core.eol lf', true);
135
136 return true;
137 }
138
139 /**
140 * Finds Git repositories either from cache or by scanning the directories.
141 *
142 * @param bool $cache Whether to use the cached repositories list.
143 * @return array The list of found repositories.
144 */
145 public function findRepos($cache = true) {
146 $result = array();
147
148 if ($cache) {
149 if (file_exists($this->reposCacheFile)) {
150 $repos = file($this->reposCacheFile);
151 foreach ($repos as $repo) {
152 array_push($result, trim($repo));
153 }
154 }
155 } else {
156 if (!empty($this->repos)) {
157 foreach ($this->repos as $repo) {
158 $foundRepos = Util::findRepos($repo, $repo,'.git/config');
159 if (!empty($foundRepos)) {
160 foreach ($foundRepos as $foundRepo) {
161 array_push($result, $foundRepo);
162 }
163 }
164 }
165 }
166 $strResult = implode(PHP_EOL, $result);
167 file_put_contents($this->reposCacheFile, $strResult);
168 }
169
170 return $result;
171 }
172
173 /**
174 * Sets the version of the Git tool module.
175 *
176 * @param string $version The version to set.
177 */
178 public function setVersion($version) {
179 global $bearsamppConfig;
180 $this->version = $version;
181 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
182 $this->reload();
183 }
184
185 /**
186 * Retrieves the list of repositories.
187 *
188 * @return array The list of repositories.
189 */
190 public function getRepos() {
191 return $this->repos;
192 }
193
194 /**
195 * Retrieves the path to the Git executable.
196 *
197 * @return string The path to the Git executable.
198 */
199 public function getExe() {
200 return $this->exe;
201 }
202
203 /**
204 * Retrieves the path to the Git Bash executable.
205 *
206 * @return string The path to the Git Bash executable.
207 */
208 public function getBash() {
209 return $this->bash;
210 }
211
212 /**
213 * Checks if the Git tool module is set to scan repositories at startup.
214 *
215 * @return bool True if set to scan at startup, false otherwise.
216 */
217 public function isScanStartup() {
218 return $this->scanStartup == Config::ENABLED;
219 }
220
221 /**
222 * Sets whether the Git tool module should scan repositories at startup.
223 *
224 * @param bool $scanStartup True to enable scanning at startup, false to disable.
225 */
226 public function setScanStartup($scanStartup) {
227 $this->scanStartup = $scanStartup;
228 Util::replaceInFile($this->bearsamppConf, array(
229 '/^' . self::LOCAL_CFG_SCAN_STARTUP . '/' => self::LOCAL_CFG_SCAN_STARTUP . ' = "' . $this->scanStartup . '"'
230 ));
231 }
232}
$result
global $bearsamppLang
global $bearsamppRoot
const ENABLED
const ERROR_FILE_NOT_FOUND
const GIT
const ERROR_CONF_NOT_FOUND
const ERROR_EXE_NOT_FOUND
findRepos($cache=true)
const REPOS_FILE
setScanStartup($scanStartup)
const ROOT_CFG_VERSION
const REPOS_CACHE_FILE
const LOCAL_CFG_BASH
setVersion($version)
const LOCAL_CFG_SCAN_STARTUP
updateConfig($version=null, $sub=0, $showWindow=false)
__construct($id, $type)
reload($id=null, $type=null)
const LOCAL_CFG_EXE
static logReloadClass($classInstance)
static findRepos($initPath, $startPath, $checkFile, $maxDepth=1)
static replaceInFile($path, $replaceList)
static logError($data, $file=null)
static formatUnixPath($path)
static logDebug($data, $file=null)
static logInitClass($classInstance)
static logInfo($data, $file=null)
static logWarning($data, $file=null)
global $bearsamppConfig
Definition homepage.php:26