forked from salesagility/SuiteCRM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpackage_manager.php
195 lines (160 loc) · 6.29 KB
/
package_manager.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
class GitPatch {
public $name;
public $fpath;
public function __construct($name, $fpath){
$this->name = $name;
$this->fpath = $fpath;
}
}
function usage($error = "") {
if (!empty($error)) print(PHP_EOL . "Error: " . $error . PHP_EOL);
print(" php " . __FILE__ . " --install package_name --remove package_name" . PHP_EOL);
exit(1);
}
// Create Patch file with the current changes of the packages
function create_backup($git_patch_name){
exec("find . -type d -name '.git'", $folders);
$patch_array = array();
foreach ($folders as $key=>$folder) {
$folder_path = dirname($folder);
$patch_name = $git_patch_name.$key.".patch";
$pt = new GitPatch($patch_name, $folder_path);
exec("git -C $pt->fpath diff --no-color -G. > $pt->name");
array_push($patch_array, $pt);
}
return $patch_array;
}
// Restore the files to their previous state
function restore_files($patch_list){
$script_path = dirname(__FILE__);
foreach ($patch_list as $patch) {
if (filesize("$script_path/$patch->name")) {
// If is the root folder, we avoid git reset --hard
// to avoid breaking symlinks
if ($patch->fpath == ".") {
exec("find . -type l", $links);
foreach ($links as $link) {
exec("git add $link");
}
exec("git -C $patch->fpath apply --ignore-space-change --ignore-whitespace $script_path/$patch->name");
foreach ($links as $link) {
exec("git restore --staged $link");
}
} else {
exec("git -C $patch->fpath reset --hard");
exec("git -C $patch->fpath apply --ignore-space-change --ignore-whitespace $script_path/$patch->name");
}
}
shell_exec("rm $script_path/$patch->name");
}
}
function uninstall_package($package_name, $language){
$pm = new PackageManager();
# Uninstall previous installed packages
$pm->performUninstall($package_name);
clearAllJsAndJsLangFilesWithoutOutput();
$cache_key = "app_list_strings.".$language;
sugar_cache_clear($cache_key);
sugar_cache_reset();
}
// only allow CLI
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != "cli") {
die(__FILE__ . " is CLI only.");
}
// get command line params
$option = getopt("", array("install:", "remove:"));
if (!isset($option["install"]) && !isset($option["remove"])){
usage();
}
// sugar basic setup
define("sugarEntry", true);
require_once("include/entryPoint.php");
if (empty($current_language)) {
$current_language = $sugar_config["default_language"];
}
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
$mod_strings = return_module_language($current_language, "Administration");
global $current_user;
$current_user = BeanFactory::getBean("Users");
$current_user->getSystemUser();
$start_time = microtime(true);
require_once("modules/ModuleBuilder/MB/ModuleBuilder.php");
require_once("modules/ModuleBuilder/parsers/ParserFactory.php");
require_once("modules/ModuleBuilder/Module/StudioModuleFactory.php");
require_once("modules/ModuleBuilder/parsers/constants.php");
require_once("ModuleInstall/PackageManager/PackageManager.php");
//increment etag for menu so the new module shows up when the AJAX UI reloads
$current_user->incrementETag("mainMenuETag");
$mb = new ModuleBuilder();
$patch_name = "tempchanges";
if (!empty($option["install"])) {
$ipackage_name = $option["install"];
if (in_array($ipackage_name, $mb->getPackageList())) {
$patch_list = create_backup($patch_name);
$zip = $mb->getPackage($ipackage_name);
$pm = new PackageManager();
$info = $mb->packages [ $ipackage_name ]->build(false);
$uploadDir = $pm->upload_dir."/upgrades/module/";
mkdir_recursive($uploadDir);
rename($info [ "zip" ], $uploadDir . $info [ "name" ] . ".zip");
copy($info [ "manifest" ], $uploadDir . $info [ "name" ] . "-manifest.php");
$_REQUEST['install_file'] = $uploadDir. $info [ "name" ] . ".zip";
# Uninstall previous installed packages
uninstall_package($ipackage_name, $current_language);
//clear end
$pm->performInstall($_REQUEST['install_file'], true);
//clear the unified_search_module.php file
require_once("modules/Home/UnifiedSearchAdvanced.php");
UnifiedSearchAdvanced::unlinkUnifiedSearchModulesFile();
//clear workflow admin modules cache
if (isset($_SESSION["get_workflow_admin_modules_for_user"])) {
unset($_SESSION["get_workflow_admin_modules_for_user"]);
}
// clear "is_admin_for_module" cache
$sessionVar = "MLA_".$current_user->user_name;
foreach ($mb->packages as $package) {
foreach ($package->modules as $module) {
$_SESSION[$sessionVar][$package->name . "_" . $module->name] = true;
}
}
// recreate acl cache
$actions = ACLAction::getUserActions($current_user->id, true);
restore_files($patch_list);
echo "Package installed\n";
} else {
echo "Package don't exist, available packages:\n";
foreach($mb->getPackageList() as $available_package) {
echo $available_package."\n";
}
}
}
if (!empty($option["remove"])) {
$rpackage_name = $option["remove"];
$package_deployed = false;
if (in_array($rpackage_name, $mb->getPackageList())) {
$mbpackage = $mb->getPackage($rpackage_name);
foreach ($mbpackage->modules as $a_module) {
if (in_array($a_module->key_name, $GLOBALS['moduleList'])) {
$package_deployed = true;
break;
}
}
if ($package_deployed) {
$patch_list = create_backup($patch_name);
uninstall_package($rpackage_name, $current_language);
restore_files($patch_list);
echo "\nPackage Uninstalled\n";
} else {
echo "Package not installed\n";
}
} else {
echo "Package don't exist, available packages:\n";
foreach($mb->getPackageList() as $available_package) {
echo $available_package."\n";
}
}
}
?>