示例#1
0
        public async Task MinifyFileAsync(MinifiedFile minifiedFile)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var root = Path.GetDirectoryName(Solution.GetFullName());

            var destinationFile = minifiedFile.GetFullOutputPath(root);

            if (!File.Exists(destinationFile))
            {
                InfoBar.NewMessage()
                .WithErrorImage()
                .WithText("The path ")
                .WithText(minifiedFile.OutputFile, underline: true)
                .WithText(" is not valid.")
                .Publish();

                return;
            }

            MinifyResult result = null;

            switch (minifiedFile.MinifyType)
            {
            case MinifyType.CSHtml:
                result = await CSHtmlMinifier.MinifyFileAsync(minifiedFile.GetFullSourcePath(root), destinationFile, ((CsHtmlMinifiedFile)minifiedFile).UsePreMailer);

                break;

            case MinifyType.Js:
                if (!await JsMinifier.TryMinifyFileAsync("esbuild.exe", minifiedFile.GetFullSourcePath(root), destinationFile, ((JsMinifiedFile)minifiedFile).Options))
                {
                    InfoBar.NewMessage()
                    .WithErrorImage()
                    .WithText("The file ")
                    .WithText(minifiedFile.SourceFile, underline: true)
                    .WithText(" contains invalid js.")
                    .Publish();
                }
                break;

            default:
                throw new InvalidOperationException("Please create an issue on GitHub, if this throws.");
            }

            if (result is object && !result.Success)
            {
                InfoBar.NewMessage()
                .WithErrorImage()
                .WithText("The file ")
                .WithText(minifiedFile.SourceFile, underline: true)
                .WithText(" produced warnings. ")
                .WithText(result.Message)
                .Publish();
            }
        }
示例#2
0
        public bool TryUpdateConfigFile(string fullName, bool showMessageOnError = true)
        {
            var root = Path.GetDirectoryName(Solution.GetFullName());

            UserSettings settings = null;

            try
            {
                var content = File.ReadAllText(fullName);

                // Updates older config files.
                content = content.Replace("\"Files\"", "\"CSHtmlFiles\"");

                settings = JsonConvert.DeserializeObject <UserSettings>(content);

                settings.CSHtmlFiles = settings.CSHtmlFiles ?? new List <CsHtmlMinifiedFile>();
                settings.JsFiles     = settings.JsFiles ?? new List <JsMinifiedFile>();

                foreach (var file in settings.CSHtmlFiles.Cast <MinifiedFile>().Union(settings.JsFiles))
                {
                    var isSourceValid = File.Exists(file.GetFullSourcePath(root));
                    var isOutputValid = File.Exists(file.GetFullOutputPath(root));

                    if (!isSourceValid || !isOutputValid)
                    {
                        InfoBar.NewMessage()
                        .WithErrorImage()
                        .WithText("The path ")
                        .WithText(isSourceValid ? file.OutputFile : file.SourceFile, underline: true)
                        .WithText(" is not valid.")
                        .Publish();

                        return(false);
                    }
                }
            }
            catch
            {
                if (showMessageOnError)
                {
                    InfoBar.NewMessage()
                    .WithErrorImage()
                    .WithText("The content of your ")
                    .WithText(RazorMinifier.ConfigName, true)
                    .WithText(" contains invalid json.")
                    .Publish();
                }

                return(false);
            }

            Config.UserSettings = settings;

            return(true);
        }
示例#3
0
        public void CreateConfig()
        {
            var(project, _) = Solution.GetStartupProjects().FirstOrDefault();

            if (project is null)
            {
                project = Solution.GetAllProjects().FirstOrDefault();

                if (project is null)
                {
                    InfoBar.NewMessage()
                    .WithErrorImage()
                    .WithText("There is no project present, in which the config file should be generated in.")
                    .WithButton("Ok")
                    .Publish();

                    return;
                }
            }

            _ignoreAddedFile = true;

            var success = project.CreateDocument(RazorMinifier.ConfigName);

            _ignoreAddedFile = false;

            if (success != VSADDRESULT.ADDRESULT_Success)
            {
                return;
            }

            var document = GetConfigFile(project);

            var fullName = document.GetFullName();

            SetConfigFile(fullName);

            TryLoadLocalConfigFile(project);
        }