/// <summary> /// Creates a new <see cref="VersionFileList"/>. /// </summary> /// <param name="rootDir">The root directory containing the files to include.</param> /// <param name="filters">The filters to include.</param> /// <returns>The <see cref="VersionFileList"/> created from the given parameters.</returns> public static VersionFileList Create(string rootDir, IEnumerable<string> filters) { if (log.IsDebugEnabled) log.DebugFormat("VersionFileList.Create(rootDir: {0}, fiters: ...)", rootDir); var addFiles = new List<VersionFileInfo>(); var files = Directory.GetFiles(rootDir, "*", SearchOption.AllDirectories); // Get the length of the root directory so we know how much to chop off so that each file starts relative to // the rootDir, and does not start with a path separator var rootDirLen = rootDir.Length; if (!rootDir.EndsWith(Path.DirectorySeparatorChar.ToString()) && !rootDir.EndsWith(Path.AltDirectorySeparatorChar.ToString())) rootDirLen++; foreach (var f in files) { // Get the file information var fi = new FileInfo(f); // Generate the properties we are interested in var relativePath = fi.FullName.Substring(rootDirLen); var hash = Hasher.GetFileHash(fi.FullName); var size = fi.Length; // Add to the list var vfi = new VersionFileInfo(relativePath, hash, size); addFiles.Add(vfi); } var vfl = new VersionFileList(addFiles, filters); return vfl; }
/// <summary> /// Attempts to get the <see cref="VersionFileInfo"/> for a file. /// </summary> /// <param name="relativeFile">The relative file to get the <see cref="VersionFileInfo"/> for.</param> /// <param name="fileInfo">When this method returns true, contains the <see cref="VersionFileInfo"/> for the /// <paramref name="relativeFile"/>.</param> /// <returns>True if the <paramref name="relativeFile"/> exists in this version; otherwise false.</returns> public bool TryGetFileInfo(string relativeFile, out VersionFileInfo fileInfo) { // Make sure it doesn't start with the directory seperator character if (relativeFile.StartsWith(Path.DirectorySeparatorChar.ToString()) || relativeFile.StartsWith(Path.AltDirectorySeparatorChar.ToString())) { Debug.Fail("You shouldn't have the directory seperator character prefixed..."); relativeFile = relativeFile.Substring(1); } return _filesDict.TryGetValue(relativeFile, out fileInfo); }
/// <summary> /// Creates the <see cref="VersionFileList"/> from a string. /// </summary> /// <param name="contents">The version file list contents.</param> /// <returns>The <see cref="VersionFileList"/> loaded from the <paramref name="contents"/>.</returns> /// <exception cref="InvalidDataException">Any of the lines are invalid.</exception> public static VersionFileList CreateFromString(string contents) { if (log.IsDebugEnabled) { log.DebugFormat("VersionFileList.CreateFromString(contents: {0})", contents); } var lines = contents.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); // Create the lists var files = new List <VersionFileInfo>(); var filters = new List <string>(); // Add to file list instead of the filters by default var addToFiles = true; var strComp = StringComparer.OrdinalIgnoreCase; foreach (var line in lines) { if (string.IsNullOrEmpty(line)) { continue; } var l = line.Trim(); // Check for a header if (strComp.Equals(IncludeHeader, l)) { addToFiles = true; continue; } else if (strComp.Equals(IgnoreHeader, l)) { addToFiles = false; continue; } // Not a header, so split according to the type if (addToFiles) { var vfi = VersionFileInfo.FromString(l); files.Add(vfi); } else { // Expect a line for a filter if (l.Contains(Delimiter)) { const string errmsg = "Was expecting a line containing a filter on line: {0}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, line); } throw new InvalidDataException(string.Format(errmsg, line)); } filters.Add(l); } } var vfl = new VersionFileList(files, filters); return(vfl); }