예제 #1
0
    private static async Task RenderTemplate(HypermediaType schema, string templatePath, string outputPath, string? @namespace, string?includeFile)
    {
        var template       = Template.Parse(await File.ReadAllTextAsync(templatePath), templatePath);
        var includeContent = string.IsNullOrEmpty(includeFile) ? string.Empty : await File.ReadAllTextAsync(includeFile);

        var scriptObject = new ScriptObject();

        scriptObject.Import(schema, renamer: GetMemberName);
        scriptObject.Import(new
        {
            Namespace      = @namespace,
            IncludeContent = includeContent,
        }, renamer: GetMemberName);
        scriptObject.Import(new CustomFunctions());

        var templateContext = new TemplateContext
        {
            MemberRenamer  = GetMemberName,
            TemplateLoader = new DiskLoader(),
        };

        templateContext.PushGlobal(scriptObject);

        var code = await template.RenderAsync(templateContext);

        await File.WriteAllTextAsync(outputPath, code);

        string GetMemberName(MemberInfo member) => member.Name;
    }
        private JObject GetProperty(PropertyInfo property, object instance, HypermediaType hypermediaInstance)
        {
            var value = instance != null ? property.GetValue(instance) : null;

            if (!property.PropertyType.IsValueType && property.PropertyType != typeof(string)) {
                return CreateObject(property.PropertyType, property.GetCustomAttributes(true).Cast<Attribute>().ToArray(), value);
            }

            HypermediaType type;
            if (!hypermediaInstance.Properties.TryGetValue(property.Name, out type)) {
                type = hypermediaInstance.ConfigureProperty(property, () => value);
            }

            return SerializeValueType(type);
        }
        private JObject SerializeValueType(HypermediaType type)
        {
            var jObject = JObject.FromObject(type, Serializer);

            jObject.Add("links", JArray.FromObject(_resolver.Resolve(type.Links), Serializer));
            jObject.Add("errors", JArray.FromObject(type.Errors, Serializer));

            return jObject;
        }