Example #1
0
        public void Test_Simple_PNG_Resize()
        {
            var basePath = GetBasePath();

            var r = new Resizetizer.Engine();

            r.Run(basePath, new Config {
                Assets = new List <ImageAsset> {
                    new ImageAsset {
                        File = "./input/happy.png",
                        Size = "100x100"
                    },
                },
                OutputBasePath       = "./output/",
                AutoAddPlatformSizes = false,
                Outputs = new List <OutputConfig> {
                    new OutputConfig {
                        Ratio = 1.0
                    },
                    new OutputConfig {
                        Ratio      = 2.0,
                        FileSuffix = "x2"
                    },
                    new OutputConfig {
                        Ratio      = 3.0,
                        FileSuffix = "x3"
                    },
                }
            });

            Assert.IsTrue(File.Exists(Path.Combine(basePath, "output", "happy.png")));
            Assert.IsTrue(File.Exists(Path.Combine(basePath, "output", "happyx2.png")));
            Assert.IsTrue(File.Exists(Path.Combine(basePath, "output", "happyx3.png")));
        }
Example #2
0
        public void Test_ExternalCommand()
        {
            var basePath = GetBasePath();

            var r = new Resizetizer.Engine();

            var config = new Config
            {
                Assets = new List <ImageAsset> {
                    new ImageAsset {
                        File = "./input/happy.svg",
                        Size = "100x100"
                    },
                },
                OutputBasePath       = "./output/",
                AutoAddPlatformSizes = false,
                Outputs = new List <OutputConfig> {
                    new OutputConfig {
                        Ratio = 1.0,
                    },
                    new OutputConfig {
                        Ratio      = 2.0,
                        FileSuffix = "x2",
                    },
                    new OutputConfig {
                        Ratio      = 3.0,
                        FileSuffix = "x3",
                    },
                }
            };

            r.Run(basePath, config);

            var happyFile   = new FileInfo(Path.Combine(basePath, "output", "happy.png"));
            var happyx2File = new FileInfo(Path.Combine(basePath, "output", "happyx2.png"));
            var happyx3File = new FileInfo(Path.Combine(basePath, "output", "happyx3.png"));

            Assert.IsTrue(happyFile.Exists);
            Assert.IsTrue(happyx2File.Exists);
            Assert.IsTrue(happyx3File.Exists);

            var happySize   = happyFile.Length;
            var happyx2Size = happyx2File.Length;
            var happyx3Size = happyx3File.Length;

            config.ExternalCommands.Add("/usr/local/bin/optipng -o7 \"{outputFile}\"");
            r.Run(basePath, config);

            Assert.IsTrue(happyFile.Exists);
            Assert.IsTrue(happyx2File.Exists);
            Assert.IsTrue(happyx3File.Exists);

            var happySize2   = happyFile.Length;
            var happyx2Size2 = happyx2File.Length;
            var happyx3Size2 = happyx3File.Length;

            Assert.LessOrEqual(happySize2, happySize);
            Assert.LessOrEqual(happyx2Size2, happyx2Size);
            Assert.LessOrEqual(happyx3Size2, happyx3Size);
        }
Example #3
0
        public override bool Execute()
        {
            Log.LogMessage("Starting Resizetizing...");

            if (ConfigFiles == null || !ConfigFiles.Any())
            {
                Log.LogMessage("No ResizetizerConfig Build Action items specified, skipping task.");
                return(true);
            }

            Task.Run(() =>
            {
                try
                {
                    var resizer = new Resizetizer.Engine();
                    foreach (var cf in ConfigFiles)
                    {
                        var file           = CleanPath(cf.ItemSpec);
                        var configFileInfo = new FileInfo(file);

                        if (!configFileInfo.Exists)
                        {
                            LogMessage("File does not exist, skipping: {0}", file);
                            continue;
                        }


                        LogMessage("Resizetizing: {0}", file);

                        var configBasePath = resizer.GetConfigBasePath(file);
                        var configs        = resizer.Load(file);

                        var intermediatePath = IntermediateOutputPath ?? configBasePath;

                        var stampsFile = Path.Combine(intermediatePath, "resizetizer.stamps");

                        var stamps = LoadStamps(stampsFile);

                        foreach (var config in configs)
                        {
                            var absoluteOutputBasePath = config.GetAbsoluteOutputBasePath(configBasePath);

                            foreach (var asset in config.Assets)
                            {
                                var assetFileInfo = new FileInfo(asset.GetAbsoluteFile(absoluteOutputBasePath));

                                foreach (var output in config.Outputs.ToArray())
                                {
                                    var outputFileInfo = new FileInfo(output.GetAbsoluteFilename(absoluteOutputBasePath, asset.File));

                                    if (stamps.ContainsKey(outputFileInfo.FullName) &&
                                        assetFileInfo.LastWriteTimeUtc <= stamps[outputFileInfo.FullName] &&
                                        configFileInfo.LastWriteTimeUtc <= stamps[outputFileInfo.FullName])
                                    {
                                        LogMessage("Source unchanged, skipped resizing: {0}", outputFileInfo.FullName);
                                        config.Outputs.Remove(output);
                                    }
                                    else
                                    {
                                        LogMessage("Resizing: {0}", outputFileInfo.FullName);
                                    }
                                }
                            }

                            // Run the resizing operation on any remaining assets
                            resizer.Run(configBasePath, config);

                            // For any asset that is left and was processed
                            foreach (var asset in config.Assets)
                            {
                                var assetFileInfo = new FileInfo(asset.GetAbsoluteFile(absoluteOutputBasePath));

                                var newestModifyTime = assetFileInfo.LastWriteTimeUtc;
                                if (configFileInfo.LastWriteTimeUtc > assetFileInfo.LastWriteTimeUtc)
                                {
                                    newestModifyTime = configFileInfo.LastWriteTimeUtc;
                                }

                                foreach (var output in config.Outputs.ToArray())
                                {
                                    var outputFileInfo = new FileInfo(output.GetAbsoluteFilename(absoluteOutputBasePath, asset.File));

                                    if (stamps.ContainsKey(outputFileInfo.FullName))
                                    {
                                        stamps[outputFileInfo.FullName] = newestModifyTime;
                                    }
                                    else
                                    {
                                        stamps.Add(outputFileInfo.FullName, newestModifyTime);
                                    }
                                }
                            }
                        }

                        SaveStamps(stampsFile, stamps);
                    }
                }
                catch (Exception ex)
                {
                    LogErrorFromException(ex);
                }
                finally
                {
                    Complete();
                }
            });

            var result = base.Execute();

            Log.LogMessage("Finished Resizetizing.");

            return(result && !Log.HasLoggedErrors);
        }