Slight refactorings and remove maven-model dependency (#1365) (#1366)

* trying to allow manual trigger for publishing vscode pre-releases

* remove usage of proposed chat variable resolver api for now

* remove maven-model dependency and clean up code

---------

Co-authored-by: Udayani Vaka <79973862+vudayani@users.noreply.github.com>
Co-authored-by: Martin Lippert <martin.lippert@broadcom.com>
This commit is contained in:
Alex Boyko
2024-10-03 08:36:38 -04:00
committed by GitHub
parent 53dbdeaaec
commit 19e3b38804
8 changed files with 12 additions and 107 deletions

View File

@@ -117,7 +117,6 @@ public class ORDocUtils {
log.error("Diff conversion failed", ex);
}
}
System.out.println("edits "+ edit);
return Optional.of(edit);
}
return Optional.empty();
@@ -170,7 +169,6 @@ public class ORDocUtils {
createDeleteFileEdit(docUri, we);
} else {
String docUri = result.getBefore().getSourcePath().toUri().toASCIIString();
System.out.println(result.getBefore().getSourcePath().toString());
createUpdateFileEdit(documents, docUri, result.getBefore().printAll(), result.getAfter().printAll(), changeAnnotationId, we);
}

View File

@@ -210,16 +210,6 @@
<artifactId>commonmark</artifactId>
<version>0.22.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.8.1</version>
</dependency>
<!-- <dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>

View File

