private static void Pack(FolderElement info, bool openFolder = true) { string zipTargetPath; using (var tempFolder = new TempDirectory(info.Target)) { Print.Info($"Start Copying: {info.Source} --> {info.Target}"); info.Flatten() .Where(item => !(item is FolderElement folder) || !folder.Files.Any() && !folder.Folders.Any()) .ForEach(item => item.Source.CopyTo(item.Target)); Print.Info("Completed Copying. "); zipTargetPath = $"{tempFolder.Path}.zip"; Print.Info($"Start Package: {zipTargetPath}..."); if (File.Exists(zipTargetPath)) { File.Delete(zipTargetPath); } ZipFile.CreateFromDirectory( tempFolder.Path, zipTargetPath, CompressionLevel.Optimal, true); } if (openFolder) { Print.Info($"Opening the file: {zipTargetPath}..."); Process.Start("explorer.exe", $"/select, {zipTargetPath}"); } }
public static IEnumerable <FileElement> Flatten(this FolderElement @this) { yield return(@this); foreach (var file in @this.Files) { yield return(file); } foreach (var folder in @this.Folders) { foreach (var element in folder.Flatten()) { yield return(element); } } }
protected static T FileElementPipe <T>(T element, XElement node, FolderElement parent) where T : FileElement { element.Parent = parent; element.Target = node.Attribute(nameof(Target))?.Value; element.Source = node.Attribute(nameof(Source))?.Value; if (string.IsNullOrWhiteSpace(element.Source)) { element.Source = Path.Combine(parent.Source, element.Target ?? throw new InvalidOperationException()); } element.Source = element.Source .ReplaceConfigurationPlaceholder() .ReplaceVersionPlaceholder() .GetFullPath(parent?.Source); element.Target = element.Target .ReplaceConfigurationPlaceholder() .ReplaceVersionPlaceholder(element.Source) .GetFullPath(parent?.Target); return(element); }
private static void TryPack(string path, bool openFolder = true) { var name = Path.GetFileName(path); try { Print.Divider(); Print.Info($">>> Start executing {name} file..."); BaseDirectory = Path.GetDirectoryName(path); var element = FolderElement.Create(File.ReadAllText(path)); Pack(element, openFolder); Print.Info($"<<< Completed the {name} file. "); } catch (FileNotFoundException e) { Print.Error($"<In {name}> Missing {e.FileName}!"); } catch (Exception e) { Print.Error($"<In {name}> {e.Message}"); } }
public static FileElement Create(XElement node, FolderElement parent) { return(FileElementPipe(new FileElement(), node, parent)); }