Beispiel #1
0
        public static ButlerProperty Create(JToken jToken)
        {
            if (!(jToken is JProperty))
            {
                throw new ArgumentException($"Token {jToken} is not a valid property.", nameof(jToken));
            }

            JProperty jProperty = (JProperty)jToken;

            // Create property from type.
            bool requiresNewClass = JsonUtilities.GetTypeFrom(jProperty.Value, out string typeName);

            string propertyId   = jProperty.Name;
            string propertyName = propertyId.ToPascalCase();

            // Create additional type if needed.
            var dependencies = new List <ButlerClass> ();

            if (requiresNewClass)
            {
                typeName = propertyName;
                ButlerClass dependency = ButlerClassFactory.Create(typeName, jProperty.Path, jProperty.Value);
                dependencies.Add(dependency);
                dependencies.AddRange(dependency.Dependencies);
            }

            ButlerProperty bProperty = new ButlerProperty(propertyName, propertyId, typeName);

            bProperty.AddDependencyRange(dependencies);

            return(bProperty);
        }
        public ButlerConstructor(ButlerClass owner)
        {
            _owner = owner;

            SyntaxToken classId = SyntaxFactory.Identifier(owner.Name);

            Info = SyntaxFactory.ConstructorDeclaration(classId);
        }
Beispiel #3
0
        public string Generate()
        {
            if (string.IsNullOrEmpty(SourceJson))
            {
                throw new InvalidOperationException("Source JSON for code generation cannot be empty.");
            }

            CompilationUnitSyntax compileUnit = SyntaxFactory.CompilationUnit();

            compileUnit = compileUnit.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")));
            compileUnit = compileUnit.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Newtonsoft.Json")));

            ButlerNamespace bNamespace = ButlerNamespaceFactory.Create(Namespace);

            JToken jToken = JToken.Parse(SourceJson);

            if (jToken == null)
            {
                throw new InvalidOperationException("Source JSON is not valid JSON.");
            }

            ButlerClass bClass = ButlerClassFactory.Create(ClassName, ClassName, jToken);

            // Add the class to the namespace.
            bNamespace.AddClass(bClass);

            // Add created classes (dependencies) to the namespace.
            bNamespace.AddClasses(bClass.Dependencies);

            compileUnit = compileUnit.AddMembers(bNamespace.Info);
            compileUnit = compileUnit.NormalizeWhitespace();

            string code = compileUnit.ToFullString();

            return(code);
        }
 public static ButlerConstructor Create(ButlerClass bClass)
 {
     return(new ButlerConstructor(bClass));
 }
Beispiel #5
0
 public void AddDependency(ButlerClass bClass)
 {
     Dependencies.Add(bClass);
 }
Beispiel #6
0
 public void AddClass(ButlerClass bClass)
 {
     _classes.Add(bClass);
     Info = Info.AddMembers(bClass.Info);
 }