@@ -63,8 +63,7 @@ public class CopilotAgentCommandHandler {
private CompletableFuture<WorkspaceEdit> createLspEdits(ExecuteCommandParams params) throws IOException {
log.info("Command Handler for lsp edits: ");
String docURI = ((JsonElement) params.getArguments().get(0)).getAsString();
String path = ((JsonElement) params.getArguments().get(0)).getAsString();
String content = ((JsonElement) params.getArguments().get(2)).getAsString();
String content = ((JsonElement) params.getArguments().get(1)).getAsString();
IJavaProject project = this.projectFinder.find(new TextDocumentIdentifier(docURI)).get();
List<ProjectArtifact> projectArtifacts = computeProjectArtifacts(content);

View File

@@ -19,15 +19,12 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.eclipse.lsp4j.ChangeAnnotation;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.openrewrite.Result;
import org.openrewrite.xml.tree.Xml;
import org.springframework.ide.vscode.boot.java.copilot.InjectMavenActionHandler.MavenDependencyMetadata;
import org.springframework.ide.vscode.boot.java.copilot.util.ClassNameExtractor;
import org.springframework.ide.vscode.boot.java.copilot.util.PomReader;
import org.springframework.ide.vscode.boot.java.copilot.util.PropertyFileUtils;
import org.springframework.ide.vscode.boot.java.copilot.util.SpringCliException;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
@@ -40,12 +37,6 @@ public class ProjectArtifactEditGenerator {
private final Path projectPath;
private final String readmeFileName;
private final Pattern compiledGroupIdPattern;
private final Pattern compiledArtifactIdPattern;
private final SimpleTextDocumentService simpleTextDocumentService;
public ProjectArtifactEditGenerator(SimpleTextDocumentService simpleTextDocumentService,
@@ -53,9 +44,6 @@ public class ProjectArtifactEditGenerator {
this.simpleTextDocumentService = simpleTextDocumentService;
this.projectArtifacts = projectArtifacts;
this.projectPath = projectPath;
this.readmeFileName = readmeFileName;
compiledGroupIdPattern = Pattern.compile("<groupId>(.*?)</groupId>");
compiledArtifactIdPattern = Pattern.compile("<artifactId>(.*?)</artifactId>");
}
public ProcessArtifactResult<WorkspaceEdit> process() throws IOException {
@@ -136,24 +124,19 @@ public class ProjectArtifactEditGenerator {
private void writeMavenDependencies(ProjectArtifact projectArtifact, Path projectPath, String changeAnnotationId,
WorkspaceEdit we) {
PomReader pomReader = new PomReader();
Path currentProjectPomPath = this.projectPath.resolve("pom.xml");
if (Files.notExists(currentProjectPomPath)) {
throw new SpringCliException("Could not find pom.xml in " + this.projectPath
+ ". Make sure you are running the command in the project's root directory.");
}
Model currentModel = pomReader.readPom(currentProjectPomPath.toFile());
List<Dependency> currentDependencies = currentModel.getDependencies();
InjectMavenActionHandler injectMavenActionHandler = new InjectMavenActionHandler(null, new HashMap<>(),
projectPath);
// Move the parsing to injectMavenActionHandler
List<Xml.Document> xmlDocuments = injectMavenActionHandler.parseToXml(projectArtifact.getText());
for (Xml.Document xmlDocument : xmlDocuments) {
MavenDependencyMetadata dep = injectMavenActionHandler.findMavenDependencyTags(xmlDocument);
if (!candidateDependencyAlreadyPresent(dep, currentDependencies)) {
injectMavenActionHandler.injectDependency(dep);
}
injectMavenActionHandler.injectDependency(dep);
}
List<Result> res = injectMavenActionHandler.run().getChangeset().getAllResults();
@@ -164,23 +147,6 @@ public class ProjectArtifactEditGenerator {
}
}
private boolean candidateDependencyAlreadyPresent(MavenDependencyMetadata dep,
List<Dependency> currentDependencies) {
String candidateGroupId = dep.groupId();
String candidateArtifactId = dep.artifactId();
boolean candidateDependencyAlreadyPresent = false;
for (Dependency currentDependency : currentDependencies) {
String currentGroupId = currentDependency.getGroupId();
String currentArtifactId = currentDependency.getArtifactId();
if (candidateGroupId.equals(currentGroupId) && candidateArtifactId.equals(currentArtifactId)) {
candidateDependencyAlreadyPresent = true;
break;
}
}
return candidateDependencyAlreadyPresent;
}
private void writeApplicationProperties(ProjectArtifact projectArtifact, Path projectPath,
String changeAnnotationId, WorkspaceEdit we) throws IOException {
Path applicationPropertiesPath = projectPath.resolve("src").resolve("main").resolve("resources")

View File

@@ -1,45 +0,0 @@
package org.springframework.ide.vscode.boot.java.copilot.util;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* @author Ryan Baxter
*/
public class PomReader {
/**
* Returns a parsed POM.
*/
public Model readPom(File file) {
File pom = file;
if (file.isDirectory()) {
pom = new File(file, "pom.xml");
}
if (!pom.exists()) {
return null;
}
String fileText = "";
try (Reader reader = new FileReader(pom)) {
if (file.isFile()) {
fileText = new String(Files.readAllBytes(file.toPath()));
}
MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
return xpp3Reader.read(reader);
}
catch (XmlPullParserException | IOException ex) {
if (file.isFile() && fileText.length() == 0) {
throw new IllegalStateException("File [" + pom.getAbsolutePath() + "] is empty", ex);
}
throw new IllegalStateException("Failed to read file: " + pom.getAbsolutePath(), ex);
}
}
}

View File

@@ -56,7 +56,6 @@ import org.openrewrite.config.RecipeDescriptor;
import org.openrewrite.config.YamlResourceLoader;
import org.openrewrite.internal.InMemoryLargeSourceSet;
import org.openrewrite.java.JavaParser;
import org.openrewrite.maven.AddDependency;
import org.openrewrite.maven.MavenParser;
import org.openrewrite.tree.ParseError;
import org.slf4j.Logger;

View File

@@ -21,7 +21,7 @@ export async function applyLspEdit(uri: Uri) {
}, async (progress, cancellation) => {
progress.report({ message: "applying edits..." });
const fileContent = (await readResponseFromFile(uri)).toString();
const lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), path.dirname(uri.fsPath), fileContent);
const lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), fileContent);
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);

View File

@@ -65,23 +65,21 @@ export default class SpringBootChatAgent {
// Chat request to copilot LLM
const response = await this.copilotRequest.chatRequest(messages, {}, cancellationToken);
// write the response to markdown file
const targetMarkdownUri = await writeResponseToFile(response, bootProjInfo.name, selectedProject.fsPath);
let documentContent;
if (!targetMarkdownUri) {
documentContent = 'Note: The code provided is just an example and may not be suitable for production use. \n ' + response;
if (response == null || response === '') {
documentContent = 'Failed to process the request. Please try again.';
} else {
// modify the response from copilot LLM i.e. make response Boot 3 compliant if necessary
if (bootProjInfo.springBootVersion.startsWith('3')) {
const enhancedResponse = await commands.executeCommand("sts/copilot/agent/enhanceResponse", response) as string;
await writeResponseToFile(enhancedResponse, bootProjInfo.name, selectedProject.fsPath);
documentContent = await commands.executeCommand("sts/copilot/agent/enhanceResponse", response) as string;
} else {
documentContent = 'Note: The code provided is just an example and may not be suitable for production use. \n ' + response;
}
documentContent = await workspace.fs.readFile(targetMarkdownUri);
}
// write the final response to markdown file
await writeResponseToFile(documentContent, bootProjInfo.name, selectedProject.fsPath);
const chatResponse = Buffer.from(documentContent).toString();
stream.markdown(chatResponse);
stream.button({