コード例 #1
0
        // Don't try-catch this method: We need to "address" all the bugs,
        // which may occur as the (node.js-based) service implement changes.
        private async Task<CompilerResult> ProcessResult(CompilerResult result, string sourceFileName, string targetFileName)
        {
            if (result == null)
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compilation failed: The service failed to respond to this request\n\t\t\tPossible cause: Syntax Error!");
                return await CompilerResultFactory.GenerateResult(sourceFileName, targetFileName);
            }
            if (!result.IsSuccess)
            {
                var firstError = result.Errors.Where(e => e != null).Select(e => e.Message).FirstOrDefault();

                if (firstError != null)
                    Logger.Log(ServiceName + ": " + Path.GetFileName(result.SourceFileName) + " compilation failed: " + firstError);

                return result;
            }

            string resultString = PostProcessResult(result);

            if (result.TargetFileName != null && (MinifyInPlace || !File.Exists(result.TargetFileName) ||
                !ReferenceEquals(string.Intern(resultString), string.Intern(await FileHelpers.ReadAllTextRetry(result.TargetFileName)))))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.TargetFileName);
                await FileHelpers.WriteAllTextRetry(result.TargetFileName, resultString);
            }
            if (GenerateSourceMap && (!File.Exists(result.MapFileName) ||
                !ReferenceEquals(string.Intern(result.ResultMap), string.Intern(await FileHelpers.ReadAllTextRetry(result.MapFileName)))))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.MapFileName);
                await FileHelpers.WriteAllTextRetry(result.MapFileName, result.ResultMap);
            }

            return CompilerResult.UpdateResult(result, resultString);
        }
コード例 #2
0
        protected async override Task RtlVariantHandler(CompilerResult result)
        {
            if (!WESettings.Instance.Css.RtlCss || result.RtlTargetFileName == null)
                return;

            string value = PostProcessResult(result.RtlResult, result.RtlTargetFileName, result.RtlSourceFileName);

            // Write output file
            if (result.RtlTargetFileName != null && (MinifyInPlace || !File.Exists(result.RtlTargetFileName) ||
                value != await FileHelpers.ReadAllTextRetry(result.RtlTargetFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlTargetFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlTargetFileName, value);
                ProjectHelpers.AddFileToProject(result.RtlSourceFileName, result.RtlTargetFileName);
            }

            // Write map file
            if (GenerateSourceMap && (!File.Exists(result.RtlMapFileName) ||
                result.RtlResultMap != await FileHelpers.ReadAllTextRetry(result.RtlMapFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlMapFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlMapFileName, result.RtlResultMap);
                ProjectHelpers.AddFileToProject(result.RtlTargetFileName, result.RtlMapFileName);
            }
        }
コード例 #3
0
ファイル: CssCompilerBase.cs プロジェクト: waodng/VSIX
 protected async override Task PostWritingResult(CompilerResult result)
 {
     if (WESettings.Instance.Css.RtlCss && result.RtlTargetFileName != null)
     {
         await HandleRtlCss(result);
     }
 }
コード例 #4
0
        protected override string PostProcessResult(CompilerResult result)
        {
            string resultString = result.Result;

            // If the caller wants us to renormalize URLs to a different filename, do so.
            if (result.TargetFileName != null &&
                Path.GetDirectoryName(result.TargetFileName) != Path.GetDirectoryName(result.SourceFileName) &&
                result.Result.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0)
            {
                try
                {
                    resultString = CssUrlNormalizer.NormalizeUrls(
                        tree: new CssParser().Parse(result.Result, true),
                        targetFile: result.TargetFileName,
                        oldBasePath: result.SourceFileName
                    );

                    Logger.Log(ServiceName + ": " + Path.GetFileName(result.SourceFileName) + " compiled.");
                }
                catch (Exception ex)
                {
                    Logger.Log(ServiceName + ": An error occurred while normalizing generated paths in " + result.SourceFileName + "\r\n" + ex);
                }
            }
            else
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(result.SourceFileName) + " compiled.");
            }

            return resultString;
        }
