@@ -35,30 +35,11 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
35
35
36
36
public function unconfigure (Recipe $ recipe , $ config , Lock $ lock ): void
37
37
{
38
- foreach ($ config as $ patch ) {
39
- if (!isset ($ patch ['file ' ])) {
40
- $ this ->write (sprintf ('The "file" key is required for the "add-lines" configurator for recipe "%s". Skipping ' , $ recipe ->getName ()));
41
-
42
- continue ;
43
- }
44
-
45
- // Ignore "requires": the target packages may have just become uninstalled.
46
- // Checking for a "content" match is enough.
47
-
48
- $ file = $ this ->path ->concatenate ([$ this ->options ->get ('root-dir ' ), $ patch ['file ' ]]);
49
- if (!is_file ($ file )) {
50
- continue ;
51
- }
38
+ $ changes = $ this ->getUnconfigureFileChanges ($ recipe , $ config );
52
39
53
- if (!isset ($ patch ['content ' ])) {
54
- $ this ->write (sprintf ('The "content" key is required for the "add-lines" configurator for recipe "%s". Skipping ' , $ recipe ->getName ()));
55
-
56
- continue ;
57
- }
58
- $ value = $ patch ['content ' ];
59
-
60
- $ newContents = $ this ->getUnPatchedContents ($ file , $ value );
61
- file_put_contents ($ file , $ newContents );
40
+ foreach ($ changes as $ file => $ change ) {
41
+ $ this ->write (sprintf ('[add-lines] Reverting file "%s" ' , $ file ));
42
+ file_put_contents ($ file , $ change );
62
43
}
63
44
}
64
45
@@ -158,6 +139,38 @@ public function getConfigureFileChanges(Recipe $recipe, $config): array
158
139
return $ changes ;
159
140
}
160
141
142
+ public function getUnconfigureFileChanges (Recipe $ recipe , $ config ): array
143
+ {
144
+ $ changes = [];
145
+ foreach ($ config as $ patch ) {
146
+ if (!isset ($ patch ['file ' ])) {
147
+ $ this ->write (sprintf ('The "file" key is required for the "add-lines" configurator for recipe "%s". Skipping ' , $ recipe ->getName ()));
148
+
149
+ continue ;
150
+ }
151
+
152
+ // Ignore "requires": the target packages may have just become uninstalled.
153
+ // Checking for a "content" match is enough.
154
+
155
+ $ file = $ this ->path ->concatenate ([$ this ->options ->get ('root-dir ' ), $ patch ['file ' ]]);
156
+ if (!is_file ($ file )) {
157
+ continue ;
158
+ }
159
+
160
+ if (!isset ($ patch ['content ' ])) {
161
+ $ this ->write (sprintf ('The "content" key is required for the "add-lines" configurator for recipe "%s". Skipping ' , $ recipe ->getName ()));
162
+
163
+ continue ;
164
+ }
165
+ $ value = $ patch ['content ' ];
166
+
167
+ $ newContents = $ this ->getUnPatchedContents ($ file , $ value );
168
+ $ changes [$ file ] = $ newContents ;
169
+ }
170
+
171
+ return $ changes ;
172
+ }
173
+
161
174
private function getPatchedContents (string $ file , string $ value , string $ position , ?string $ target , bool $ warnIfMissing ): string
162
175
{
163
176
$ fileContents = file_get_contents ($ file );
0 commit comments