DynamicInstaller.cs (2650B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 4 namespace WinGetSourceCreator.Model 5 { 6 using Microsoft.WinGetSourceCreator; 7 using System.IO.Compression; 8 9 public class DynamicInstaller : Installer 10 { 11 // Input depends on the Type. 12 // For zip it is the directories or files that need to included in the zip 13 public List<string> Input { get; set; } = new List<string>(); 14 15 internal new void Validate() 16 { 17 base.Validate(); 18 } 19 20 public string Create(string workingDirectory) 21 { 22 string outputFile = this.Name; 23 if (!Path.IsPathFullyQualified(outputFile)) 24 { 25 outputFile = Path.Combine(workingDirectory, outputFile); 26 } 27 28 var parent = Path.GetDirectoryName(outputFile); 29 if (!string.IsNullOrEmpty(parent)) 30 { 31 Directory.CreateDirectory(parent); 32 } 33 34 if (this.Type == InstallerType.Zip) 35 { 36 CreateZipInstaller(outputFile); 37 } 38 else 39 { 40 throw new NotImplementedException(); 41 } 42 43 return outputFile; 44 } 45 46 private void CreateZipInstaller(string outputFile) 47 { 48 var tmpPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); 49 if (Directory.Exists(tmpPath)) 50 { 51 Directory.Delete(tmpPath, true); 52 } 53 Directory.CreateDirectory(tmpPath); 54 55 foreach (var input in this.Input) 56 { 57 if (!Path.IsPathFullyQualified(input)) 58 { 59 throw new InvalidOperationException($"Must be a fully qualified name {input}"); 60 } 61 62 if (File.Exists(input)) 63 { 64 // TODO: maybe we want to preserve the dir? 65 File.Copy(input, Path.Combine(tmpPath, Path.GetFileName(input)), true); 66 } 67 else if (Directory.Exists(input)) 68 { 69 Helpers.CopyDirectory(input, tmpPath); 70 } 71 else 72 { 73 throw new InvalidOperationException(input); 74 } 75 } 76 77 ZipFile.CreateFromDirectory(tmpPath, outputFile); 78 79 try 80 { 81 Directory.Delete(tmpPath, true); 82 } 83 catch (Exception) 84 { 85 } 86 } 87 } 88 }