Пример #1
0
        /// <summary>
        /// Executes the processor on the specified configuration.
        /// </summary>
        public Task ExecuteAsync(IAssetContext context)
        {
            var           content      = new Dictionary <string, byte[]>();
            var           env          = (IWebHostEnvironment)context.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment));
            IFileProvider fileProvider = context.Asset.GetFileProvider(env);

            foreach (string route in context.Content.Keys)
            {
                IFileInfo file     = fileProvider.GetFileInfo(route);
                var       settings = new ScssOptions {
                    InputFile = file.PhysicalPath
                };
                if (options != null)
                {
                    settings.IncludePaths.AddRange(options.IncludePaths);
                    settings.GenerateSourceMap      = options.GenerateSourceMap;
                    settings.Indent                 = options.Indent;
                    settings.IsIndentedSyntaxSource = options.IsIndentedSyntaxSource;
                    settings.Linefeed               = options.Linefeed;
                    settings.OmitSourceMapUrl       = options.OmitSourceMapUrl;
                    settings.SourceComments         = options.SourceComments;
                    settings.SourceMapContents      = options.SourceMapContents;
                    settings.SourceMapEmbed         = options.SourceMapEmbed;
                    settings.SourceMapRoot          = options.SourceMapRoot;
                }

                ScssResult result = Scss.ConvertToCss(context.Content[route].AsString(), settings);

                content[route] = result.Css.AsByteArray();
            }

            context.Content = content;

            return(Task.CompletedTask);
        }
Пример #2
0
        /// <summary>
        /// Executes the processor on the specified configuration.
        /// </summary>
        public Task ExecuteAsync(IAssetContext context)
        {
            var           content      = new Dictionary <string, byte[]>();
            var           env          = (IWebHostEnvironment)context.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment));
            IFileProvider fileProvider = context.Asset.GetFileProvider(env);

            foreach (string route in context.Content.Keys)
            {
                IFileInfo file = fileProvider.GetFileInfo(route);

                var settings = new ScssOptions {
                    InputFile = file.PhysicalPath
                };
                settings.TryImport =
                    (string file, string parentPath, out string scss, out string map) =>
                {
                    // System.Console.WriteLine("File to import is " + file);
                    // System.Console.WriteLine("Parent Path is " + parentPath);
                    var basePath = "/styles/";
                    var path     = GetFilePath(file, parentPath);
                    var f        = fileProvider.GetFileInfo(basePath + path + ".scss");
                    using (var reader = new System.IO.StreamReader(f.CreateReadStream(), System.Text.Encoding.UTF8))
                    {
                        string value = reader.ReadToEnd();
                        // System.Console.WriteLine("SCSS is " + value);
                        scss = value;
                        map  = null;
                    }
                    return(true);
                };

                System.Console.WriteLine("FP " + fileProvider.GetType().Name);
                System.Console.WriteLine("Route is " + route);
                System.Console.WriteLine("File is " + file.PhysicalPath);
                System.Console.WriteLine("Settings: " + settings.IncludePaths);

                ScssResult result = Scss.ConvertToCss(context.Content[route].AsString(), settings);

                content[route] = result.Css.AsByteArray();
            }

            context.Content = content;

            foreach (string key in context.Content.Keys)
            {
                IFileInfo input  = fileProvider.GetFileInfo(key);
                IFileInfo output = fileProvider.GetFileInfo(context.Asset.Route);

                System.Console.WriteLine("Input is " + input.Name + " + " + input.PhysicalPath);
                System.Console.WriteLine("Output is " + output.Name + " + " + output.PhysicalPath);
                string absoluteOutputPath = new System.IO.FileInfo(output.PhysicalPath).FullName;
                System.Console.WriteLine("Output actual path is " + absoluteOutputPath);
            }


            return(Task.CompletedTask);
        }
Пример #3
0
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            var options = new ScssOptions();

            ScssResult   result   = Scss.ConvertFileToCss(inputFileName, options);
            UglifyResult minified = Uglify.Css(result.Css);

            return(Encoding.UTF8.GetBytes(minified.Code));
        }
Пример #4
0
        protected override async Task <IEnumerable <IDocument> > ExecuteAsync(IDocument input, IExecutionContext context)
        {
            context.LogDebug($"Processing Sass for {input.ToSafeDisplayString()}");

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

            if (inputPath?.IsAbsolute != true)
            {
                inputPath = context.FileSystem.GetInputFile(new FilePath(Path.GetRandomFileName())).Path;
                context.LogWarning($"No input path found for document {input.ToSafeDisplayString()}, using {inputPath.FileName.FullPath}");
            }

            string content = await input.GetStringAsync();

            // 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 != null)
                                                .Select(x => x.IsAbsolute ? x.FullPath : context.FileSystem.GetContainingInputPath(x)?.Combine(x)?.FullPath)
                                                .Where(x => x != null);

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

            // Process the result
            DirectoryPath relativeDirectory = context.FileSystem.GetContainingInputPath(inputPath);
            FilePath      relativePath      = relativeDirectory?.GetRelativePath(inputPath) ?? inputPath.FileName;

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

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

            return(cssDocument.Yield());
        }