コード例 #5
0
        protected async override Task RtlVariantHandler(CompilerResult result)
        {
            if (!WESettings.Instance.Css.RtlCss || result.RtlTargetFileName == null)
            {
                return;
            }

            string value = PostProcessResult(result.RtlResult, result.RtlTargetFileName, result.RtlSourceFileName);

            // Write output file
            if (result.RtlTargetFileName != null && (MinifyInPlace || !File.Exists(result.RtlTargetFileName) ||
                                                     value != await FileHelpers.ReadAllTextRetry(result.RtlTargetFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlTargetFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlTargetFileName, value);

                ProjectHelpers.AddFileToProject(result.RtlSourceFileName, result.RtlTargetFileName);
            }

            // Write map file
            if (GenerateSourceMap && (!File.Exists(result.RtlMapFileName) ||
                                      result.RtlResultMap != await FileHelpers.ReadAllTextRetry(result.RtlMapFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlMapFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlMapFileName, result.RtlResultMap);

                ProjectHelpers.AddFileToProject(result.RtlTargetFileName, result.RtlMapFileName);
            }
        }
コード例 #6
0
        public virtual async Task <CompilerResult> CompileAsync(string sourceFileName, string targetFileName)
        {
            bool onlyPreview = false;

            if (WEIgnore.TestWEIgnore(sourceFileName, this is ILintCompiler ? "linter" : "compiler", ServiceName.ToLowerInvariant()))
            {
                if (WESettings.Instance.General.ShowWEIgnoreLogs)
                {
                    Logger.Log(String.Format(CultureInfo.CurrentCulture,
                                             "{0}: The file {1} is ignored by .weignore. Skipping..",
                                             ServiceName, Path.GetFileName(sourceFileName)));
                }

                if (!Previewing)
                {
                    return(await CompilerResultFactory.GenerateResult(sourceFileName, targetFileName, string.Empty, false, string.Empty, string.Empty, Enumerable.Empty <CompilerError>(), true));
                }

                onlyPreview = true;
            }

            CompilerResult response = await NodeServer.CallServiceAsync(GetPath(sourceFileName, targetFileName));

            return(await ProcessResult(response, onlyPreview, sourceFileName, targetFileName));
        }
コード例 #7
0
        public static async Task<CompilerResult> Compile(string filename, string targetFilename = null)
        {
            string output = Path.GetTempFileName();

            string webEssentialsDir = Path.GetDirectoryName(typeof(LessCompiler).Assembly.Location);
            string lessc = Path.Combine(webEssentialsDir, @"Resources\nodejs\node_modules\.bin\lessc.cmd");
            string arguments = String.Format("--no-color --relative-urls \"{0}\" \"{1}\"", filename, output);
            if (WESettings.GetBoolean(WESettings.Keys.LessSourceMaps))
                arguments = String.Format(
                  "--relative-urls --source-map=\"{0}.map\" \"{1}\" \"{2}\"",
                  targetFilename ?? filename,
                  filename,
                  output);

            ProcessStartInfo start = new ProcessStartInfo(lessc)
            {
                WindowStyle = ProcessWindowStyle.Hidden,
                WorkingDirectory = Path.Combine(webEssentialsDir, @"Resources\nodejs"),
                CreateNoWindow = true,
                Arguments = arguments,
                UseShellExecute = false,
                RedirectStandardError = true
            };

            using (var process = await ExecuteAsync(start))
            {
                CompilerResult result = new CompilerResult(filename);

                ProcessResult(output, process, result);

                if (result.IsSuccess)
                {
                    // Inserts an empty row between each rule and replace two space indentation with 4 space indentation
                    result.Result = _endingCurlyBraces.Replace(_linesStartingWithTwoSpaces.Replace(result.Result.Trim(), "$1$2"), "$&\n");

                    // If the caller wants us to renormalize URLs to a different filename, do so.
                    if (targetFilename != null && result.Result.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        try
                        {
                            result.Result = CssUrlNormalizer.NormalizeUrls(
                                tree: new CssParser().Parse(result.Result, true),
                                targetFile: targetFilename,
                                oldBasePath: filename
                            );
                        }
                        catch (Exception ex)
                        {
                            Logger.Log("An error occurred while normalizing generated paths in " + filename + "\r\n" + ex);
                        }
                    }
                }
                Logger.Log(Path.GetFileName(filename) + " compiled");
                return result;
            }
        }
コード例 #8
0
        // Don't try-catch this method: We need to "address" all the bugs,
        // which may occur as the (node.js-based) service implement changes.
        private async Task <CompilerResult> ProcessResult(CompilerResult result, bool onlyPreview, string sourceFileName, string targetFileName)
        {
            if (result == null)
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compilation failed: The service failed to respond to this request\n\t\t\tPossible cause: Syntax Error!");
                return(await CompilerResultFactory.GenerateResult(sourceFileName, targetFileName));
            }
            if (!result.IsSuccess)
            {
                var firstError = result.Errors.Where(e => e != null).Select(e => e.Message).FirstOrDefault();

                if (firstError != null)
                {
                    Logger.Log(firstError);
                }

                return(result);
            }

            string resultString = PostProcessResult(result.Result, result.TargetFileName, result.SourceFileName);

            if (!onlyPreview)
            {
                // Write output file
                if (result.TargetFileName != null && (MinifyInPlace || !File.Exists(result.TargetFileName) ||
                                                      resultString != await FileHelpers.ReadAllTextRetry(result.TargetFileName)))
                {
                    ProjectHelpers.CheckOutFileFromSourceControl(result.TargetFileName);
                    await FileHelpers.WriteAllTextRetry(result.TargetFileName, resultString);

                    if (!(this is ILintCompiler))
                    {
                        ProjectHelpers.AddFileToProject(result.SourceFileName, result.TargetFileName);
                    }
                }

                // Write map file
                if (GenerateSourceMap && (!File.Exists(result.MapFileName) ||
                                          result.ResultMap != await FileHelpers.ReadAllTextRetry(result.MapFileName)))
                {
                    ProjectHelpers.CheckOutFileFromSourceControl(result.MapFileName);
                    await FileHelpers.WriteAllTextRetry(result.MapFileName, result.ResultMap);

                    if (!(this is ILintCompiler))
                    {
                        ProjectHelpers.AddFileToProject(result.TargetFileName, result.MapFileName);
                    }
                }

                await RtlVariantHandler(result);
            }

            return(CompilerResult.UpdateResult(result, resultString));
        }
コード例 #9
0
ファイル: NodeServer.cs プロジェクト: waodng/VSIX
        public static async Task <CompilerResult> CallServiceAsync(string path, bool reattempt = false)
        {
            await Up();

            if (_server != null)
            {
                return(await _server.CallService(path, reattempt));
            }
            else
            {
                return(CompilerResult.GenerateResult(path, "", "", false, "Unable to start node", "", null, false));
            }
        }
コード例 #10
0
        private void Completed(CompilerResult result)
        {
            if (result.IsSuccess)
            {
                OnCompilationDone(result.Result, result.FileName);
            }
            else
            {
                result.Error.Message = "LESS: " + result.Error.Message;

                CreateTask(result.Error);

                base.OnCompilationDone("ERROR:", Document.FilePath);
            }
        }
コード例 #11
0
        private void Completed(CompilerResult result)
        {
            if (result.IsSuccess)
            {
                OnCompilationDone(result.Result, result.FileName);
            }
            else
            {
                result.Error.Message = "LESS: " + result.Error.Message;

                CreateTask(result.Error);

                base.OnCompilationDone("ERROR:", Document.FilePath);
            }
        }
コード例 #12
0
        // Don't try-catch this method: We need to "address" all the bugs,
        // which may occur as the (node.js-based) service implement changes.
        private async Task<CompilerResult> ProcessResult(CompilerResult result, bool onlyPreview, string sourceFileName, string targetFileName)
        {
            if (result == null)
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compilation failed: The service failed to respond to this request\n\t\t\tPossible cause: Syntax Error!");
                return await CompilerResultFactory.GenerateResult(sourceFileName, targetFileName);
            }
            if (!result.IsSuccess)
            {
                var firstError = result.Errors.Where(e => e != null).Select(e => e.Message).FirstOrDefault();

                if (firstError != null)
                    Logger.Log(firstError);

                return result;
            }

            string resultString = PostProcessResult(result.Result, result.TargetFileName, result.SourceFileName);

            if (!onlyPreview)
            {
                // Write output file
                if (result.TargetFileName != null && (MinifyInPlace || !File.Exists(result.TargetFileName) ||
                    resultString != await FileHelpers.ReadAllTextRetry(result.TargetFileName)))
                {
                    ProjectHelpers.CheckOutFileFromSourceControl(result.TargetFileName);
                    await FileHelpers.WriteAllTextRetry(result.TargetFileName, resultString);

                    if (!(this is ILintCompiler))
                        ProjectHelpers.AddFileToProject(result.SourceFileName, result.TargetFileName);
                }

                // Write map file
                if (GenerateSourceMap && (!File.Exists(result.MapFileName) ||
                    result.ResultMap != await FileHelpers.ReadAllTextRetry(result.MapFileName)))
                {
                    ProjectHelpers.CheckOutFileFromSourceControl(result.MapFileName);
                    await FileHelpers.WriteAllTextRetry(result.MapFileName, result.ResultMap);

                    if (!(this is ILintCompiler))
                        ProjectHelpers.AddFileToProject(result.TargetFileName, result.MapFileName);
                }

                await RtlVariantHandler(result);
            }

            return CompilerResult.UpdateResult(result, resultString);
        }
