예제 #1
0
        static void Main(string[] args)
        {
            string str = @"Hello World!";

            IStrategyInterface strategy = WriterFactory.CreateWriter("CONSOLE");

            strategy.Write(str);
        }
예제 #2
0
        public async Task TestOutput()
        {
            using (RedirectConsole console = new RedirectConsole())
            {
                IStrategyInterface strategy = WriterFactory.CreateWriter("CONSOLE");
                strategy.Write(@"Hello World!");

                Assert.IsTrue(console.ToString().Contains("Hello World!"));
            }
        }
        private void WriteOverrideConfigs(List <Bundle> bundles)
        {
            var groupings = bundles.GroupBy(r => r.ContainingConfig).ToList();

            foreach (var grouping in groupings)
            {
                var path       = RequireJsNet.Helpers.PathHelpers.GetOverridePath(grouping.Key);
                var writer     = WriterFactory.CreateWriter(path, null);
                var collection = this.ComposeCollection(grouping.ToList());
                writer.WriteConfig(collection);
            }
        }
예제 #4
0
 private static WriterInfo CreateWriterInfo(
     IWriterSetup writerSetup,
     IEnumerable <GeneratorInfo> generatorInfos,
     SetupInfo setupInfo,
     int writerSetupIndex,
     int?generatorSetupIndex,
     IWriter?previousWriter)
 {
     return(new WriterInfo(
                writerSetup,
                WriterFactory.CreateWriter(writerSetup.Writer, setupInfo, writerSetupIndex, generatorSetupIndex, previousWriter),
                generatorInfos));
 }
