public void AddsExtensionToEmptyPath(string extension)
            {
                // Given
                NormalizedPath path = NormalizedPath.Empty;

                // When
                path = path.ChangeExtension(extension);

                // Then
                path.FullPath.ShouldBe(".foo");
            }
            public void ShouldChangeExtension(string extension, string expected)
            {
                // Given
                NormalizedPath path = new NormalizedPath("temp/hello.txt");

                // When
                path = path.ChangeExtension(extension);

                // Then
                Assert.AreEqual(expected, path.ToString());
            }
예제 #3
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            context.LogDebug($"Processing Sass for {input.ToSafeDisplayString()}");

            NormalizedPath inputPath = await _inputPath.GetValueAsync(input, context);

            if (inputPath.IsNull || inputPath.IsRelative)
            {
                inputPath = context.FileSystem.GetInputFile(new NormalizedPath(Path.GetRandomFileName())).Path;
                context.LogWarning($"No input path found for document {input.ToSafeDisplayString()}, using {inputPath.FileName.FullPath}");
            }

            string content = await input.GetContentStringAsync();

            // Sass conversion
            FileImporter importer = new FileImporter(context.FileSystem, _importPathFunc);
            ScssOptions  options  = new ScssOptions
            {
                OutputStyle       = _outputStyle,
                GenerateSourceMap = _generateSourceMap,
                SourceComments    = _includeSourceComments,
                InputFile         = inputPath.FullPath,
                TryImport         = importer.TryImport
            };
            IEnumerable <string> includePaths = _includePaths
                                                .Where(x => !x.IsNull)
                                                .Select(x =>
            {
                if (x.IsAbsolute)
                {
                    return(x.FullPath);
                }
                NormalizedPath containingInputPath = context.FileSystem.GetContainingInputPath(x);
                return(containingInputPath.IsNull ? null : containingInputPath.Combine(x).FullPath);
            })
                                                .Where(x => x != null);

            options.IncludePaths.AddRange(includePaths);
            ScssResult result = Scss.ConvertToCss(content, options);

            // Process the result
            NormalizedPath relativeDirectory = context.FileSystem.GetContainingInputPath(inputPath);
            NormalizedPath relativePath      = relativeDirectory.IsNull ? NormalizedPath.Null : relativeDirectory.GetRelativePath(inputPath);

            if (relativePath.IsNull)
            {
                relativePath = inputPath.FileName;
            }

            NormalizedPath cssPath     = relativePath.ChangeExtension("css");
            IDocument      cssDocument = input.Clone(
                cssPath,
                await context.GetContentProviderAsync(result.Css ?? string.Empty, MediaTypes.Css));

            // Generate a source map if requested
            if (_generateSourceMap && result.SourceMap != null)
            {
                NormalizedPath sourceMapPath     = relativePath.ChangeExtension("map");
                IDocument      sourceMapDocument = input.Clone(
                    sourceMapPath,
                    await context.GetContentProviderAsync(result.SourceMap));
                return(new[] { cssDocument, sourceMapDocument });
            }

            return(cssDocument.Yield());
        }