コード例 #13
0
        public virtual async Task RunLinterAsync()
        {
            if (_isDisposed)
            {
                return;
            }

            EditorExtensionsPackage.DTE.StatusBar.Text = "Web Essentials: Running " + _compiler.ServiceName + "...";

            CompilerResult result = await _compiler.CheckAsync(FileName);

            EditorExtensionsPackage.DTE.StatusBar.Clear();

            // Hack to select result from Error:
            // See https://github.com/madskristensen/WebEssentials2013/issues/392#issuecomment-31566419
            ReadResult(result.Errors);
        }
コード例 #14
0
        private static void ProcessResult(string outputFile, Process process, CompilerResult result)
        {
            if (!File.Exists(outputFile))
                throw new FileNotFoundException("LESS compiled output not found", outputFile);

            if (process.ExitCode == 0)
            {
                result.Result = File.ReadAllText(outputFile);
                result.IsSuccess = true;
            }
            else
            {
                using (StreamReader reader = process.StandardError)
                    result.Error = ParseError(reader.ReadToEnd());
            }

            File.Delete(outputFile);
        }
コード例 #15
0
 private void ValidateResult(Process process, string outputFile, string errorText, CompilerResult result)
 {
     try
     {
         if (process.ExitCode == 0)
         {
             result.Result = File.ReadAllText(outputFile);
             result.IsSuccess = true;
         }
         else
         {
             result.Error = ParseError(errorText.Replace("\r", ""));
         }
     }
     catch (FileNotFoundException missingFileException)
     {
         Logger.Log(ServiceName + ": " + Path.GetFileName(outputFile) + " compilation failed. " + missingFileException.Message);
     }
 }
