Exemple #1
0
        /// <summary>
        /// Generates the configuration.
        /// </summary>
        /// <param name="args">The args to generate configuration from.</param>
        /// <returns>The populated <see cref="ConsoleConfiguration"/>.</returns>
        /// <exception cref="System.ArgumentNullException">args is null.</exception>
        private static ConsoleConfiguration GenerateConfiguration(IEnumerable <string> args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            ConsoleConfiguration consoleConfiguration = new ConsoleConfiguration();

            // Process the arguments into variables
            foreach (string arg in args)
            {
                // Check that the argument starts with char '-'
                if (arg[0] == '-')
                {
                    // Split the argument into a key/value pair using a colon as the delimiter.
                    int    split = arg.IndexOf(':');
                    string key   = arg.Substring(1, (split > 0 ? split : arg.Length) - 1);
                    string value = split > -1 ? arg.Substring(split + 1) : string.Empty;

                    switch (key.ToUpperInvariant())
                    {
                    case "T":
                        consoleConfiguration.TargetType =
                            value == "css" ? CrunchTargetType.CSS : CrunchTargetType.JavaScript;
                        break;

                    case "M":
                        consoleConfiguration.Minify = true;
                        break;

                    case "IN":
                        consoleConfiguration.InputPath = value;
                        break;

                    case "OUT":
                        consoleConfiguration.OutputPath = value;
                        break;
                    }
                }
            }

            return(consoleConfiguration);
        }
Exemple #2
0
        /// <summary>
        /// The crunch css.
        /// </summary>
        /// <param name="configuration">
        /// The configuration.
        /// </param>
        private static void Crunch(ConsoleConfiguration configuration)
        {
            // Get the currently operating directory.
            // http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in
            string directoryName = Path.GetDirectoryName(configuration.InputPath);
            string rootFolder    = Path.IsPathRooted(directoryName) ? directoryName : AppDomain.CurrentDomain.BaseDirectory;
            string fileName      = configuration.InputPath;

            if (!string.IsNullOrWhiteSpace(fileName) && !string.IsNullOrWhiteSpace(rootFolder))
            {
                string actualInputPath = Path.GetFullPath(Path.Combine(rootFolder, fileName));
                string outputPath      = GetOutPutPath(
                    rootFolder,
                    actualInputPath,
                    configuration.OutputPath,
                    configuration.TargetType,
                    configuration.Minify);

                CruncherOptions options = new CruncherOptions
                {
                    Minify           = configuration.Minify,
                    AllowRemoteFiles = true,
                    RootFolder       = Path.GetDirectoryName(actualInputPath)
                };

                string crunched;

                if (configuration.TargetType == CrunchTargetType.CSS)
                {
                    CssCruncher cssCruncher = new CssCruncher(options);
                    crunched = cssCruncher.Crunch(actualInputPath);
                    crunched = cssCruncher.Minify(crunched);
                }
                else
                {
                    JavaScriptCruncher javaScriptCruncher = new JavaScriptCruncher(options);
                    crunched = javaScriptCruncher.Crunch(actualInputPath);
                    crunched = javaScriptCruncher.Minify(crunched);
                }

                FileHelper.WriteFile(outputPath, crunched);
            }
        }
Exemple #3
0
 /// <summary>
 /// Mains the specified args.
 /// </summary>
 /// <param name="args">The args.</param>
 internal static void Main(string[] args)
 {
     try
     {
         if (args == null || !args.Any())
         {
             Console.WriteLine(ResourceStrings.Usage);
             Console.ReadLine();
         }
         else
         {
             ConsoleConfiguration consoleConfiguration = GenerateConfiguration(args);
             Crunch(consoleConfiguration);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         Console.ReadLine();
     }
 }