Skip to content

Commit

Permalink
Got for 55
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Jul 10, 2024
1 parent 0a07390 commit 433621d
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@
*/
package org.sonatype.maven.polyglot;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelProcessor;
Expand All @@ -31,11 +26,10 @@
import org.apache.maven.model.io.ModelReader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.eclipse.sisu.Priority;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.codehaus.plexus.util.ReaderFactory;

/**
* Polyglot model processor.
Expand All @@ -44,140 +38,140 @@
*
* @since 0.7
*/
//@Component(role = ModelProcessor.class, hint = "tesla-polyglot")
// @Component(role = ModelProcessor.class, hint = "tesla-polyglot")
@Singleton
@Named
@Priority(10)
public class TeslaModelProcessor implements ModelProcessor {

private static final String DEFAULT_POM_FILE = "pom.xml";
private static final String NEW_LINE = System.getProperty("line.separator");
private static final String WARNING = "?>" + NEW_LINE + "<!--" +
NEW_LINE + "" +
NEW_LINE + "" +
NEW_LINE + "DO NOT MODIFY - GENERATED CODE" +
NEW_LINE + "" +
NEW_LINE + "" +
NEW_LINE + "-->";
private static final String POM_FILE_PREFIX = ".polyglot.";

@Inject
private PolyglotModelManager manager;

private Logger log = LoggerFactory.getLogger( TeslaModelProcessor.class );

@Override
public File locatePom(final File dir) {
assert manager != null;

File pomFile = manager.findPom(dir);
if (pomFile == null) {
return new File(dir, DEFAULT_POM_FILE);
}
if (pomFile.getName().equals(DEFAULT_POM_FILE) && pomFile.getParentFile().equals(dir)) {
// behave like proper maven in case there is no pom from manager
return pomFile;
}
File polyglotPomFile = new File(pomFile.getParentFile(), POM_FILE_PREFIX + pomFile.getName());
try {
if (!polyglotPomFile.exists() && polyglotPomFile.createNewFile()) {
polyglotPomFile.deleteOnExit();
}
} catch (IOException e) {
throw new RuntimeException("error creating empty file", e);
private static final String DEFAULT_POM_FILE = "pom.xml";
private static final String NEW_LINE = System.getProperty("line.separator");
private static final String WARNING = "?>" + NEW_LINE + "<!--" + NEW_LINE
+ "" + NEW_LINE
+ "" + NEW_LINE
+ "DO NOT MODIFY - GENERATED CODE" + NEW_LINE
+ "" + NEW_LINE
+ "" + NEW_LINE
+ "-->";
private static final String POM_FILE_PREFIX = ".polyglot.";

@Inject
private PolyglotModelManager manager;

private Logger log = LoggerFactory.getLogger(TeslaModelProcessor.class);

@Override
public File locatePom(final File dir) {
assert manager != null;

File pomFile = manager.findPom(dir);
if (pomFile == null) {
return new File(dir, DEFAULT_POM_FILE);
}
if (pomFile.getName().equals(DEFAULT_POM_FILE)
&& pomFile.getParentFile().equals(dir)) {
// behave like proper maven in case there is no pom from manager
return pomFile;
}
File polyglotPomFile = new File(pomFile.getParentFile(), POM_FILE_PREFIX + pomFile.getName());
try {
if (!polyglotPomFile.exists() && polyglotPomFile.createNewFile()) {
polyglotPomFile.deleteOnExit();
}
} catch (IOException e) {
throw new RuntimeException("error creating empty file", e);
}
return polyglotPomFile;
}
return polyglotPomFile;
}

@Override
public Model read(final File input, final Map<String, ?> options) throws IOException, ModelParseException {
Model model;
@Override
public Model read(final File input, final Map<String, ?> options) throws IOException, ModelParseException {
Model model;

try (Reader reader = ReaderFactory.newXmlReader(input)) {
model = read(reader, options);
model.setPomFile(input);
try (Reader reader = ReaderFactory.newXmlReader(input)) {
model = read(reader, options);
model.setPomFile(input);
}
return model;
}
return model;
}

@Override
public Model read(final InputStream input, final Map<String, ?> options) throws IOException, ModelParseException {
try (Reader reader = ReaderFactory.newXmlReader(input)) {
return read(reader, options);
}
}

@Override
@SuppressWarnings({
"unchecked", "rawtypes"
})
public Model read(final Reader input, final Map<String, ?> options) throws IOException, ModelParseException {
assert manager != null;
Optional<File> optionalPomXml = getPomXmlFile(options);
if (optionalPomXml.isPresent()) {
File pom = optionalPomXml.get();
log.debug(pom.toString());
File realPom = new File(pom.getPath().replaceFirst(Pattern.quote(POM_FILE_PREFIX), ""));

((Map) options).put(ModelProcessor.SOURCE, new FileModelSource(realPom));

ModelReader reader = manager.getReaderFor(options);
Model model = reader.read(realPom, options);
PolyglotPropertiesEnhancer.enhanceModel(manager.getEnhancementPropertiesFor(options), model);
MavenXpp3Writer xmlWriter = new MavenXpp3Writer();
StringWriter xml = new StringWriter();
xmlWriter.write(xml, model);

FileUtils.fileWrite(pom, xml.toString());

// dump pom if filename is given via the pom properties
String dump = model.getProperties().getProperty("polyglot.dump.pom");
if (dump == null) {
// just nice to dump the pom.xml via commandline switch
dump = System.getProperty("polyglot.dump.pom");
}
if (dump != null) {
File dumpPom = new File(pom.getParentFile(), dump);
if (!dumpPom.exists() || !FileUtils.fileRead(dumpPom).equals(xml.toString().replace("?>", WARNING))) {
dumpPom.setWritable(true);
FileUtils.fileWrite(dumpPom, xml.toString().replace("?>", WARNING));
if ("true".equals(model.getProperties().getProperty("polyglot.dump.readonly"))) {
dumpPom.setReadOnly();
}
@Override
public Model read(final InputStream input, final Map<String, ?> options) throws IOException, ModelParseException {
try (Reader reader = ReaderFactory.newXmlReader(input)) {
return read(reader, options);
}
}
}

model.setPomFile(pom);
return model;
} else {
ModelReader reader = manager.getReaderFor(options);
return reader.read(input, options);
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Model read(final Reader input, final Map<String, ?> options) throws IOException, ModelParseException {
assert manager != null;
Optional<File> optionalPomXml = getPomXmlFile(options);
if (optionalPomXml.isPresent()) {
File pom = optionalPomXml.get();
log.debug(pom.toString());
File realPom = new File(pom.getPath().replaceFirst(Pattern.quote(POM_FILE_PREFIX), ""));

((Map) options).put(ModelProcessor.SOURCE, new FileModelSource(realPom));

ModelReader reader = manager.getReaderFor(options);
Model model = reader.read(realPom, options);
PolyglotPropertiesEnhancer.enhanceModel(manager.getEnhancementPropertiesFor(options), model);
MavenXpp3Writer xmlWriter = new MavenXpp3Writer();
StringWriter xml = new StringWriter();
xmlWriter.write(xml, model);

FileUtils.fileWrite(pom, xml.toString());

// dump pom if filename is given via the pom properties
String dump = model.getProperties().getProperty("polyglot.dump.pom");
if (dump == null) {
// just nice to dump the pom.xml via commandline switch
dump = System.getProperty("polyglot.dump.pom");
}
if (dump != null) {
File dumpPom = new File(pom.getParentFile(), dump);
if (!dumpPom.exists()
|| !FileUtils.fileRead(dumpPom).equals(xml.toString().replace("?>", WARNING))) {
dumpPom.setWritable(true);
FileUtils.fileWrite(dumpPom, xml.toString().replace("?>", WARNING));
if ("true".equals(model.getProperties().getProperty("polyglot.dump.readonly"))) {
dumpPom.setReadOnly();
}
}
}

model.setPomFile(pom);
return model;
} else {
ModelReader reader = manager.getReaderFor(options);
return reader.read(input, options);
}
}
}

private Optional<File> getPomXmlFile(Map<String, ?> options) {
ModelSource source = (ModelSource) options.get(ModelProcessor.SOURCE);
if (source != null) {
return getPomXmlFile(new File(source.getLocation()));
private Optional<File> getPomXmlFile(Map<String, ?> options) {
ModelSource source = (ModelSource) options.get(ModelProcessor.SOURCE);
if (source != null) {
return getPomXmlFile(new File(source.getLocation()));
}
return Optional.empty();
}
return Optional.empty();
}

Optional<File> getPomXmlFile(File sourceFile) {
String filename = sourceFile.getName();
if (filename.startsWith(POM_FILE_PREFIX)) {
return Optional.of(sourceFile);
} else if (!filename.equals("pom.xml") && !filename.endsWith(".pom")) {
File parent = sourceFile.getParentFile();
if (parent == null) {
// "virtual" model
return Optional.empty();
}
File pom = locatePom(parent);
if (pom.getName().startsWith(POM_FILE_PREFIX)) {
return Optional.of(pom);
}

Optional<File> getPomXmlFile(File sourceFile) {
String filename = sourceFile.getName();
if (filename.startsWith(POM_FILE_PREFIX)) {
return Optional.of(sourceFile);
} else if (!filename.equals("pom.xml") && !filename.endsWith(".pom")) {
File parent = sourceFile.getParentFile();
if (parent == null) {
// "virtual" model
return Optional.empty();
}
File pom = locatePom(parent);
if (pom.getName().startsWith(POM_FILE_PREFIX)) {
return Optional.of(pom);
}
}
return Optional.empty();
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.sonatype.maven.polyglot;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import static java.util.Objects.requireNonNull;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.project.*;
import org.eclipse.sisu.Priority;

import static java.util.Objects.requireNonNull;

@Singleton
@Named
@Priority(10)
Expand All @@ -37,22 +35,26 @@ public ProjectBuildingResult build(File pomFile, ProjectBuildingRequest request)
}

@Override
public ProjectBuildingResult build(ModelSource modelSource, ProjectBuildingRequest request) throws ProjectBuildingException {
public ProjectBuildingResult build(ModelSource modelSource, ProjectBuildingRequest request)
throws ProjectBuildingException {
return convert(defaultProjectBuilder.build(modelSource, request));
}

@Override
public ProjectBuildingResult build(Artifact artifact, ProjectBuildingRequest request) throws ProjectBuildingException {
public ProjectBuildingResult build(Artifact artifact, ProjectBuildingRequest request)
throws ProjectBuildingException {
return convert(defaultProjectBuilder.build(artifact, request));
}

@Override
public ProjectBuildingResult build(Artifact artifact, boolean allowStubModel, ProjectBuildingRequest request) throws ProjectBuildingException {
public ProjectBuildingResult build(Artifact artifact, boolean allowStubModel, ProjectBuildingRequest request)
throws ProjectBuildingException {
return convert(defaultProjectBuilder.build(artifact, allowStubModel, request));
}

@Override
public List<ProjectBuildingResult> build(List<File> pomFiles, boolean recursive, ProjectBuildingRequest request) throws ProjectBuildingException {
public List<ProjectBuildingResult> build(List<File> pomFiles, boolean recursive, ProjectBuildingRequest request)
throws ProjectBuildingException {
List<ProjectBuildingResult> results = defaultProjectBuilder.build(pomFiles, recursive, request);
return results.stream().map(this::convert).collect(Collectors.toList());
}
Expand Down Expand Up @@ -81,11 +83,12 @@ private static class TeslaProjectBuildingResult implements ProjectBuildingResult
private final List<ModelProblem> problems;
private final DependencyResolutionResult dependencyResolutionResult;

public TeslaProjectBuildingResult(String projectId,
File pomFile,
MavenProject project,
List<ModelProblem> problems,
DependencyResolutionResult dependencyResolutionResult) {
public TeslaProjectBuildingResult(
String projectId,
File pomFile,
MavenProject project,
List<ModelProblem> problems,
DependencyResolutionResult dependencyResolutionResult) {
this.projectId = projectId;
this.pomFile = pomFile;
this.project = project;
Expand Down
Loading

0 comments on commit 433621d

Please sign in to comment.