Skip to content

Commit

Permalink
Load version from Manifest (#43)
Browse files Browse the repository at this point in the history
- Simplify the code, get rid of a bunch of version flags that had no real value
  • Loading branch information
jsotuyod committed Nov 30, 2016
1 parent fa347bb commit 7fb6ff5
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 202 deletions.
2 changes: 1 addition & 1 deletion eclipsePlugin/src/de/tobject/findbugs/FindbugsPlugin.java
Expand Up @@ -210,7 +210,7 @@ public FindbugsPlugin() {
public void start(BundleContext context) throws Exception {
super.start(context);

Version.registerApplication("FindBugs-Eclipse", Version.RELEASE);
Version.registerApplication("FindBugs-Eclipse", Version.VERSION_STRING);

// configure debugging
configurePluginDebugOptions();
Expand Down
Expand Up @@ -68,7 +68,7 @@ public WorkspaceSettingsTab(TabFolder tabFolder, final FindbugsPropertyPage page
tabDetector.setControl(this);

Label versionLabel = new Label(this, SWT.NONE);
versionLabel.setText("FindBugs version: " + Version.RELEASE);
versionLabel.setText("SpotBugs version: " + Version.VERSION_STRING);

runAsExtraJob = new Button(this, SWT.CHECK);
runAsExtraJob.setSelection(store.getBoolean(FindBugsConstants.KEY_RUN_ANALYSIS_AS_EXTRA_JOB));
Expand Down
1 change: 1 addition & 0 deletions findbugs/build.gradle
Expand Up @@ -56,6 +56,7 @@ jar {

manifest {
attributes 'Main-Class': 'edu.umd.cs.findbugs.LaunchAppropriateUI',
'Bundle-Version': project.version,
'Class-Path': 'bcel.jar dom4j-1.6.1.jar jaxen-1.1.6.jar asm-debug-all-6.0_ALPHA jsr305.jar jFormatString.jar commons-lang-2.6.jar'
}
}
Expand Down
4 changes: 2 additions & 2 deletions findbugs/src/gui/edu/umd/cs/findbugs/gui2/AboutDialog.java
Expand Up @@ -64,8 +64,8 @@ public AboutDialog(JFrame parent, Logger l, boolean modal) {
l.logMessage(Logger.ERROR, e.toString());
}

setTitle(MessageFormat.format(edu.umd.cs.findbugs.L10N.getLocalString("dlg.aboutfindbugs_ttl", "About FindBugs {0}"),
new Object[] { Version.RELEASE }));
setTitle(MessageFormat.format(edu.umd.cs.findbugs.L10N.getLocalString("dlg.aboutfindbugs_ttl", "About SpotBugs {0}"),
new Object[] { Version.VERSION_STRING }));
}

static Pattern pattern = Pattern.compile("@VERSION@");
Expand Down
2 changes: 1 addition & 1 deletion findbugs/src/gui/edu/umd/cs/findbugs/gui2/Driver.java
Expand Up @@ -55,7 +55,7 @@ public static void main(String[] args) throws Exception {
if (JavaWebStart.isRunningViaJavaWebstart()) {
name = "SpotBugs webstart GUI";
}
Version.registerApplication(name, Version.RELEASE);
Version.registerApplication(name, Version.VERSION_STRING);

if (SystemProperties.getProperty("os.name").startsWith("Mac")) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
Expand Down
3 changes: 1 addition & 2 deletions findbugs/src/java/edu/umd/cs/findbugs/Plugin.java
Expand Up @@ -118,8 +118,7 @@ public Plugin(String pluginId, String version, Date releaseDate, @Nonnull Plugin
if (version == null) {
version = "";
} else if (USE_FINDBUGS_VERSION.equals(version)) {
version = Version.COMPUTED_RELEASE;
releaseDate = Version.getReleaseDate();
version = Version.VERSION_STRING;
}
assert enabled || !cannotDisable;
myGlobalOptions = new HashMap<String, String>();
Expand Down
7 changes: 2 additions & 5 deletions findbugs/src/java/edu/umd/cs/findbugs/PluginLoader.java
Expand Up @@ -150,8 +150,8 @@ public class PluginLoader {
static HashSet<String> loadedPluginIds = new HashSet<String>();
static {
if (DEBUG) {
System.out.println("Debugging plugin loading. FindBugs version "
+ Version.getReleaseWithDateIfDev());
System.out.println("Debugging plugin loading. SpotBugs version "
+ Version.VERSION_STRING);
}
loadInitialPlugins();
}
Expand Down Expand Up @@ -1155,9 +1155,6 @@ private Plugin constructMinimalPlugin(Document pluginDescriptor, List<Document>
String version = pluginDescriptor.valueOf("/FindbugsPlugin/@version");
String releaseDate = pluginDescriptor.valueOf("/FindbugsPlugin/@releaseDate");

if ((releaseDate == null || releaseDate.length() == 0) && isCorePlugin()) {
releaseDate = Version.CORE_PLUGIN_RELEASE_DATE;
}
// Create the Plugin object (but don't assign to the plugin field yet,
// since we're still not sure if everything will load correctly)
Date parsedDate = parseDate(releaseDate);
Expand Down
3 changes: 1 addition & 2 deletions findbugs/src/java/edu/umd/cs/findbugs/ShowHelp.java
Expand Up @@ -28,8 +28,7 @@
public class ShowHelp {

public static void main(String[] args) {

System.out.println("SpotBugs version " + Version.RELEASE + ", " + Version.WEBSITE);
System.out.println("SpotBugs version " + Version.VERSION_STRING + ", " + Version.WEBSITE);

DetectorFactoryCollection.instance();
System.out.println("Command line options");
Expand Down
Expand Up @@ -97,7 +97,7 @@ public class SortedBugCollection implements BugCollection {

long analysisTimestamp = System.currentTimeMillis();

String analysisVersion = Version.RELEASE;
String analysisVersion = Version.VERSION_STRING;

boolean earlyStats = SystemProperties.getBoolean("findbugs.report.summaryFirst");

Expand Down
206 changes: 20 additions & 186 deletions findbugs/src/java/edu/umd/cs/findbugs/Version.java
Expand Up @@ -21,189 +21,50 @@

import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.jar.Manifest;

import javax.annotation.CheckForNull;

import edu.umd.cs.findbugs.cloud.CloudPlugin;
import edu.umd.cs.findbugs.updates.UpdateChecker;
import edu.umd.cs.findbugs.util.FutureValue;
import edu.umd.cs.findbugs.util.Util;

/**
* Version number and release date information.
*/
public class Version {
/**
* Major version number.
* SpotBugs website.
*/
public static final int MAJOR = 3;

/**
* Minor version number.
*/
public static final int MINOR = 1;

/**
* Patch level.
*/
public static final int PATCHLEVEL = 0;

/**
* Development version or release candidate?
*/
public static final boolean IS_DEVELOPMENT = true;

/**
* Release candidate number. "0" indicates that the version is not a release
* candidate.
*/
public static final int RELEASE_CANDIDATE = 0;


public static final String GIT_REVISION = System.getProperty("git.revision", "UNKNOWN");

/**
* Release date.
*/
private static final String COMPUTED_DATE;

public static final String DATE;

public static final String CORE_PLUGIN_RELEASE_DATE;

private static final String COMPUTED_ECLIPSE_DATE;

private static final String COMPUTED_PLUGIN_RELEASE_DATE;

public static final String WEBSITE = "https://spotbugs.github.io/";

public final static String VERSION_STRING;

private static String applicationName = "";

private static String applicationVersion = "";

static {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss z, dd MMMM, yyyy", Locale.ENGLISH);
SimpleDateFormat eclipseDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
SimpleDateFormat releaseDateFormat = new SimpleDateFormat(UpdateChecker.PLUGIN_RELEASE_DATE_FMT, Locale.ENGLISH);
Date now = new Date();
COMPUTED_DATE = dateFormat.format(now);
COMPUTED_ECLIPSE_DATE = eclipseDateFormat.format(now);
String tmp = releaseDateFormat.format(now);
COMPUTED_PLUGIN_RELEASE_DATE = tmp;
}

/**
* Preview release number. "0" indicates that the version is not a preview
* release.
*/
public static final int PREVIEW = 0;

private static final String RELEASE_SUFFIX_WORD;
static {
String suffix;
if (RELEASE_CANDIDATE > 0) {
suffix = "rc" + RELEASE_CANDIDATE;
} else if (PREVIEW > 0) {
suffix = "preview" + PREVIEW;
} else {
suffix = "dev-" + COMPUTED_ECLIPSE_DATE;
if (!"Unknown".equals(GIT_REVISION)) {
suffix += "-" + GIT_REVISION;
}
}
RELEASE_SUFFIX_WORD = suffix;
}

public static final String RELEASE_BASE = MAJOR + "." + MINOR + "." + PATCHLEVEL;

/**
* Release version string.
*/
public static final String COMPUTED_RELEASE = RELEASE_BASE + (IS_DEVELOPMENT ? "-" + RELEASE_SUFFIX_WORD : "");

/**
* Release version string.
*/
public static final String RELEASE;

/**
* Version of Eclipse plugin.
*/
private static final String COMPUTED_ECLIPSE_UI_VERSION = RELEASE_BASE + "." + COMPUTED_ECLIPSE_DATE;

static {
Class<Version> c = Version.class;
URL u = c.getResource(c.getSimpleName() + ".class");
boolean fromFile = "file".equals(u.getProtocol());
InputStream in = null;
String release = null;
String date = null;
String plugin_release_date = null;
final URL u = Version.class.getResource(Version.class.getSimpleName() + ".class");
final boolean fromFile = "file".equals(u.getProtocol());

String version = "(Unknown)";
if (!fromFile) {
try {
Properties versionProperties = new Properties();
in = Version.class.getResourceAsStream("version.properties");
if (in != null) {
versionProperties.load(in);
release = (String) versionProperties.get("release.number");
date = (String) versionProperties.get("release.date");
plugin_release_date = (String) versionProperties.get("plugin.release.date");
}
try (final InputStream in = Version.class.getResourceAsStream("META-INF/MANIFEST.MF")) {
final Manifest manifest = new Manifest(in);
version = manifest.getMainAttributes().getValue("Bundle-Version");
} catch (Exception e) {
assert true; // ignore
} finally {
Util.closeSilently(in);
}
}
if (release == null) {
release = COMPUTED_RELEASE;
}
if (date == null) {
date = COMPUTED_DATE;
}
if (plugin_release_date == null) {
plugin_release_date = COMPUTED_PLUGIN_RELEASE_DATE;
}

RELEASE = release;
DATE = date;
CORE_PLUGIN_RELEASE_DATE = plugin_release_date;
Date parsedDate;
try {
SimpleDateFormat fmt = new SimpleDateFormat(UpdateChecker.PLUGIN_RELEASE_DATE_FMT, Locale.ENGLISH);

parsedDate = fmt.parse(CORE_PLUGIN_RELEASE_DATE);
} catch (ParseException e) {
if (SystemProperties.ASSERTIONS_ENABLED) {
e.printStackTrace();
// ignore it
}
parsedDate = null;
} else {
version = "Development";
}
releaseDate = parsedDate;

VERSION_STRING = version;
}

/**
* FindBugs website.
*/
public static final String WEBSITE = "http://findbugs.sourceforge.net";

/**
* Downloads website.
*/
public static final String DOWNLOADS_WEBSITE = "http://prdownloads.sourceforge.net/findbugs";

/**
* Support email.
*/
public static final String SUPPORT_EMAIL = "http://findbugs.sourceforge.net/reportingBugs.html";
private static Date releaseDate;

public static void registerApplication(String name, String version) {
applicationName = name;
applicationVersion = version;
Expand All @@ -218,10 +79,6 @@ public static void registerApplication(String name, String version) {
}

public static void main(String[] argv) throws InterruptedException {

if (!IS_DEVELOPMENT && RELEASE_CANDIDATE != 0) {
throw new IllegalStateException("Non developmental version, but is release candidate " + RELEASE_CANDIDATE);
}
if (argv.length == 0) {
printVersion(false);
return;
Expand All @@ -230,18 +87,7 @@ public static void main(String[] argv) throws InterruptedException {
String arg = argv[0];

if ("-release".equals(arg)) {
System.out.println(RELEASE);
} else if ("-date".equals(arg)) {
System.out.println(DATE);
} else if ("-props".equals(arg)) {
System.out.println("release.base=" + RELEASE_BASE);
System.out.println("release.number=" + COMPUTED_RELEASE);
System.out.println("release.date=" + COMPUTED_DATE);
System.out.println("plugin.release.date=" + COMPUTED_PLUGIN_RELEASE_DATE);
System.out.println("eclipse.ui.version=" + COMPUTED_ECLIPSE_UI_VERSION);
System.out.println("findbugs.website=" + WEBSITE);
System.out.println("findbugs.downloads.website=" + DOWNLOADS_WEBSITE);
System.out.println("findbugs.git.revision=" + GIT_REVISION);
System.out.println(VERSION_STRING);
} else if ("-plugins".equals(arg)) {
DetectorFactoryCollection.instance();
for(Plugin p : Plugin.getAllPlugins()) {
Expand All @@ -261,7 +107,6 @@ public static void main(String[] argv) throws InterruptedException {
} else if ("-configuration".equals(arg)){
printVersion(true);
} else {

usage();
System.exit(1);
}
Expand All @@ -271,23 +116,12 @@ private static void usage() {
System.err.println("Usage: " + Version.class.getName() + " [(-release|-date|-props|-configuration)]");
}

public static String getReleaseWithDateIfDev() {
if (IS_DEVELOPMENT) {
return RELEASE + " (" + DATE + ")";
}
return RELEASE;
}

public static @CheckForNull Date getReleaseDate() {
return releaseDate;
}

/**
* @param justPrintConfiguration
* @throws InterruptedException
*/
public static void printVersion(boolean justPrintConfiguration) throws InterruptedException {
System.out.println("SpotBugs " + Version.COMPUTED_RELEASE);
System.out.println("SpotBugs " + Version.VERSION_STRING);
if (justPrintConfiguration) {
for (Plugin plugin : Plugin.getAllPlugins()) {
System.out.printf("Plugin %s, version %s, loaded from %s%n", plugin.getPluginId(), plugin.getVersion(),
Expand Down
Expand Up @@ -268,7 +268,7 @@ protected final void writeXml(OutputStream out, Collection<Plugin> plugins, Stri
xmlOutput.beginDocument();

xmlOutput.startTag("findbugs-invocation");
xmlOutput.addAttribute("version", Version.RELEASE);
xmlOutput.addAttribute("version", Version.VERSION_STRING);
String applicationName = Version.getApplicationName();
if (applicationName == null || "".equals(applicationName)) {
int lastDot = entryPoint.lastIndexOf('.');
Expand Down

0 comments on commit 7fb6ff5

Please sign in to comment.