コード例 #16
0
        private CompilerResult ProcessResult(Process process, string errorText, string sourceFileName, string targetFileName)
        {
            CompilerResult result = new CompilerResult(sourceFileName);

            ValidateResult(process, targetFileName, errorText, result);

            if (result.IsSuccess)
            {
                result.Result = PostProcessResult(result.Result, sourceFileName, targetFileName);
                if (!InUnitTests)
                    ProjectHelpers.AddFileToProject(sourceFileName, targetFileName);
            }
            else
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compilation failed.");
            }

            return result;
        }
コード例 #17
0
        public static async Task<CompilerResult> Compile(string filename, string targetFilename = null)
        {
            string output = Path.GetTempFileName();

            string webEssentialsDir = Path.GetDirectoryName(typeof(LessCompiler).Assembly.Location);
            string lessc = Path.Combine(webEssentialsDir, @"Resources\nodejs\node_modules\.bin\lessc.cmd");

            ProcessStartInfo start = new ProcessStartInfo(lessc)
            {
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                Arguments = "--relative-urls \"" + filename + "\" \"" + output + "\"",
                UseShellExecute = false,
                RedirectStandardError = true
            };

            using (var process = await ExecuteAsync(start))
            {
                CompilerResult result = new CompilerResult(filename);

                ProcessResult(output, process, result);

                // If the caller wants us to renormalize URLs to a different filename, do so.
                if (targetFilename != null && result.IsSuccess && result.Result.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    try
                    {
                        result.Result = CssUrlNormalizer.NormalizeUrls(
                            tree: new CssParser().Parse(result.Result, true),
                            targetFile: targetFilename,
                            oldBasePath: filename
                        );
                    }
                    catch (Exception ex)
                    {
                        Logger.Log("An error occurred while normalizing generated paths in " + filename + "\r\n" + ex);
                    }
                }
                Logger.Log(Path.GetFileName(filename) + " compiled");
                return result;
            }
        }