Пример #5
0
        /// <summary>
        /// Executes the processor on the specified configuration.
        /// </summary>
        public Task ExecuteAsync(IAssetContext context)
        {
            var content = new Dictionary<string, byte[]>();
            var env = (IWebHostEnvironment)context.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment));
            IFileProvider fileProvider = context.Asset.GetFileProvider(env);

            foreach (string route in context.Content.Keys)
            {
                IFileInfo file = fileProvider.GetFileInfo(route);
                var settings = new ScssOptions { InputFile = file.PhysicalPath };

                ScssResult result = Scss.ConvertToCss(context.Content[route].AsString(), settings);

                content[route] = result.Css.AsByteArray();
            }

            context.Content = content;

            return Task.CompletedTask;
        }
Пример #6
0
        private async System.Threading.Tasks.Task TranspileAsync(string filePath)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            DTE2 dte = await AsyncServiceProvider.GlobalProvider.GetServiceAsync <DTE, DTE2>();

            var options = new ScssOptions();

            options.IncludePaths.Add(Path.GetDirectoryName(filePath));
            ScssResult result = Scss.ConvertToCss(_doc.TextBuffer.CurrentSnapshot.GetText(), options);

            var cssPath = Path.ChangeExtension(filePath, ".css");

            dte.SourceControl.CheckOutItemSafely(cssPath);

            using (var writer = new StreamWriter(cssPath))
            {
                await writer.WriteAsync(result.Css);
            }

            ProjectItem scssItem = dte.Solution.FindProjectItem(filePath);
            ProjectItem cssItem  = dte.Solution.FindProjectItem(cssPath);

            if (cssItem != null || scssItem.TryAddNestedFile(cssPath, out cssItem))
            {
                UglifyResult minified = Uglify.Css(result.Css);
                var          minPath  = Path.ChangeExtension(cssPath, ".min.css");

                dte.SourceControl.CheckOutItemSafely(minPath);

                using (var writer = new StreamWriter(minPath))
                {
                    await writer.WriteAsync(minified.Code);
                }

                cssItem.TryAddNestedFile(minPath, out _);
            }
        }
Пример #7
0
        /// <inheritdoc />
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            return(inputs
                   .AsParallel()
                   .SelectMany(context, input =>
            {
                Trace.Verbose($"Processing Sass for {input.SourceString()}");

                FilePath inputPath = _inputPath.Invoke <FilePath>(input, context);
                if (inputPath?.IsAbsolute != true)
                {
                    inputPath = context.FileSystem.GetInputFile(new FilePath(Path.GetRandomFileName())).Path;
                    Trace.Warning($"No input path found for document {input.SourceString()}, using {inputPath.FileName.FullPath}");
                }

                string content = input.Content;

                // 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
                };
                options.IncludePaths.AddRange(
                    _includePaths
                    .Where(x => x != null)
                    .Select(x => x.IsAbsolute ? x.FullPath : context.FileSystem.GetContainingInputPath(x)?.Combine(x)?.FullPath)
                    .Where(x => x != null));
                ScssResult result = Scss.ConvertToCss(content, options);

                // Process the result
                DirectoryPath relativeDirectory = context.FileSystem.GetContainingInputPath(inputPath);
                FilePath relativePath = relativeDirectory?.GetRelativePath(inputPath) ?? inputPath.FileName;

                FilePath cssPath = relativePath.ChangeExtension("css");
                IDocument cssDocument = context.GetDocument(
                    input,
                    context.GetContentStream(result.Css ?? string.Empty),
                    new MetadataItems
                {
                    { Keys.RelativeFilePath, cssPath },
                    { Keys.WritePath, cssPath }
                });

                IDocument sourceMapDocument = null;
                if (_generateSourceMap && result.SourceMap != null)
                {
                    FilePath sourceMapPath = relativePath.ChangeExtension("map");
                    sourceMapDocument = context.GetDocument(
                        input,
                        context.GetContentStream(result.SourceMap),
                        new MetadataItems
                    {
                        { Keys.RelativeFilePath, sourceMapPath },
                        { Keys.WritePath, sourceMapPath }
                    });
                }

                return new[] { cssDocument, sourceMapDocument };
            })
                   .Where(x => x != null));
        }