Esempio n. 1
0
        private static bool MinifyFile(string file, string minFile, CodeSettings settings)
        {
            Minifier minifier = new Minifier();

            // If the source file is not itself mapped, add the filename for mapping
            // TODO: Make sure this works for compiled output too. (check for .map?)
            if (!(File.ReadLines(file)
                  .SkipWhile(string.IsNullOrWhiteSpace)
                  .FirstOrDefault() ?? "")
                .Trim()
                .StartsWith("///#source", StringComparison.CurrentCulture))
            {
                minifier.FileName = Path.GetFileName(file);
            }

            string content = minifier.MinifyJavaScript(File.ReadAllText(file), settings);

            content += "\r\n/*\r\n//# sourceMappingURL=" + Path.GetFileName(minFile) + ".map\r\n*/";

            if (File.Exists(minFile) && content == File.ReadAllText(minFile))
            {
                return(false);
            }

            ProjectHelpers.CheckOutFileFromSourceControl(minFile);
            File.WriteAllText(minFile, content, Encoding.UTF8);
            ProjectHelpers.AddFileToProject(file, minFile);

            return(true);
        }
        ///<summary>Compiles the specified source file, notifying all <see cref="ICompilationConsumer"/>s.</summary>
        ///<param name="sourcePath">The path to the source file.</param>
        ///<param name="targetPath">The path to save the compiled output, or null to compile in-memory.</param>
        public async Task <CompilerResult> CompileAsync(string sourcePath, string targetPath)
        {
            var result = await RunCompilerAsync(sourcePath, targetPath);

            if (result.IsSuccess && !string.IsNullOrEmpty(targetPath))
            {
                ProjectHelpers.AddFileToProject(sourcePath, targetPath);

                var mapFile = targetPath + ".map";

                if (GenerateSourceMap && File.Exists(mapFile))
                {
                    ProjectHelpers.AddFileToProject(targetPath, mapFile);
                }

                if (!File.Exists(result.TargetFileName))
                {
                    return(result);
                }

                foreach (var listener in _listeners)
                {
                    await listener.FileSaved(TargetContentType, result.TargetFileName, true, Settings != null?Settings.MinifyInPlace : false);

                    if (File.Exists(result.RtlTargetFileName))
                    {
                        await listener.FileSaved(TargetContentType, result.RtlTargetFileName, true, Settings != null?Settings.MinifyInPlace : false);
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        private async static Task <bool> MinifyFile(string file, string minFile, CodeSettings settings)
        {
            Minifier minifier = new Minifier();

            // If the source file is not itself mapped, add the filename for mapping
            // TODO: Make sure this works for compiled output too. (check for .map?)
            if (!((await FileHelpers.ReadAllLinesRetry(file))
                  .SkipWhile(string.IsNullOrWhiteSpace)
                  .FirstOrDefault() ?? "")
                .Trim()
                .StartsWith("///#source", StringComparison.CurrentCulture))
            {
                minifier.FileName = file;
            }

            string content = minifier.MinifyJavaScript(await FileHelpers.ReadAllTextRetry(file), settings);

            if (File.Exists(minFile) && content == await FileHelpers.ReadAllTextRetry(minFile))
            {
                return(false);
            }

            ProjectHelpers.CheckOutFileFromSourceControl(minFile);
            await FileHelpers.WriteAllTextRetry(minFile, content);

            ProjectHelpers.AddFileToProject(file, minFile);

            return(true);
        }
Esempio n. 4
0
        private async static Task <bool> MinifyFileWithSourceMap(string file, string minFile)
        {
            bool         result;
            string       mapPath = minFile + ".map";
            StringWriter writer  = new StringWriter();

            ProjectHelpers.CheckOutFileFromSourceControl(mapPath);

            using (V3SourceMap sourceMap = new V3SourceMap(writer))
            {
                var settings = CreateSettings();

                settings.SymbolsMap = sourceMap;
                sourceMap.StartPackage(minFile, mapPath);

                // This fails when debugger is attached. Bug raised with Ron Logan
                result = await MinifyFile(file, minFile, settings);
            }

            await FileHelpers.WriteAllTextRetry(mapPath, writer.ToString(), false);

            ProjectHelpers.AddFileToProject(minFile, mapPath);

            return(result);
        }
Esempio n. 5
0
        private async Task GenerateAsync(SpriteDocument sprite, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            ProjectHelpers.AddFileToProject(Path.GetDirectoryName(sprite.FileName), sprite.FileName);

            string imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);

            if (string.IsNullOrEmpty(sprite.OutputDirectory))
            {
                imageFile = ProjectHelpers.GetAbsolutePathFromSettings(sprite.OutputDirectory, Path.ChangeExtension(sprite.FileName, sprite.FileExtension));
            }

            ProjectHelpers.CreateDirectoryInProject(imageFile);

            IEnumerable <SpriteFragment> fragments = await SpriteGenerator.MakeImage(sprite, imageFile, UpdateSpriteAsync);

            ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);

            if (!hasUpdated)
            {
                WebEssentialsPackage.DTE.ItemOperations.OpenFile(sprite.FileName);
            }

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
Esempio n. 6
0
        private async Task GenerateAsync(SpriteDocument sprite, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToActiveProject(sprite.FileName);
            }

            //Default file name is the sprite name with specified file extension.
            string imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);

            IEnumerable <SpriteFragment> fragments = await SpriteGenerator.MakeImage(sprite, imageFile, UpdateSpriteAsync);

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);
                EditorExtensionsPackage.DTE.ItemOperations.OpenFile(sprite.FileName);
            }

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
Esempio n. 7
0
 private static void Export(IEnumerable <SpriteFragment> fragments, string imageFile)
 {
     foreach (ExportFormat format in (ExportFormat[])Enum.GetValues(typeof(ExportFormat)))
     {
         string exportFile = SpriteExporter.Export(fragments, imageFile, format);
         ProjectHelpers.AddFileToProject(imageFile, exportFile);
     }
 }
