Skip to content

Commit

Permalink
Bug 553630 - invent a way to publish general purpose passage bundles
Browse files Browse the repository at this point in the history
 - make `pommod` be executable in java8 environment
 - exterior refactoring

Signed-off-by: eparovyshnaya <[email protected]>
  • Loading branch information
eparovyshnaya committed Aug 7, 2021
1 parent 11f06fb commit 233e4d0
Showing 1 changed file with 48 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,52 +40,35 @@

public final class CompletePom {

public static void main(String[] args) {
CompletePom instance = new CompletePom();
CmdLineParser parser = new CmdLineParser(instance);
try {
parser.parseArgument(args);
} catch (CmdLineException e) {
System.err.println(e.getMessage());
parser.printUsage(System.err);
return;
}
try {
instance.run();
} catch (IOException e) {
System.err.println(e.getMessage());
}
private final String year;
private File base;
private boolean dry;

public CompletePom() {
this.year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
}

@Option(name = "-dir", usage = "Sets the base directory to scan for POM files", required = true)
public void setBase(@NonNull File dir) {
this.base = dir;
}

@Option(name = "-dry", usage = "When set, files are not modified and result is dumped to sysout")
public void setDry(boolean dryRun) {
this.dry = dryRun;
}

private final String year;
private File base;
private boolean dry;

public CompletePom() {
this.year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
@Option(name = "-dry", usage = "When set, files are not modified and result is dumped to sysout")
public void setDry(boolean dry) {
this.dry = dry;
}

public void run() throws IOException {
System.out.println("Making POMs compliant for Maven Central."); //$NON-NLS-1$
System.out.println("Processing *.pom files in " + base.getAbsolutePath() + "..."); //$NON-NLS-1$ //$NON-NLS-2$
Files.find(base.toPath(), 20, (path, basicFileAttributes) -> path.toFile().getPath().endsWith(".pom")) //$NON-NLS-1$
.forEach(path -> enhancePOMFile(path));
.forEach(path -> enhancePomFile(path));
System.out.println("Done."); //$NON-NLS-1$
}

private void enhancePOMFile(Path path) {
private void enhancePomFile(Path path) {
boolean modified = false;
Model model = load(path.toFile());
Model model = loaded(path.toFile());

if (model.getName() == null) {
model.setName(model.getArtifactId());
Expand Down Expand Up @@ -122,12 +105,12 @@ private void enhancePOMFile(Path path) {
model.addDeveloper(developer);
modified = true;
}
String groupID = model.getGroupId();
String group = model.getGroupId();
String artifactID = model.getArtifactId();
String version = model.getVersion();
String description = model.getDescription();

boolean exists = exists(groupID, artifactID, version);
boolean exists = exists(group, artifactID, version);

if (modified || dry) {
if (modified) {
Expand All @@ -139,35 +122,35 @@ private void enhancePOMFile(Path path) {
if (!exists) {
String pomFileName = path.getFileName().toString();
Path publish = path.getParent().resolve(pomFileName.substring(0, pomFileName.length() - 3) + "publish"); //$NON-NLS-1$
String title = getJavadocWindowTitle(description, version);
String footer = getJavadocFooter();
String title = javadocWindowTitle(description, version);
String footer = javadocFooter();
savePublish(publish.toFile(), title, footer);
}
System.out.println();
}
}

protected String getJavadocWindowTitle(String string, String version) {
private String javadocWindowTitle(String string, String version) {
return string + " " + version.substring(0, version.lastIndexOf('.')) + " API Specification"; //$NON-NLS-1$ //$NON-NLS-2$
}

protected String getJavadocFooter() {
private String javadocFooter() {
String product = "Passage"; //$NON-NLS-1$
return "<font size=\"-1\">Copyright &copy; 2018, " + year //$NON-NLS-1$
+ "ArSysOp and others" //$NON-NLS-1$
+ ". Licensed under the <a href=\"https://www.eclipse.org/legal/epl-2.0/\">Eclipse Public License v2.0</a>. All rights reserved.<br/><a href=\"https://bugs.eclipse.org/bugs/enter_bug.cgi?product=" //$NON-NLS-1$
+ product + "\">Submit a bug or feature</a><br/></font>"; //$NON-NLS-1$
}

protected Model load(File file) {
private Model loaded(File file) {
try (FileReader reader = new FileReader(file)) {
return new MavenXpp3Reader().read(reader);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

protected void save(Model model, File target) {
private void save(Model model, File target) {
MavenXpp3Writer writer = new MavenXpp3Writer();
try {
if (dry) {
Expand All @@ -182,27 +165,28 @@ protected void save(Model model, File target) {
}
}

protected void savePublish(File target, String javadocWindowTitle, String javadocFooter) {
private void savePublish(File target, String title, String footer) {
try {
if (dry) {
System.out.println(javadocWindowTitle);
System.out.println(javadocFooter);
System.out.println(title);
System.out.println(footer);
} else {
try (FileOutputStream fos = new FileOutputStream(target)) {
PrintStream out = new PrintStream(fos, false, StandardCharsets.UTF_8);
out.println(javadocWindowTitle);
out.println(javadocFooter);
PrintStream out = new PrintStream(fos, false, "UTF-8"); //$NON-NLS-1$ use j1.4 constructor instead
// of j10
out.println(title);
out.println(footer);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected boolean exists(String groupID, String artifactID, String version) {
private boolean exists(String group, String artifact, String version) {
String spec = String.format(
"https://oss.sonatype.org/service/local/lucene/search?g=%s&a=%s&v=%s&repositoryId=releases", // //$NON-NLS-1$
groupID, artifactID, version);
"https://oss.sonatype.org/service/local/lucene/search?g=%s&a=%s&v=%s&repositoryId=releases", //$NON-NLS-1$
group, artifact, version);
try {
URL url = new URL(spec);
try (InputStream in = url.openStream();
Expand All @@ -220,4 +204,21 @@ protected boolean exists(String groupID, String artifactID, String version) {
return false;
}

public static void main(String[] args) {
CompletePom instance = new CompletePom();
CmdLineParser parser = new CmdLineParser(instance);
try {
parser.parseArgument(args);
} catch (CmdLineException e) {
System.err.println(e.getMessage());
parser.printUsage(System.err);
return;
}
try {
instance.run();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}

}

0 comments on commit 233e4d0

Please sign in to comment.