Added automatic build information properties management (!version…).

This commit is contained in:
Christian P. MOMON 2018-01-12 00:58:15 +01:00
parent c68a47b538
commit fad7aacd9a
5 changed files with 218 additions and 4 deletions

View File

@ -83,12 +83,13 @@
<mkdir dir="${dist.dir}"/> <mkdir dir="${dist.dir}"/>
<!-- Update build_information.properties file --> <!-- Update build_information.properties file -->
<property name="build.information.file" value="${build.dir}/build_information.properties" /> <property name="build.information.file" value="${build.classes}/org/april/hebdobot/build_information.properties" />
<propertyfile file="${build.information.file}" > <propertyfile file="${build.information.file}" >
<entry key="product.name" value="${product.name}"/> <entry key="product.name" value="${product.name}"/>
<entry key="product.revision.major" value="${product.revision.major}"/> <entry key="product.revision.major" value="${product.revision.major}"/>
<entry key="product.revision.minor" value="${product.revision.minor}"/> <entry key="product.revision.minor" value="${product.revision.minor}"/>
<entry key="product.revision.build" value="${build.number}"/> <entry key="product.revision.build" value="${build.number}"/>
<entry key="product.revision.snapshot" value="${dist.snapshot}"/>
<entry key="product.revision.date" type="date" value="now"/> <entry key="product.revision.date" type="date" value="now"/>
<entry key="product.revision.generator" value="Ant"/> <entry key="product.revision.generator" value="Ant"/>
<entry key="product.revision.author" value="${user.name}"/> <entry key="product.revision.author" value="${user.name}"/>

View File

@ -7,7 +7,7 @@
<tstamp> <tstamp>
<format property="dist.snapshot.number" pattern="yyyyMMddHHmmss" /> <format property="dist.snapshot.number" pattern="yyyyMMddHHmmss" />
</tstamp> </tstamp>
<property name="dist.snapshot" value="-SNAPSHOT-${dist.snapshot.number}" /> <property name="dist.snapshot" value="-SNAPSHOT_${dist.snapshot.number}" />
<!-- ***** Store ***** --> <!-- ***** Store ***** -->
<target name="store" description="Store the build number version"> <target name="store" description="Store the build number version">

View File

@ -33,6 +33,7 @@ import org.apache.log4j.PropertyConfigurator;
import org.april.hebdobot.HebdobotException; import org.april.hebdobot.HebdobotException;
import org.april.hebdobot.model.Hebdobot; import org.april.hebdobot.model.Hebdobot;
import org.april.hebdobot.model.UserAliases; import org.april.hebdobot.model.UserAliases;
import org.april.hebdobot.util.BuildInformation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,7 +46,6 @@ public class HebdobotCLI
{ {
private static final Logger logger = LoggerFactory.getLogger(HebdobotCLI.class); private static final Logger logger = LoggerFactory.getLogger(HebdobotCLI.class);
public static final String HEBDOBOT_VERSION = "v2.0";
private static final String DEFAULT_CONFIG_FILE = "hebdobot.conf"; private static final String DEFAULT_CONFIG_FILE = "hebdobot.conf";
private static final String DEFAULT_ALIAS_FILE = "users.conf"; private static final String DEFAULT_ALIAS_FILE = "users.conf";
@ -63,10 +63,11 @@ public class HebdobotCLI
{ {
StringList message = new StringList(); StringList message = new StringList();
message.append("Hebdobot version ").appendln(HEBDOBOT_VERSION); message.append("Hebdobot ").appendln(new BuildInformation().version());
message.appendln("Usage:"); message.appendln("Usage:");
message.appendln(" hebdobot [ -h | -help | --help ]"); message.appendln(" hebdobot [ -h | -help | --help ]");
message.appendln(" hebdobot [ -c config-file ]"); message.appendln(" hebdobot [ -c config-file ]");
message.appendln(" hebdobot [ -version ]");
System.out.println(message.toString()); System.out.println(message.toString());
} }
@ -86,6 +87,7 @@ public class HebdobotCLI
// //
Options options = new Options(); Options options = new Options();
options.addOption("h", "help", false, "Help option"); options.addOption("h", "help", false, "Help option");
options.addOption("version", false, "Version option");
options.addOption("c", true, "Config file"); options.addOption("c", true, "Config file");
CommandLineParser parser = new DefaultParser(); CommandLineParser parser = new DefaultParser();
@ -98,6 +100,10 @@ public class HebdobotCLI
HelpFormatter formatter = new HelpFormatter(); HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("hebdobot", options); formatter.printHelp("hebdobot", options);
} }
else if (commandLine.hasOption("version"))
{
showVersion();
}
else else
{ {
// Find the configuration file. // Find the configuration file.
@ -161,6 +167,7 @@ public class HebdobotCLI
logger.info("Basic log configuration done."); logger.info("Basic log configuration done.");
logger.info("Configuration file was not found in [{}].", loggerConfigFile.getAbsoluteFile()); logger.info("Configuration file was not found in [{}].", loggerConfigFile.getAbsoluteFile());
} }
System.out.println(new BuildInformation().toString());
// Load configuration file. // Load configuration file.
logger.info("Configuration file loading… ({}).", configFile.getAbsolutePath()); logger.info("Configuration file loading… ({}).", configFile.getAbsolutePath());
@ -235,4 +242,16 @@ public class HebdobotCLI
help(); help();
} }
} }
/**
* version.
*/
public static void showVersion()
{
StringList message = new StringList();
message.appendln(new BuildInformation().toString());
System.out.println(message.toString());
}
} }

