public void Configure(JsModelsConfiguration configuration)
        {
            // save path
            Path = configuration.Path;

            // compute js
            var generator = new JsModelGenerator(configuration.Models);
            _js = generator.GenerateModels(configuration.Models);

            // minify
            _js = (new Minifier()).MinifyJavaScript(_js);

            // get version hash
            var encoding = new UTF8Encoding();
            var bytes = encoding.GetBytes(_js);
            VersionHash = Convert.ToBase64String(SHA512.Create().ComputeHash(bytes));

            // compress and read out to byte array
            using (var ms = new MemoryStream())
            {
                using (var stream = new GZipStream(ms, CompressionLevel.Optimal, false))
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
                _jsCompressed = ms.ToArray();
            }
        }
Exemple #2
0
        public static int Main(string[] args)
        {
            try
            {
                var options = new Options();
                if (!CommandLine.Parser.Default.ParseArguments(args, options)) return 1;

                // check options
                if (string.IsNullOrWhiteSpace(options.InputAssembly))
                {
                    Console.WriteLine("Input Assembly required.");
                    Console.WriteLine("");
                    Console.WriteLine(options.GetUsage());
                    return 1;
                }

                if (string.IsNullOrWhiteSpace(options.Classes))
                {
                    Console.WriteLine("Classes Option must be provided");
                    Console.WriteLine("");
                    Console.WriteLine(options.GetUsage());
                    return 1;
                }

                if (string.IsNullOrWhiteSpace(options.OutputDirectory))
                {
                    options.OutputDirectory = Environment.CurrentDirectory;
                }

                if (!Directory.Exists(options.OutputDirectory))
                {
                    Directory.CreateDirectory(options.OutputDirectory);
                }

                // load assembly
                var assembly = Assembly.LoadFile(options.InputAssembly);

                // find types
                var names = options.Classes.Split(',').Select(n => n.Trim()).ToList();
                var models = new List<Type>();
                foreach (var name in names)
                {
                    var model = assembly.GetType(name);
                    if (model == null)
                    {
                        Console.WriteLine("Could not find model by name of: {0}", name);
                        
                        var lower = name.ToLower();
                        var results = assembly.GetTypes().Where(t => t.Name.ToLower() == lower).ToList();
                        if (!results.Any())
                        {
                            Console.WriteLine("Could not find model by any other name. Exiting.");
                            return 1;
                        }

                        if (results.Count() > 1)
                        {
                            Console.WriteLine("Multiple models found by that name. Exiting.");
                            return 1;
                        }
                        model = results.First();

                        Console.WriteLine("Found model by name of: {0}", model.FullName);
                    }

                    models.Add(model);
                }

                if (!models.Any())
                {
                    Console.WriteLine("No models found. Exiting.");
                    return 1;
                }

                // use types for references
                var generator = new JsModelGenerator(models);

                foreach (var model in models)
                {
                    Console.WriteLine("Generating model for: {0}", model.FullName);

                    var filename = string.Format("{0}\\{1}.js", options.OutputDirectory, model.Name);
                    using (var writer = new StreamWriter(filename, false))
                    {
                        writer.WriteLine("// This file autogenerated by JsModels");
                        generator.GenerateModel(model, writer);
                        writer.Flush();
                    }
                }

                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return 1;
            }
        }