string[] FilterFiles(string[] files) { return(files.Where(p => { var extension = GetFullFileExtension(Path.GetFileName(p)); return IncludeExtensions.Contains(extension) && !ExcludeExtensions.Contains(extension); }).ToArray()); }
protected void context_PostReleaseRequestState(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; //if debug is enabled do not minify if (app != null && !context.IsDebuggingEnabled) { //ignore /sitecore urls since it causes issues with sitecore desktop content editor //if HTMLContentType setting is true then process the file as long as it doesnt exist in the excluded extensions setting if ((Settings.GetBoolSetting("NTT.Minifier.HTMLContentType", false) && context.Response.ContentType == "text/html") && !ExcludeURL(app.Request.RawUrl.ToLower()) && ExcludeExtensions.Any(ext => ext != GetExtension(app.Request.Url.AbsoluteUri))) { app.Response.Filter = new ResponseMemoryStream(app.Response.Filter); Log.Info(String.Format("NTT Minifier URL:{0} - Content Type:{1} < MINIFIED", app.Request.RawUrl.ToLower(), context.Response.ContentType), this); } } }
public bool StartRenaming(string sourcePath, string destinationPath) { if (string.IsNullOrEmpty(sourcePath)) { throw new ArgumentException("sourcePath cannot be null"); } if (string.IsNullOrEmpty(destinationPath)) { throw new ArgumentException("destinationPath cannot be null"); } if (!Directory.Exists(sourcePath)) { throw new DirectoryNotFoundException(string.Format("sourcePath '{0}' could not be found", sourcePath)); } if (!Directory.Exists(destinationPath)) { throw new DirectoryNotFoundException(string.Format("destinationPath '{0}' could not be found", destinationPath)); } var startFileNames = Directory.GetFiles(sourcePath); foreach (string fileName in startFileNames) { var currentFile = new FileInfo(fileName); var currentFileExtension = currentFile.Extension.ToLower(); // Set new filepath and apply filename rename var destinationFileName = currentFile.Name; foreach (var renameWord in RenameWords) { destinationFileName = StringHelpers.ReplaceEx(destinationFileName, renameWord.Key, renameWord.Value); } var destinationFilePath = Path.Combine(destinationPath, destinationFileName); if (ExcludeExtensions.Contains(currentFileExtension)) { // Just copy the file to the destination, and rename the filename File.Copy(currentFile.FullName, destinationFilePath); } else if (RemoveExtensions.Contains(currentFileExtension)) { continue; // Skip this } else if (RenameExtensions.Contains(currentFileExtension)) { // Rename inside the file, and the filename string fileContent = ""; if (UtfExtensions.Contains(currentFileExtension)) { fileContent = File.ReadAllText(currentFile.FullName, System.Text.Encoding.UTF8); } else { fileContent = File.ReadAllText(currentFile.FullName); } foreach (var renameWord in RenameWords) { fileContent = StringHelpers.ReplaceEx(fileContent, renameWord.Key, renameWord.Value); } if (UtfExtensions.Contains(currentFileExtension)) { File.WriteAllText(destinationFilePath, fileContent, System.Text.Encoding.UTF8); } else { File.WriteAllText(destinationFilePath, fileContent); } } } var startDirectories = Directory.GetDirectories(sourcePath); foreach (var directoryName in startDirectories) { var currentDirectory = new DirectoryInfo(directoryName); // Directory needs to be renamed var destinationDirectoryName = currentDirectory.Name; foreach (var renameWord in RenameWords) { destinationDirectoryName = StringHelpers.ReplaceEx(destinationDirectoryName, renameWord.Key, renameWord.Value); } string destinationDirectoryPath = Path.Combine(destinationPath, destinationDirectoryName); Directory.CreateDirectory(destinationDirectoryPath); StartRenaming(currentDirectory.FullName, destinationDirectoryPath); } return(true); }
public ConversionOptions GetConfiguration() { var options = new ConversionOptions(); if (Path.HasValue()) { options.Paths = Path.Values; } if (File.HasValue()) { options.Files = File.Values; } if (List.HasValue()) { options.ListFile = List.Value(); } if (options.Paths.Count == 0 && options.Files.Count == 0 && String.IsNullOrEmpty(options.ListFile)) { throw new ConfigurationException("Nothing to process, must specify one of --path, --file or --list"); } if (IndentStyle.HasValue()) { var style = IndentStyle.Value().ToLower(); if (style == "tabs") { options.Indentation = IndentationStyle.Tabs; } else if (style == "spaces") { options.Indentation = IndentationStyle.Spaces; } else if (style == "leave") { options.Indentation = IndentationStyle.Leave; } else { throw new ConfigurationException($"'{style}' is an invalid indentation style"); } } if (LineEndings.HasValue()) { var lineEndingStyle = LineEndings.Value().ToLower(); if (lineEndingStyle == "crlf") { options.LineEndingStyle = LineEnding.CRLF; } else if (lineEndingStyle == "lf") { options.LineEndingStyle = LineEnding.LF; } else { throw new ConfigurationException("Line Endings must be crlf or lf"); } } options.StripTrailingSpaces = StripTrailingSpaces.HasValue(); // no point going any further if one of the change options isn't actually specified if (options.StripTrailingSpaces == false && options.Indentation == IndentationStyle.Leave && options.LineEndingStyle == LineEnding.Leave) { throw new ConfigurationException("Nothing to do, you must specify one of --strip-trailing-spaces, --line-endings or --indent"); } if (TabWidth.HasValue()) { if (!Int32.TryParse(TabWidth.Value(), out int tabWidth)) { throw new ConfigurationException("tabwidth must be a valid number"); } options.TabWidth = tabWidth; } if (IncludeExtensions.HasValue()) { options.IncludeExtensions = ParseFileExtensionsOption(IncludeExtensions.Values); } if (ExcludeExtensions.HasValue()) { options.ExcludeExtensions = ParseFileExtensionsOption(ExcludeExtensions.Values); } if (ExcludeFolders.HasValue()) { options.ExcludeFolders = ExcludeFolders.Values; } // the presence of recurse|dryrun means it's on, there is no value options.Recurse = Recurse.HasValue(); options.DryRun = DryRun.HasValue(); options.Verbose = Verbose.HasValue(); return(options); }