/// <summary> /// Determines whether a given FilterChain is null or empty. /// </summary> /// <returns> /// <c>true</c> if <paramref name="filterChain"/> is null or empty; /// otherwise, <c>false</c>. /// </returns> /// <param name='filterChain'> /// The FilterChain to check. /// </param> internal static bool IsNullOrEmpty(FilterChain filterChain) { if (filterChain == null) { return(true); } else { return(filterChain.Filters.Count <= 0); } }
private void LoadFile(FilterChain filterChain) { string content = null; try { content = FileUtils.ReadFile(File.FullName, filterChain, Encoding); } catch (IOException ex) { throw new BuildException("The properties file could not be read.", Location, ex); } PropertyDictionary properties = Project.Properties; PropertyTask propertyTask = new PropertyTask(); propertyTask.Parent = this; propertyTask.Project = Project; using (StringReader sr = new StringReader(content)) { string line = sr.ReadLine(); int current_line = 0; while (line != null) { current_line++; // skip empty lines and comments if (String.IsNullOrEmpty(line) || line.StartsWith ("#")) { line = sr.ReadLine (); continue; } int equals_pos = line.IndexOf ('='); if (equals_pos == -1) throw new BuildException (string.Format(CultureInfo.InvariantCulture, "Invalid property defined on line {0}.", current_line), Location); string name = line.Substring(0, equals_pos).Trim(); string value = line.Substring (equals_pos + 1, line.Length - equals_pos - 1).Trim(); string expandedValue = properties.ExpandProperties(value, Location); propertyTask.PropertyName = name; propertyTask.Value = expandedValue; propertyTask.Execute(); line = sr.ReadLine (); } } }
/// <summary> /// Determines whether a given FilterChain is null or empty. /// </summary> /// <returns> /// <c>true</c> if <paramref name="filterChain"/> is null or empty; /// otherwise, <c>false</c>. /// </returns> /// <param name='filterChain'> /// The FilterChain to check. /// </param> internal static bool IsNullOrEmpty(FilterChain filterChain) { return(filterChain == null || filterChain.Filters.Count <= 0); }
/// <summary> /// Reads a file filtering its content through the filter chain. /// </summary> /// <param name="fileName">The file to read.</param> /// <param name="filterChain">Chain of filters to apply when reading, or <see langword="null" /> is no filters should be applied.</param> /// <param name="inputEncoding">The encoding used to read the file.</param> /// <remarks> /// If <paramref name="inputEncoding" /> is <see langword="null" />, /// then the system's ANSI code page will be used to read the file. /// </remarks> public static string ReadFile(string fileName, FilterChain filterChain, Encoding inputEncoding) { string content = null; // determine character encoding to use Encoding encoding = (inputEncoding != null) ? inputEncoding : Encoding.Default; // read file using (StreamReader sr = new StreamReader(fileName, encoding, true)) { if (filterChain == null || filterChain.Filters.Count == 0) { content = sr.ReadToEnd(); } else { Filter baseFilter = filterChain.GetBaseFilter( new PhysicalTextReader(sr)); StringWriter sw = new StringWriter(); while (true) { int character = baseFilter.Read(); if (character == -1) break; sw.Write((char) character); } content = sw.ToString(); } } return content; }
/// <summary> /// Moves a file filtering its content through the filter chain. /// </summary> /// <param name="sourceFileName">The file to move.</param> /// <param name="destFileName">The file to move move to.</param> /// <param name="filterChain">Chain of filters to apply when moving, or <see langword="null" /> is no filters should be applied.</param> /// <param name="inputEncoding">The encoding used to read the soure file.</param> /// <param name="outputEncoding">The encoding used to write the destination file.</param> public static void MoveFile(string sourceFileName, string destFileName, FilterChain filterChain, Encoding inputEncoding, Encoding outputEncoding) { // if no filters have been defined, and no input or output encoding // is set, we can just use the File.Move method if ((filterChain == null || filterChain.Filters.Count == 0) && inputEncoding == null && outputEncoding == null) { File.Move(sourceFileName, destFileName); } else { CopyFile(sourceFileName, destFileName, filterChain, inputEncoding, outputEncoding); File.Delete(sourceFileName); } }
/// <summary> /// Copies a file filtering its content through the filter chain. /// </summary> /// <param name="sourceFileName">The file to copy</param> /// <param name="destFileName">The file to copy to</param> /// <param name="filterChain">Chain of filters to apply when copying, or <see langword="null" /> is no filters should be applied.</param> /// <param name="inputEncoding">The encoding used to read the soure file.</param> /// <param name="outputEncoding">The encoding used to write the destination file.</param> public static void CopyFile(string sourceFileName, string destFileName, FilterChain filterChain, Encoding inputEncoding, Encoding outputEncoding) { // determine if filters are available bool filtersAvailable = filterChain != null && filterChain.Filters.Count > 0; // if no filters have been defined, and no input or output encoding // is set, we can just use the File.Copy method if (!filtersAvailable && inputEncoding == null && outputEncoding == null) { File.Copy(sourceFileName, destFileName, true); } else { // determine actual input encoding to use. if no explicit input // encoding is specified, we'll use the system's current ANSI // code page Encoding actualInputEncoding = (inputEncoding != null) ? inputEncoding : Encoding.Default; // get base filter built on the file's reader. Use a 8k buffer. using (StreamReader sourceFileReader = new StreamReader(sourceFileName, actualInputEncoding, true, 8192)) { Encoding actualOutputEncoding = outputEncoding; if (actualOutputEncoding == null) { // if no explicit output encoding is specified, we'll // just use the encoding of the input file as determined // by the runtime // // Note : the input encoding as specified on the filterchain // might not match the current encoding of the streamreader // // eg. when specifing an ANSI encoding, the runtime might // still detect the file is using UTF-8 encoding, because // we use BOM detection actualOutputEncoding = sourceFileReader.CurrentEncoding; } // writer for destination file using (StreamWriter destFileWriter = new StreamWriter(destFileName, false, actualOutputEncoding, 8192)) { if (filtersAvailable) { Filter baseFilter = filterChain.GetBaseFilter(new PhysicalTextReader(sourceFileReader)); bool atEnd = false; int character; while (!atEnd) { character = baseFilter.Read(); if (character > -1) { destFileWriter.Write((char)character); } else { atEnd = true; } } } else { char[] buffer = new char[8192]; while (true) { int charsRead = sourceFileReader.Read(buffer, 0, buffer.Length); if (charsRead == 0) { break; } destFileWriter.Write(buffer, 0, charsRead); } } } } } }
/// <summary> /// Moves a directory while filtering its file content through the filter chain. /// </summary> /// <param name="sourceDirectory"> /// Source directory to move from. /// </param> /// <param name="destDirectory"> /// Destination directory to move to. /// </param> /// <param name="filterChain"> /// Chain of filters to apply when copying, or <see langword="null" /> is no /// filters should be applied. /// </param> /// <param name="inputEncoding"> /// The encoding used to read the soure file. /// </param> /// <param name="outputEncoding"> /// The encoding used to write the destination file. /// </param> internal static void MoveDirectory( string sourceDirectory, string destDirectory, FilterChain filterChain, Encoding inputEncoding, Encoding outputEncoding) { // If the source directory does not exist, throw an exception. if (!Directory.Exists(sourceDirectory)) { throw new BuildException( String.Format("Cannot move directory: Source Directory {0} does not exist", sourceDirectory)); } // if no filters have been defined, and no input or output encoding // is set, proceed with a straight directory move. if (FilterChain.IsNullOrEmpty(filterChain) && inputEncoding == null && outputEncoding == null) { // If the source & target paths are completely the same, including // case, throw an exception. if (sourceDirectory.Equals(destDirectory, StringComparison.InvariantCulture)) { throw new BuildException("Source and Target paths are identical"); } try { // wants to rename a directory with the same name but different casing // (ie: C:\nant to C:\NAnt), then the move needs to be staged. if (PlatformHelper.IsWindows) { // If the directory names are the same but different casing, stage // the move by moving the source directory to a temp location // before moving it to the destination. if (sourceDirectory.Equals(destDirectory, StringComparison.InvariantCultureIgnoreCase)) { // Since the directory is being renamed with different // casing, the temp directory should be in the same // location as the destination directory to avoid // possible different volume errors. string rootPath = Directory.GetParent(destDirectory).FullName; string stagePath = Path.Combine(rootPath, Path.GetRandomFileName()); try { // Move the source dir to the stage path // before moving everything to the destination // path. Directory.Move(sourceDirectory, stagePath); Directory.Move(stagePath, destDirectory); } catch { // If an error occurred during the staged directory // move, check to see if the stage path exists. If // the source directory successfully moved to the // stage path, move the stage path back to the // source path and rethrow the exception. if (Directory.Exists(stagePath)) { if (!Directory.Exists(sourceDirectory)) { Directory.Move(stagePath, sourceDirectory); } } throw; } } // If the directory source and destination names are // different, use Directory.Move. else { Directory.Move(sourceDirectory, destDirectory); } } // Non-Windows systems, such as Linux/Unix, filenames and directories // are case-sensitive. So as long as the directory names are not // identical, with the check above, the Directory.Move method // can be used. else { Directory.Move(sourceDirectory, destDirectory); } } // Catch and rethrow any IO exceptions that may arise during // the directory move. catch (IOException ioEx) { // If the error occurred because the destination directory // exists, throw a build exception to tell the user that the // destination directory already exists. if (Directory.Exists(destDirectory)) { throw new BuildException( string.Format(CultureInfo.InvariantCulture, "Failed to move directory {0}." + "Directory '{1}' already exists.", sourceDirectory, destDirectory)); } // Any other IOExceptions should be displayed to the user // via build exception. else { throw new BuildException( String.Format("Unhandled IOException when trying to move directory '{0}' to '{1}'", sourceDirectory, destDirectory), ioEx); } } } else { // Otherwise, use the copy directory method and directory.delete // method to move the directory over. CopyDirectory(sourceDirectory, destDirectory, filterChain, inputEncoding, outputEncoding); Directory.Delete(sourceDirectory, true); } }
/// <summary> /// Copies a directory while filtering its file content through the filter chain. /// </summary> /// <param name="sourceDirectory"> /// Source directory to copy from. /// </param> /// <param name="destDirectory"> /// Destination directory to copy to. /// </param> /// <param name="filterChain"> /// Chain of filters to apply when copying, or <see langword="null" /> is no /// filters should be applied. /// </param> /// <param name="inputEncoding"> /// The encoding used to read the soure file. /// </param> /// <param name="outputEncoding"> /// The encoding used to write the destination file. /// </param> internal static void CopyDirectory( string sourceDirectory, string destDirectory, FilterChain filterChain, Encoding inputEncoding, Encoding outputEncoding) { // If the source directory does not exist, throw an exception. if (!Directory.Exists(sourceDirectory)) { throw new BuildException( String.Format("Cannot copy directory: Source Directory {0} does not exist", sourceDirectory) ); } // Create the destination directory if it does not exist. if (!Directory.Exists(destDirectory)) { Directory.CreateDirectory(destDirectory); } // Copy all of the files to the destination directory using // the CopyFile static method. foreach (string file in Directory.GetFiles(sourceDirectory)) { string targetFile = CombinePaths(destDirectory, Path.GetFileName(file)); CopyFile(file, targetFile, filterChain, inputEncoding, outputEncoding); } // Copy all of the subdirectory information by calling this method again. foreach (string dir in Directory.GetDirectories(sourceDirectory)) { string targetDir = CombinePaths(destDirectory, Path.GetFileName(dir)); CopyDirectory(dir, targetDir, filterChain, inputEncoding, outputEncoding); } }
/// <summary> /// Moves a file filtering its content through the filter chain. /// </summary> /// <param name="sourceFileName"> /// The file to move. /// </param> /// <param name="destFileName"> /// The file to move move to. /// </param> /// <param name="filterChain"> /// Chain of filters to apply when moving, or <see langword="null" /> is no /// filters should be applied. /// </param> /// <param name="inputEncoding"> /// The encoding used to read the soure file. /// </param> /// <param name="outputEncoding"> /// The encoding used to write the destination file. /// </param> public static void MoveFile( string sourceFileName, string destFileName, FilterChain filterChain, Encoding inputEncoding, Encoding outputEncoding) { // If the source file does not exist, throw an exception. if (!File.Exists(sourceFileName)) { throw new BuildException( String.Format("Cannot move file: Source File {0} does not exist", sourceFileName)); } // if no filters have been defined, and no input or output encoding // is set, we can just use the File.Move method if (FilterChain.IsNullOrEmpty(filterChain) && inputEncoding == null && outputEncoding == null) { File.Move(sourceFileName, destFileName); } else { CopyFile(sourceFileName, destFileName, filterChain, inputEncoding, outputEncoding); File.Delete(sourceFileName); } }
/// <summary> /// Determines whether a given FilterChain is null or empty. /// </summary> /// <returns> /// <c>true</c> if <paramref name="filterChain"/> is null or empty; /// otherwise, <c>false</c>. /// </returns> /// <param name='filterChain'> /// The FilterChain to check. /// </param> internal static bool IsNullOrEmpty(FilterChain filterChain) { return (filterChain == null || filterChain.Filters.Count <= 0); }