Making version maintaining easier (only update in SubtitleEdit AssemblyInfo.cs - libse auto updated)

This commit is contained in:
niksedk 2015-08-27 22:41:16 +02:00
parent a546bdacb1
commit 6e438005b0
6 changed files with 175 additions and 111 deletions

View File

@ -38,9 +38,9 @@ IF NOT EXIST "src\UpdateAssemblyInfo\bin\Release\UpdateAssemblyInfo.exe" IF NOT
) )
IF EXIST "src\UpdateAssemblyInfo\bin\Release\UpdateAssemblyInfo.exe" ( IF EXIST "src\UpdateAssemblyInfo\bin\Release\UpdateAssemblyInfo.exe" (
"src\UpdateAssemblyInfo\bin\Release\UpdateAssemblyInfo.exe" "src\Properties\AssemblyInfo.cs.template" "src\Properties\AssemblyInfo.cs" "src\UpdateAssemblyInfo\bin\Release\UpdateAssemblyInfo.exe" "src\Properties\AssemblyInfo.cs.template" "libse\Properties\AssemblyInfo.cs.template"
) ELSE ( ) ELSE (
"src\UpdateAssemblyInfo\bin\Debug\UpdateAssemblyInfo.exe" "src\Properties\AssemblyInfo.cs.template" "src\Properties\AssemblyInfo.cs" "src\UpdateAssemblyInfo\bin\Debug\UpdateAssemblyInfo.exe" "src\Properties\AssemblyInfo.cs.template" "libse\Properties\AssemblyInfo.cs.template"
) )
IF %ERRORLEVEL% NEQ 0 ( IF %ERRORLEVEL% NEQ 0 (

View File

@ -437,6 +437,11 @@
<Compile Include="ZipExtractor.cs" /> <Compile Include="ZipExtractor.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>IF NOT EXIST "$(ProjectDir)Properties\AssemblyInfo.cs" (
COPY "$(ProjectDir)Properties\AssemblyInfo.cs.template" "$(ProjectDir)Properties\AssemblyInfo.cs"
)</PreBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">

View File

@ -1,35 +0,0 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("libse")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("libse")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("eab3b9db-0f82-4843-982b-05a4c51888a2")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,15 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("libse")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("libse")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("eab3b9db-0f82-4843-982b-05a4c51888a2")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1547,7 +1547,7 @@ namespace Nikse.SubtitleEdit.Core
{ {
get get
{ {
return Assembly.GetExecutingAssembly().GetName().Version.ToString(); return Assembly.GetEntryAssembly().GetName().Version.ToString();
} }
} }
@ -1555,15 +1555,14 @@ namespace Nikse.SubtitleEdit.Core
{ {
get get
{ {
Assembly assy = Assembly.GetExecutingAssembly(); var assembly = Assembly.GetEntryAssembly();
String assyName = assy.GetName().Name; string assemblyName = assembly.GetName().Name;
bool isdef = Attribute.IsDefined(assy, typeof(AssemblyDescriptionAttribute)); if (Attribute.IsDefined(assembly, typeof(AssemblyDescriptionAttribute)))
if (isdef)
{ {
Console.WriteLine(assyName); Console.WriteLine(assemblyName);
var adAttr = (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(assy, typeof(AssemblyDescriptionAttribute)); var descriptionAttribute = (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyDescriptionAttribute));
if (adAttr != null) if (descriptionAttribute != null)
return adAttr.Description; return descriptionAttribute.Description;
} }
return null; return null;
} }

View File

@ -7,96 +7,171 @@ namespace UpdateAssemblyInfo
internal class Program internal class Program
{ {
private class Template private class VersionInfo
{ {
private string templateFile; public string Version { get; set; }
private string templateText; public string RevisionGuid { get; set; }
public string BuildNumber { get; set; }
public Template(string path)
{
templateFile = path;
} }
public void Replace(string source, string replacement) private static void UpdateAssemblyInfo(string templateFileName, VersionInfo versionInfo)
{ {
if (templateText == null) var lines = File.ReadAllLines(templateFileName);
var sb = new StringBuilder();
bool change = false;
foreach (var line in lines)
{ {
templateText = File.ReadAllText(templateFile); var original = line;
var l = line.Trim();
while (l.Contains(" "))
{
l = l.Replace(" ", " ");
}
if (l.StartsWith("[assembly: AssemblyVersion", StringComparison.Ordinal) ||
l.StartsWith("[assembly:AssemblyVersion", StringComparison.Ordinal) ||
l.StartsWith("[assembly: AssemblyFileVersion", StringComparison.Ordinal) ||
l.StartsWith("[assembly:AssemblyFileVersion", StringComparison.Ordinal))
{
int begin = original.IndexOf('"');
int end = original.LastIndexOf('"');
if (end > begin && begin > 0)
{
begin++;
string oldVersion = original.Substring(begin, end - begin);
if (oldVersion != versionInfo.Version)
{
change = true;
original = original.Substring(0, begin) + versionInfo.Version + original.Remove(0, end);
}
}
}
else if (l.StartsWith("[assembly: AssemblyDescription", StringComparison.Ordinal) ||
l.StartsWith("[assembly:AssemblyDescription", StringComparison.Ordinal))
{
int begin = original.IndexOf('"');
int end = original.LastIndexOf('"');
if (end > begin && begin > 0)
{
begin++;
string oldVersion = original.Substring(begin, end - begin);
if (oldVersion != versionInfo.Version)
{
change = true;
original = original.Substring(0, begin) + versionInfo.RevisionGuid + original.Remove(0, end);
}
}
}
sb.AppendLine(original);
}
if (change)
{
File.WriteAllText(templateFileName.Replace(".template", string.Empty), sb.ToString().Trim());
} }
templateText = templateText.Replace(source, replacement);
} }
public void Save(string target) private static VersionInfo GetOldVersionNumber(string subtitleEditTemplateFileName)
{ {
File.WriteAllText(target, templateText, Encoding.UTF8); var version = new VersionInfo { Version = "1.0.0.0", RevisionGuid = "0" };
var lines = File.ReadAllLines(subtitleEditTemplateFileName.Replace(".template", string.Empty));
foreach (var line in lines)
{
var l = line.Trim();
while (l.Contains(" "))
{
l = l.Replace(" ", " ");
} }
if (l.StartsWith("[assembly: AssemblyVersion", StringComparison.Ordinal) ||
l.StartsWith("[assembly:AssemblyVersion", StringComparison.Ordinal) ||
l.StartsWith("[assembly: AssemblyFileVersion", StringComparison.Ordinal) ||
l.StartsWith("[assembly:AssemblyFileVersion", StringComparison.Ordinal))
{
int begin = l.IndexOf('"');
int end = l.LastIndexOf('"');
if (end > begin && begin > 0)
{
begin++;
version.Version = l.Substring(begin, end - begin);
version.BuildNumber = version.Version.Substring(version.Version.LastIndexOf('.') + 1);
}
}
else if (l.StartsWith("[assembly: AssemblyDescription", StringComparison.Ordinal) ||
l.StartsWith("[assembly:AssemblyDescription", StringComparison.Ordinal))
{
int begin = l.IndexOf("\"", StringComparison.Ordinal);
int end = l.LastIndexOf("\"", StringComparison.Ordinal);
if (end > begin && begin > 0)
{
begin++;
version.RevisionGuid = l.Substring(begin, end - begin);
}
}
}
if (string.IsNullOrWhiteSpace(version.Version))
{
Console.WriteLine("WARNING: Could not find version number - will use 1.0.0");
}
return version;
}
private static VersionInfo GetNewVersion()
{
var version = new VersionInfo { Version = "1.0.0.0", RevisionGuid = "0", BuildNumber = "9999" };
var workingFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var clrHash = new CommandLineRunner();
var clrTags = new CommandLineRunner();
var gitPath = GetGitPath();
if (clrHash.RunCommandAndGetOutput(gitPath, "rev-parse --verify HEAD", workingFolder) && clrTags.RunCommandAndGetOutput(gitPath, "describe --tags", workingFolder))
{
version.RevisionGuid = clrHash.Result;
version.Version = clrTags.Result.Substring(0, clrTags.Result.LastIndexOf('-'));
version.Version = version.Version.Replace("-", ".");
version.BuildNumber = version.Version.Remove(0, version.Version.LastIndexOf('.')).Trim('.');
}
if (version.RevisionGuid == "0" && version.BuildNumber == "9999")
{
Console.WriteLine("WARNING: Could not run Git - build number will be 9999!");
}
return version;
} }
private static int Main(string[] args) private static int Main(string[] args)
{ {
var myName = Environment.GetCommandLineArgs()[0]; var myName = Environment.GetCommandLineArgs()[0];
myName = Path.GetFileNameWithoutExtension(string.IsNullOrWhiteSpace(myName) myName = Path.GetFileNameWithoutExtension(string.IsNullOrWhiteSpace(myName) ? System.Reflection.Assembly.GetEntryAssembly().Location : myName);
? System.Reflection.Assembly.GetEntryAssembly().Location
: myName);
if (args.Length != 2) if (args.Length != 2)
{ {
Console.Write("Usage: " + myName + @" <template> <target> Console.WriteLine("Usage: " + myName + " <se-assmbly-template> <libse-assmbly-template>");
<template> Path to the template file with [GITHASH] and [REVNO]
<target> Path to the target file (AssemblyInfo.cs)
");
return 1; return 1;
} }
Console.WriteLine("Updating assembly info..."); try
var workingFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); {
var templateFile = args[0]; var seTemplateFileName = Environment.GetCommandLineArgs()[1];
var targetFile = args[1]; var libSeTmplateFileName = Environment.GetCommandLineArgs()[2];
var template = new Template(templateFile); Console.Write("Updating version number... ");
var oldSeVersion = GetOldVersionNumber(seTemplateFileName);
var oldLibSeVersion = GetOldVersionNumber(libSeTmplateFileName);
var newVersion = GetNewVersion();
var clrHash = new CommandLineRunner(); if (oldSeVersion.RevisionGuid != newVersion.RevisionGuid || oldSeVersion.Version != newVersion.Version ||
var clrTags = new CommandLineRunner(); oldLibSeVersion.RevisionGuid != newVersion.RevisionGuid || oldLibSeVersion.Version != newVersion.Version)
var gitPath = GetGitPath();
string exceptionMessage;
if (clrHash.RunCommandAndGetOutput(gitPath, "rev-parse --verify HEAD", workingFolder) &&
clrTags.RunCommandAndGetOutput(gitPath, "describe --tags", workingFolder))
{ {
try Console.WriteLine("updating version number to " + newVersion.Version + " " + newVersion.RevisionGuid);
{ UpdateAssemblyInfo(seTemplateFileName, newVersion);
template.Replace("[GITHASH]", clrHash.Result); UpdateAssemblyInfo(libSeTmplateFileName, newVersion);
if (clrTags.Result.IndexOf('-') < 0)
clrTags.Result += "-0";
template.Replace("[REVNO]", clrTags.Result.Split('-')[1]);
template.Save(targetFile);
return 0; return 0;
} }
catch (Exception ex) Console.WriteLine("no changes");
{
exceptionMessage = ex.Message;
}
}
else
{
try
{
// allow to compile without git
Console.WriteLine("WARNING: Could not run Git - build number will be 9999!");
template.Replace("[GITHASH]", string.Empty);
template.Replace("[REVNO]", "9999");
template.Save(targetFile);
return 0; return 0;
} }
catch (Exception ex) catch (Exception exception)
{ {
exceptionMessage = ex.Message; Console.WriteLine("Something bad happened when running " + myName + ": " + exception.Message + Environment.NewLine + exception.StackTrace);
return 0;
} }
} }
Console.WriteLine(myName + ": Could not update AssemblyInfo: " + exceptionMessage);
Console.WriteLine(" - Git folder: " + workingFolder);
Console.WriteLine(" - Template: " + templateFile);
Console.WriteLine(" - Target: " + targetFile);
return 1;
}
private static string GetGitPath() private static string GetGitPath()
{ {
@ -130,6 +205,10 @@ namespace UpdateAssemblyInfo
} }
var envSystemDrive = Environment.GetEnvironmentVariable("SystemDrive"); var envSystemDrive = Environment.GetEnvironmentVariable("SystemDrive");
if (string.IsNullOrEmpty(envSystemDrive))
{
throw new Exception("Environment.GetEnvironmentVariable('SystemDrive') returned null!");
}
if (!string.IsNullOrWhiteSpace(envSystemDrive)) if (!string.IsNullOrWhiteSpace(envSystemDrive))
{ {
var path = Path.Combine(envSystemDrive, "Program Files", gitPath); var path = Path.Combine(envSystemDrive, "Program Files", gitPath);
@ -163,8 +242,9 @@ namespace UpdateAssemblyInfo
return path; return path;
} }
} }
catch catch (Exception exception)
{ {
throw new Exception("UpdateAssemblyInfo - GetGitPath: " + exception.Message);
} }
Console.WriteLine("WARNING: Might not be able to run Git command line tool!"); Console.WriteLine("WARNING: Might not be able to run Git command line tool!");