Esempio n. 8
0
        private async static Task Export(IEnumerable <SpriteFragment> fragments, string imageFile, SpriteDocument sprite)
        {
            foreach (ExportFormat format in (ExportFormat[])Enum.GetValues(typeof(ExportFormat)))
            {
                string exportFile = await SpriteExporter.Export(fragments, sprite, imageFile, format);

                ProjectHelpers.AddFileToProject(imageFile, exportFile);
            }
        }
Esempio n. 9
0
        public async static Task CreateMinFile(IContentType contentType, string sourcePath)
        {
            var settings = WESettings.Instance.ForContentType <IMinifierSettings>(contentType);
            var minPath  = GetMinFileName(sourcePath);

            await MinifyFile(contentType, sourcePath, minPath, settings);

            if (settings.GzipMinifiedFiles)
            {
                ProjectHelpers.AddFileToProject(minPath, minPath + ".gzip");
            }
        }
Esempio n. 10
0
        public virtual bool MinifyFile(string sourcePath, string targetPath)
        {
            var result = MinifyString(File.ReadAllText(sourcePath));

            if (result != null && (!File.Exists(targetPath) || result != File.ReadAllText(targetPath)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(targetPath);
                File.WriteAllText(targetPath, result, Encoding.UTF8);
                ProjectHelpers.AddFileToProject(sourcePath, targetPath);

                return(true);
            }

            return(false);
        }
Esempio n. 11
0
        public async virtual Task <bool> MinifyFile(string sourcePath, string targetPath)
        {
            var result = MinifyString(await FileHelpers.ReadAllTextRetry(sourcePath));

            if (result != null && (!File.Exists(targetPath) || result != await FileHelpers.ReadAllTextRetry(targetPath)))
            {
                ProjectHelpers.CheckOutFileFromSourceControl(targetPath);
                await FileHelpers.WriteAllTextRetry(targetPath, result);

                ProjectHelpers.AddFileToProject(sourcePath, targetPath);

                return(true);
            }

            return(false);
        }
Esempio n. 12
0
        private async Task GenerateAsync(SpriteDocument sprite)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            string imageFile;
            var    fragments = SpriteGenerator.CreateImage(sprite, out imageFile);

            ProjectHelpers.AddFileToActiveProject(sprite.FileName);
            ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
Esempio n. 13
0
        private static bool MinifyFileWithSourceMap(string file, string minFile)
        {
            string mapPath = minFile + ".map";

            ProjectHelpers.CheckOutFileFromSourceControl(mapPath);

            using (TextWriter writer = new StreamWriter(mapPath, false, new UTF8Encoding(false)))
                using (V3SourceMap sourceMap = new V3SourceMap(writer))
                {
                    var settings = CreateSettings();
                    settings.SymbolsMap = sourceMap;
                    sourceMap.StartPackage(Path.GetFileName(minFile), Path.GetFileName(mapPath));

                    // This fails when debugger is attached. Bug raised with Ron Logan
                    bool result = MinifyFile(file, minFile, settings);
                    ProjectHelpers.AddFileToProject(minFile, mapPath);

                    return(result);
                }
        }