This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
composer-cmd.php
175 lines (153 loc) · 4.57 KB
/
composer-cmd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* This file is part of EspoCRM and/or TreoCore.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2019 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* TreoCore is EspoCRM-based Open Source application.
* Copyright (C) 2017-2019 TreoLabs GmbH
* Website: https://treolabs.com
*
* TreoCore as well as EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TreoCore as well as EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word
* and "TreoCore" word.
*/
declare(strict_types=1);
use Treo\Composer\PostUpdate;
use Treo\Core\Utils\Util;
/**
* Class ComposerCmd
*
* @author r.ratsun@treolabs.com
*/
class ComposerCmd
{
const DIFF_PATH = 'data/composer-diff';
/**
* Before update
*/
public static function preUpdate(): void
{
if (class_exists(Util::class)) {
// delete diff cache
Util::removeDir(self::DIFF_PATH);
}
}
/**
* After update
*/
public static function postUpdate(): void
{
// change directory
chdir(dirname(__FILE__));
// set the include_path
set_include_path(dirname(__FILE__));
// autoload
require_once 'vendor/autoload.php';
// run post update actions
(new PostUpdate())->run();
}
/**
* After package install
*
* @param mixed $event
*
* @return void
*/
public static function postPackageInstall($event): void
{
try {
$name = $event->getOperation()->getPackage()->getName();
} catch (\Throwable $e) {
}
if (isset($name)) {
self::createPackageActionFile($name, 'install');
}
}
/**
* @param mixed $event
*
* @return void
*/
public static function postPackageUpdate($event): void
{
// get composer update pretty line
$prettyLine = (string)$event->getOperation();
preg_match_all("/^Updating (.*) \((.*)\) to (.*) \((.*)\)$/", $prettyLine, $matches);
if (count($matches) == 5) {
self::createPackageActionFile($matches[1][0], 'update', $matches[2][0] . '_' . $matches[4][0]);
}
}
/**
* Before package uninstall
*
* @param mixed $event
*
* @return void
*/
public static function prePackageUninstall($event): void
{
try {
$name = $event->getOperation()->getPackage()->getName();
} catch (\Throwable $e) {
}
if (isset($name)) {
self::createPackageActionFile($name, 'delete');
}
}
/**
* @param string $name
* @param string $dir
* @param string $content
*
* @return bool
*/
protected static function createPackageActionFile(string $name, string $dir, string $content = ''): bool
{
// find composer.json file
$file = "vendor/$name/composer.json";
if (!file_exists($file)) {
return false;
}
// try to parse composer.json file
try {
$data = json_decode(file_get_contents($file), true);
} catch (\Throwable $e) {
return false;
}
// exit if is not treo package
if (!isset($data['extra']['treoId'])) {
return false;
}
// prepare dir path
$dirPath = self::DIFF_PATH . "/$dir";
// create dir if it needs
if (!file_exists($dirPath)) {
mkdir($dirPath, 0755, true);
}
// prepare content
$content = (empty($content)) ? $name : $name . '_' . $content;
// save
file_put_contents("$dirPath/{$data['extra']['treoId']}.txt", $content);
return true;
}
}