org.nuiton.jredmine.plugin.announcement.AnnouncementGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jredmine-maven-plugin Show documentation
Show all versions of jredmine-maven-plugin Show documentation
JRedmine maven plugin to interacts with Redmine's server
/*
* #%L
* JRedmine :: Maven plugin
*
* $Id: AnnouncementGenerator.java 364 2012-10-13 21:50:04Z tchemit $
* $HeadURL: https://svn.nuiton.org/jredmine/tags/jredmine-1.7/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java $
* %%
* Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.jredmine.plugin.announcement;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.changes.model.Action;
import org.apache.maven.plugins.changes.model.Release;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import org.nuiton.jredmine.model.Attachment;
import org.nuiton.plugin.PluginHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author tchemit
* @since 1.0.0
*/
public class AnnouncementGenerator {
/** The token any of urls denoting the base URL for the given url. */
private static final String URL_TOKEN = "%URL%";
/** The token in {@link #getAttachmentLinkTemplate()} denoting the attachment ID. */
private static final String ATTACHMENT_TOKEN = "%FILE%";
private static final Pattern ARTIFACT_PATTERN =
Pattern.compile("(.+)?--(.+)?");
private final AnnouncementGeneratorConfiguration configuration;
public AnnouncementGenerator(AnnouncementGeneratorConfiguration configuration) {
this.configuration = configuration;
}
public String getAttachmentLinkTemplate() {
return configuration.getAttachmentLinkTemplate();
}
public Log getLog() {
return configuration.getLog();
}
public String getUrl() {
return configuration.getUrl().toString();
}
public Map getAttachmentsUrls(Attachment[] attachments) {
// transform attachments urls
boolean hasAttachmentLinks = canGenerateAttachmentLinks();
Map urls = null;
if (hasAttachmentLinks) {
urls = new LinkedHashMap();
for (Attachment a : attachments) {
String u = parseAttachmentLink(a.getId() + "");
urls.put(a, u);
}
} else {
getLog().warn("can not render attachments urls");
}
return urls;
}
private class ArtifactInfo {
private File src;
private String groupId;
private String artifactId;
private String url;
private String extension;
private String filename;
public ArtifactInfo(String url, String versionId, File srcfile) {
String artifactInfo = srcfile.getParentFile().getName();
Matcher matcher = ARTIFACT_PATTERN.matcher(artifactInfo);
if (!matcher.matches()) {
getLog().error("no matching for file " + artifactInfo +
" (" + srcfile + ")");
}
artifactId = matcher.group(2);
groupId = matcher.group(1).replaceAll("\\.", "/") + "/" + artifactId;
String filename = srcfile.getName();
if ("pom.xml".equals(filename)) {
filename = artifactId + "-" + versionId + ".pom";
src = new File(filename);
} else {
src = srcfile;
}
this.url = url + groupId + "/" + versionId + "/" + filename;
extension = FileUtils.getExtension(src.getName());
this.filename = src.getName().substring(0, extension.length() - 1);
}
}
protected class ArtifactComparator implements Comparator {
final List groupIds;
public ArtifactComparator(List groupIds) {
this.groupIds = groupIds;
}
@Override
public int compare(ArtifactInfo o1, ArtifactInfo o2) {
// sort by groupId
int result = groupIds.indexOf(o1.groupId) -
groupIds.indexOf(o2.groupId);
if (result != 0) {
return result;
}
result = o1.extension.compareTo(o2.extension);
if (result != 0) {
if ("pom".equals(o1.extension)) {
return -1;
}
if ("pom".equals(o2.extension)) {
return 1;
}
return result;
}
// sort on filename
result = o1.filename.compareTo(o2.filename);
return result;
}
}
public Map getArtifactsUrls(String url,
String versionId,
boolean verbose,
File[] files) {
Map urls = new LinkedHashMap();
List groupIds = new ArrayList();
List list = new ArrayList();
for (File f : files) {
ArtifactInfo artifactInfo = new ArtifactInfo(url, versionId, f);
list.add(artifactInfo);
if (!groupIds.contains(artifactInfo.groupId)) {
if (verbose) {
getLog().info("detected groupId " + artifactInfo.groupId);
}
groupIds.add(artifactInfo.groupId);
}
}
Collections.sort(list, new ArtifactComparator(groupIds));
for (ArtifactInfo a : list) {
if (verbose) {
getLog().info("artifact file " + a.src.getName() + " --> " +
a.url);
}
urls.put(a.src, a.url);
}
return urls;
}
public Context createVelocityContext(List> releases) throws MojoExecutionException {
// prepare velocity context
Context context = new VelocityContext();
context.put("releases", releases);
context.put("groupId", configuration.getGroupId());
context.put("artifactId", configuration.getArtifactId());
context.put("version", configuration.getVersionId());
context.put("packaging", configuration.getPackaging());
context.put("url", configuration.getUrl());
context.put("projectUrl", configuration.getProjectUrl());
Release release = getLatestRelease(releases, configuration.getVersionId());
context.put("release", release);
context.put("introduction", configuration.getIntroduction());
context.put("developmentTeam", configuration.getDevelopmentTeam());
context.put("finalName", configuration.getFinalName());
context.put("urlDownload", configuration.getUrlDownload());
context.put("mavenRepoUrl", configuration.getDeploymentUrl());
context.put("project", configuration.getProject());
// add artifacts in context
Map artifactUrls = configuration.getArtifactUrls();
if (artifactUrls == null || artifactUrls.isEmpty()) {
context.put("withArtifacts", false);
} else {
context.put("withArtifacts", true);
context.put("artifactUrls", artifactUrls);
}
// add attachments in context
Map attachmentUrls =
configuration.getAttachmentUrls();
if (attachmentUrls == null || attachmentUrls.isEmpty()) {
context.put("withAttachments", false);
} else {
context.put("withAttachments", true);
context.put("attachmentUrls", attachmentUrls);
}
Map announceParameters =
configuration.getAnnounceParameters();
if (announceParameters == null) {
// empty Map to prevent NPE in velocity execution
context.put("announceParameters",
Collections.