Deployment mapping's path update by plugin

Answered

I have a project with Git enabled as VCS. There is a deployment configuration defined for my project as you can see in the screenshot.


I work with many branches and I switch among them time to time. When I switch a branch, I then manually need to update these deployment mappings to upload my changes to the right branch hosted on my server as you see in the 2nd screenshot. Doing it manually is tedius task.

So what I did was: I wrote a powershell script which switchd the branch using git checkout command and then it modifies the deployment mappings settings in .idea/deployment.xml file and it is reflected on the Deployment Mapping screen correctly The problem is PHPStorm doesn't pick this change happened in the physical file unless I manually update it in the Deployment screen. As you can see in 3rd screenshot, it was uploading changes to the last path which was when it read the file last time.

To work around this, I wrote a plugin [Targets Intellij Ultimate & PHPStrom] to do so. But I can get it to work. Here is what I did:
- I registered a git branch change listener
- When the branch change happened, my powershell script had already updated the deployment.xml file
- I got this far with my plugin development that it receives the git branch update when I do so
- Here I used VirtualFileManager (VFS) to refresh the deployment.xml file, hoping it would inform PHPStrom to reflect the change, but it didn't work. Here is the code:

       VirtualFileManager vfs = VirtualFileManager.getInstance();
       Path relativePath = Paths.get(".idea", "deployment.xml");
       Path absolutePath = Paths.get(project.getBasePath(), relativePath.toString());

       if (!absolutePath.toFile().exists()) {
           return;
       }
       vfs.findFileByNioPath(absolutePath).refresh(false, false);

I also did other things which I don't think really necessary or even right!

       vfs.getFileSystem(null).refresh(false);
       vfs.syncRefresh();

0
10 comments

Hi,

I suggest trying a different approach to the problem. Instead of modifying deployment.xml externally by a script, consider modifying the mapping object directly. Try getting the com.jetbrains.plugins.webDeployment.config.PublishConfig service, find the mapping, and then modify it:

PublishConfig publishConfig = project.getService(PublishConfig.class);
// find a DeploymentPathMapping you want to modify
deploymentPathMapping.setLocalPath("local path");
deploymentPathMapping.setDeployPath("server path");
1

Thank you for your reply. Really appreciate.

I am facing a problem.
Cannot resolve symbol 'PublishConfig'

Do I need add any dependencies for this in build.gradle.kts file? If yes, could you please tell me in details how to do it? 

Thank you.

0

Yes, you should add a dependency on com.jetbrains.plugins.webDeployment plugin in the Gradle build script:

plugins.set(listOf(…, “com.jetbrains.plugins.webDeployment”))

and plugin.xml:

<depends>com.jetbrains.plugins.webDeployment</depends>

See: https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html for more information about adding dependencies.

1

Hi Karol,
I tried your approach, however found no luck! I have been able to update the deployment path mappings using PublishConfig. After changing the path using the plugin, I then modify a file and hit save[Ctrl + S], it then uploads to the last path!! Not to the updated one! Here is what I did:

PublishConfig config = project.getService(PublishConfig.class);
String defServerName = config.getDefaultServerOrGroupName();

PublishConfig.State state = config.getState();
PublishConfig.ServerData data = state.myNameServerData.get(defServerName);
List<DeploymentPathMapping> mappings = data.myPathMappings;

for (DeploymentPathMapping map : mappings) {
    map.setDeployPath(branchName);
    map.setWebPath(branchName);
}

config.loadState(state);

 

0

Hi,

Why do you call config.loadState(state)? It loads the data from the storage, so deployment.xml file. It overwrites any changes you did before.

1

Hello,
I followed your suggestion by removing this code:

config.loadState(state);

Didn't make any difference. When the above line is removed it doesn't update mappings in the UI!
To help you understand what I have done so far, I have included two zip files.

  1. Plugin source code https://drive.google.com/file/d/182YR5h8DtqJdAtED038_afir7W_WSPCR/view?usp=sharing
  2. Plugin itself https://drive.google.com/file/d/1Uiq_tG7_PtxH7GpHPm0fOLQt7PvVT7UO/view?usp=sharing

 

0

Hi,

I can't even check the project, as it fails on:

> Task :prepareKotlinBuildScriptModel UP-TO-DATE

FAILURE: Build failed with an exception.

* What went wrong:
A build operation failed.
   org/gradle/api/publish/ivy/internal/publication/DefaultIvyPublicationIdentity
org/gradle/api/publish/ivy/internal/publication/DefaultIvyPublicationIdentity
org.gradle.api.publish.ivy.internal.publication.DefaultIvyPublicationIdentity

Please prepare working plugin sources and describe the exact steps to reproduce the issue.

0

This can be closed.

0

Hi Abdul,

Could you please share the solution so other developers facing the same issue can use it?

1

Here is waht I did finally to update deployment mappings for default server and notify the IDE about that change to reflect:

// Get the PublishConfig
PublishConfig config = project.getService(PublishConfig.class); 

// Get the default serve rname 
String defServerName = config.getDefaultServerOrGroupName(); 

// Get mappings for default server & update the mappings
PublishConfig.State state = config.getState(); 
PublishConfig.ServerData data = state.myNameServerData.get(defServerName);

List<DeploymentPathMapping> mappings = data.myPathMappings;
for (DeploymentPathMapping map : mappings) {
    map.setDeployPath(branch);
    map.setWebPath(branch);
} 

// save the changes to deployment.xml file
config.loadState(state);

// Finally notify the IDE about this change to reflect! 
project.getMessageBus().syncPublisher(WebDeploymentTopics.DEPLOYMENT_CONFIG).optionsChanged();
1

Please sign in to leave a comment.