• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Camelcade / Perl5-IDEA / #525521553

27 May 2025 10:24AM UTC coverage: 82.286% (-0.03%) from 82.32%
#525521553

push

github

hurricup
Removed redundant implementations, moved up to the abstract class

1 of 1 new or added line in 1 file covered. (100.0%)

128 existing lines in 30 files now uncovered.

30886 of 37535 relevant lines covered (82.29%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.68
/plugin/core/src/main/java/com/perl5/lang/perl/idea/sdk/PerlConfig.java
1
/*
2
 * Copyright 2015-2025 Alexandr Evstigneev
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.perl5.lang.perl.idea.sdk;
18

19
import com.intellij.configurationStore.XmlSerializer;
20
import com.intellij.openapi.application.ApplicationManager;
21
import com.intellij.openapi.application.WriteAction;
22
import com.intellij.openapi.diagnostic.Logger;
23
import com.intellij.openapi.progress.ProgressManager;
24
import com.intellij.openapi.projectRoots.Sdk;
25
import com.intellij.openapi.util.Key;
26
import com.intellij.openapi.util.text.StringUtil;
27
import com.intellij.util.xmlb.annotations.Tag;
28
import com.perl5.PerlBundle;
29
import com.perl5.lang.perl.util.PerlRunUtil;
30
import org.jdom.Element;
31
import org.jetbrains.annotations.Contract;
32
import org.jetbrains.annotations.NotNull;
33
import org.jetbrains.annotations.Nullable;
34
import org.jetbrains.annotations.TestOnly;
35

36
import java.util.Collections;
37
import java.util.HashMap;
38
import java.util.Map;
39

40
public final class PerlConfig {
41
  private static final Key<Boolean> INIT_KEY = Key.create("perl.config.read");
1✔
42
  private static final Logger LOG = Logger.getInstance(PerlConfig.class);
1✔
43
  private static final String TAG_NAME = "PerlConfig";
44
  private static final PerlConfig EMPTY_CONFIG = new PerlConfig(Collections.emptyMap());
1✔
45

46
  private static final String ARCHNAME = "archname";
47
  private static final String API_VERSIONSTRING = "api_versionstring";
48
  private static final String VERSION = "version";
49

50
  @Tag("configMap")
51
  private final @NotNull Map<String, String> myConfig;
52

53
  @SuppressWarnings("unused")
54
  private PerlConfig() {
55
    this(Collections.emptyMap());
1✔
56
  }
1✔
57

58
  private PerlConfig(@NotNull Map<String, String> config) {
1✔
59
    myConfig = new HashMap<>(config);
1✔
60
  }
1✔
61

62
  @Contract("null->null")
63
  public @Nullable String get(@Nullable String key) {
64
    return myConfig.get(key);
1✔
65
  }
66

67
  public @Nullable String getArchname() {
68
    return get(ARCHNAME);
1✔
69
  }
70

71
  public @Nullable String getApiVersionString() {
UNCOV
72
    return get(API_VERSIONSTRING);
×
73
  }
74

75
  public @Nullable String getVersion() {
76
    return get(VERSION);
1✔
77
  }
78

79
  public boolean isEmpty() {
80
    return this == EMPTY_CONFIG;
1✔
81
  }
82

83
  /**
84
   * Loading config from xml
85
   */
86
  public static @NotNull PerlConfig load(@NotNull Element parentElement) {
87
    Element container = parentElement.getChild(TAG_NAME);
1✔
88
    if (container == null) {
1✔
UNCOV
89
      return EMPTY_CONFIG;
×
90
    }
91
    return intern(XmlSerializer.deserialize(container, PerlConfig.class));
1✔
92
  }
93

94
  private static @NotNull PerlConfig intern(@Nullable PerlConfig configMap) {
95
    return configMap == null || configMap.myConfig.isEmpty() ? EMPTY_CONFIG : configMap;
1✔
96
  }
97

98
  public static @NotNull PerlConfig empty() {
99
    return EMPTY_CONFIG;
1✔
100
  }
101

102
  public void save(@NotNull Element parentElement) {
103
    var container = new Element(TAG_NAME);
1✔
104
    XmlSerializer.serializeObjectInto(this, container);
1✔
105
    parentElement.addContent(container);
1✔
106
  }
1✔
107

108
  @Contract("null->null")
109
  public static @Nullable PerlConfig from(@Nullable Sdk sdk) {
110
    var sdkAdditionalData = PerlSdkAdditionalData.from(sdk);
1✔
111
    return sdkAdditionalData == null ? null : sdkAdditionalData.getConfig();
1✔
112
  }
113

114
  public static void init(@Nullable Sdk sdk) {
115
    var currentConfig = from(sdk);
1✔
116
    if (currentConfig == null || !currentConfig.isEmpty() || INIT_KEY.get(sdk) != null) {
1✔
117
      return;
1✔
118
    }
119
    try {
120
      INIT_KEY.set(sdk, true);
1✔
121
      PerlConfig perlConfig = ProgressManager.getInstance().runProcessWithProgressSynchronously(
1✔
122
        () -> readConfig(sdk), PerlBundle.message("dialog.title.reading.perl.config"), false, null
1✔
123
      );
124
      if (perlConfig != null) {
1✔
125
        var sdkModificator = sdk.getSdkModificator();
1✔
126
        PerlSdkAdditionalData.notNullFrom(sdkModificator).setConfig(perlConfig);
1✔
127
        ApplicationManager.getApplication().invokeAndWait(()-> WriteAction.run(sdkModificator::commitChanges));
1✔
128
      }
129
    }
130
    finally {
131
      INIT_KEY.set(sdk, null);
1✔
132
    }
133
  }
1✔
134

135
  private static @Nullable PerlConfig readConfig(@NotNull Sdk sdk) {
136
    var configOutput =
1✔
137
      PerlRunUtil.getOutputFromPerl(sdk, "-MConfig", "-e", "print \"$_\\1$Config{$_}\\0\" for grep {$Config{$_}} sort keys %Config");
1✔
138
    if (configOutput.isEmpty()) {
1✔
UNCOV
139
      LOG.warn("Error reading config for " + sdk);
×
140
      return null;
×
141
    }
142
    var configData = new HashMap<String, String>();
1✔
143
    var configPairs = StringUtil.split(String.join("", configOutput), "\0");
1✔
144
    for (String configPair : configPairs) {
1✔
145
      var keyValPair = StringUtil.split(configPair, "\1");
1✔
146
      if (keyValPair.size() != 2) {
1✔
UNCOV
147
        LOG.warn("Unexpected parsing of key/value pair: " + keyValPair);
×
148
        continue;
×
149
      }
150
      configData.put(keyValPair.get(0), keyValPair.get(1));
1✔
151
    }
1✔
152
    return intern(new PerlConfig(configData));
1✔
153
  }
154

155
  @TestOnly
156
  public static PerlConfig create(@NotNull Map<String, String> dataMap) {
157
    return intern(new PerlConfig(dataMap));
1✔
158
  }
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc