public void CompilationOfCodeWithAppAbsolutePaths()
        {
            // Arrange
            var virtualFileManagerMock = new Mock <IFileManager>();

            virtualFileManagerMock
            .SetupGet(fm => fm.SupportsVirtualPaths)
            .Returns(true)
            ;
            virtualFileManagerMock
            .Setup(fm => fm.GetCurrentDirectory())
            .Returns("/")
            ;

            IFileManager virtualFileManager = new VirtualFileManager(virtualFileManagerMock);
            var          options            = new CompilationOptions {
                SourceMap = true
            };

            // Act
            CompilationResult result;

            using (var compiler = new SassCompiler(virtualFileManager))
            {
                result = compiler.Compile(_siteInputFileContent, _siteInputFileAbsolutePath, options: options);
            }

            // Assert
            Assert.AreEqual(_siteOutputFileContent, result.CompiledContent);
            Assert.AreEqual(1, result.IncludedFilePaths.Count);
            Assert.AreEqual(_siteInputFileAbsolutePath, result.IncludedFilePaths[0]);
            Assert.AreEqual(_siteSourceMapFileContent, result.SourceMap);
        }
Beispiel #2
0
        public void CompilationOfFileWithAppAbsolutePaths()
        {
            // Arrange
            var virtualFileManagerMock = new Mock <IFileManager>();

            virtualFileManagerMock
            .SetupGet(fm => fm.SupportsVirtualPaths)
            .Returns(true)
            ;
            virtualFileManagerMock
            .Setup(fm => fm.GetCurrentDirectory())
            .Returns("/")
            ;
            virtualFileManagerMock
            .Setup(fm => fm.FileExists(It.IsAny <string>()))
            .Returns((string p) => {
                if (p == _siteInputFileAbsolutePath)
                {
                    return(true);
                }

                return(_siteImportedFiles.ContainsKey(p));
            })
            ;
            virtualFileManagerMock
            .Setup(fm => fm.ReadFile(It.IsAny <string>()))
            .Returns((string p) => {
                if (p == _siteInputFileAbsolutePath)
                {
                    return(_siteInputFileContent);
                }

                return(_siteImportedFiles[p]);
            })
            ;

            IFileManager virtualFileManager = new VirtualFileManager(virtualFileManagerMock);
            var          options            = new CompilationOptions {
                SourceMap = true
            };

            // Act
            CompilationResult result;

            using (var compiler = new SassCompiler(virtualFileManager))
            {
                result = compiler.CompileFile(_siteInputFileAbsolutePath, options: options);
            }

            // Assert
            Assert.AreEqual(_siteOutputFileContent, result.CompiledContent);
            Assert.AreEqual(_siteIncludedFilePaths, result.IncludedFilePaths);
            Assert.AreEqual(_siteSourceMapFileContent, result.SourceMap);
        }
