JD2022-TU1/main/tools/apps/TeaBox/Script/Build/Version/VersionUtilities.cs

164 lines
No EOL
6.5 KiB
C#

using System;
using System.Data;
using System.IO;
using MySql.Data.MySqlClient;
using TeaBox.Macros;
namespace TeaBox.BuildVersion
{
internal class VersionUtilities
{
internal static IExecutionContext Context { get; set; }
internal static string JdVersion { get; set; }
internal static Platform Platform { get; set; }
internal static string Tag { get; set; }
internal static string CodeChangelist { get; set; }
internal static string DataChangelist { get; set; }
public static string GetVersionId()
{
// Sql connection informations
// Numbering is platform specific for the candidate master versions
var server = VersionConfigurations.GetSqlServerAddress();
var user = VersionConfigurations.GetSqlUser();
var password = VersionConfigurations.GetSqlPassword();
const string databaseName = "jd5";
var connectionString =
$"Server={server}; Database={databaseName}; Uid={user}; Password={password};";
// open new mysql connection
using (var sqlConnection = new MySqlConnection(connectionString))
{
try
{
sqlConnection.Open();
}
catch (MySqlException ex)
{
Context.LogException(ex, $"Exception found : {ex.Message}");
}
// if it fails to open the connection to the database
if (sqlConnection.State != ConnectionState.Open)
{
return "Unknown";
}
Context.LogMessage("*** Current version infos ***");
Context.LogMessage($"*** Project : {JdVersion}");
Context.LogMessage($"*** Platform is {Platform}");
Context.LogMessage($"*** Engine CL is {CodeChangelist}");
Context.LogMessage($"*** Data CL is {DataChangelist}");
Context.LogMessage($"*** Tag is {Tag}");
string id;
string latestId;
try
{
Context.LogMessage("*** Querying version id ***");
// This seems to always return an exception so id is always Unknown at this point
id = SqlManager.SqlExecuteCommand(SqlManager.GetId(), sqlConnection);
}
catch (MySqlException ex)
{
//if (ex.Message.Contains())
Context.LogMessage(ex.Message);
id = "Unknown";
}
Context.LogMessage($"Version Id = {id}");
var outputFile = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "versionId.txt");
if (id != "Unknown")
{
System.IO.File.WriteAllText(outputFile, id);
return id;
}
try
{
// Get value of the next available id
latestId = SqlManager.SqlExecuteCommand(SqlManager.GetLatestId(), sqlConnection);
Context.LogMessage($"Latest Id = {latestId}");
}
catch (Exception)
{
latestId = "0";
}
id = (int.Parse(latestId) + 1).ToString();
Context.LogMessage($"Next Build Id = {id}");
// Insert new id into the database
SqlManager.SqlExecuteCommand(SqlManager.InsertId(id), sqlConnection);
System.IO.File.WriteAllText(outputFile, id);
return id;
}
}
// Get Version id from SQL Database but don't add it
public static string GetVersionIdFromSQL()
{
// Credentials to connect to the SQL Database --------------------------------------------------------------
var server = VersionConfigurations.GetSqlServerAddress();
var user = VersionConfigurations.GetSqlUser();
var password = VersionConfigurations.GetSqlPassword();
const string databaseName = "jd5";
var connectionString = $"Server={server}; Database={databaseName}; Uid={user}; Password={password};";
using (var sqlConnection = new MySqlConnection(connectionString))
{
// Trying to connect to the sql database ----------------------------------------------------------------
try
{
sqlConnection.Open();
}
catch (MySqlException ex)
{
Context.LogException(ex, $"Can't connect to sql database : Exception found : {ex.Message}");
}
// if it fails to open the connection to the database
if (sqlConnection.State != ConnectionState.Open)
{
Context.LogMessage("Failed to connect to sql database ! Version ID will be Unknown.");
return "Unknown";
}
Context.LogMessage("*** Current version infos ***");
Context.LogMessage($"*** Project : {JdVersion}");
Context.LogMessage($"*** Platform is {Platform}");
Context.LogMessage($"*** Engine CL is {CodeChangelist}");
Context.LogMessage($"*** Data CL is {DataChangelist}");
Context.LogMessage($"*** Tag is {Tag}");
string latestId;
// VersionId.txt will contain the latest version ID found in the sql database ------------------------
var outputFile = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "versionId.txt");
try
{
// Get value of the latest id
Context.LogMessage($"Catching Version Id from SQL Database...");
latestId = SqlManager.SqlExecuteCommand(SqlManager.GetLatestId(), sqlConnection);
Context.LogMessage($"Latest Id = {latestId}");
}
catch (Exception)
{
Context.LogMessage($"Failed to get Version ID !");
latestId = "Unknown";
}
Context.LogMessage($"Version Id = {latestId}");
// Write the latest version ID in the sql database
System.IO.File.WriteAllText(outputFile, latestId);
return latestId;
}
}
}
}