private static async Task <XDocument> MigrateBundle(XDocument doc, string fileName, string root, string folder) { string[] attrNames = new[] { "runOnBuild", "minify", "output" }; XElement bundle = doc.Descendants("bundle").FirstOrDefault(); string[] attributes = bundle.Attributes() .Where(a => attrNames.Contains(a.Name.ToString())) .Select(a => a.Name.ToString()) .ToArray(); if (attributes.Count() == 0) { return(doc); } IEnumerable <string> constituentFiles = ProjectHelpers.GetBundleConstituentFiles(doc.Descendants("file").Select(s => s.Value), root, folder, fileName); BundleDocument newDoc = new BundleDocument(fileName, constituentFiles.ToArray()); if (attributes.Contains("runOnBuild")) { newDoc.RunOnBuild = bundle.Attribute("runOnBuild").Value.Equals("true", StringComparison.OrdinalIgnoreCase); } if (attributes.Contains("minify")) { newDoc.Minified = bundle.Attribute("minify").Value.Equals("true", StringComparison.OrdinalIgnoreCase); } return(await newDoc.WriteBundleRecipe()); }
public static async Task <BundleDocument> FromFile(string fileName) { string root = ProjectHelpers.GetProjectFolder(fileName); string folder = Path.GetDirectoryName(root); if (folder == null || root == null) { return(null); } XDocument doc = null; string contents = await FileHelpers.ReadAllTextRetry(fileName); try { doc = XDocument.Parse(contents); } catch (XmlException) { return(null); } // Migrate old bundles doc = await MigrateBundle(doc, fileName, root, folder); if (doc == null) { return(null); } XElement element = null; IEnumerable <string> rawConstituents = doc.Descendants("file").Select(s => s.Value); IEnumerable <string> constituentFiles = ProjectHelpers.GetBundleConstituentFiles(rawConstituents, root, folder, fileName); if (constituentFiles.Count() > 0) { BundleDocument bundle = new BundleDocument(fileName, constituentFiles.ToArray()) { OriginalBundleAssets = new List <string>(rawConstituents) }; element = doc.Descendants("minify").FirstOrDefault(); if (element != null) { bundle.Minified = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase); } element = doc.Descendants("runOnBuild").FirstOrDefault(); if (element != null) { bundle.RunOnBuild = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase); } if (bundle.isCss) { element = doc.Descendants("adjustRelativePaths").FirstOrDefault(); if (element != null) { bundle.AdjustRelativePaths = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase); } } element = doc.Descendants("outputDirectory").FirstOrDefault(); if (element != null) { bundle.OutputDirectory = element.Value; } return(bundle); } else { return(null); } }