Skip to content

Commit

Permalink
Working automatic composer require on add ss ref. Fixes for update …
Browse files Browse the repository at this point in the history
…reference.
  • Loading branch information
Layoric committed Oct 19, 2023
1 parent 29de49f commit 0248ad6
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.servicestack.idea;

import com.intellij.codeInsight.intention.impl.QuickEditAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
Expand Down Expand Up @@ -49,7 +50,15 @@ public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile psiF

@Override
public void invoke(@NotNull Project project, Editor editor, final PsiFile psiFile) throws IncorrectOperationException {
UpdateServiceStackUtils.updateServiceStackReference(psiFile);
// First check if on write thread
if (ApplicationManager.getApplication().isWriteAccessAllowed()) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
UpdateServiceStackUtils.updateServiceStackReference(psiFile);
}
});
}
}

@Override
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/net/servicestack/idea/UpdateServiceStackUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import net.servicestack.idea.common.Analytics;
import net.servicestack.idea.common.INativeTypesHandler;
import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -102,21 +104,7 @@ public static void updateServiceStackReference(PsiFile psiFile) {
serverUrl.append(option.getKey()).append("=").append(option.getValue().trim().replaceAll("\\u0020", ""));
count++;
}
URL javaCodeUrl = new URL(serverUrl.toString());

URLConnection javaCodeConnection = javaCodeUrl.openConnection();
javaCodeConnection.setRequestProperty("content-type", "application/json; charset=utf-8");
BufferedReader javaCodeBufferReader = new BufferedReader(
new InputStreamReader(
javaCodeConnection.getInputStream()));
String javaCodeInput;
StringBuilder javaCodeResponse = new StringBuilder();
while ((javaCodeInput = javaCodeBufferReader.readLine()) != null) {
javaCodeResponse.append(javaCodeInput);
//All documents inside IntelliJ IDEA always use \n line separators.
//http://confluence.jetbrains.net/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview
javaCodeResponse.append("\n");
}
StringBuilder javaCodeResponse = getJavaCodeResponse(serverUrl);

String javaCode = javaCodeResponse.toString();
if (!javaCode.startsWith(nativeTypesHandler.getOptionsCommentStart())) {
Expand All @@ -143,6 +131,26 @@ public static void updateServiceStackReference(PsiFile psiFile) {
}
}

@NotNull
private static StringBuilder getJavaCodeResponse(StringBuilder serverUrl) throws IOException {
URL javaCodeUrl = new URL(serverUrl.toString());

URLConnection javaCodeConnection = javaCodeUrl.openConnection();
javaCodeConnection.setRequestProperty("content-type", "application/json; charset=utf-8");
BufferedReader javaCodeBufferReader = new BufferedReader(
new InputStreamReader(
javaCodeConnection.getInputStream()));
String javaCodeInput;
StringBuilder javaCodeResponse = new StringBuilder();
while ((javaCodeInput = javaCodeBufferReader.readLine()) != null) {
javaCodeResponse.append(javaCodeInput);
//All documents inside IntelliJ IDEA always use \n line separators.
//http://confluence.jetbrains.net/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview
javaCodeResponse.append("\n");
}
return javaCodeResponse;
}

