2
0

switch_version.props 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project DefaultTargets="Build" InitialTargets="GitVersion" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3. <PropertyGroup>
  4. <GitSkipCache>true</GitSkipCache>
  5. </PropertyGroup>
  6. <ImportGroup Label="PropertySheets">
  7. <Import Project="basedir.props" Condition=" '$(BaseDirImported)' == ''"/>
  8. <Import Project="Setup\GitInfo\GitInfo.targets" />
  9. </ImportGroup>
  10. <PropertyGroup>
  11. <switch_versionPropsImported>true</switch_versionPropsImported>
  12. </PropertyGroup>
  13. <UsingTask TaskName="SwitchVersionTask"
  14. TaskFactory="CodeTaskFactory"
  15. AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  16. <ParameterGroup>
  17. <commits Required="true" />
  18. <revision Required="true" />
  19. </ParameterGroup>
  20. <Task>
  21. <Reference Include="Microsoft.Build" />
  22. <Reference Include="Microsoft.Build.Framework" />
  23. <Code Type="Class" Language="cs">
  24. <![CDATA[
  25. using System;
  26. using System.IO;
  27. using System.Collections.Generic;
  28. using System.Text.RegularExpressions;
  29. using Microsoft.Build.Framework;
  30. public class SwitchVersionTask : Microsoft.Build.Utilities.Task
  31. {
  32. [Required]
  33. public string revision { get; set; }
  34. public string commits { get; set; }
  35. private string basedir;
  36. private string TemplatePath;
  37. private string DestinationPath;
  38. public override bool Execute()
  39. {
  40. basedir = Path.GetFullPath(@"$(BaseDir)");
  41. int commit_count = 0;
  42. Int32.TryParse(commits, out commit_count);
  43. Log.LogMessage(MessageImportance.High,
  44. "Parsing FreeSWITCH version.");
  45. string ConfigureAC = File.ReadAllText(@"$(BaseDir)configure.ac");
  46. string pattern = @"AC_SUBST\((SWITCH_VERSION_[A-Z_]+),.*\[(.*)\]\)";
  47. Dictionary<string, string> v = new Dictionary<string, string>();
  48. string year = DateTime.UtcNow.Year.ToString();
  49. v.Add("SWITCH_VERSION_YEAR", year);
  50. foreach (Match m in Regex.Matches(ConfigureAC, pattern)) {
  51. string value = m.Groups[2].Value.Trim();
  52. if (value.Contains("-")) {
  53. string[] tokens = value.Split('-');
  54. value = tokens[0];
  55. }
  56. var name = m.Groups[1].Value.Trim();
  57. value = value.Trim();
  58. if (name.StartsWith("SWITCH_VERSION_REVISION")) {
  59. if (string.IsNullOrWhiteSpace(value)) {
  60. value = revision;
  61. }
  62. if (name != "SWITCH_VERSION_REVISION_HUMAN") {
  63. value = "~" + value;
  64. if (commit_count > 0) {
  65. value = "-dev-"+ commits + value;
  66. } else {
  67. value = "-release" + value;
  68. }
  69. }
  70. }
  71. v.Add(name, value);
  72. Log.LogMessage(MessageImportance.High, name + " = '" + value + "'");
  73. }
  74. //---------------------------------------------------------
  75. Log.LogMessage(MessageImportance.High,
  76. "Generating switch_version.h");
  77. TemplatePath = basedir + @"src\include\switch_version.h.template";
  78. DestinationPath = basedir + @"src\include\switch_version.h";
  79. string template = File.ReadAllText(TemplatePath);
  80. foreach (var version_bit in v) {
  81. template = template.Replace("@" + version_bit.Key + "@", version_bit.Value);
  82. }
  83. File.WriteAllText(DestinationPath, template);
  84. //---------------------------------------------------------
  85. Log.LogMessage(MessageImportance.High,
  86. "Generating switch_version.inc");
  87. TemplatePath = @"$(ProjectDir)\switch_version.inc.template";
  88. DestinationPath = @"$(ProjectDir)\switch_version.inc";
  89. template = File.ReadAllText(TemplatePath);
  90. foreach (var version_bit in v) {
  91. template = template.Replace("@" + version_bit.Key + "@", version_bit.Value);
  92. }
  93. File.WriteAllText(DestinationPath, template);
  94. //---------------------------------------------------------
  95. return true;
  96. }
  97. }
  98. ]]>
  99. </Code>
  100. </Task>
  101. </UsingTask>
  102. <Target Name="SwitchVersionTarget" BeforeTargets="CustomBuild;Build">
  103. <SwitchVersionTask commits="$(GitCommits)" revision="$(GitCommit)">
  104. </SwitchVersionTask>
  105. </Target>
  106. </Project>