public async static Task <Dictionary <string, string> > WatchFiles(BundleDocument document, Func <string, bool, Task> updateBundle) { Dictionary <string, string> files = new Dictionary <string, string>(); if (document == null) { return(null); } await new BundleFileObserver().AttachFileObserver(document.FileName, document.FileName, updateBundle); foreach (string asset in document.BundleAssets) { string absolute = asset.Contains(":\\") ? asset : ProjectHelpers.ToAbsoluteFilePath(asset, document.FileName); if (File.Exists(absolute)) { if (!files.ContainsKey(absolute)) { files.Add(absolute, "/" + FileHelpers.RelativePath(ProjectHelpers.GetProjectFolder(document.FileName), asset)); await new BundleFileObserver().AttachFileObserver(absolute, document.FileName, updateBundle); } } else { EditorExtensionsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", asset)); return(null); } } return(files); }
private async Task GenerateAsync(BundleDocument bundle, string extension, bool hasUpdated = false) { _dte.StatusBar.Text = "Generating bundle..."; if (!hasUpdated) { ProjectHelpers.AddFileToActiveProject(bundle.FileName); } string bundleFile = Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName)); bool hasChanged = await BundleGenerator.MakeBundle(bundle, bundleFile, UpdateBundleAsync); if (!hasUpdated) { ProjectHelpers.AddFileToProject(bundle.FileName, bundleFile); EditorExtensionsPackage.DTE.ItemOperations.OpenFile(bundle.FileName); } if (bundle.Minified) { await BundleGenerator.MakeMinFile(bundleFile, extension, hasChanged); } _dte.StatusBar.Text = "Bundle generated"; }
private async Task UpdateBundleAsync(string bundleFileName, string extension, bool isBuild = false) { if (_ignoreFolders.Any(p => bundleFileName.Contains("\\" + p + "\\"))) { return; } try { BundleDocument doc = await BundleDocument.FromFile(bundleFileName); if (doc == null) { Logger.Log("Note: Bundle file " + bundleFileName + " is not in Web Essentials bundle format."); return; } if (!isBuild || doc.RunOnBuild) { await GenerateAsync(doc, extension, true); } } catch (FileNotFoundException ex) { Logger.Log(ex.Message); MessageBox.Show("The file '" + Path.GetFileName(ex.Message) + "' does not exist"); _dte.StatusBar.Text = "Bundle was not created"; } catch (Exception ex) { Logger.Log(ex); MessageBox.Show("Error generating the bundle. See output window for details"); } }
public async static Task <bool> MakeBundle(BundleDocument document, string bundleFile, Func <string, bool, Task> updateBundle) { // filePath must end in ".targetExtension.bundle" string extension = Path.GetExtension(Path.GetFileNameWithoutExtension(document.FileName)); if (string.IsNullOrEmpty(extension)) { Logger.Log("Skipping bundle file " + document.FileName + " without extension. Bundle files must end with the output extension, followed by '.bundle'."); return(false); } Dictionary <string, string> files = await WatchFiles(document, updateBundle, bundleFile); string combinedContent = await CombineFiles(files, extension, document, bundleFile); bool bundleChanged = !File.Exists(bundleFile) || await FileHelpers.ReadAllTextRetry(bundleFile) != combinedContent; if (bundleChanged) { if (!ProjectHelpers.CheckOutFileFromSourceControl(bundleFile)) { throw new Exception("There was a problem checking out the file: " + bundleFile); } await FileHelpers.WriteAllTextRetry(bundleFile, combinedContent); Logger.Log("Web Essentials: Updated bundle: " + Path.GetFileName(bundleFile)); } ProjectHelpers.AddFileToProject(document.FileName, bundleFile); return(bundleChanged); }
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 = from f in doc.Descendants("file") select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder); 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()); }
private async void SolutionEvents_Opened() { foreach (Project project in ProjectHelpers.GetAllProjects()) { if (project.ProjectItems.Count == 0) { continue; } string folder = ProjectHelpers.GetRootFolder(project); Func <string, bool, Task> bundleFunc = new BundleFilesMenu().UpdateBundleAsync; Func <string, bool, Task> spriteFunc = new SpriteImageMenu().UpdateSpriteAsync; foreach (string file in Directory.EnumerateFiles(folder, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".bundle") || s.EndsWith(".sprite"))) { if (ProjectHelpers.GetProjectItem(file) == null) { continue; } if (file.EndsWith(".bundle", StringComparison.OrdinalIgnoreCase)) { await BundleGenerator.WatchFiles(await BundleDocument.FromFile(file), bundleFunc); } else { await SpriteGenerator.WatchFiles(await SpriteDocument.FromFile(file), spriteFunc); } } } }
public async static Task<bool> MakeBundle(BundleDocument document, string bundleFile, Func<string, bool, Task> updateBundle) { if (document == null) return false; // filePath must end in ".targetExtension.bundle" string extension = Path.GetExtension(Path.GetFileNameWithoutExtension(document.FileName)); if (string.IsNullOrEmpty(extension)) { Logger.Log("Skipping bundle file " + document.FileName + " without extension. Bundle files must end with the output extension, followed by '.bundle'."); return false; } Dictionary<string, string> files = await WatchFiles(document, updateBundle); if (files == null) return false; string combinedContent = await CombineFiles(files, extension, document, bundleFile); bool bundleChanged = !File.Exists(bundleFile) || await FileHelpers.ReadAllTextRetry(bundleFile) != combinedContent; if (bundleChanged) { ProjectHelpers.CheckOutFileFromSourceControl(bundleFile); await FileHelpers.WriteAllTextRetry(bundleFile, combinedContent); Logger.Log("Web Essentials: Updated bundle: " + Path.GetFileName(bundleFile)); } ProjectHelpers.AddFileToProject(document.FileName, bundleFile); return bundleChanged; }
public async static Task<Dictionary<string, string>> WatchFiles(BundleDocument document, Func<string, bool, Task> updateBundle) { Dictionary<string, string> files = new Dictionary<string, string>(); await new BundleFileObserver().AttachFileObserver(document.FileName, document.FileName, updateBundle); foreach (string asset in document.BundleAssets) { string absolute = asset.Contains(":\\") ? asset : ProjectHelpers.ToAbsoluteFilePath(asset, document.FileName); if (File.Exists(absolute)) { if (!files.ContainsKey(absolute)) { files.Add(absolute, "/" + FileHelpers.RelativePath(ProjectHelpers.GetProjectFolder(document.FileName), asset)); await new BundleFileObserver().AttachFileObserver(absolute, document.FileName, updateBundle); } } else { EditorExtensionsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", asset)); return null; } } return files; }
private async Task GenerateAsync(BundleDocument bundle, string extension, bool hasUpdated = false) { _dte.StatusBar.Text = "Generating bundle..."; if (ProjectHelpers.GetProjectItem(bundle.FileName) == null) { ProjectHelpers.AddFileToActiveProject(bundle.FileName); } string bundleFile = Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName)); if (!string.IsNullOrEmpty(bundle.OutputDirectory)) { bundleFile = ProjectHelpers.GetAbsolutePathFromSettings(bundle.OutputDirectory, Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName))); } ProjectHelpers.CreateDirectoryInProject(bundleFile); bool hasChanged = await BundleGenerator.MakeBundle(bundle, bundleFile, UpdateBundleAsync); ProjectHelpers.AddFileToProject(bundle.FileName, bundleFile); if (!hasUpdated) { WebEssentialsPackage.DTE.ItemOperations.OpenFile(bundle.FileName); } if (bundle.Minified) { await BundleGenerator.MakeMinFile(bundleFile, extension, hasChanged); } _dte.StatusBar.Text = "Bundle generated"; }
private async static Task<string> CombineFiles(Dictionary<string, string> files, string extension, BundleDocument bundle, string bundleFile) { StringBuilder sb = new StringBuilder(); foreach (string file in files.Keys) { if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase) && WESettings.Instance.JavaScript.GenerateSourceMaps) { sb.AppendLine("///#source 1 1 " + files[file]); } var source = await FileHelpers.ReadAllTextRetry(file); if (extension.Equals(".css", StringComparison.OrdinalIgnoreCase)) { // If the bundle is in the same folder as the CSS, // or if does not have URLs, no need to normalize. if (Path.GetDirectoryName(file) != Path.GetDirectoryName(bundleFile) && source.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0 && bundle.AdjustRelativePaths) source = CssUrlNormalizer.NormalizeUrls( tree: new CssParser().Parse(source, true), targetFile: bundleFile, oldBasePath: file ); } sb.AppendLine(source); } return sb.ToString(); }
public async static Task <Dictionary <string, string> > WatchFiles(BundleDocument document, Func <string, bool, Task> updateBundle, string bundleFile = null) { if (document == null) { return(null); } if (bundleFile == null && document.OutputDirectory != null) { bundleFile = ProjectHelpers.GetAbsolutePathFromSettings(document.OutputDirectory, Path.Combine(Path.GetDirectoryName(document.FileName), Path.GetFileNameWithoutExtension(document.FileName))); } Dictionary <string, string> files = new Dictionary <string, string>(); await new BundleFileObserver().AttachFileObserver(document, document.FileName, updateBundle); foreach (string asset in document.OriginalBundleAssets) { string absolute = asset.Contains(":\\") ? asset : ProjectHelpers.ToAbsoluteFilePath(asset, document.FileName); string absoluteActual = GetActualAsset(absolute); if (!File.Exists(absoluteActual)) { WebEssentialsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", asset)); return(null); } if (!File.Exists(absolute)) { WebEssentialsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", absolute)); return(null); } if (!files.ContainsKey(absoluteActual)) { if (Path.IsPathRooted(asset)) { files.Add(absolute, asset); } else { files.Add(absolute, FileHelpers.RelativePath(bundleFile, Path.GetFullPath(Path.Combine(Path.GetDirectoryName(document.FileName), asset)))); } await new BundleFileObserver().AttachFileObserver(document, absoluteActual, updateBundle); } } return(files); }
public async static Task<bool> MakeBundle(BundleDocument document, string bundleFile, Func<string, bool, Task> updateBundle) { if (document == null) return false; Dictionary<string, string> files = new Dictionary<string, string>(); // filePath must end in ".targetExtension.bundle" string extension = Path.GetExtension(Path.GetFileNameWithoutExtension(document.FileName)); if (string.IsNullOrEmpty(extension)) { Logger.Log("Skipping bundle file " + document.FileName + " without extension. Bundle files must end with the output extension, followed by '.bundle'."); return false; } await new BundleFileObserver().AttachFileObserver(document.FileName, document.FileName, updateBundle); foreach (string asset in document.BundleAssets) { string absolute = asset.Contains(":\\") ? asset : ProjectHelpers.ToAbsoluteFilePath(asset, document.FileName); if (File.Exists(absolute)) { if (!files.ContainsKey(absolute)) { files.Add(absolute, asset); await new BundleFileObserver().AttachFileObserver(absolute, document.FileName, updateBundle); } } else { EditorExtensionsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", asset)); return false; } } string combinedContent = await CombineFiles(files, extension, document, bundleFile); bool bundleChanged = !File.Exists(bundleFile) || await FileHelpers.ReadAllTextRetry(bundleFile) != combinedContent; if (bundleChanged) { ProjectHelpers.CheckOutFileFromSourceControl(bundleFile); await FileHelpers.WriteAllTextRetry(bundleFile, combinedContent); Logger.Log("Web Essentials: Updated bundle: " + Path.GetFileName(bundleFile)); } ProjectHelpers.AddFileToProject(document.FileName, bundleFile); return bundleChanged; }
private async static Task<string> CombineFiles(Dictionary<string, string> files, string extension, BundleDocument bundle, string bundleFile) { StringBuilder sb = new StringBuilder(); foreach (string file in files.Keys) { string actualFile = file; if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase) && WESettings.Instance.JavaScript.GenerateSourceMaps) { sb.AppendLine("///#source 1 1 " + files[file]); } var source = await FileHelpers.ReadAllTextRetry(actualFile); if (extension.Equals(".css", StringComparison.OrdinalIgnoreCase)) { // If the bundle is in the same folder as the CSS, // or if does not have URLs, no need to normalize. if (Path.GetDirectoryName(actualFile) != Path.GetDirectoryName(bundleFile) && source.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0 && bundle.AdjustRelativePaths) source = CssUrlNormalizer.NormalizeUrls( tree: new CssParser().Parse(source, true), targetFile: bundleFile, oldBasePath: actualFile ); } else if (Path.GetExtension(file).Equals(".ts", StringComparison.OrdinalIgnoreCase)) { // If it is a Type Script include, we might want to alter the define(... at the start of the script to specify the resource location. if (source.StartsWith("define([\"require\", \"exports\"")) { string moduleName = files[file]; moduleName = Path.Combine(Path.GetDirectoryName(moduleName), Path.GetFileNameWithoutExtension(moduleName)).Replace('\\', '/'); if (moduleName.ToLower().StartsWith(WESettings.Instance.JavaScript.RootDirectory.ToLower())) moduleName = moduleName.Substring(WESettings.Instance.JavaScript.RootDirectory.Length); source = source.Replace("define([\"require\", \"exports\"", "define(\"" + moduleName + "\", [\"require\", \"exports\""); } } sb.AppendLine(source); } return sb.ToString(); }
public async static Task<Dictionary<string, string>> WatchFiles(BundleDocument document, Func<string, bool, Task> updateBundle, string bundleFile = null) { if (document == null) return null; if (bundleFile == null && document.OutputDirectory != null) bundleFile = ProjectHelpers.GetAbsolutePathFromSettings(document.OutputDirectory, Path.Combine(Path.GetDirectoryName(document.FileName), Path.GetFileNameWithoutExtension(document.FileName))); Dictionary<string, string> files = new Dictionary<string, string>(); await new BundleFileObserver().AttachFileObserver(document, document.FileName, updateBundle); foreach (string asset in document.OriginalBundleAssets) { string absolute = asset.Contains(":\\") ? asset : ProjectHelpers.ToAbsoluteFilePath(asset, document.FileName); string absoluteActual = GetActualAsset(absolute); if (!File.Exists(absoluteActual)) { WebEssentialsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", asset)); return null; } if (!File.Exists(absolute)) { WebEssentialsPackage.DTE.ItemOperations.OpenFile(document.FileName); Logger.ShowMessage(String.Format(CultureInfo.CurrentCulture, "Bundle error: The file '{0}' doesn't exist", absolute)); return null; } if (!files.ContainsKey(absoluteActual)) { if (Path.IsPathRooted(asset)) files.Add(absolute, asset); else files.Add(absolute, FileHelpers.RelativePath(bundleFile, Path.GetFullPath(Path.Combine(Path.GetDirectoryName(document.FileName), asset)))); await new BundleFileObserver().AttachFileObserver(document, absoluteActual, updateBundle); } } return files; }
private async Task MakeBundleAsync(string extension) { string bundleFile; if (!GetFileName(out bundleFile, extension)) { return; } try { BundleDocument doc = new BundleDocument(bundleFile, _files.ToArray()); await doc.WriteBundleRecipe(); await GenerateAsync(doc, extension); } catch (Exception ex) { Logger.Log(ex); MessageBox.Show("Error generating the bundle. See output window for details"); _dte.StatusBar.Text = "Bundle was not created"; } }
public static BundleDocument FromFile(string fileName) { string root = ProjectHelpers.GetProjectFolder(fileName); string folder = Path.GetDirectoryName(root); if (folder == null || root == null) return null; XDocument doc = null; try { doc = XDocument.Load(fileName); } catch (XmlException) { return null; } XElement element = null; IEnumerable<string> imageFiles = from f in doc.Descendants("file") select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder); BundleDocument bundle = new BundleDocument(fileName, imageFiles.ToArray()); 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); 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; }
private async Task MakeBundleAsync(string extension) { string bundleFile; if (!GetFileName(out bundleFile, extension)) return; try { BundleDocument doc = new BundleDocument(bundleFile, _files.ToArray()); await doc.WriteBundleRecipe(); await GenerateAsync(doc, extension); } catch (Exception ex) { Logger.Log(ex); MessageBox.Show("Error generating the bundle. See output window for details"); _dte.StatusBar.Text = "Bundle was not created"; } }
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); if (constituentFiles.Count() > 0) { 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(); } else { return null; } }
private static 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 = from f in doc.Descendants("file") select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder); 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.RunOnBuild = bundle.Attribute("minify").Value.Equals("true", StringComparison.OrdinalIgnoreCase); newDoc.WriteBundleRecipe().DoNotWait("Migrating bundle to new schema."); try { return XDocument.Load(fileName); } catch (XmlException) { return null; } }
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); } }
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; } }
private async Task GenerateAsync(BundleDocument bundle, string extension, bool hasUpdated = false) { _dte.StatusBar.Text = "Generating bundle..."; string bundleFile = Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName)); bool hasChanged = await BundleGenerator.MakeBundle(bundle, bundleFile, UpdateBundleAsync); if (!hasUpdated) { ProjectHelpers.AddFileToActiveProject(bundle.FileName); ProjectHelpers.AddFileToProject(bundle.FileName, bundleFile); EditorExtensionsPackage.DTE.ItemOperations.OpenFile(bundle.FileName); } if (bundle.Minified) await BundleGenerator.MakeMinFile(bundleFile, extension, hasChanged); _dte.StatusBar.Text = "Bundle generated"; }
public async Task <IBundleDocument> LoadFromFile(string fileName) { return(await BundleDocument.FromFile(fileName)); }
private async static Task <string> CombineFiles(Dictionary <string, string> files, string extension, BundleDocument bundle, string bundleFile) { StringBuilder sb = new StringBuilder(); foreach (string file in files.Keys) { if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase) && WESettings.Instance.JavaScript.GenerateSourceMaps) { sb.AppendLine("///#source 1 1 " + files[file]); } var source = await FileHelpers.ReadAllTextRetry(file); if (extension.Equals(".css", StringComparison.OrdinalIgnoreCase)) { // If the bundle is in the same folder as the CSS, // or if does not have URLs, no need to normalize. if (Path.GetDirectoryName(file) != Path.GetDirectoryName(bundleFile) && source.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0 && bundle.AdjustRelativePaths) { source = CssUrlNormalizer.NormalizeUrls( tree: new CssParser().Parse(source, true), targetFile: bundleFile, oldBasePath: file ); } } sb.AppendLine(source); } return(sb.ToString()); }
private async static Task <string> CombineFiles(Dictionary <string, string> files, string extension, BundleDocument bundle, string bundleFile) { StringBuilder sb = new StringBuilder(); foreach (string file in files.Keys) { string actualFile = file; if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase) && WESettings.Instance.JavaScript.GenerateSourceMaps) { sb.AppendLine("///#source 1 1 " + string.Format("{0}{1}", WESettings.Instance.JavaScript.BaseUrl, files[file])); } var source = await FileHelpers.ReadAllTextRetry(actualFile); if (extension.Equals(".css", StringComparison.OrdinalIgnoreCase)) { // If the bundle is in the same folder as the CSS, // or if does not have URLs, no need to normalize. if (Path.GetDirectoryName(actualFile) != Path.GetDirectoryName(bundleFile) && source.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0 && bundle.AdjustRelativePaths) { source = CssUrlNormalizer.NormalizeUrls( tree: new CssParser().Parse(source, true), targetFile: bundleFile, oldBasePath: actualFile); } } else if (Path.GetExtension(file).Equals(".ts", StringComparison.OrdinalIgnoreCase)) { // If it is a Type Script include, we might want to alter the define(... at the start of the script to specify the resource location. if (source.StartsWith("define([\"require\", \"exports\"")) { string moduleName = files[file]; moduleName = Path.Combine(Path.GetDirectoryName(moduleName), Path.GetFileNameWithoutExtension(moduleName)).Replace('\\', '/'); if (moduleName.ToLower().StartsWith(WESettings.Instance.JavaScript.RootDirectory.ToLower())) { moduleName = moduleName.Substring(WESettings.Instance.JavaScript.RootDirectory.Length); } source = source.Replace("define([\"require\", \"exports\"", "define(\"" + moduleName + "\", [\"require\", \"exports\""); } } sb.AppendLine(source); } return(sb.ToString()); }
public static async Task <BundleDocument> FromFile(string fileName) { var extension = Path.GetExtension(fileName).TrimStart('.').ToLowerInvariant(); 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> constituentFiles = from f in doc.Descendants("file") select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder); BundleDocument bundle = new BundleDocument(fileName, constituentFiles.ToArray()); 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 (extension == "css") { 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); }
private async Task GenerateAsync(BundleDocument bundle, string extension, bool hasUpdated = false) { _dte.StatusBar.Text = "Generating bundle..."; ProjectHelpers.AddFileToActiveProject(bundle.FileName); string bundleFile = Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName)); if (!string.IsNullOrEmpty(bundle.OutputDirectory)) bundleFile = ProjectHelpers.GetAbsolutePathFromSettings(bundle.OutputDirectory, Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName))); ProjectHelpers.CreateDirectoryInProject(bundleFile); bool hasChanged = await BundleGenerator.MakeBundle(bundle, bundleFile, UpdateBundleAsync); ProjectHelpers.AddFileToProject(bundle.FileName, bundleFile); if (!hasUpdated) WebEssentialsPackage.DTE.ItemOperations.OpenFile(bundle.FileName); if (bundle.Minified) await BundleGenerator.MakeMinFile(bundleFile, extension, hasChanged); _dte.StatusBar.Text = "Bundle generated"; }
public static async Task<BundleDocument> FromFile(string fileName) { var extension = Path.GetExtension(fileName).TrimStart('.').ToLowerInvariant(); 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> constituentFiles = from f in doc.Descendants("file") select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder); BundleDocument bundle = new BundleDocument(fileName, constituentFiles.ToArray()); 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 (extension == "css") { 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; }