NodeAssets.Compilers.LessCompiler.Compile C# (CSharp) Method

Compile() public method

public Compile ( string initial, FileInfo originalFile ) : Task
initial string
originalFile System.IO.FileInfo
return Task
        public async Task<CompileResult> Compile(string initial, FileInfo originalFile)
        {
            initial = initial ?? string.Empty;

            dynamic options = new JObject();
            options.dumpLineNumbers = "comments";
            if (originalFile != null)
            {
                options.filename = originalFile.FullName.Replace('\\', '/');
            }

            var script = _executeScript
                .Replace("{0}", JsonConvert.SerializeObject(options));

            var command = _executor.ExecuteJsScript(script);
            command.StdIn.Write(initial);
            command.StdIn.Flush();
            command.StdIn.Close();

            var jsonStr = await _executor.RunCommand(command).ConfigureAwait(false);
            dynamic resultData;
            try
            {
                resultData = JsonConvert.DeserializeObject(jsonStr);
            }
            catch (JsonReaderException)
            {
                throw new CompileException(jsonStr);
            }
            catch (Exception e)
            {
                throw new CompileException(e.Message, e);
            }

            var deps = new List<string>();
            if (resultData.imports != null)
            {
                foreach(string i in resultData.imports)
                {
                    deps.Add(i);
                }
            }

            return new CompileResult
            {
                Output = (string)resultData.css,
                AdditionalDependencies = deps
            };
        }
    }

Usage Example

        public void Compile_InvalidLessFile_Exception()
        {
            var file = new FileInfo(TestContext.CurrentContext.TestDirectory + "/../../Data/invalidLess.less");
            var styl = File.ReadAllText(TestContext.CurrentContext.TestDirectory + "/../../Data/invalidLess.less");
            var compiler = new LessCompiler(_executor);

            Assert.ThrowsAsync(typeof(CompileException), async () =>
            {
                await compiler.Compile(styl, file).ConfigureAwait(false);
            });
        }
All Usage Examples Of NodeAssets.Compilers.LessCompiler::Compile