public static String combinePath(String path, String segment) {
if (path == null || path.isEmpty()) {
return "/" + segment;
Expand Down
55 changes: 25 additions & 30 deletions src/main/java/net/servicestack/idea/php/AddPhpRefHandler.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package net.servicestack.idea.php;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.jetbrains.php.composer.ComposerDataService;
import com.jetbrains.php.composer.addDependency.ComposerPackage;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.ParseException;
import net.servicestack.idea.common.Analytics;
import net.servicestack.idea.common.DialogErrorMessages;
import net.servicestack.idea.common.IDEAUtils;
import net.servicestack.idea.common.INativeTypesHandler;
import com.jetbrains.php.composer.actions.ComposerInstallAction;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -76,7 +83,8 @@ private static void tryUpdateComposerJson(Module module, StringBuilder errorMess
// This is very inconsistent and doesn't always work
// Editing the `composer.json` manually causes errors from `composer`.
// Leaving this out until I can figure out a better way to do this.
// installPackage(module);
installPackage(module);

} catch (IOException | ParseException e) {
errorMessage.append(e.getMessage());
e.printStackTrace();
Expand All @@ -85,35 +93,22 @@ private static void tryUpdateComposerJson(Module module, StringBuilder errorMess
}
}

// public static void installPackage(Module module) {
// // Retrieve the module's root manager
// ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
//
// // Retrieve the module's content roots
// VirtualFile[] roots = moduleRootManager.getContentRoots();
//
// if (roots.length == 0) {
// Logger.getInstance(AddPhpRefHandler.class).warn("No content roots found");
// return;
// }
//
// VirtualFile root = roots[0]; // Assuming first content root is the main project root
// String projectBasePath = root.getPath();
//
// ApplicationManager.getApplication().executeOnPooledThread(() -> {
// GeneralCommandLine commandLine = new GeneralCommandLine();
// commandLine.withWorkDirectory(projectBasePath);
// commandLine.setExePath("composer");
// commandLine.addParameter("require");
// commandLine.addParameter("servicestack/client");
//
// try {
// commandLine.createProcess();
// } catch (Exception e) {
// Logger.getInstance(AddPhpRefHandler.class).error(e);
// }
// });
// }
public static void installPackage(Module module) {
Project project = module.getProject();
ComposerDataService composerDataService = project.getService(ComposerDataService.class);
VirtualFile configFile = composerDataService.getConfigFile();
if(configFile == null) {
return;
}
ComposerPackageManager composerPackageManager = new ComposerPackageManager(project);
ComposerPackageManager.DependentPackage dependentPackage = ComposerPackageManager.DependentPackage.SERVICESTACK_CLIENT;
ComposerPackage composerPackage = composerPackageManager.findPackage(dependentPackage);
if (composerPackage == null) {
composerPackageManager.installPackage(dependentPackage, configFile);
}
}



private static List<String> getDtoLines(String addressUrl, INativeTypesHandler nativeTypesHandler,
StringBuilder errorMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.servicestack.idea.php;

import com.intellij.execution.process.CapturingProcessAdapter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.php.composer.ComposerDataService;
import com.jetbrains.php.composer.ComposerUtils;
import com.jetbrains.php.composer.actions.ComposerCommandExecutor;
import com.jetbrains.php.composer.actions.ComposerCommandRunner;
import com.jetbrains.php.composer.actions.ComposerUpdateAction;
import com.jetbrains.php.composer.addDependency.ComposerPackage;
import com.jetbrains.php.composer.addDependency.ComposerPackagesUtil;
import com.jetbrains.php.composer.execution.ComposerExecution;
import com.jetbrains.php.composer.statistics.ComposerActionStatistics;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ComposerPackageManager {

private final Project project;

public ComposerPackageManager(Project project) {
this.project = project;
}

public ComposerPackage findPackage(DependentPackage dependentPackage) {
ComposerExecution execution = ComposerDataService.getInstance(project).getComposerExecution();
ComposerCommandRunner executor = new ComposerCommandRunner(execution, project, project.getBasePath(), new ComposerProgressIndicator());
CapturingProcessAdapter outputCapturingAdapter = new CapturingProcessAdapter();
List<String> command = Arrays.asList("show", dependentPackage.getName(), "--no-ansi", "-d", project.getBasePath());
ComposerCommandRunner.ExecutionResult executionResult = executor.runCommand(command, outputCapturingAdapter);
if (executionResult.myProgressIndicatorCancelled || !executionResult.isSuccess()) {
return null;
}

try {
return ComposerPackagesUtil.parsePackageDescriptionCommandOutput(dependentPackage.getName(), outputCapturingAdapter.getOutput().getStdout());
} catch (IOException e) {
return null;
}
}

public void installPackage(DependentPackage dependentPackage, VirtualFile configFile) {
ComposerCommandExecutor commandExecutor = ComposerUpdateAction.createExecutor(
project,
configFile,
"--no-interaction --no-ansi -d " + project.getBasePath(),
"",
ComposerActionStatistics.Action.REQUIRE,
ComposerUtils.getInstallationCommand(dependentPackage.getName(), dependentPackage.getVersion()));
commandExecutor.execute();
}

public enum DependentPackage {
SERVICESTACK_CLIENT("servicestack/client", "");
private final String name;
private final String version;

DependentPackage(String name, String version) {
this.name = name;
this.version = version;
}

public String getName() {
return name;
}

public String getVersion() {
return version;
}
}
}
116 changes: 116 additions & 0 deletions src/main/java/net/servicestack/idea/php/ComposerProgressIndicator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package net.servicestack.idea.php;

import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ComposerProgressIndicator implements ProgressIndicator {
@Override
public void start() {

}

@Override
public void stop() {

}

@Override
public boolean isRunning() {
return false;
}

@Override
public void cancel() {

}

@Override
public boolean isCanceled() {
return false;
}


@Override
public void setText(@NlsContexts.ProgressText String text) {

}

@Override
public @NlsContexts.ProgressText String getText() {
return null;
}

@Override
public void setText2(@NlsContexts.ProgressDetails String text) {

}

@Override
public @NlsContexts.ProgressDetails String getText2() {
return null;
}

@Override
public double getFraction() {
return 0;
}

@Override
public void setFraction(double fraction) {

}

@Override
public void pushState() {

}

@Override
public void popState() {

}

@Override
public boolean isModal() {
return false;
}

@Override
public @NotNull ModalityState getModalityState() {
return null;
}

@Override
public void setModalityProgress(@Nullable ProgressIndicator modalityProgress) {

}

@Override
public boolean isIndeterminate() {
return false;
}

@Override
public void setIndeterminate(boolean indeterminate) {

}

@Override
public void checkCanceled() throws ProcessCanceledException {

}

@Override
public boolean isPopupWasShown() {
return false;
}

@Override
public boolean isShowing() {
return false;
}
}

0 comments on commit 0248ad6

Please sign in to comment.