Shims.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using Nuke.Common;
  7. using Nuke.Common.IO;
  8. using Numerge;
  9. public partial class Build
  10. {
  11. static void Information(string info)
  12. {
  13. Logger.Info(info);
  14. }
  15. static void Information(string info, params object[] args)
  16. {
  17. Logger.Info(info, args);
  18. }
  19. private void Zip(AbsolutePath target, params string[] paths) => Zip(target, paths.AsEnumerable());
  20. private void Zip(AbsolutePath target, IEnumerable<string> paths)
  21. {
  22. var targetPath = target.ToString();
  23. bool finished = false, atLeastOneFileAdded = false;
  24. try
  25. {
  26. using (var targetStream = File.Create(targetPath))
  27. using(var archive = new System.IO.Compression.ZipArchive(targetStream, ZipArchiveMode.Create))
  28. {
  29. void AddFile(string path, string relativePath)
  30. {
  31. var e = archive.CreateEntry(relativePath.Replace("\\", "/"), CompressionLevel.Optimal);
  32. using (var entryStream = e.Open())
  33. using (var fileStream = File.OpenRead(path))
  34. fileStream.CopyTo(entryStream);
  35. atLeastOneFileAdded = true;
  36. }
  37. foreach (var path in paths)
  38. {
  39. if (Directory.Exists(path))
  40. {
  41. var dirInfo = new DirectoryInfo(path);
  42. var rootPath = Path.GetDirectoryName(dirInfo.FullName);
  43. foreach(var fsEntry in dirInfo.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
  44. {
  45. if (fsEntry is FileInfo)
  46. {
  47. #if NET6
  48. var relPath = Path.GetRelativePath(rootPath, fsEntry.FullName);
  49. #else
  50. var relPath = GetRelativePath(rootPath, fsEntry.FullName);
  51. #endif
  52. AddFile(fsEntry.FullName, relPath);
  53. }
  54. }
  55. }
  56. else if(File.Exists(path))
  57. {
  58. var name = Path.GetFileName(path);
  59. AddFile(path, name);
  60. }
  61. }
  62. }
  63. finished = true;
  64. }
  65. finally
  66. {
  67. try
  68. {
  69. if (!finished || !atLeastOneFileAdded)
  70. File.Delete(targetPath);
  71. }
  72. catch
  73. {
  74. //Ignore
  75. }
  76. }
  77. }
  78. private static string GetRelativePath(string relativeTo, string path)
  79. {
  80. var uri = new Uri(relativeTo);
  81. var rel = Uri.UnescapeDataString(uri.MakeRelativeUri(new Uri(path)).ToString()).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
  82. if (rel.Contains(Path.DirectorySeparatorChar.ToString()) == false)
  83. {
  84. rel = $".{Path.DirectorySeparatorChar}{rel}";
  85. }
  86. return rel;
  87. }
  88. class NumergeNukeLogger : INumergeLogger
  89. {
  90. public void Log(NumergeLogLevel level, string message)
  91. {
  92. if(level == NumergeLogLevel.Error)
  93. Logger.Error(message);
  94. else if (level == NumergeLogLevel.Warning)
  95. Logger.Warn(message);
  96. else
  97. Logger.Info(message);
  98. }
  99. }
  100. }