-
Notifications
You must be signed in to change notification settings - Fork 926
Use file hash to check for file changes before transpiling (#299) #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
223789b
30cd331
36f9d1a
4a84edb
3bb1810
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -350,12 +350,36 @@ string filename | |
) | ||
{ | ||
var outputPath = GetOutputPath(filename); | ||
var sourceMapPath = GetSourceMapOutputPath(filename); | ||
var contents = _fileSystem.ReadAsString(filename); | ||
var result = TransformWithHeader(filename, contents, null); | ||
_fileSystem.WriteAsString(outputPath, result.Code); | ||
_fileSystem.WriteAsString(sourceMapPath, result.SourceMap == null ? string.Empty : result.SourceMap.ToJson()); | ||
return outputPath; | ||
if (MustTranspileFile(contents, outputPath)) | ||
{ | ||
var result = TransformWithHeader(filename, contents, null); | ||
|
||
var sourceMapPath = GetSourceMapOutputPath(filename); | ||
_fileSystem.WriteAsString(outputPath, result.Code); | ||
_fileSystem.WriteAsString(sourceMapPath, result.SourceMap == null ? string.Empty : result.SourceMap.ToJson()); | ||
} | ||
return outputPath; | ||
} | ||
|
||
/// <summary> | ||
/// Checks whether an input file (given as inputFileContents) should be transpiled | ||
/// by calculating the hash and comparing it to the hash value stored | ||
/// in the file given by outputPath. If the outputPath file does not | ||
/// exist the input file should always be transpiled. | ||
/// </summary> | ||
/// <param name="inputFileContents">The contents of the input file.</param> | ||
/// <param name="outputPath">The output path of the (possibly previously) generated file.</param> | ||
/// <returns>Returns true if the file should be transpiled, false otherwise.</returns> | ||
public virtual bool MustTranspileFile(string inputFileContents, string outputPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would prefer calling this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ValidateCache is rather vague and unspecific - CacheIsValid would be a better name, since it better hints at what state the cache is actually in, when true/false is returned. And I like flipping the true/false values, since it would enable me to turn around the if in your next comment ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, Also please make it |
||
{ | ||
if (!File.Exists(outputPath)) | ||
return true; | ||
|
||
var hashForInputFile = _fileCacheHash.CalculateHash(inputFileContents); | ||
var existingOutputContents = _fileSystem.ReadAsString(outputPath); | ||
var fileHasNotChanged = _fileCacheHash.ValidateHash(existingOutputContents, hashForInputFile); | ||
return !fileHasNotChanged; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could return early here, rather than wrapping the entire thing in the
if
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I didn't do that in the first place is that NOTs (!) in C# is rather subtle and easy to miss. Renaming the MustTranspileFile to e.g. CacheIsValid would allow me to do that. Good suggestion.
It would give two
return outputPath;
statements, though.