public void ShouldChangeFileName(string fullPath, string fileName, string expected) { // Given FilePath path = new FilePath(fullPath); // When FilePath result = path.ChangeFileName(fileName); // Then result.FullPath.ShouldBe(expected); }
public async Task <string> TryImportAsync(string requestedFile, string parentPath) { // Modify the requested file if we have an import path function string modifiedParentPath = null; if (_importPathFunc != null) { requestedFile = _importPathFunc(requestedFile); modifiedParentPath = _importPathFunc(parentPath); } if (string.IsNullOrWhiteSpace(requestedFile)) { return(null); } // Get the input relative path to the parent file // Make sure to try checking for a previously processed parent post-modification FilePath parentFilePath = new FilePath(parentPath); FilePath requestedFilePath = new FilePath(requestedFile); if (parentFilePath.IsRelative && !_parentAbsolutePaths.TryGetValue(parentFilePath, out parentFilePath) && (modifiedParentPath == null || !_parentAbsolutePaths.TryGetValue(modifiedParentPath, out parentFilePath))) { // Relative parent path and no available absolute path, try with the relative path parentFilePath = new FilePath(parentPath); } // Try to get the relative path to the parent file from inside the input virtual file system // But if the parent file isn't under an input path, just use it directly DirectoryPath containingInputPath = _fileSystem.GetContainingInputPath(parentFilePath); FilePath parentRelativePath = containingInputPath != null ? containingInputPath.GetRelativePath(parentFilePath) : parentFilePath; // Find the requested file by first combining with the parent FilePath filePath = parentRelativePath.ChangeFileName(requestedFilePath); string scss = await GetFileVariationsAsync(filePath, requestedFilePath); if (scss != null) { return(scss); } // That didn't work so try it again as a relative path from the input folder scss = await GetFileVariationsAsync(requestedFilePath, requestedFilePath); if (!requestedFilePath.IsAbsolute && scss != null) { return(scss); } return(null); }
public static FilePath <FileNameSimple> GetSampleFilePathFromFile(string classfFilePath) { string sampleFileNameNoExt = SampleFileNameFromFile(classfFilePath); if (sampleFileNameNoExt.IsVoid()) { return(null); } FilePath <FileNameSimple> sampleFilePath = DeriveSampleFolderPath(classfFilePath); sampleFilePath.ChangeFileName(sampleFileNameNoExt, FilePathConstants.SAMPLE_FILE_EXT); return(sampleFilePath); }
private async Task <string> GetFileVariationsAsync(FilePath filePath, FilePath requestedFilePath) { // ...as specified string scss = await GetFileAsync(filePath, requestedFilePath); if (scss != null) { return(scss); } // ...with extension (if not already) if (!filePath.HasExtension || filePath.Extension != ".scss") { FilePath extensionPath = filePath.AppendExtension(".scss"); scss = await GetFileAsync(extensionPath, requestedFilePath); if (scss != null) { return(scss); } // ...and with underscore prefix (if not already) if (!extensionPath.FileName.FullPath.StartsWith("_")) { extensionPath = extensionPath.ChangeFileName("_" + extensionPath.FileName.FullPath); scss = await GetFileAsync(extensionPath, requestedFilePath); if (scss != null) { return(scss); } } } // ...with underscore prefix (if not already) if (!filePath.FileName.FullPath.StartsWith("_")) { filePath = filePath.ChangeFileName("_" + filePath.FileName.FullPath); scss = await GetFileAsync(filePath, requestedFilePath); if (scss != null) { return(scss); } } return(null); }
private IFile GetInputFile(FilePath filePath) { // Find the requested file // ...as specified IFile file = _fileSystem.GetInputFile(filePath); if (file.Exists) { return(file); } // ...with extension (if not already) if (!filePath.HasExtension || filePath.Extension != ".less") { FilePath extensionPath = filePath.AppendExtension(".less"); IFile extensionFile = _fileSystem.GetInputFile(extensionPath); if (extensionFile.Exists) { return(extensionFile); } // ...and with underscore prefix (if not already) if (!extensionPath.FileName.FullPath.StartsWith("_")) { extensionPath = extensionPath.ChangeFileName("_" + extensionPath.FileName.FullPath); extensionFile = _fileSystem.GetInputFile(extensionPath); if (extensionFile.Exists) { return(extensionFile); } } } // ...with underscore prefix (if not already) if (!filePath.FileName.FullPath.StartsWith("_")) { filePath = filePath.ChangeFileName("_" + filePath.FileName.FullPath); IFile underscoreFile = _fileSystem.GetInputFile(filePath); if (underscoreFile.Exists) { return(underscoreFile); } } // Can't find it, default to the original return(file); }
/// <inheritdoc /> public override IDocument Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { // Get the included path relative to the document FilePath includedPath = new FilePath(args.SingleValue()); if (_sourcePath == null) { // Cache the source path for this shortcode instance since it'll be the same for all future shortcodes _sourcePath = document.GetFilePath("IncludeShortcodeSource", document.Source); } // Try to find the file relative to the current document path IFile includedFile = null; if (includedPath.IsRelative && _sourcePath != null) { includedFile = context.FileSystem.GetFile(_sourcePath.ChangeFileName(includedPath)); } // If that didn't work, try relative to the input folder if (includedFile == null || !includedFile.Exists) { includedFile = context.FileSystem.GetInputFile(includedPath); } // Get the included file if (!includedFile.Exists) { context.LogWarning($"Included file {includedPath.FullPath} does not exist"); return(context.CreateDocument()); } // Set the currently included shortcode source so nested includes can use it return(context.CreateDocument( new MetadataItems { { "IncludeShortcodeSource", includedFile.Path.FullPath } }, includedFile.GetContentProvider())); }
public void PropertyTest() { var filePath = new FilePath(DirectoryPath.OS.Temp, "Test.txt"); Assert.True(filePath.SplitValues.Count > 0); Assert.Equal("Test.txt", filePath.SplitValuesLast); Assert.Equal("Test.txt", filePath.FileName); Assert.Equal("Test", filePath.FileNameWithoutExtension); Assert.Equal(".txt", filePath.Extension); Assert.Equal(DirectoryPath.OS.Temp.IfEndsWithPathThenRemove(), filePath.DirectoryName); filePath.ChangeFileName("Hallo.xxx"); Assert.Equal("Hallo.xxx", filePath.FileName); Assert.Equal("Hallo", filePath.FileNameWithoutExtension); Assert.Equal(".xxx", filePath.Extension); Assert.Equal(DirectoryPath.OS.Temp.IfEndsWithPathThenRemove(), filePath.DirectoryName); filePath.ChangeExtension(".json"); Assert.Equal("Hallo.json", filePath.FileName); Assert.Equal("Hallo", filePath.FileNameWithoutExtension); Assert.Equal(".json", filePath.Extension); Assert.Equal(DirectoryPath.OS.Temp.IfEndsWithPathThenRemove(), filePath.DirectoryName); filePath.ChangeFileNameWithoutExtension("Test"); Assert.Equal("Test.json", filePath.FileName); Assert.Equal("Test", filePath.FileNameWithoutExtension); Assert.Equal(".json", filePath.Extension); Assert.Equal(DirectoryPath.OS.Temp.IfEndsWithPathThenRemove(), filePath.DirectoryName); filePath.RemoveExtension(); Assert.Equal("Test", filePath.FileName); Assert.Equal("Test", filePath.FileNameWithoutExtension); Assert.Equal("", filePath.Extension); Assert.Equal(DirectoryPath.OS.Temp.IfEndsWithPathThenRemove(), filePath.DirectoryName); }
// Returns null if the content wasn't modified private async Task <string> ProcessIncludesAsync(string content, FilePath source, IExecutionContext context) { bool modified = false; int start = 0; while (start >= 0) { start = content.IndexOf("^\"", start, StringComparison.Ordinal); if (start >= 0) { // Check if the include is escaped if (start > 0 && content[start - 1] == '\\') { modified = true; content = content.Remove(start - 1, 1); start++; } else { // This is a valid include int end = content.IndexOf('\"', start + 2); if (end > 0) { modified = true; // Get the correct included path FilePath includedPath = new FilePath(content.Substring(start + 2, end - (start + 2))); if (includedPath.IsRelative) { if (source == null) { throw new ExecutionException($"Cannot include file at relative path {includedPath.FullPath} because document source is null"); } includedPath = source.ChangeFileName(includedPath); } // Get and read the file content IFile includedFile = context.FileSystem.GetFile(includedPath); string includedContent = string.Empty; if (!includedFile.Exists) { context.LogWarning($"Included file {includedFile.Path.FullPath} does not exist"); } else { includedContent = await includedFile.ReadAllTextAsync(); } // Recursively process include statements if (_recursion) { string nestedContent = await ProcessIncludesAsync(includedContent, includedPath, context); if (nestedContent != null) { includedContent = nestedContent; } } // Do the replacement content = content.Remove(start, end - start + 1).Insert(start, includedContent); start += includedContent.Length; } } } } return(modified ? content : null); }