forked from chucknorris/roundhouse
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SpecifiedOrder File Locator and added a /scriptorder option which allows you to select between the traverse, recurse or scriptorder options. The /searchallinsteadoftraverse option also controls the /scriptorder so RoundHouse stays backwards compatibile. The SpecifiedOrder Locator looks at file (set by the /SpecifiedOrderFile option, otherwise scriptorder.txt) in the migration folder and orders each file in that migration folder and it's subdirectories based on the position that file has in the specified order file. Files that aren't inside the specified order file are returned after the files that are and are sorted via the culture invariant case-insensitive comparer on the filename relative to the migration directory. Use Cases: 1. Using the scriptorder file allows to specify the script order in your integration branch as you merge different feature branches into it. This means that feature branch scripts can always rely on the existing scripts in the integration branch being run first. 2. Stored Procedure A depends on Stored Procedure B. Notes: The script order file considers lines starting with # as comment lines and are ignored.
- Loading branch information
Showing
9 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
product/roundhouse/infrastructure/filesystem/filelocators/SpecifiedOrdering.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace roundhouse.infrastructure.filesystem.filelocators | ||
{ | ||
/// <summary> | ||
/// Supports the use of a specific ordering in a directory, via | ||
/// the use of a file which specifies the order of each file, using | ||
/// a relative path. | ||
/// | ||
/// Files in the directory or subdirectories that are not in the specified order file | ||
/// are sorted according to their relative path. | ||
/// | ||
/// The Script Ordering File: | ||
/// - Lines starting with # are ignored | ||
/// - Each relative file path are on it's own line, without the leading directory seperator | ||
/// </summary> | ||
public class SpecifiedOrdering : FileLocator | ||
{ | ||
/// <summary> | ||
/// Construct a SpecifiedOrdering FileLocator. | ||
/// </summary> | ||
/// <param name="ordering_file_name">The path to the specific ordering file relative to the directory.</param> | ||
public SpecifiedOrdering(string ordering_file_name) | ||
{ | ||
this.ordering_file_name = ordering_file_name; | ||
} | ||
|
||
private readonly string ordering_file_name; | ||
|
||
/// <summary> | ||
/// Return the files in this directory and all subdirectories, | ||
/// the order is as follows: | ||
/// 1. Files specified in the ordering file, in the order of their entry in that file | ||
/// 2. Files not specified in the ordering, in the culture invariant case-insensitive order | ||
/// of the relative file name. | ||
/// </summary> | ||
/// <param name="directory">The directory to search</param> | ||
/// <param name="pattern">The file pattern you are searching for</param> | ||
/// <returns>The ordered list of files.</returns> | ||
public string[] locate_all_files_in(string directory, string pattern) | ||
{ | ||
var ordering_file = Path.Combine(directory, ordering_file_name); | ||
// Load the specified order file and create a dictionary of the filenames and their positions. | ||
var specifiedOrder = File.Exists(ordering_file) | ||
? File.ReadAllLines(ordering_file) | ||
// skip comment lines. | ||
.Where(line => !line.StartsWith("#")) | ||
.Select((relativeFileName, order) => new KeyValuePair<string, int>(relativeFileName, order)) | ||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, StringComparer.InvariantCultureIgnoreCase) | ||
: new Dictionary<string, int>(); | ||
|
||
var files = Directory.GetFiles(directory, pattern, SearchOption.AllDirectories); | ||
// Sort the files according to their position in the ordering file | ||
// or failing that their relative file paths. | ||
Array.Sort(files, (lhsRaw, rhsRaw) => | ||
{ | ||
// obtain the file names relative to the search directory. | ||
var lhs = | ||
lhsRaw.Substring(directory.Length).TrimStart( | ||
Path.DirectorySeparatorChar); | ||
var rhs = | ||
rhsRaw.Substring(directory.Length).TrimStart( | ||
Path.DirectorySeparatorChar); | ||
int lhsPosition; | ||
int rhsPosition; | ||
// Figure out whether the files are in the specified order file. | ||
bool lhsIsSpecified = specifiedOrder.TryGetValue(lhs, out lhsPosition); | ||
bool rhsIsSpecified = specifiedOrder.TryGetValue(rhs, out rhsPosition); | ||
if(lhsIsSpecified && rhsIsSpecified) | ||
{ | ||
// Both in specified ordering file, | ||
// Order them by their location in that file. | ||
return Comparer<int>.Default.Compare(lhsPosition, rhsPosition); | ||
} else if(lhsIsSpecified) | ||
{ | ||
// Left hand side is in the specified file, | ||
// so it goes before the right hand side. | ||
return -1; | ||
} else if (rhsIsSpecified) | ||
{ | ||
// Right hand side is in the specified file, | ||
// so it goes before the left hand side. | ||
return 1; | ||
} | ||
// neither side is in the ordering file, | ||
// use the string comparer against the the relative path to | ||
// each file. | ||
return StringComparer.InvariantCultureIgnoreCase.Compare(lhs, rhs); | ||
} | ||
); | ||
|
||
return files; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters