Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssemblyVersionProcessor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Reflection;
7 
8 using Mono.Cecil;
9 
10 namespace SiliconStudio.AssemblyProcessor
11 {
13  {
14  public bool Process(AssemblyProcessorContext context)
15  {
16  var assembly = context.Assembly;
17  var mscorlibAssembly = CecilExtensions.FindCorlibAssembly(assembly);
18  if (mscorlibAssembly == null)
19  throw new InvalidOperationException("Missing mscorlib.dll from assembly");
20 
21 
22  // Resolve mscorlib types
23  var assemblyFileVersionAttributeType = mscorlibAssembly.MainModule.GetTypeResolved(typeof(AssemblyFileVersionAttribute).FullName);
24  var assemblyMethodConstructor = assembly.MainModule.Import(assemblyFileVersionAttributeType.Methods.FirstOrDefault(method => method.IsConstructor && method.Parameters.Count == 1));
25  var stringType = mscorlibAssembly.MainModule.GetTypeResolved(typeof(string).FullName);
26 
27  // TODO: Git Commit SHA
28  var gitCommitShortId = "0";
29 
30  // Use epoch time to get a "unique" build number (different each time)
31  var build = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
32 
33  // Get current AssemblyVersion and clone it
34  var version = assembly.Name.Version;
35  var fileVersion = string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, build, gitCommitShortId);
36 
37  // Copy build/revision to the AssemblyFileVersion
38  bool fileVersionUpdated = false;
39  for (int i = 0; i < assembly.CustomAttributes.Count; i++)
40  {
41  var customAttribute = assembly.CustomAttributes[i];
42  if (customAttribute.AttributeType.FullName == typeof(AssemblyFileVersionAttribute).FullName)
43  {
44  customAttribute.ConstructorArguments.Clear();
45  customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(stringType, fileVersion));
46  fileVersionUpdated = true;
47  break;
48  }
49  }
50 
51  if (!fileVersionUpdated)
52  {
53  var assemblyFileVersion = new CustomAttribute(assemblyMethodConstructor);
54  assemblyFileVersion.ConstructorArguments.Add(new CustomAttributeArgument(stringType, fileVersion));
55  assembly.CustomAttributes.Add(assemblyFileVersion);
56  }
57 
58  return true;
59  }
60  }
61 }