コード例 #18
0
 private void ValidateResult(Process process, string outputFile, string errorText, CompilerResult result)
 {
     try
     {   // Subjected to change, depending on https://github.com/andrew/node-sass/issues/207
         if (!errorText.Contains("error: ") && process.ExitCode == 0)
         {
             if (!string.IsNullOrEmpty(outputFile))
                 result.Result = File.ReadAllText(outputFile);
             result.IsSuccess = true;
         }
         else
         {
             result.Errors = ParseErrors(errorText.Replace("\r", ""));
         }
     }
     catch (FileNotFoundException missingFileException)
     {
         Logger.Log(ServiceName + ": " + Path.GetFileName(outputFile) + " compilation failed. " + missingFileException.Message);
     }
 }
コード例 #19
0
        private void ProcessExited(object sender, EventArgs e)
        {
            using (Process process = (Process)sender)
            {
                string         fileName = process.StartInfo.EnvironmentVariables["fileName"];
                CompilerResult result   = new CompilerResult(fileName);

                try
                {
                    ProcessResult(process, result);
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                    Callback(result);
                }

                process.Exited -= ProcessExited;

                Logger.Log(Path.GetFileName(fileName) + " compiled");
            }
        }
コード例 #20
0
ファイル: CompilerResultFactory.cs プロジェクト: waodng/VSIX
        public async static Task <CompilerResult> GenerateResult(string sourceFileName, string targetFileName, string mapFileName, bool isSuccess, string result, string resultMap, IEnumerable <CompilerError> errors, bool hasResult = false, string rtlSourceFileName = "", string rtlTargetFileName = "", string rtlMapFileName = "", string rtlResult = "", string rtlResultMap = "")
        {
            CompilerResult instance;

            mapFileName = mapFileName ?? targetFileName + ".map";

            if (result == null && File.Exists(targetFileName))
            {
                result = await FileHelpers.ReadAllTextRetry(targetFileName);
            }

            if (targetFileName != null && Path.GetExtension(targetFileName).Equals(".css", StringComparison.OrdinalIgnoreCase))
            {
                instance = CssCompilerResult.GenerateResult(sourceFileName, targetFileName, mapFileName, isSuccess, result, resultMap, errors, rtlSourceFileName, rtlTargetFileName, rtlMapFileName, rtlResult, rtlResultMap, hasResult);
            }
            else
            {
                instance = CompilerResult.GenerateResult(sourceFileName, targetFileName, mapFileName, isSuccess, result, resultMap, errors, hasResult);
            }

            return(instance);
        }
コード例 #21
0
        private void ProcessExited(object sender, EventArgs e)
        {
            using (Process process = (Process)sender)
            {
                string fileName = process.StartInfo.EnvironmentVariables["fileName"];
                CompilerResult result = new CompilerResult(fileName);

                try
                {
                    ProcessResult(process, result);
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                    Callback(result);
                }

                process.Exited -= ProcessExited;

                Logger.Log(Path.GetFileName(fileName) + " compiled");
            }
        }
