Skip to content

Commit 92c8a5b

Browse files
authored
Merge pull request #57 from php-perfect/deployment-mapping
Add deployment configuration for the ddev mount
2 parents 889d533 + 44e42eb commit 92c8a5b

File tree

11 files changed

+237
-11
lines changed

11 files changed

+237
-11
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
org.gradle.caching=true
2-
org.gradle.jvmargs=-Xmx3G -XX:MaxPermSize=2G -XX:MaxMetaspaceSize=1G
2+
org.gradle.jvmargs=-Xmx3G -XX:MaxMetaspaceSize=1G

src/main/java/de/php_perfect/intellij/ddev/cmd/Description.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,25 @@ public enum Status {
3737
@SerializedName("dbinfo")
3838
private final @Nullable DatabaseInfo databaseInfo;
3939

40+
@SerializedName("primary_url")
41+
private final @Nullable String primaryUrl;
42+
4043
public Description(@Nullable String name, @Nullable String phpVersion, @Nullable Status status,
41-
@Nullable String mailHogHttpsUrl, @Nullable String mailHogHttpUrl, @Nullable DatabaseInfo databaseInfo) {
42-
this(name, phpVersion, status, mailHogHttpsUrl, mailHogHttpUrl, new HashMap<>(), databaseInfo);
44+
@Nullable String mailHogHttpsUrl, @Nullable String mailHogHttpUrl, @Nullable DatabaseInfo databaseInfo, @Nullable String primaryUrl) {
45+
this(name, phpVersion, status, mailHogHttpsUrl, mailHogHttpUrl, new HashMap<>(), databaseInfo, primaryUrl);
4346
}
4447

4548
public Description(@Nullable String name, @Nullable String phpVersion, @Nullable Status status,
4649
@Nullable String mailHogHttpsUrl, @Nullable String mailHogHttpUrl, Map<String, Service> services,
47-
@Nullable DatabaseInfo databaseInfo) {
50+
@Nullable DatabaseInfo databaseInfo, @Nullable String primaryUrl) {
4851
this.name = name;
4952
this.phpVersion = phpVersion;
5053
this.status = status;
5154
this.mailHogHttpsUrl = mailHogHttpsUrl;
5255
this.mailHogHttpUrl = mailHogHttpUrl;
5356
this.services = services;
5457
this.databaseInfo = databaseInfo;
58+
this.primaryUrl = primaryUrl;
5559
}
5660

5761
public @Nullable String getName() {
@@ -88,6 +92,10 @@ public Description(@Nullable String name, @Nullable String phpVersion, @Nullable
8892
return databaseInfo;
8993
}
9094

95+
public @Nullable String getPrimaryUrl() {
96+
return primaryUrl;
97+
}
98+
9199
@Override
92100
public boolean equals(Object o) {
93101
if (this == o) {
@@ -100,12 +108,14 @@ public boolean equals(Object o) {
100108
return Objects.equals(getName(), that.getName()) && Objects.equals(getPhpVersion(), that.getPhpVersion())
101109
&& getStatus() == that.getStatus() && Objects.equals(getMailHogHttpsUrl(), that.getMailHogHttpsUrl())
102110
&& Objects.equals(getMailHogHttpUrl(), that.getMailHogHttpUrl()) && Objects.equals(getServices(),
103-
that.getServices()) && Objects.equals(getDatabaseInfo(), that.getDatabaseInfo());
111+
that.getServices()) && Objects.equals(getDatabaseInfo(), that.getDatabaseInfo())
112+
&& Objects.equals(getPrimaryUrl(), that.getPrimaryUrl());
104113
}
105114

106115
@Override
107116
public int hashCode() {
108-
return Objects.hash(getName(), getPhpVersion(), getStatus(), getMailHogHttpsUrl(), getMailHogHttpUrl(), getServices(), getDatabaseInfo());
117+
return Objects.hash(getName(), getPhpVersion(), getStatus(), getMailHogHttpsUrl(), getMailHogHttpUrl(),
118+
getServices(), getDatabaseInfo(), getPrimaryUrl());
109119
}
110120

111121
@Override
@@ -118,6 +128,7 @@ public String toString() {
118128
", mailHogHttpUrl='" + mailHogHttpUrl + '\'' +
119129
", services=" + services +
120130
", databaseInfo=" + databaseInfo +
131+
", primaryUrl=" + primaryUrl +
121132
'}';
122133
}
123134
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package de.php_perfect.intellij.ddev.php.server;
2+
3+
import com.intellij.openapi.project.Project;
4+
import de.php_perfect.intellij.ddev.DescriptionChangedListener;
5+
import de.php_perfect.intellij.ddev.cmd.Description;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
11+
12+
public final class ConfigureServerListener implements DescriptionChangedListener {
13+
private final @NotNull Project project;
14+
15+
public ConfigureServerListener(@NotNull Project project) {
16+
this.project = project;
17+
}
18+
19+
@Override
20+
public void onDescriptionChanged(@Nullable Description description) {
21+
String localPath = this.project.getBasePath();
22+
23+
if (description == null || localPath == null) {
24+
return;
25+
}
26+
27+
if (description.getPrimaryUrl() == null) {
28+
return;
29+
}
30+
31+
URL url;
32+
try {
33+
url = new URL(description.getPrimaryUrl());
34+
} catch (MalformedURLException ignored) {
35+
return;
36+
}
37+
38+
ServerConfig serverConfig = new ServerConfig(localPath, "/var/www/html", url);
39+
ServerConfigManager.getInstance(this.project).configure(serverConfig);
40+
}
41+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package de.php_perfect.intellij.ddev.php.server;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.net.URL;
6+
import java.util.Objects;
7+
8+
public final class ServerConfig {
9+
private final @NotNull String localPath;
10+
11+
private final @NotNull String remotePath;
12+
13+
private final @NotNull URL url;
14+
15+
public ServerConfig(@NotNull String localPath, @NotNull String remotePathPath, @NotNull URL url) {
16+
this.localPath = localPath;
17+
this.remotePath = remotePathPath;
18+
this.url = url;
19+
}
20+
21+
public @NotNull String getLocalPath() {
22+
return localPath;
23+
}
24+
25+
public @NotNull String getRemotePath() {
26+
return remotePath;
27+
}
28+
29+
public @NotNull URL getUrl() {
30+
return url;
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
ServerConfig that = (ServerConfig) o;
38+
return getLocalPath().equals(that.getLocalPath()) && getRemotePath().equals(that.getRemotePath()) && getUrl().equals(that.getUrl());
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
return Objects.hash(getLocalPath(), getRemotePath(), getUrl());
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "ServerConfig{" +
49+
"localPath='" + localPath + '\'' +
50+
", remotePath='" + remotePath + '\'' +
51+
", url=" + url +
52+
'}';
53+
}
54+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.php_perfect.intellij.ddev.php.server;
2+
3+
import com.intellij.openapi.project.Project;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
public interface ServerConfigManager {
7+
void configure(@NotNull ServerConfig serverConfig);
8+
9+
static ServerConfigManager getInstance(@NotNull Project project) {
10+
return project.getService(ServerConfigManager.class);
11+
}
12+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.php_perfect.intellij.ddev.php.server;
2+
3+
import com.intellij.openapi.diagnostic.Logger;
4+
import com.intellij.openapi.project.Project;
5+
import com.intellij.openapi.util.text.StringUtil;
6+
import com.intellij.util.PathMappingSettings;
7+
import com.jetbrains.php.config.servers.PhpServer;
8+
import com.jetbrains.php.config.servers.PhpServersWorkspaceStateComponent;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import java.util.List;
12+
13+
public final class ServerConfigManagerImpl implements ServerConfigManager {
14+
private static final @NotNull Logger LOG = Logger.getInstance(ServerConfigManagerImpl.class);
15+
16+
private final static @NotNull String SERVER_NAME = "DDEV";
17+
18+
private final @NotNull Project project;
19+
20+
public ServerConfigManagerImpl(final @NotNull Project project) {
21+
this.project = project;
22+
}
23+
24+
public void configure(final @NotNull ServerConfig serverConfig) {
25+
LOG.debug(String.format("Updating server configuration %s", SERVER_NAME));
26+
this.addOrReplaceServer(
27+
PhpServersWorkspaceStateComponent.getInstance(project).getServers(),
28+
createServerConfiguration(serverConfig)
29+
);
30+
}
31+
32+
private @NotNull PhpServer createServerConfiguration(final @NotNull ServerConfig serverConfig) {
33+
final PhpServer phpServer = new PhpServer();
34+
phpServer.setName(SERVER_NAME);
35+
phpServer.setHost(serverConfig.getUrl().getHost());
36+
phpServer.setUsePathMappings(true);
37+
phpServer.getMappings().add(new PathMappingSettings.PathMapping(serverConfig.getLocalPath(), serverConfig.getRemotePath()));
38+
39+
return phpServer;
40+
}
41+
42+
private void addOrReplaceServer(final @NotNull List<PhpServer> servers, final @NotNull PhpServer server) {
43+
servers.removeIf(phpServer -> StringUtil.equals(phpServer.getName(), server.getName()));
44+
servers.add(server);
45+
}
46+
}

src/main/resources/META-INF/DdevIntegration-withPhp.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<depends>Docker</depends>
44

55
<extensions defaultExtensionNs="com.intellij">
6+
<projectService serviceImplementation="de.php_perfect.intellij.ddev.php.server.ServerConfigManagerImpl"
7+
serviceInterface="de.php_perfect.intellij.ddev.php.server.ServerConfigManager"/>
68
<projectService serviceImplementation="de.php_perfect.intellij.ddev.php.PhpInterpreterProviderImpl"
79
serviceInterface="de.php_perfect.intellij.ddev.php.PhpInterpreterProvider"/>
810
<projectService serviceImplementation="de.php_perfect.intellij.ddev.php.DdevComposeFileLoaderImpl"
@@ -16,6 +18,8 @@
1618
</extensions>
1719

1820
<projectListeners>
21+
<listener class="de.php_perfect.intellij.ddev.php.server.ConfigureServerListener"
22+
topic="de.php_perfect.intellij.ddev.DescriptionChangedListener"/>
1923
<listener class="de.php_perfect.intellij.ddev.php.AutoConfigurePhpInterpreterListener"
2024
topic="de.php_perfect.intellij.ddev.DescriptionChangedListener"/>
2125
</projectListeners>

src/test/java/de/php_perfect/intellij/ddev/cmd/DdevImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void version() throws CommandFailedException, IOException {
3333

3434
@Test
3535
public void describe() throws CommandFailedException, IOException {
36-
Description expected = new Description("acol", "8.1", Description.Status.STOPPED, null, null, new HashMap<>(), null);
36+
Description expected = new Description("acol", "8.1", Description.Status.STOPPED, null, null, new HashMap<>(), null, "https://acol.ddev.site");
3737

3838
ProcessOutput processOutput = new ProcessOutput(Files.readString(Path.of("src/test/resources/ddev_describe.json")), "", 0, false, false);
3939

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package de.php_perfect.intellij.ddev.php.server;
2+
3+
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
4+
import com.jetbrains.php.config.servers.PhpServer;
5+
import com.jetbrains.php.config.servers.PhpServersWorkspaceStateComponent;
6+
import org.junit.Assert;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
import java.util.List;
13+
import java.util.Objects;
14+
15+
final class ServerConfigManagerImplTest extends BasePlatformTestCase {
16+
17+
@Override
18+
@BeforeEach
19+
protected void setUp() throws Exception {
20+
super.setUp();
21+
}
22+
23+
@Test
24+
void configure() throws MalformedURLException {
25+
final ServerConfigManager serverConfigManager = ServerConfigManager.getInstance(this.getProject());
26+
27+
final ServerConfig serverConfig = new ServerConfig(
28+
Objects.requireNonNull(this.getProject().getBasePath()),
29+
"/var/www/html",
30+
new URL("https://test.ddev.site")
31+
);
32+
33+
serverConfigManager.configure(serverConfig);
34+
// Check server gets replaced
35+
serverConfigManager.configure(serverConfig);
36+
37+
assertServerConfigMatches(serverConfig);
38+
}
39+
40+
private void assertServerConfigMatches(ServerConfig serverConfig) {
41+
final List<PhpServer> servers = PhpServersWorkspaceStateComponent.getInstance(this.getProject()).getServers();
42+
43+
Assert.assertEquals(1, servers.size());
44+
45+
final PhpServer server = servers.get(0);
46+
Assert.assertEquals("DDEV", server.getName());
47+
Assert.assertEquals("test.ddev.site", server.getHost());
48+
49+
var mappings = server.getMappings();
50+
51+
Assert.assertEquals(1, mappings.size());
52+
53+
var mapping = mappings.get(0);
54+
55+
Assert.assertEquals(serverConfig.getLocalPath(), mapping.getLocalRoot());
56+
Assert.assertEquals(serverConfig.getRemotePath(), mapping.getRemoteRoot());
57+
}
58+
}

src/test/java/de/php_perfect/intellij/ddev/serviceActions/ServiceActionManagerImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ private Description aDescription(String name, Description.Status status) {
5858
var httpUrl = String.format("http://www.%s.com", name);
5959
var httpsUrl = String.format("https://www.%s.com", name);
6060

61-
return new Description(name, "7.4", status, httpsUrl, httpUrl, Map.of(name, new Service(name, httpsUrl, httpUrl)), dataBaseInfo);
61+
return new Description(name, "7.4", status, httpsUrl, httpUrl, Map.of(name, new Service(name, httpsUrl, httpUrl)), dataBaseInfo, null);
6262
}
6363
}

0 commit comments

Comments
 (0)