@@ -49,33 +49,34 @@ public static void SetNewPhpVersion(string newVersion, bool isDebug)
49
49
50
50
string currentPhpVersion = PhpHelper . GetCurrentPhpVersion ( ) ;
51
51
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[/]" ) ;
55
57
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[/]" ) ;
58
60
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[/]" ) ;
62
64
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[/]" ) ;
65
67
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[/]" ) ;
68
70
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
+ }
79
80
80
81
string testResult = PhpHelper . GetCurrentPhpVersionOutput ( ) ;
81
82
@@ -94,20 +95,39 @@ public static void SetNewPhpVersion(string newVersion, bool isDebug)
94
95
ctx . Status ( "Checking if file with additional packages exists..." ) ;
95
96
96
97
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 ( "/" ) ;
99
106
100
- if ( isDebug ) {
101
- AnsiConsole . WriteLine ( updateRes ) ;
107
+ packagesCleaned . Add ( tmp [ 0 ] . Trim ( ) ) ;
102
108
}
103
109
104
- string packages = string . Join ( " " , GptConfigHelper . Config . Php . Packages ) . Replace ( "VERSION" , newVersion ) . Replace ( "php-" , "php" + newVersion + "-" ) ;
110
+ var packagesToCheck = GptConfigHelper . Config . Php . Packages ;
105
111
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
+ }
111
131
}
112
132
} else {
113
133
AnsiConsole . MarkupLine ( "Checking if file with additional packages exists...[cyan3]Not found[/]" ) ;
0 commit comments