JD2022-TU1/main/build/sharpmake/Tools/Helpers/AssemblyInfoGenerator.cs

165 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using JustDance.Tools;
using Sharpmake;
using Ubisoft.Perforce;
namespace sharpmake_sharpmake.Tools.Helpers
{
internal class AssemblyInfoGenerator
{
private static readonly int _changelist;
private static readonly string _clientName;
private readonly string _assemblyFilePath;
private readonly string _description = "";
static AssemblyInfoGenerator()
{
TryGetPerforceInformation(BaseProject.ToolsPath, out _changelist, out _clientName);
}
public AssemblyInfoGenerator(Project project)
{
_assemblyFilePath = project.ResolveString(BaseProject.BuildPath + @"\tools\[project.Name]\Properties\" + "AssemblyInfo.cs");
if (!Directory.Exists(Path.GetDirectoryName(_assemblyFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(_assemblyFilePath));
}
project.SourceFiles.Add(_assemblyFilePath);
var descriptionAttribute = project.GetType().GetCustomAttributes(typeof(DescriptionAttribute), true).Cast<DescriptionAttribute>().FirstOrDefault();
if (descriptionAttribute != null)
{
_description = descriptionAttribute.Description;
}
}
public void AfterConfigureAll(Project.Configuration conf)
{
var version = string.Format("{0}.{1}.{2}.{3}", DateTime.Today.Year, DateTime.Today.Month, _changelist / 10000, _changelist % 10000);
var usings = new List<string>();
usings.Add("System.Reflection");
usings.Add("System.Runtime.InteropServices");
var attributes = new List<AssemblyAttribute>();
attributes.Add(new AssemblyAttribute("AssemblyTitle", conf.Project.Name));
attributes.Add(new AssemblyAttribute("AssemblyDescription", _description));
attributes.Add(new AssemblyAttribute("AssemblyConfiguration", ""));
attributes.Add(new AssemblyAttribute("AssemblyCompany", "Ubisoft"));
attributes.Add(new AssemblyAttribute("AssemblyProduct", conf.Project.Name));
attributes.Add(new AssemblyAttribute("AssemblyCopyright", "Copyright (C) Ubisoft. All rights reserved."));
attributes.Add(new AssemblyAttribute("AssemblyTrademark", ""));
attributes.Add(new AssemblyAttribute("AssemblyCulture", ""));
attributes.Add(new AssemblyAttribute("AssemblyVersion", version));
attributes.Add(new AssemblyAttribute("AssemblyFileVersion", version));
attributes.Add(new AssemblyAttribute("AssemblyInformationalVersion", _changelist.ToString()));
attributes.Add(new AssemblyAttribute("AssemblyMetadata", "BuildPerforceChangelist", _changelist.ToString()));
attributes.Add(new AssemblyAttribute("AssemblyMetadata", "BuildPerforceClientName", _clientName));
attributes.Add(new AssemblyAttribute("AssemblyMetadata", "BuildUserName", Environment.UserName));
attributes.Add(new AssemblyAttribute("AssemblyMetadata", "BuildMachineName", Environment.MachineName));
attributes.Add(new AssemblyAttribute("AssemblyMetadata", "BuildCreationTime", DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture)));
if (conf.ReferencesByName.Contains("PresentationFramework"))
{
attributes.Add(new AssemblyAttribute("ThemeInfo", false, "ResourceDictionaryLocation.None", "ResourceDictionaryLocation.SourceAssembly"));
usings.Add("System.Windows");
}
var content = string.Format(Template,
string.Join("\n", usings.Select(x => "using " + x + ";")),
string.Join("\n", attributes.Select(x => "[assembly: " + x.ToString() + "]")));
File.WriteAllText(_assemblyFilePath, content);
}
private const string Template =
@"{0}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
{1}
";
private static void TryGetPerforceInformation(string path, out int changelist, out string clientName)
{
changelist = 0;
clientName = "";
try
{
GetPerforceInformation(path, out changelist, out clientName);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void GetPerforceInformation(string path, out int changelist, out string clientName)
{
changelist = 0;
clientName = "";
var codePerforce = "p4-jd-code-mrc.ubisoft.org:1920";
var perforce = new PerforceWrapper("", Environment.UserName, "", codePerforce, true);
var client = perforce.GetClients(Environment.UserName, Environment.MachineName).FirstOrDefault(x => Path.GetFullPath(path).ToLower().StartsWith(Path.GetFullPath(x.Root).ToLower()));
if (client != null)
{
clientName = client.ClientName;
}
else
{
return;
}
var result = perforce.Run("changes", "-m1", "@" + clientName);
if (result.Records.Length == 0)
return;
if (result.Records[0].Fields.ContainsKey("change"))
{
int.TryParse(result.Records[0].Fields["change"], out changelist);
}
}
private class AssemblyAttribute
{
public AssemblyAttribute(string name, params string[] parameters)
{
Name = name;
Parameters = parameters;
QuoteArguments = true;
}
public AssemblyAttribute(string name, bool quoteArguments, params string[] parameters)
{
Name = name;
Parameters = parameters;
QuoteArguments = quoteArguments;
}
public string Name { get; set; }
public string[] Parameters { get; set; }
public bool QuoteArguments { get; set; }
/// <inheritdoc />
public override string ToString()
{
var parameters = QuoteArguments ? Parameters.Select(x => "\"" + x.ToString() + "\"") : Parameters.Select(x => x.ToString());
return Name + "(" + string.Join(", ", parameters) + ")";
}
}
}
}