コード例 #1
0
        public static BuildOptions Load(string path)
        {
            var options = new BuildOptions();

            if (!File.Exists(path))
            {
                return options;
            }

            var doc = XDocument.Load(path);

            var element = doc.Element(ElementName);

            if (element != null)
            {
                var atttrib = element.Attribute(LanguageAttribute);
                if (atttrib != null)
                {
                    options.Language = (OutputLanguage)Enum.Parse(typeof(OutputLanguage), (string)atttrib);
                }

                atttrib = element.Attribute(OutputFolderAttribute);
                if (atttrib != null)
                {
                    options.OutputFolder = (string)atttrib;
                }

                atttrib = element.Attribute(NamespaceAttribute);
                if (atttrib != null)
                {
                    options.EntityNamespace = (string)atttrib;
                }

                atttrib = element.Attribute(EntityModifierAttribute);
                if (atttrib != null)
                {
                    options.EntityModifier = (TypeAttributes)Enum.Parse(typeof(TypeAttributes), (string)atttrib);
                }
            }

            return options;
        }
コード例 #2
0
 internal void SetBuildOptions(BuildOptions options)
 {
     options.Save(this.OptionsFile);
 }
コード例 #3
0
        internal void GenerateCode(BuildOptions options)
        {
            using (var csProvider = CodeDomProvider.CreateProvider("C#"))  // TODO: add VB support
            {

                foreach (var entity in Structure)
                {
                    var ccu = new CodeCompileUnit();
                    var root = new CodeNamespace(options.EntityNamespace);

                    ccu.Namespaces.Add(root);

                    AddImports(root);

                    var entityClass = new CodeTypeDeclaration(entity.Entity.NameInStore);
                    entityClass.TypeAttributes = options.EntityModifier;

                    // TODO: determine KeyScheme
                    // TODO: set NameInStore if different than class name (user overridden)
                    var entityAttributeDeclaration = new CodeAttributeDeclaration("Entity",
                        new CodeAttributeArgument(new CodeSnippetExpression("KeyScheme." + entity.Entity.KeyScheme.ToString()))
                        // new CodeAttributeArgument(new CodeSnippetExpression("NameInStore=\"" + entity.Entity.NameInStore + "\""))
                        );

                    //new CodeAttributeArgument("KeyScheme", new CodePrimitiveExpression(KeyScheme.None))
                    entityClass.CustomAttributes.Add(entityAttributeDeclaration);

                    var fieldList = new List<CodeMemberField>();
                    var propList = new List<CodeMemberProperty>();

                    // TODO: timespans
                    // TODO: enums
                    // TODO: objects
                    foreach (var field in entity.Fields)
                    {
                        // TODO: optionally make it bindable with RaisePropertyChanged
                        var type = new CodeTypeReference(field.DataType.ToManagedType(field.AllowsNulls));
                        var backingFieldName = "m_" + field.FieldName.ToLower(); // TODO: let the use specify format
                        var backingField = new CodeMemberField(type, backingFieldName);
                        backingField.Attributes = MemberAttributes.Private;
                        fieldList.Add(backingField);

                        var prop = new CodeMemberProperty();
                        prop.Name = field.FieldName;
                        prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;  // TODO: get from UI
                        prop.CustomAttributes.Add(new CodeAttributeDeclaration("Field", GenerateFieldArguments(field)));
                        prop.Type = type;
                        prop.GetStatements.Add(
                            new CodeMethodReturnStatement(
                                new CodeFieldReferenceExpression(
                                    new CodeThisReferenceExpression(), backingFieldName)));
                        prop.SetStatements.Add(
                            new CodeAssignStatement(
                                new CodeFieldReferenceExpression(
                                    new CodeThisReferenceExpression(), backingFieldName),
                                    new CodePropertySetValueReferenceExpression()));

                        propList.Add(prop);
                    }

                    // this is outside the loop above to give some semblance of structure to the code
                    foreach (var backingField in fieldList)
                    {
                        entityClass.Members.Add(backingField);
                    }
                    foreach (var prop in propList)
                    {
                        entityClass.Members.Add(prop);
                    }

                    root.Types.Add(entityClass);

                    // Generate the C# source code for this class
                    GenerateClassFile(options, csProvider, ccu, entity.Entity.NameInStore);
                }

            }
        }
コード例 #4
0
        private void GenerateClassFile(BuildOptions buildOptions, CodeDomProvider provider, CodeCompileUnit ccu, string entityname)
        {
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);

            // todo: make these settable
            var options = new CodeGeneratorOptions();
            options.BlankLinesBetweenMembers = true;
            options.BracingStyle = "C";
            options.ElseOnClosing = false;
            options.IndentString = "    ";
            options.VerbatimOrder = true;

            provider.GenerateCodeFromCompileUnit(ccu, sw, options);
            var fileName = entityname + ".cs";  // TODO: add VB support
            var path = buildOptions.OutputFolder;
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            path = Path.Combine(path, fileName);

            // TODO: make auto-delete optional or give a warning
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            try
            {
                using (var writer = File.CreateText(path))
                {
                    writer.Write(sb.ToString());
                    writer.Close();
                }
            }
            catch (IOException ex)
            {
                // TODO: handle this
                throw ex;
            }
        }