Beispiel #3
0
        /// <summary>
        /// Sets a virtual file system wrapper
        /// </summary>
        /// <param name="virtualFileSystemWrapper">Virtual file system wrapper</param>
        public static void SetVirtualFileSystemWrapper(IVirtualFileSystemWrapper virtualFileSystemWrapper)
        {
            if (virtualFileSystemWrapper == null)
            {
                throw new ArgumentNullException("virtualFileSystemWrapper",
                                                string.Format(CoreStrings.Common_ArgumentIsNull, "virtualFileSystemWrapper"));
            }

            IFileManager virtualFileManager = new VirtualFileManager(virtualFileSystemWrapper);

            lock (_initializationSynchronizer)
            {
                SassCompiler.FileManager = virtualFileManager;
                _initialized             = true;
            }
        }
        /// <summary>
        /// Initializes a Sass compiler
        /// </summary>
        private static void Initialize()
        {
            if (!_initialized)
            {
                lock (_initializationSynchronizer)
                {
                    if (!_initialized)
                    {
                        IVirtualFileSystemWrapper virtualFileSystemWrapper =
                            BundleTransformerContext.Current.FileSystem.GetVirtualFileSystemWrapper();
                        IFileManager virtualFileManager = new VirtualFileManager(virtualFileSystemWrapper);

                        SassCompiler.FileManager = virtualFileManager;
                        _initialized             = true;
                    }
                }
            }
        }
        /// <summary>
        /// Constructs a instance of LESS translator
        /// </summary>
        /// <param name="createJsEngineInstance">Delegate that creates an instance of JS engine</param>
        /// <param name="virtualFileSystemWrapper">Virtual file system wrapper</param>
        /// <param name="lessConfig">Configuration settings of LESS translator</param>
        public LessTranslator(Func <IJsEngine> createJsEngineInstance,
                              IVirtualFileSystemWrapper virtualFileSystemWrapper,
                              LessSettings lessConfig)
        {
            _virtualFileManager = new VirtualFileManager(virtualFileSystemWrapper);

            UseNativeMinification = lessConfig.UseNativeMinification;
            IncludePaths          = lessConfig.IncludePaths
                                    .Cast <IncludedPathRegistration>()
                                    .Select(p => p.Path)
                                    .ToList()
            ;
            IeCompat          = lessConfig.IeCompat;
            Math              = lessConfig.Math;
            StrictUnits       = lessConfig.StrictUnits;
            DumpLineNumbers   = lessConfig.DumpLineNumbers;
            JavascriptEnabled = lessConfig.JavascriptEnabled;
            GlobalVariables   = lessConfig.GlobalVariables;
            ModifyVariables   = lessConfig.ModifyVariables;
            Severity          = lessConfig.Severity;

            if (createJsEngineInstance == null)
            {
                string jsEngineName = lessConfig.JsEngine.Name;
                if (string.IsNullOrWhiteSpace(jsEngineName))
                {
                    throw new ConfigurationErrorsException(
                              string.Format(CoreStrings.Configuration_JsEngineNotSpecified,
                                            "less",
                                            @"
  * JavaScriptEngineSwitcher.Msie
  * JavaScriptEngineSwitcher.V8
  * JavaScriptEngineSwitcher.ChakraCore",
                                            "MsieJsEngine")
                              );
                }

                createJsEngineInstance = () => JsEngineSwitcher.Current.CreateEngine(jsEngineName);
            }
            _createJsEngineInstance = createJsEngineInstance;
        }
        /// <summary>
        /// Constructs a instance of TypeScript translator
        /// </summary>
        /// <param name="createJsEngineInstance">Delegate that creates an instance of JS engine</param>
        /// <param name="virtualFileSystemWrapper">Virtual file system wrapper</param>
        /// <param name="tsConfig">Configuration settings of TypeScript translator</param>
        public TypeScriptTranslator(Func <IJsEngine> createJsEngineInstance,
                                    IVirtualFileSystemWrapper virtualFileSystemWrapper,
                                    TypeScriptSettings tsConfig)
        {
            _virtualFileManager = new VirtualFileManager(virtualFileSystemWrapper);

            AllowUnreachableCode             = tsConfig.AllowUnreachableCode;
            AllowUnusedLabels                = tsConfig.AllowUnusedLabels;
            AlwaysStrict                     = tsConfig.AlwaysStrict;
            DownlevelIteration               = tsConfig.DownlevelIteration;
            ForceConsistentCasingInFileNames = tsConfig.ForceConsistentCasingInFileNames;
            Libs = tsConfig.Libs
                   .Cast <LibraryFileRegistration>()
                   .Select(l => l.LibraryFileName)
                   .ToList()
            ;
            NewLine                    = tsConfig.NewLine;
            NoEmit                     = tsConfig.NoEmit;
            NoEmitHelpers              = tsConfig.NoEmitHelpers;
            NoEmitOnError              = tsConfig.NoEmitOnError;
            NoErrorTruncation          = tsConfig.NoErrorTruncation;
            NoFallthroughCasesInSwitch = tsConfig.NoFallthroughCasesInSwitch;
            NoImplicitAny              = tsConfig.NoImplicitAny;
            NoImplicitReturns          = tsConfig.NoImplicitReturns;
            NoImplicitThis             = tsConfig.NoImplicitThis;
            NoLib                          = tsConfig.NoLib;
            NoResolve                      = tsConfig.NoResolve;
            NoStrictGenericChecks          = tsConfig.NoStrictGenericChecks;
            NoUnusedLocals                 = tsConfig.NoUnusedLocals;
            NoUnusedParameters             = tsConfig.NoUnusedParameters;
            PreserveConstEnums             = tsConfig.PreserveConstEnums;
            RemoveComments                 = tsConfig.RemoveComments;
            SkipDefaultLibCheck            = tsConfig.SkipDefaultLibCheck;
            SkipLibCheck                   = tsConfig.SkipLibCheck;
            StrictNullChecks               = tsConfig.StrictNullChecks;
            StripInternal                  = tsConfig.StripInternal;
            SuppressExcessPropertyErrors   = tsConfig.SuppressExcessPropertyErrors;
            SuppressImplicitAnyIndexErrors = tsConfig.SuppressImplicitAnyIndexErrors;
            SuppressTypeCheckingErrors     = tsConfig.SuppressTypeCheckingErrors;
            Target                         = tsConfig.Target;
            TranspileOnly                  = tsConfig.TranspileOnly;

            if (createJsEngineInstance == null)
            {
                string jsEngineName = tsConfig.JsEngine.Name;
                if (string.IsNullOrWhiteSpace(jsEngineName))
                {
                    throw new ConfigurationErrorsException(
                              string.Format(CoreStrings.Configuration_JsEngineNotSpecified,
                                            "typeScript",
                                            @"
  * JavaScriptEngineSwitcher.Msie
  * JavaScriptEngineSwitcher.V8
  * JavaScriptEngineSwitcher.ChakraCore",
                                            "MsieJsEngine")
                              );
                }

                createJsEngineInstance = () => JsEngineSwitcher.Instance.CreateEngine(jsEngineName);
            }
            _createJsEngineInstance = createJsEngineInstance;
        }
        /// <summary>
        /// Constructs a instance of TypeScript translator
        /// </summary>
        /// <param name="createJsEngineInstance">Delegate that creates an instance of JS engine</param>
        /// <param name="virtualFileSystemWrapper">Virtual file system wrapper</param>
        /// <param name="tsConfig">Configuration settings of TypeScript translator</param>
        public TypeScriptTranslator(Func <IJsEngine> createJsEngineInstance,
                                    IVirtualFileSystemWrapper virtualFileSystemWrapper,
                                    TypeScriptSettings tsConfig)
        {
            _virtualFileManager = new VirtualFileManager(virtualFileSystemWrapper);

            AllowUnreachableCode             = tsConfig.AllowUnreachableCode;
            AllowUnusedLabels                = tsConfig.AllowUnusedLabels;
            AlwaysStrict                     = tsConfig.AlwaysStrict;
            DownlevelIteration               = tsConfig.DownlevelIteration;
            ExactOptionalPropertyTypes       = tsConfig.ExactOptionalPropertyTypes;
            ForceConsistentCasingInFileNames = tsConfig.ForceConsistentCasingInFileNames;
            KeyofStringsOnly                 = tsConfig.KeyofStringsOnly;
            Libs = tsConfig.Libs
                   .Cast <LibraryFileRegistration>()
                   .Select(l => l.LibraryFileName)
                   .ToList()
            ;
            NewLine                    = tsConfig.NewLine;
            NoEmit                     = tsConfig.NoEmit;
            NoEmitHelpers              = tsConfig.NoEmitHelpers;
            NoEmitOnError              = tsConfig.NoEmitOnError;
            NoErrorTruncation          = tsConfig.NoErrorTruncation;
            NoFallthroughCasesInSwitch = tsConfig.NoFallthroughCasesInSwitch;
            NoImplicitAny              = tsConfig.NoImplicitAny;
            NoImplicitOverride         = tsConfig.NoImplicitOverride;
            NoImplicitReturns          = tsConfig.NoImplicitReturns;
            NoImplicitThis             = tsConfig.NoImplicitThis;
            NoLib = tsConfig.NoLib;
            NoPropertyAccessFromIndexSignature = tsConfig.NoPropertyAccessFromIndexSignature;
            NoResolve                      = tsConfig.NoResolve;
            NoStrictGenericChecks          = tsConfig.NoStrictGenericChecks;
            NoUncheckedIndexedAccess       = tsConfig.NoUncheckedIndexedAccess;
            NoUnusedLocals                 = tsConfig.NoUnusedLocals;
            NoUnusedParameters             = tsConfig.NoUnusedParameters;
            PreserveConstEnums             = tsConfig.PreserveConstEnums;
            RemoveComments                 = tsConfig.RemoveComments;
            SkipDefaultLibCheck            = tsConfig.SkipDefaultLibCheck;
            SkipLibCheck                   = tsConfig.SkipLibCheck;
            StrictBindCallApply            = tsConfig.StrictBindCallApply;
            StrictNullChecks               = tsConfig.StrictNullChecks;
            StrictFunctionTypes            = tsConfig.StrictFunctionTypes;
            StrictPropertyInitialization   = tsConfig.StrictPropertyInitialization;
            StripInternal                  = tsConfig.StripInternal;
            SuppressExcessPropertyErrors   = tsConfig.SuppressExcessPropertyErrors;
            SuppressImplicitAnyIndexErrors = tsConfig.SuppressImplicitAnyIndexErrors;
            SuppressTypeCheckingErrors     = tsConfig.SuppressTypeCheckingErrors;
            Target                     = tsConfig.Target;
            TranspileOnly              = tsConfig.TranspileOnly;
            UseDefineForClassFields    = tsConfig.UseDefineForClassFields;
            UseUnknownInCatchVariables = tsConfig.UseUnknownInCatchVariables;

            if (createJsEngineInstance == null)
            {
                string jsEngineName = tsConfig.JsEngine.Name;
                if (string.IsNullOrWhiteSpace(jsEngineName))
                {
                    throw new ConfigurationErrorsException(
                              string.Format(CoreStrings.Configuration_JsEngineNotSpecified,
                                            "typeScript",
                                            @"
  * JavaScriptEngineSwitcher.Msie (only in the Chakra “Edge” JsRT mode)
  * JavaScriptEngineSwitcher.V8
  * JavaScriptEngineSwitcher.ChakraCore",
                                            "MsieJsEngine")
                              );
                }

                createJsEngineInstance = () => JsEngineSwitcher.Current.CreateEngine(jsEngineName);
            }
            _createJsEngineInstance = createJsEngineInstance;
        }