private List <string> GetAllReferences(string dacpacPath, bool isRootDacpac) { if (!isRootDacpac && !File.Exists(dacpacPath)) { // When the root DACPAC has a reference with SuppressMissingDependenciesErrors = true, // second-level references of that first-level reference don't have to be referenced from the root project. // For that case, DACPACs not referenced directly by the root DACPAC are optional. return(Enumerable.Empty <string>().ToList()); } var parser = new HeaderParser(dacpacPath); var references = parser.GetCustomData() .Where(p => p.Category == "Reference" && p.Type == "SqlSchema") .ToList(); if (references.Count == 0) { return(Enumerable.Empty <string>().ToList()); } var fileNames = references .SelectMany(r => r.Items) .Where(i => i.Name == "LogicalName") .Where(i => !i.Value.Equals("master.dacpac", StringComparison.OrdinalIgnoreCase)) .Select(i => Path.GetFullPath(Path.Combine(Path.GetDirectoryName(dacpacPath), i.Value))) .ToList(); if (!isRootDacpac) { // If we're looking for references of a non-root DACPAC, // any reference is optional (see SuppressMissingDependenciesErrors = true). // Therefore this DACPACs won't be included, if they don't exist in the build output directory. fileNames = fileNames.Where(File.Exists).ToList(); } if (fileNames.Count() == 0) { return(Enumerable.Empty <string>().ToList()); } var additionalFiles = new List <string>(); foreach (var fileName in fileNames) { additionalFiles.AddRange(GetAllReferences(fileName, false)); } fileNames.AddRange(additionalFiles); return(fileNames.Distinct().ToList()); }
public void Can_Delete_Reference() { var parser = new HeaderParser(".\\Test.dacpac"); string fileName = string.Format("c:\\bloonblah{0}.dacpac", Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "")); const string logicalName = "blooblah.dacpac"; const string externalParts = "[$(blooblah)]"; const string suppressMissingDependenciesErrors = "False"; var newCustomData = new CustomData("Reference", "SqlSchema"); newCustomData.AddMetadata("FileName", fileName); newCustomData.AddMetadata("LogicalName", logicalName); newCustomData.AddMetadata("ExternalParts", externalParts); newCustomData.AddMetadata("SupressMissingDependenciesErrors", suppressMissingDependenciesErrors); var writer = new HeaderWriter(".\\Test.dacpac", new DacHacFactory()); writer.AddCustomData(newCustomData); writer.Close(); var actualItem = parser.GetCustomData() .Where( p => p.Category == "Reference" && p.Type == "SqlSchema" && p.Items.Any(item => item.Name == "FileName" && item.Value == fileName)); writer = new HeaderWriter(".\\Test.dacpac", new DacHacFactory()); writer.DeleteCustomData(newCustomData); writer.Close(); actualItem = parser.GetCustomData() .Where( p => p.Category == "Reference" && p.Type == "SqlSchema" && p.Items.Any(item => item.Name == "FileName" && item.Value == fileName)); Assert.IsNotNull(actualItem); }