コード例 #22
0
        private static void Completed(CompilerResult result)
        {
            if (result.IsSuccess)
            {
                string cssFileName = MarginBase.GetCompiledFileName(result.FileName, ".css", WESettings.GetBoolean(WESettings.Keys.LessCompileToFolder));// result.FileName.Replace(".less", ".css");

                if (File.Exists(cssFileName))
                {
                    string old = File.ReadAllText(cssFileName);

                    if (old != result.Result)
                    {
                        ProjectHelpers.CheckOutFileFromSourceControl(cssFileName);
                        try
                        {
                            bool useBom = WESettings.GetBoolean(WESettings.Keys.UseBom);
                            using (StreamWriter writer = new StreamWriter(cssFileName, false, new UTF8Encoding(useBom)))
                            {
                                writer.Write(result.Result);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(ex);
                        }
                    }
                }

                MinifyFile(result.FileName, result.Result);
            }
            else if (result.Error != null && !string.IsNullOrEmpty(result.Error.Message))
            {
                Logger.Log(result.Error.Message);
            }
            else
            {
                Logger.Log("Error compiling LESS file: " + result.FileName);
            }
        }
コード例 #23
0
        private static void Completed(CompilerResult result)
        {
            if (result.IsSuccess)
            {
                string cssFileName = MarginBase.GetCompiledFileName(result.FileName, ".css", WESettings.GetBoolean(WESettings.Keys.LessCompileToFolder));// result.FileName.Replace(".less", ".css");

                if (File.Exists(cssFileName))
                {
                    string old = File.ReadAllText(cssFileName);

                    if (old != result.Result)
                    {
                        ProjectHelpers.CheckOutFileFromSourceControl(cssFileName);
                        try
                        {
                            bool useBom = WESettings.GetBoolean(WESettings.Keys.UseBom);
                            using (StreamWriter writer = new StreamWriter(cssFileName, false, new UTF8Encoding(useBom)))
                            {
                                writer.Write(result.Result);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(ex);
                        }
                    }
                }

                MinifyFile(result.FileName, result.Result);
            }
            else if (result.Error != null && !string.IsNullOrEmpty(result.Error.Message))
            {
                Logger.Log(result.Error.Message);
            }
            else
            {
                Logger.Log("Error compiling LESS file: " + result.FileName);
            }
        }
コード例 #24
0
        private async Task HandleRtlCss(CompilerResult result)
        {
            string value = result.RtlResult;

            // Write output file
            if (result.RtlTargetFileName != null && (MinifyInPlace || !File.Exists(result.RtlTargetFileName) ||
                value != await FileHelpers.ReadAllTextRetry(result.RtlTargetFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlTargetFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlTargetFileName, value);
                ProjectHelpers.AddFileToProject(result.RtlSourceFileName, result.RtlTargetFileName);
            }

            // Write map file
            if (GenerateSourceMap && (!File.Exists(result.RtlMapFileName) ||
                result.RtlResultMap != await FileHelpers.ReadAllTextRetry(result.RtlMapFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlMapFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlMapFileName, result.RtlResultMap);
                ProjectHelpers.AddFileToProject(result.RtlTargetFileName, result.RtlMapFileName);
            }
        }
コード例 #25
0
        public static async Task<CompilerResult> Compile(string filename, string targetFilename = null)
        {
            string output = Path.GetTempFileName();

            ProcessStartInfo start = new ProcessStartInfo(@"cscript")
            {
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                Arguments = "//nologo //s \"" + GetScriptPath() + "\" \"" + filename + "\" \"" + output + "\"",
                UseShellExecute = false,
                RedirectStandardError = true
            };

            using (var process = await ExecuteAsync(start))
            {
                CompilerResult result = new CompilerResult(filename);

                ProcessResult(output, process, result);

                if (result.IsSuccess && result.Result.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    try
                    {
                        result.Result = CssUrlNormalizer.NormalizeUrls(
                            tree: new CssParser().Parse(result.Result, true),
                            targetFile: targetFilename ?? filename,
                            oldBasePath: filename
                        );
                    }
                    catch (Exception ex)
                    {
                        Logger.Log("An error occurred while normalizing generated paths in " + filename + "\r\n" + ex);
                    }
                }
                Logger.Log(Path.GetFileName(filename) + " compiled");
                return result;
            }
        }
コード例 #26
0
        private static void WriteResult(CompilerResult result, string cssFileName)
        {
            MinifyFile(result.FileName, result.Result);
            if (!File.Exists(cssFileName))
                return;

            string old = File.ReadAllText(cssFileName);
            if (old == result.Result)
                return;

            ProjectHelpers.CheckOutFileFromSourceControl(cssFileName);
            try
            {
                using (StreamWriter writer = new StreamWriter(cssFileName, false, new UTF8Encoding(true)))
                {
                    writer.Write(result.Result);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }
コード例 #27
0
        private void ProcessResult(Process process, CompilerResult result)
        {
            string output = process.StartInfo.EnvironmentVariables["output"];

            if (File.Exists(output))
            {
                if (process.ExitCode == 0)
                {
                    result.IsSuccess = true;
                    result.Result = File.ReadAllText(output);
                }
                else
                {
                    using (StreamReader reader = process.StandardError)
                    {
                        result.Error = ParseError(reader.ReadToEnd());
                    }
                }

                File.Delete(output);
            }

            Callback(result);
        }
コード例 #28
0
        private CompilerResult ProcessResult(Process process, string errorText, string sourceFileName, string targetFileName)
        {
            CompilerResult result = new CompilerResult(sourceFileName, targetFileName);

            ValidateResult(process, targetFileName, errorText, result);

            if (result.IsSuccess)
            {
                var renewedResult = PostProcessResult(result.Result, sourceFileName, targetFileName);

                if (!ReferenceEquals(result.Result, renewedResult))
                {
                    File.WriteAllText(targetFileName, renewedResult, Encoding.UTF8);
                    result.Result = renewedResult;
                }
            }
            else
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName)
                           + " compilation failed: " + result.Errors.Select(e => e.Message).FirstOrDefault());
            }

            return(result);
        }
コード例 #29
0
ファイル: CssCompilerBase.cs プロジェクト: waodng/VSIX
        private async Task HandleRtlCss(CompilerResult result)
        {
            string value = result.RtlResult;

            // Write output file
            if (result.RtlTargetFileName != null && (MinifyInPlace || !File.Exists(result.RtlTargetFileName) ||
                                                     value != await FileHelpers.ReadAllTextRetry(result.RtlTargetFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlTargetFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlTargetFileName, value);

                ProjectHelpers.AddFileToProject(result.RtlSourceFileName, result.RtlTargetFileName);
            }

            // Write map file
            if (GenerateSourceMap && (!File.Exists(result.RtlMapFileName) ||
                                      result.RtlResultMap != await FileHelpers.ReadAllTextRetry(result.RtlMapFileName)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(result.RtlMapFileName);
                await FileHelpers.WriteAllTextRetry(result.RtlMapFileName, result.RtlResultMap);

                ProjectHelpers.AddFileToProject(result.RtlTargetFileName, result.RtlMapFileName);
            }
        }
コード例 #30
0
        private void ProcessResult(Process process, CompilerResult result)
        {
            string output = process.StartInfo.EnvironmentVariables["output"];

            if (File.Exists(output))
            {
                if (process.ExitCode == 0)
                {
                    result.IsSuccess = true;
                    result.Result    = File.ReadAllText(output);
                }
                else
                {
                    using (StreamReader reader = process.StandardError)
                    {
                        result.Error = ParseError(reader.ReadToEnd());
                    }
                }

                File.Delete(output);
            }

            Callback(result);
        }
コード例 #31
0
        private CompilerResult ProcessResult(Process process, string errorText, string sourceFileName, string targetFileName)
        {
            CompilerResult result = new CompilerResult(sourceFileName, targetFileName);

            ValidateResult(process, targetFileName, errorText, result);

            if (result.IsSuccess)
            {
                var renewedResult = PostProcessResult(result.Result, sourceFileName, targetFileName);

                if (!ReferenceEquals(result.Result, renewedResult))
                {
                    File.WriteAllText(targetFileName, renewedResult, Encoding.UTF8);
                    result.Result = renewedResult;
                }
            }
            else
            {
                Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName)
                         + " compilation failed: " + result.Errors.Select(e => e.Message).FirstOrDefault());
            }

            return result;
        }
コード例 #32
0
 internal static CompilerResult UpdateResult(CompilerResult result, string resultString)
 {
     result.Result = resultString;
     return(result);
 }
コード例 #33
0
 protected async override Task PostWritingResult(CompilerResult result)
 {
     if (WESettings.Instance.Css.RtlCss && result.RtlTargetFileName != null)
         await HandleRtlCss(result);
 }
コード例 #34
0
ファイル: NodeExecutorBase.cs プロジェクト: waodng/VSIX
 protected virtual Task PostWritingResult(CompilerResult result)
 {
     return(Task.Factory.StartNew(() => { }));
 }
コード例 #35
0
 protected abstract void UpdateMargin(CompilerResult result);
コード例 #36
0
 private void ValidateResult(Process process, string outputFile, string errorText, CompilerResult result)
 {
     try
     {
         if (process.ExitCode == 0 &&
             /* Temporary hack see; //github.com/mdevils/node-jscs/issues/212 */
             (!errorText.StartsWith("<?xml version=", StringComparison.CurrentCulture) ||
              errorText == "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"4.3\">\n</checkstyle>"))
         {
             if (!string.IsNullOrEmpty(outputFile))
             {
                 result.Result = File.ReadAllText(outputFile);
             }
             result.IsSuccess = true;
         }
         else
         {
             result.Errors = ParseErrors(errorText);
         }
     }
     catch (FileNotFoundException missingFileException)
     {
         Logger.Log(ServiceName + ": " + Path.GetFileName(outputFile) + " compilation failed. " + missingFileException.Message);
     }
 }
コード例 #37
0
 protected virtual string PostProcessResult(CompilerResult result)
 {
     Logger.Log(ServiceName + ": " + Path.GetFileName(result.SourceFileName) + " compiled.");
     return result.Result;
 }
コード例 #38
0
 protected abstract void UpdateMargin(CompilerResult result);
コード例 #39
0
 protected virtual Task PostWritingResult(CompilerResult result)
 {
     return Task.Factory.StartNew(() => { });
 }
コード例 #40
0
 protected virtual Task RtlVariantHandler(CompilerResult result)
 {
     return Task.Factory.StartNew(() => { });
 }
コード例 #41
0
 private void ValidateResult(Process process, string outputFile, string errorText, CompilerResult result)
 {
     try
     {
         if (process.ExitCode == 0 &&
             /* Temporary hack see; //github.com/mdevils/node-jscs/issues/212 */
             (!errorText.StartsWith("<?xml version=", StringComparison.CurrentCulture) ||
              errorText == "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"4.3\">\n</checkstyle>"))
         {
             if (!string.IsNullOrEmpty(outputFile))
                 result.Result = File.ReadAllText(outputFile);
             result.IsSuccess = true;
         }
         else
         {
             result.Errors = ParseErrors(errorText);
         }
     }
     catch (FileNotFoundException missingFileException)
     {
         Logger.Log(ServiceName + ": " + Path.GetFileName(outputFile) + " compilation failed. " + missingFileException.Message);
     }
 }
コード例 #42
0
 protected virtual Task RtlVariantHandler(CompilerResult result)
 {
     return(Task.Factory.StartNew(() => { }));
 }
コード例 #43
0
 internal static CompilerResult UpdateResult(CompilerResult result, string resultString)
 {
     result.Result = resultString;
     return result;
 }