protected override void FinishItemCore() { if (!Directory.Exists(_folder)) { throw new SwixSemanticException(CurrentLine, $"Directory '{_folder}' does not exists"); } var excludePathRegex = PrepareExcludeRegex(); if (!_directlySetAttributes.TryGetValue("filter", out string filter)) { filter = "*.*"; } bool withSubfolders = _directlySetAttributes.ContainsKey("withSubfolders") && _directlySetAttributes["withSubfolders"] == "yes"; var manuallySpecifiedSourcePaths = new HashSet <string>(GatheredComponents.Select(c => Path.GetFullPath(c.SourcePath)), StringComparer.OrdinalIgnoreCase); var harvestedComponents = new List <WixComponent>(); var folder = Path.GetFullPath(_folder); // eliminates relative parts like a\b\..\b2\c var files = Directory.GetFiles(folder, filter, withSubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); foreach (var filepath in files) { if (manuallySpecifiedSourcePaths.Contains(filepath)) { continue; } int harvestingRootSubstringLength = folder.Length; if (filepath[harvestingRootSubstringLength] == '\\') { harvestingRootSubstringLength++; } var relativeToHarvestingRoot = filepath.Substring(harvestingRootSubstringLength); if (excludePathRegex.IsMatch(relativeToHarvestingRoot)) { continue; } var relativeDir = Path.GetDirectoryName(relativeToHarvestingRoot); try { var component = WixComponent.FromContext(filepath, CurrentAttributeContext); if (relativeDir != null) { component.TargetDir = component.TargetDir == null ? relativeDir : Path.Combine(component.TargetDir, relativeDir); } harvestedComponents.Add(component); } catch (SwixItemParsingException e) { throw new SwixSemanticException(CurrentLine, $"During harvesting of {filepath} file, exception occurs:\n{e}"); } } GatheredComponents.AddRange(harvestedComponents); base.FinishItemCore(); }
public ZipSemanticContext(int line, string archivePath, IAttributeContext context, List <WixComponent> components) : base(line, context, components) { var originalFrom = context.GetInheritedAttribute("from"); if (originalFrom != null) { archivePath = Path.Combine(originalFrom, archivePath); } if (!File.Exists(archivePath)) { throw new SwixSemanticException(line, $"File {archivePath} not found"); } var tmp = SwixProcessor.TempDir; var g = context.GuidProvider.Get(SwixGuidType.ZipArchive, archivePath.ToLowerInvariant()).ToString("N"); var unpackedPath = Path.GetFullPath(Path.Combine(tmp, g)); try { ZipFile.ExtractToDirectory(archivePath, unpackedPath); } catch (Exception e) { throw new SwixSemanticException(line, $"Error while unzipping {archivePath} to {unpackedPath}: {e}"); } context.SetAttributes(new[] { new AhlAttribute("fromBase", unpackedPath), new AhlAttribute("from", null) }); int rootSubstringLength = unpackedPath.Length; var files = Directory.GetFiles(unpackedPath); foreach (string path in files) { var component = WixComponent.FromContext(path, context); if (path[rootSubstringLength] == '\\') { rootSubstringLength++; } var relativeDir = Path.GetDirectoryName(path.Substring(rootSubstringLength)); if (relativeDir != null) { component.TargetDir = component.TargetDir == null ? relativeDir : Path.Combine(component.TargetDir, relativeDir); } GatheredComponents.Add(component); } }
public virtual ISemanticContext Component(string key, IAttributeContext itemContext) { try { var component = WixComponent.FromContext(key, itemContext); var itemSemanticContext = new ComponentItem(CurrentLine, CurrentAttributeContext, component); itemSemanticContext.OnFinished += (s, e) => _toAdd.Add(component); return(itemSemanticContext); } catch (SwixSemanticException e) { throw new SwixSemanticException(CurrentLine, e.Message); } }