Skip to content

Commit 6d83b62

Browse files
authored
Merge pull request #32 from Derroylo/feature/15_php_restore_command
Make the php restore command faster
2 parents e4b8928 + 075cc4d commit 6d83b62

File tree

1 file changed

+52
-32
lines changed

1 file changed

+52
-32
lines changed

helper/PhpHelper.cs

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,34 @@ public static void SetNewPhpVersion(string newVersion, bool isDebug)
4949

5050
string currentPhpVersion = PhpHelper.GetCurrentPhpVersion();
5151

52-
// Update the CLI Version
53-
ExecCommand.Exec("sudo update-alternatives --set php /usr/bin/php" + newVersion);
54-
AnsiConsole.MarkupLine("update-alternatives --set php /usr/bin/php" + newVersion + "...[green1]Success[/]");
52+
// Check if we have selected another version as the currently active one
53+
if (newVersion != currentPhpVersion) {
54+
// Update the CLI Version
55+
ExecCommand.Exec("sudo update-alternatives --set php /usr/bin/php" + newVersion);
56+
AnsiConsole.MarkupLine("update-alternatives --set php /usr/bin/php" + newVersion + "...[green1]Success[/]");
5557

56-
ExecCommand.Exec("sudo update-alternatives --set php-config /usr/bin/php-config" + newVersion);
57-
AnsiConsole.MarkupLine("update-alternatives --set php-config /usr/bin/php-config" + newVersion + "...[green1]Success[/]");
58+
ExecCommand.Exec("sudo update-alternatives --set php-config /usr/bin/php-config" + newVersion);
59+
AnsiConsole.MarkupLine("update-alternatives --set php-config /usr/bin/php-config" + newVersion + "...[green1]Success[/]");
5860

59-
// Update the version apache uses
60-
ExecCommand.Exec("sudo apt-get update");
61-
AnsiConsole.MarkupLine("apt-get update...[green1]Success[/]");
61+
// Update the version apache uses
62+
ExecCommand.Exec("sudo apt-get update");
63+
AnsiConsole.MarkupLine("apt-get update...[green1]Success[/]");
6264

63-
ExecCommand.Exec("sudo apt-get install -y libapache2-mod-php" + newVersion);
64-
AnsiConsole.MarkupLine("apt-get install -y libapache2-mod-php" + newVersion + "...[green1]Success[/]");
65+
ExecCommand.Exec("sudo apt-get install -y libapache2-mod-php" + newVersion);
66+
AnsiConsole.MarkupLine("apt-get install -y libapache2-mod-php" + newVersion + "...[green1]Success[/]");
6567

66-
ExecCommand.Exec("sudo a2dismod php" + currentPhpVersion);
67-
AnsiConsole.MarkupLine("a2dismod php" + currentPhpVersion + "...[green1]Success[/]");
68+
ExecCommand.Exec("sudo a2dismod php" + currentPhpVersion);
69+
AnsiConsole.MarkupLine("a2dismod php" + currentPhpVersion + "...[green1]Success[/]");
6870

69-
ExecCommand.Exec("sudo a2enmod php" + newVersion);
70-
AnsiConsole.MarkupLine("a2enmod php" + newVersion + "...[green1]Success[/]");
71-
72-
// Restarting Apache
73-
ExecCommand.Exec("apachectl stop");
74-
ExecCommand.Exec("apachectl start");
75-
76-
AnsiConsole.MarkupLine("Restarting apache...[green1]Success[/]");
77-
78-
//ctx.Status("Validating that the new version has been set...");
71+
ExecCommand.Exec("sudo a2enmod php" + newVersion);
72+
AnsiConsole.MarkupLine("a2enmod php" + newVersion + "...[green1]Success[/]");
73+
74+
// Restarting Apache
75+
ExecCommand.Exec("apachectl stop");
76+
ExecCommand.Exec("apachectl start");
77+
78+
AnsiConsole.MarkupLine("Restarting apache...[green1]Success[/]");
79+
}
7980

8081
string testResult = PhpHelper.GetCurrentPhpVersionOutput();
8182

@@ -94,20 +95,39 @@ public static void SetNewPhpVersion(string newVersion, bool isDebug)
9495
ctx.Status("Checking if file with additional packages exists...");
9596

9697
if (GptConfigHelper.Config?.Php?.Packages?.Count > 0) {
97-
var updateRes = ExecCommand.Exec("sudo apt-get update");
98-
AnsiConsole.MarkupLine("Updating package manager list...[green1]Done[/]");
98+
// Read currently installed packages
99+
var installedPackages = ExecCommand.Exec("apt list --installed");
100+
101+
var packagesList = installedPackages.Split("\n");
102+
var packagesCleaned = new List<string>();
103+
104+
foreach (string package in packagesList) {
105+
var tmp = package.Split("/");
99106

100-
if (isDebug) {
101-
AnsiConsole.WriteLine(updateRes);
107+
packagesCleaned.Add(tmp[0].Trim());
102108
}
103109

104-
string packages = string.Join(" ", GptConfigHelper.Config.Php.Packages).Replace("VERSION", newVersion).Replace("php-", "php" + newVersion + "-");
110+
var packagesToCheck = GptConfigHelper.Config.Php.Packages;
105111

106-
var installRes = ExecCommand.Exec("sudo apt-get install -y " + packages);
107-
AnsiConsole.MarkupLine("Installing packages...[green1]Done[/]");
108-
109-
if (isDebug) {
110-
AnsiConsole.WriteLine(installRes);
112+
// Check if packages from the config file are not already installed
113+
var packagesToInstall = packagesToCheck.Where(p => !packagesCleaned.Contains(p.Replace("VERSION", newVersion).Replace("php-", "php" + newVersion + "-"))).ToArray();
114+
115+
if (packagesToInstall.Length > 0) {
116+
var updateRes = ExecCommand.Exec("sudo apt-get update");
117+
AnsiConsole.MarkupLine("Updating package manager list...[green1]Done[/]");
118+
119+
if (isDebug) {
120+
AnsiConsole.WriteLine(updateRes);
121+
}
122+
123+
string packages = string.Join(" ", packagesToInstall).Replace("VERSION", newVersion).Replace("php-", "php" + newVersion + "-");
124+
125+
var installRes = ExecCommand.Exec("sudo apt-get install -y " + packages);
126+
AnsiConsole.MarkupLine("Installing packages...[green1]Done[/]");
127+
128+
if (isDebug) {
129+
AnsiConsole.WriteLine(installRes);
130+
}
111131
}
112132
} else {
113133
AnsiConsole.MarkupLine("Checking if file with additional packages exists...[cyan3]Not found[/]");

0 commit comments

Comments
 (0)