Skip to content

Commit 028f97d

Browse files
committed
extract more possible Docker env keys
1 parent e6c81ae commit 028f97d

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/container/util/DotEnvUtil.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
import com.intellij.psi.PsiManager;
1111
import com.intellij.psi.search.FilenameIndex;
1212
import com.intellij.psi.search.GlobalSearchScope;
13-
import com.intellij.psi.util.CachedValue;
14-
import com.intellij.psi.util.CachedValueProvider;
15-
import com.intellij.psi.util.CachedValuesManager;
16-
import com.intellij.psi.util.PsiModificationTracker;
13+
import com.intellij.psi.util.*;
1714
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1815
import org.apache.commons.lang.StringUtils;
1916
import org.jetbrains.annotations.NotNull;
@@ -158,21 +155,33 @@ private static void visitEnvironment(@NotNull Project project, @NotNull Consumer
158155

159156
for (String file : DOCKER_FILES) {
160157
for (PsiFile psiFile : FilenameIndex.getFilesByName(project, file, GlobalSearchScope.allScope(project))) {
158+
// ENV DOCKERFILE_FOO /bar
161159
Matcher matcher = Pattern.compile("ENV\\s+([^\\s]*)\\s+").matcher(psiFile.getText());
162160
while(matcher.find()){
163161
consumer.accept(Pair.create(matcher.group(1), psiFile));
164162
}
163+
164+
// ENV ADMIN_USER_DOCKERFILE="mark"
165+
// ENV ADMIN_USER_DOCKERFILE ="mark"
166+
matcher = Pattern.compile("ENV\\s+([\\w+]*)\\s*=").matcher(psiFile.getText());
167+
while(matcher.find()){
168+
consumer.accept(Pair.create(matcher.group(1), psiFile));
169+
}
165170
}
166171
}
167172
}
168173

169174
/**
170175
* environment:
171176
* - FOOBAR=0
177+
*
178+
* environment:
179+
* FOOBAR: 0
172180
*/
173181
private static void visitEnvironmentSquenceItems(@NotNull Consumer<Pair<String, PsiElement>> consumer, @NotNull YAMLKeyValue yamlKeyValue) {
174182
YAMLKeyValue environment = YamlHelper.getYamlKeyValue(yamlKeyValue, "environment");
175183
if (environment != null) {
184+
// FOOBAR=0
176185
for (YAMLSequenceItem yamlSequenceItem : YamlHelper.getSequenceItems(environment)) {
177186
YAMLValue value = yamlSequenceItem.getValue();
178187
if (value instanceof YAMLScalar) {
@@ -185,6 +194,14 @@ private static void visitEnvironmentSquenceItems(@NotNull Consumer<Pair<String,
185194
}
186195
}
187196
}
197+
198+
// FOOBAR: 0
199+
YAMLMapping childOfType = PsiTreeUtil.getChildOfType(environment, YAMLMapping.class);
200+
if (childOfType != null) {
201+
for (Map.Entry<String, YAMLValue> entry : YamlHelper.getYamlArrayKeyMap(childOfType).entrySet()) {
202+
consumer.accept(Pair.create(entry.getKey(), entry.getValue()));
203+
}
204+
}
188205
}
189206
}
190207
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/yaml/YamlHelper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,28 @@ public static YAMLKeyValue getYamlKeyValue(@NotNull YAMLMapping yamlHash, String
297297
return getYamlKeyValue(yamlHash, keyName, false);
298298
}
299299

300+
/**
301+
* test:
302+
* DEBUG_WEB_1: 1
303+
* DEBUG_WEB_2: 1
304+
*/
305+
@NotNull
306+
public static Map<String, YAMLValue> getYamlArrayKeyMap(@NotNull YAMLMapping yamlHash) {
307+
Map<String, YAMLValue> keys = new HashMap<>();
308+
309+
for(YAMLKeyValue yamlKeyValue: PsiTreeUtil.getChildrenOfAnyType(yamlHash, YAMLKeyValue.class)) {
310+
String keyText = yamlKeyValue.getKeyText();
311+
if (StringUtils.isNotBlank(keyText)) {
312+
YAMLValue value = yamlKeyValue.getValue();
313+
if (value != null) {
314+
keys.put(keyText, value);
315+
}
316+
}
317+
}
318+
319+
return keys;
320+
}
321+
300322
@Nullable
301323
public static String getYamlKeyValueAsString(@NotNull YAMLMapping yamlHash, @NotNull String keyName) {
302324
YAMLKeyValue yamlKeyValue = getYamlKeyValue(yamlHash, keyName, false);

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/container/util/DotEnvUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public String getTestDataPath() {
2121
}
2222

2323
public void testGetEnvironmentVariables() {
24-
assertContainsElements(DotEnvUtil.getEnvironmentVariables(getProject()), "foobar", "DEBUG_WEB", "DEBUG_SERVICES", "DOCKERFILE_FOO");
24+
assertContainsElements(DotEnvUtil.getEnvironmentVariables(getProject()), "foobar", "DEBUG_WEB", "DEBUG_SERVICES", "DOCKERFILE_FOO", "DEBUG_WEB_2", "DEBUG_SERVICES_2", "ADMIN_USER_DOCKERFILE");
2525
}
2626

2727
public void testGetEnvironmentVariableTargets() {

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/container/util/fixtures/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ FROM busybox
22
ENV DOCKERFILE_FOO /bar
33
WORKDIR ${foo} # WORKDIR /bar
44
ADD . $foo # ADD . /bar
5-
COPY \$foo /quux # COPY $foo /quux
5+
COPY \$foo /quux # COPY $foo /quux
6+
ENV ADMIN_USER_DOCKERFILE="mark"

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/container/util/fixtures/docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ services:
22
foobar:
33
environment:
44
- DEBUG_SERVICES=1
5+
foobar2:
6+
environment:
7+
DEBUG_SERVICES_2: 1
58

69
web:
710
environment:
8-
- DEBUG_WEB=1
11+
- DEBUG_WEB=1
12+
13+
web2:
14+
environment:
15+
DEBUG_WEB_2: 1

0 commit comments

Comments
 (0)