View File

@ -41,6 +41,7 @@ import org.april.hebdobot.review.Review;
import org.april.hebdobot.review.Topic; import org.april.hebdobot.review.Topic;
import org.april.hebdobot.twitter.TwitterClient; import org.april.hebdobot.twitter.TwitterClient;
import org.april.hebdobot.twitter.TwitterSettings; import org.april.hebdobot.twitter.TwitterSettings;
import org.april.hebdobot.util.BuildInformation;
import org.jibble.pircbot.IrcException; import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException; import org.jibble.pircbot.NickAlreadyInUseException;
import org.jibble.pircbot.PircBot; import org.jibble.pircbot.PircBot;
@ -492,6 +493,12 @@ public class Hebdobot extends PircBot
sendMessage(sender sendMessage(sender
+ ", Hebdobot est un logiciel libre de l'April sous licence GNU AGPL (sources : https://agir.april.org/projects/hebdobot/repository)."); + ", Hebdobot est un logiciel libre de l'April sous licence GNU AGPL (sources : https://agir.april.org/projects/hebdobot/repository).");
} }
else if (StringsUtils.equalsAnyIgnoreCase(text, "!version"))
{
logger.info("!version caught.");
sendMessage(new BuildInformation().toString());
}
else if (text.startsWith("%")) else if (text.startsWith("%"))
{ {
logger.info("% caught."); logger.info("% caught.");

View File

@ -0,0 +1,187 @@
/**
* Copyright (C) 2016-2017 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hebdobot 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.util;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class BuildInformation.
*/
public class BuildInformation
{
private static final Logger logger = LoggerFactory.getLogger(BuildInformation.class);
private static String BUILD_INFORMATION_FILE = "/org/april/hebdobot/build_information.properties";
private String productName;
private String majorRevision;
private String minorRevision;
private String buildNumber;
private String snapshotStamp;
private String buildDate;
private String generator;
private String author;
/**
* Instantiates a new builds the information.
*/
public BuildInformation()
{
Properties build = new Properties();
try
{
//
URL buildInformationFile = BuildInformation.class.getResource(BUILD_INFORMATION_FILE);
if (buildInformationFile != null)
{
build.load(BuildInformation.class.getResource(BUILD_INFORMATION_FILE).openStream());
}
//
this.productName = build.getProperty("product.name", "DevInProgress");
this.majorRevision = build.getProperty("product.revision.major", "d");
this.minorRevision = build.getProperty("product.revision.minor", "e");
this.buildNumber = build.getProperty("product.revision.build", "v");
this.snapshotStamp = build.getProperty("product.revision.snapshot", "-s");
this.buildDate = build.getProperty("product.revision.date", "today");
this.generator = build.getProperty("product.revision.generator", "n/a");
this.author = build.getProperty("product.revision.author", "n/a");
}
catch (IOException exception)
{
//
logger.error("Error loading the build.properties file: " + exception.getMessage());
logger.error(ExceptionUtils.getStackTrace(exception));
//
this.productName = "n/a";
this.majorRevision = "n/a";
this.minorRevision = "n/a";
this.buildNumber = "n/a";
this.snapshotStamp = "n/a";
this.buildDate = "n/a";
this.generator = "n/a";
this.author = "n/a";
}
}
public String author()
{
return this.author;
}
public String buildDate()
{
return this.buildDate;
}
public String buildNumber()
{
return this.buildNumber;
}
public String generator()
{
return this.generator;
}
public String majorRevision()
{
return this.majorRevision;
}
public String minorRevision()
{
return this.minorRevision;
}
public String productName()
{
return this.productName;
}
public String snapshotStamp()
{
return this.snapshotStamp;
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString()
{
String result;
result = String.format("%s %s.%s.%s%s built on %s by %s", StringUtils.capitalize(this.productName), this.majorRevision, this.minorRevision,
this.buildNumber, this.snapshotStamp, this.buildDate, this.author);
//
return result;
}
/**
* Version.
*
* @return the string
*/
public String version()
{
String result;
result = String.format("%s.%s.%s%s", this.majorRevision, this.minorRevision, this.buildNumber, this.snapshotStamp);
//
return result;
}
/**
* Checks if is defined.
*
* @return true, if is defined
*/
public static boolean isDefined()
{
boolean result;
if (BuildInformation.class.getResource(BUILD_INFORMATION_FILE) == null)
{
result = false;
}
else
{
result = true;
}
//
return result;
}
}