@@ -29,83 +29,100 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
29
29
}
30
30
31
31
$ rootDir = $ this ->options ->get ('root-dir ' );
32
- if (
33
- (
34
- !file_exists ($ dockerComposeFile = $ rootDir .'/docker-compose.yml ' ) &&
35
- !file_exists ($ dockerComposeFile = $ rootDir .'/docker-compose.yaml ' )
36
- ) || $ this ->isFileMarked ($ recipe , $ dockerComposeFile )
37
- ) {
38
- return ;
39
- }
32
+ foreach ($ this ->normalizeConfig ($ config ) as $ file => $ extra ) {
33
+ $ dockerComposeFile = sprintf ('%s/%s ' , $ rootDir , $ file );
34
+
35
+ // Test with the ".yaml" extension if the file doesn't end up with ".yml".
36
+ if (
37
+ (!file_exists ($ dockerComposeFile ) && !file_exists ($ dockerComposeFile = substr ($ dockerComposeFile , 0 , -2 ).'aml ' )) ||
38
+ $ this ->isFileMarked ($ recipe , $ dockerComposeFile )
39
+ ) {
40
+ continue ;
41
+ }
40
42
41
- $ this ->write ('Adding Docker Compose entries ' );
43
+ $ this ->write (sprintf ( 'Adding Docker Compose entries to "%s" ' , $ dockerComposeFile ) );
42
44
43
- $ offset = 8 ;
44
- $ node = null ;
45
- $ endAt = [];
46
- $ lines = [];
47
- foreach (file ($ dockerComposeFile ) as $ i => $ line ) {
48
- $ lines [] = $ line ;
49
- $ ltrimedLine = ltrim ($ line , ' ' );
45
+ $ offset = 8 ;
46
+ $ node = null ;
47
+ $ endAt = [];
48
+ $ lines = [];
49
+ foreach (file ($ dockerComposeFile ) as $ i => $ line ) {
50
+ $ lines [] = $ line ;
51
+ $ ltrimedLine = ltrim ($ line , ' ' );
50
52
51
- // Skip blank lines and comments
52
- if (('' !== $ ltrimedLine && 0 === strpos ($ ltrimedLine , '# ' )) || '' === trim ($ line )) {
53
- continue ;
54
- }
53
+ // Skip blank lines and comments
54
+ if (('' !== $ ltrimedLine && 0 === strpos ($ ltrimedLine , '# ' )) || '' === trim ($ line )) {
55
+ continue ;
56
+ }
55
57
56
- // Extract Docker Compose keys (usually "services" and "volumes")
57
- if (!preg_match ('/^[ \'"]?([a-zA-Z0-9]+)[ \'"]?:\s*$/ ' , $ line , $ matches )) {
58
- // Detect indentation to use
59
- $ offestLine = \strlen ($ line ) - \strlen ($ ltrimedLine );
60
- if ($ offset > $ offestLine && 0 !== $ offestLine ) {
61
- $ offset = $ offestLine ;
58
+ // Extract Docker Compose keys (usually "services" and "volumes")
59
+ if (!preg_match ('/^[ \'"]?([a-zA-Z0-9]+)[ \'"]?:\s*$/ ' , $ line , $ matches )) {
60
+ // Detect indentation to use
61
+ $ offestLine = \strlen ($ line ) - \strlen ($ ltrimedLine );
62
+ if ($ offset > $ offestLine && 0 !== $ offestLine ) {
63
+ $ offset = $ offestLine ;
64
+ }
65
+ continue ;
62
66
}
63
- continue ;
67
+
68
+ // Keep end in memory (check break line on previous line)
69
+ $ endAt [$ node ] = '' !== trim ($ lines [$ i - 1 ]) ? $ i : $ i - 1 ;
70
+ $ node = $ matches [1 ];
64
71
}
72
+ $ endAt [$ node ] = \count ($ lines ) + 1 ;
65
73
66
- // Keep end in memory (check break line on previous line)
67
- $ endAt [ $ node ] = '' !== trim ( $ lines [ $ i - 1 ]) ? $ i : $ i - 1 ;
68
- $ node = $ matches [ 1 ] ;
69
- }
70
- $ endAt [ $ node ] = \count ( $ lines ) + 1 ;
74
+ foreach ( $ extra as $ key => $ value ) {
75
+ if ( isset ( $ endAt [ $ key ])) {
76
+ array_splice ( $ lines , $ endAt [ $ key ], 0 , $ this -> markData ( $ recipe , $ this -> parse ( 1 , $ offset , $ value ))) ;
77
+ continue ;
78
+ }
71
79
72
- foreach ($ config as $ key => $ value ) {
73
- if (isset ($ endAt [$ key ])) {
74
- array_splice ($ lines , $ endAt [$ key ], 0 , $ this ->markData ($ recipe , $ this ->parse (1 , $ offset , $ value )));
75
- continue ;
80
+ $ lines [] = sprintf ("\n%s: " , $ key );
81
+ $ lines [] = $ this ->markData ($ recipe , $ this ->parse (1 , $ offset , $ value ));
76
82
}
77
83
78
- $ lines [] = sprintf ("\n%s: " , $ key );
79
- $ lines [] = $ this ->markData ($ recipe , $ this ->parse (1 , $ offset , $ value ));
84
+ file_put_contents ($ dockerComposeFile , implode ('' , $ lines ));
80
85
}
81
-
82
- file_put_contents ($ dockerComposeFile , implode ('' , $ lines ));
83
86
}
84
87
85
88
public function unconfigure (Recipe $ recipe , $ config , Lock $ lock )
86
89
{
87
90
$ rootDir = $ this ->options ->get ('root-dir ' );
88
- if (! file_exists ( $ dockerCompose = $ rootDir . ' /docker-compose.yml ' ) &&
89
- ! file_exists ( $ dockerCompose = $ rootDir . ' /docker-compose.yaml ' )
90
- ) {
91
- return ;
92
- }
91
+ foreach ( $ this -> normalizeConfig ( $ config ) as $ file => $ extra ) {
92
+ $ dockerComposeFile = sprintf ( ' %s/%s ' , $ rootDir , $ file );
93
+ if (! file_exists ( $ dockerComposeFile ) && ! file_exists ( $ dockerComposeFile = substr ( $ dockerComposeFile , 0 , - 2 ). ' aml ' ) ) {
94
+ continue ;
95
+ }
93
96
94
- $ name = $ recipe ->getName ();
95
- // Remove recipe and add break line
96
- $ contents = preg_replace (sprintf ('{%s+###> %s ###.*?###< %s ###%s+}s ' , "\n" , $ name , $ name , "\n" ), PHP_EOL .PHP_EOL , file_get_contents ($ dockerCompose ), -1 , $ count );
97
- if (!$ count ) {
98
- return ;
99
- }
97
+ $ name = $ recipe ->getName ();
98
+ // Remove recipe and add break line
99
+ $ contents = preg_replace (sprintf ('{%s+###> %s ###.*?###< %s ###%s+}s ' , "\n" , $ name , $ name , "\n" ), PHP_EOL .PHP_EOL , file_get_contents ($ dockerComposeFile ), -1 , $ count );
100
+ if (!$ count ) {
101
+ return ;
102
+ }
100
103
101
- foreach ($ config as $ key => $ value ) {
102
- if (0 === preg_match (sprintf ('{^%s:[ \t\r\n]*([ \t]+\w|#)}m ' , $ key ), $ contents , $ matches )) {
103
- $ contents = preg_replace (sprintf ('{\n?^%s:[ \t\r\n]*}sm ' , $ key ), '' , $ contents , -1 , $ count );
104
+ foreach ($ extra as $ key => $ value ) {
105
+ if (0 === preg_match (sprintf ('{^%s:[ \t\r\n]*([ \t]+\w|#)}m ' , $ key ), $ contents , $ matches )) {
106
+ $ contents = preg_replace (sprintf ('{\n?^%s:[ \t\r\n]*}sm ' , $ key ), '' , $ contents , -1 , $ count );
107
+ }
104
108
}
109
+
110
+ $ this ->write (sprintf ('Removing Docker Compose entries from "%s" ' , $ dockerComposeFile ));
111
+ file_put_contents ($ dockerComposeFile , ltrim ($ contents , "\n" ));
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Normalizes the config and return the name of the main Docker Compose file if applicable.
117
+ */
118
+ private function normalizeConfig (array $ config ): array
119
+ {
120
+ foreach ($ config as $ val ) {
121
+ // Support for the short syntax recipe syntax that modifies docker-compose.yml only
122
+ return isset ($ val [0 ]) ? ['docker-compose.yml ' => $ config ] : $ config ;
105
123
}
106
124
107
- $ this ->write (sprintf ('Removing Docker Compose entries from %s ' , $ dockerCompose ));
108
- file_put_contents ($ dockerCompose , ltrim ($ contents , "\n" ));
125
+ return $ config ;
109
126
}
110
127
111
128
private function parse ($ level , $ indent , $ services ): string
0 commit comments