예제 #5
0
        public override Task ExecuteAsync(ActionContext context, JsonResult result)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            var response = context.HttpContext.Response;

            ResponseContentTypeHelper.ResolveContentTypeAndEncoding(
                result.ContentType,
                response.ContentType,
                DefaultContentType,
                out var resolvedContentType,
                out var resolvedContentTypeEncoding);

            response.ContentType = resolvedContentType;

            if (result.Value != null)
            {
                using (var writer = WriterFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
                {
                    JsonSerializeOption option = null;
                    if (result is JsonResultWrapper wrapper)
                    {
                        option = wrapper.Option;
                    }

                    var hosting = context.HttpContext.RequestServices.GetService <JsonSerializeOptionHosting>();
                    if (hosting != null)
                    {
                        option = hosting.Option;
                    }

                    option = option ?? mvcOptions.JsonSerializeOption;
                    var serializer = new JsonSerializer(option);

                    using (var jsonWriter = new JsonWriter(writer))
                    {
                        serializer.Serialize(result.Value, jsonWriter);
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #6
0
        public override Task ExecuteAsync(Microsoft.AspNetCore.Mvc.ActionContext context, JsonResult result)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            var response = context.HttpContext.Response;

            ResponseContentTypeHelper.ResolveContentTypeAndEncoding(
                result.ContentType,
                response.ContentType,
                DefaultContentType,
                out var resolvedContentType,
                out var resolvedContentTypeEncoding);

            response.ContentType = resolvedContentType;

            if (result.Value != null)
            {
                using (var writer = WriterFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
                {
                    JsonSerializeOption option = null;
                    if (result is JsonResultWrapper wrapper)
                    {
                        option = wrapper.Option;
                    }

                    option = option ?? new JsonSerializeOption();

                    var globalconverters = GlobalSetting.Converters.Where(s => s is JsonConverter).Cast <JsonConverter>();
                    option.Converters.AddRange(globalconverters);

                    var serializer = new JsonSerializer(option);
                    using (var jsonWriter = new JsonWriter(writer))
                    {
                        serializer.Serialize(result.Value, jsonWriter);
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #7
0
        public static void Main(string[] args)
        {
            if (!args.Any())
            {
                return;
            }

            var path = args[0];

            var reader = ReaderFactory.CreateReader(path, new ConfigLoaderOptions {
                ProcessAutoBundles = true, ProcessBundles = true
            });
            var config = reader.ReadConfig();

            var outPath = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(path) + ".out" + ".json";

            var writer = WriterFactory.CreateWriter(
                outPath,
                new ConfigLoaderOptions {
                ProcessAutoBundles = true, ProcessBundles = true
            });

            writer.WriteConfig(config);
        }
예제 #8
0
        public ICodeFileWriterContext CreateFile(string @namespace, string content)
        {
            var writer = WriterFactory.CreateWriter();

            writer.AddGeneratorHeader()
            .Using("System")
            .Using("System.Linq")
            .Using("Newtonsoft.Json")
            .Using("Stardust.Interstellar")
            .Using("Stardust.Particles")
            .DeclareNamespace(@namespace,
                              w =>
            {
                w.StaticClass(
                    "ConfigurationExtensions",
                    cw =>
                {
                    cw.ExtMethod("ConfigurationSettings", "GetConfiguration", "IRuntime",
                                 mw =>
                    {
                        mw.Return("ConfigurationSettings.Current");
                    });
                    cw.ExtMethod("AppSettings", "AppSettings", "IRuntime",
                                 mw =>
                    {
                        mw.Return("ConfigurationSettings.Current.AppSettings");
                    });
                    cw.ExtMethod("Environment", "Environment", "IRuntime",
                                 mw =>
                    {
                        mw.Return("ConfigurationSettings.Current.Environment");
                    });
                    cw.ExtMethod("ServiceHosts", "ServiceHosts", "IRuntime",
                                 mw =>
                    {
                        mw.Return("ConfigurationSettings.Current.ServiceHosts");
                    });
                });
                w.Class(
                    "ConfigurationSettings",
                    cw =>
                {
                    cw.Field("static ConfigurationSettings", "_current", null);
                    cw.Property("static ConfigurationSettings", "Current",
                                gw =>
                    {
                        gw.If("_current==null", bw => { bw.AssignVariable("_current", "new ConfigurationSettings()"); });
                        gw.Return("_current");
                    }, null);
                    cw.Field("AppSettings", "appSettings", "new AppSettings()");
                    cw.Property(
                        "AppSettings",
                        "AppSettings",
                        gw =>
                    {
                        gw.Return("appSettings");
                    }, null);
                    cw.Field("Environment", "environment", "new Environment()");
                    cw.Property(
                        "Environment",
                        "Environment",
                        gw =>
                    {
                        gw.Return("environment");
                    }, null);
                    cw.Field("ServiceHosts", "serviceHosts", "new ServiceHosts()");
                    cw.Property(
                        "ServiceHosts",
                        "ServiceHosts",
                        gw =>
                    {
                        gw.Return("serviceHosts");
                    }, null);
                });
                w.Class("AppSettings",
                        cw =>
                {
                    cw.InternalCtor();
                    cw.Property("string", "ConfigSetName",
                                gw =>
                    {
                        gw.GetConfigValueWithFallback("configSet");
                        //gw.Return(gw.GetValueOnKey("configSet"));
                    }, null);
                    cw.Property("string", "Environment",
                                gw =>
                    {
                        gw.GetConfigValueWithFallback("environment");
                        //gw.Return(gw.GetValueOnKey("environment"));
                    }, null);
                    cw.Property("string", "ServiceHostName",
                                gw =>
                    {
                        gw.GetConfigValueWithFallback("environment");
                        //gw.Return(gw.GetValueOnKey("serviceName"));
                    }, null);
                    cw.Property("string", "datacenter",
                                gw =>
                    {
                        gw.Var("value", gw.GetValueOnKey("dataCenterConfigKeyName"));
                        gw.If("value.IsNullOrWhiteSpace()",
                              bw =>
                        {
                            bw.Return(gw.GetValueOnKey("dataCenterName"));
                        })
                        .Else(bw => bw.Return("value"));
                    }, null);
                    cw.Property("string", "ConfigurationServiceUrl",
                                gw =>
                    {
                        gw.Return(gw.GetValueOnKey("stardust.configLocation"));
                    }, null);
                });
                w.Class("Environment",
                        cw =>
                {
                    var added = new Dictionary <string, bool>();
                    foreach (var propertyName in cw.GetEnvironments(content).GetParameters())
                    {
                        var name = propertyName.Replace(".", "").Replace("-", "").Replace(",", "").Replace("_", "");
                        if (added.ContainsKey(name.Trim()))
                        {
                            continue;
                        }
                        else
                        {
                            added.Add(name.Trim(), true);
                            var privateName = string.Format("_{0}", name);
                            if (name.ToLower().Contains("password") || name.ToLower().Contains("key") || name.ToLower().Contains("accountname") || name.ToLower().Contains("username") || name.ToLower().Contains("user") || name.ToLower().Contains("account") || name.ToLower().Contains("secret") || name.ToLower().Contains("connectionstring") || name.ToLower().Contains("thumbprint"))
                            {
                                cw.Attribute("JsonIgnore");
                            }
                            cw.Property <string>(
                                name,
                                gw =>
                            {
                                gw.Var(privateName, string.Format("RuntimeFactory.Current.Context.GetEnvironmentConfiguration().GetConfigParameter(\"{0}\")", propertyName));
                                gw.If("string.IsNullOrEmpty(" + privateName + ")", iw =>
                                {
                                    iw.Return("string.Empty");
                                })
                                .Else(ew =>
                                {
                                    ew.Return(privateName);
                                });
                            }
                                , null);
                        }
                    }
                    foreach (var propertyName in cw.GetEnvironments(content).GetSecureParameters())
                    {
                        var name = propertyName.Replace(".", "").Replace("-", "").Replace(",", "").Replace("_", "");
                        if (added.ContainsKey(name.Trim()))
                        {
                            continue;
                        }
                        else
                        {
                            added.Add(name.Trim(), true);
                            var privateName = string.Format("_{0}", name);
                            cw.Attribute("JsonIgnore");
                            cw.Property <string>(
                                name,
                                gw =>
                            {
                                gw.Var(privateName, string.Format("RuntimeFactory.Current.Context.GetEnvironmentConfiguration().GetSecureConfigParameter(\"{0}\")", propertyName));
                                gw.If(
                                    "string.IsNullOrEmpty(" + privateName + ")",
                                    iw =>
                                {
                                    iw.Return("string.Empty");
                                }).Else(
                                    ew =>
                                {
                                    ew.Return(privateName);
                                });
                            },
                                null);
                        }
                    }
                });
                w.Class("ServiceHosts",
                        cw =>
                {
                    foreach (var service in cw.GetConfig(content).Services)
                    {
                        cw.Field(service.ServiceName + "ServiceHost", "_" + service.ServiceName.ToLower(), "new " + service.ServiceName + "ServiceHost()");
                        cw.Property(
                            service.ServiceName + "ServiceHost",
                            service.ServiceName,
                            gw =>
                        {
                            gw.Return("_" + service.ServiceName.ToLower());
                        },
                            null);
                        cw.Class(
                            service.ServiceName + "ServiceHost",
                            ccw =>
                        {
                            foreach (var parameter in service.Parameters)
                            {
                                var name        = parameter.Name.Trim().Replace(".", "").Replace("-", "").Replace(",", "").Replace("_", "");
                                var privateName = string.Format("_{0}", name);
                                cw.Property <string>(
                                    name,
                                    gw =>
                                {
                                    gw.Var(privateName, string.Format("RuntimeFactory.Current.Context.GetServiceConfiguration(\"{1}\").GetConfigParameter(\"{0}\")", parameter.Name, service.ServiceName));
                                    gw.If("string.IsNullOrEmpty(" + privateName + ")", iw =>
                                    {
                                        iw.Return("string.Empty");
                                    })
                                    .Else(ew =>
                                    {
                                        ew.Return(privateName);
                                    });
                                }
                                    , null);
                            }
                        });
                    }
                });
            });
            return(writer);
        }