Shims.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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(PathConstruction.AbsolutePath target, params string[] paths) => Zip(target, paths.AsEnumerable());
  20. private void Zip(PathConstruction.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. var relPath = Path.GetRelativePath(rootPath, fsEntry.FullName);
  48. AddFile(fsEntry.FullName, relPath);
  49. }
  50. }
  51. }
  52. else if(File.Exists(path))
  53. {
  54. var name = Path.GetFileName(path);
  55. AddFile(path, name);
  56. }
  57. }
  58. }
  59. finished = true;
  60. }
  61. finally
  62. {
  63. try
  64. {
  65. if (!finished || !atLeastOneFileAdded)
  66. File.Delete(targetPath);
  67. }
  68. catch
  69. {
  70. //Ignore
  71. }
  72. }
  73. }
  74. class NumergeNukeLogger : INumergeLogger
  75. {
  76. public void Log(NumergeLogLevel level, string message)
  77. {
  78. if(level == NumergeLogLevel.Error)
  79. Logger.Error(message);
  80. else if (level == NumergeLogLevel.Warning)
  81. Logger.Warn(message);
  82. else
  83. Logger.Info(message);
  84. }
  85. }
  86. }