public override bool Resolve(IFileProperties fileProperties) { BaseFileProperties baseFileProperties = fileProperties as BaseFileProperties; if (baseFileProperties == null) { throw new RuleException($"Resolve unable to cast {nameof(fileProperties)} arg to {nameof(BaseFileProperties)}, {nameof(fileProperties)}.Type = {fileProperties.GetType()}"); } return(ResolveStringMatch(fileProperties.FileNameWithoutExtension)); }
/// <summary> /// Class that creates an appropriate <see cref="IFileProperties"/> object from a filePath /// based on the mapping <see cref="SupportedFileTypes"/> /// </summary> /// <param name="fullFilePath"></param> /// <returns></returns> public async Task <IFileProperties> FetchFileProperties(string fullFilePath) { FileInfo fileInfo = new FileInfo(fullFilePath); IFileProperties fileProperties = null; if (SupportedFileTypes.ExtensionToFilePropertiesFactory.ContainsKey(fileInfo.Extension)) { Func <FileInfo, Task <IFileProperties> > filePropertiesCreateFunc = SupportedFileTypes.ExtensionToFilePropertiesFactory[fileInfo.Extension]; fileProperties = await filePropertiesCreateFunc(fileInfo); } else { Console.WriteLine($"Warning: FileLoader attempt to load unknown file extension: {fileInfo.Extension}, defaulting to {nameof(BaseFileProperties)}"); fileProperties = new BaseFileProperties(); await fileProperties.PopuplateFromFileInfo(fileInfo); } return(fileProperties); }
/// <summary> /// Resolve a tokenized string based on a file's properties /// </summary> /// <param name="tokenString">the value of either "output_path" or "output_file"</param> /// <param name="fileProperties">the file property object of the file being checked</param> /// <param name="matchingFileCount">the current number of files that match this <see cref="ValidationConfig"/>, including the current fileProperties</param> /// <param name="jsonPropertyName">literal either "output_path" or "output_file" (used for error output)</param> /// <returns>the resolved string with the tokens substituted</returns> private string ResolveTokenString(string tokenString, IFileProperties fileProperties, uint matchingFileCount, string jsonPropertyName) { string originalFileNameWithoutExtension = fileProperties.FileNameWithoutExtension; string[] fileNameParts = originalFileNameWithoutExtension.Split(_nameSeperator); string resolvedTokenString = tokenString; foreach (Match match in tokenRegexPattern.Matches(tokenString)) { string token = match.Value; string partPrefix = "{part"; string partSuffix = "}"; if (token.StartsWith(partPrefix) && token.EndsWith(partSuffix)) { string partIndexStr = token.Substring(partPrefix.Length, token.Length - (partPrefix.Length + partSuffix.Length)); if (Int32.TryParse(partIndexStr, out int partIndex)) { if (partIndex < 0 || partIndex >= fileNameParts.Length) { throw new IndexOutOfRangeException($"'part' index: {partIndex} out of range: {fileNameParts.Length} in validation config: {Name} for file: {originalFileNameWithoutExtension}"); } resolvedTokenString = resolvedTokenString.Replace(token, fileNameParts[partIndex]); } else { throw new Exception($"Failed to parse 'part' token for file: {originalFileNameWithoutExtension} in validation cofig: {Name}"); } continue; } switch (token) { case "{current_name}": { resolvedTokenString = resolvedTokenString.Replace(token, fileProperties.FileNameWithoutExtension); break; } case "{current_extension}": { BaseFileProperties baseFileProperties = fileProperties as BaseFileProperties; string extensionWithoutDot = baseFileProperties.FileExtension.Substring(1); resolvedTokenString = resolvedTokenString.Replace(token, extensionWithoutDot); break; } case "{width}": { TextureFileProperties textureFileProperties = fileProperties as TextureFileProperties; if (textureFileProperties == null) { throw new Exception($"file: {fileProperties.FileName} is not a texture file and thus doesn't have a 'width' property"); } resolvedTokenString = resolvedTokenString.Replace(token, textureFileProperties.Width.ToString()); break; } case "{height}": { TextureFileProperties textureFileProperties = fileProperties as TextureFileProperties; if (textureFileProperties == null) { throw new Exception($"file: {fileProperties.FileName} is not a texture file and thus doesn't have a 'height' property"); } resolvedTokenString = resolvedTokenString.Replace(token, textureFileProperties.Height.ToString()); break; } case "{file_count}": { resolvedTokenString = resolvedTokenString.Replace(token, matchingFileCount.ToString("D3")); break; } default: { throw new Exception($"Unknown token: {token} found in '{jsonPropertyName}' of validation_config: {Name}"); } } } return(resolvedTokenString); }