Exemplo n.º 1
0
        private void GenerateCode(GenerationParameter parameter, ITemplateEngine templateEngine, string templateName,
                                  ref int genratedCount, ref int errorCount, ref int progressCount, AsyncOperation asyncOp)
        {
            foreach (string modelId in parameter.GenerationObjects.Keys)
            {
                string codeFileNamePath = PathHelper.GetCodeFileNamePath(ConfigManager.GenerationCodeOuputPath,
                                                                         ConfigManager.SettingsSection.Languages[parameter.Settings.Language].Alias,
                                                                         ConfigManager.SettingsSection.TemplateEngines[parameter.Settings.TemplateEngine].Name,
                                                                         ConfigManager.TemplateSection.Templates[templateName].DisplayName, parameter.Settings.Package, modelId);
                PathHelper.CreateCodeFileNamePath(codeFileNamePath);

                foreach (string objId in parameter.GenerationObjects[modelId])
                {
                    IMetaData    modelObject  = ModelManager.GetModelObject(parameter.Models[modelId], objId);
                    TemplateData templateData = TemplateDataBuilder.Build(modelObject, parameter.Settings,
                                                                          templateName, parameter.Models[modelId].Database, modelId);

                    if (templateData == null || !templateEngine.Run(templateData))
                    {
                        errorCount++;
                    }
                    else
                    {
                        genratedCount++;
                    }
                    string currentCodeFileName = templateData == null ? string.Empty : templateData.CodeFileName;

                    var args = new GenerationProgressChangedEventArgs(genratedCount,
                                                                      errorCount, currentCodeFileName, ++progressCount, asyncOp.UserSuppliedState);
                    asyncOp.Post(this.onProgressReportDelegate, args);
                }
            }
        }
Exemplo n.º 2
0
        private void GenerateWorker(GenerationParameter parameter, AsyncOperation asyncOp)
        {
            if (IsTaskCanceled(asyncOp.UserSuppliedState))
            {
                return;
            }

            int genratedCount = 0;
            int errorCount    = 0;
            int progressCount = 0;

            try
            {
                string          adapterTypeName = ConfigManager.SettingsSection.TemplateEngines[parameter.Settings.TemplateEngine].Adapter;
                ITemplateEngine templateEngine  = (ITemplateEngine)Activator.CreateInstance(Type.GetType(adapterTypeName));

                foreach (string templateName in parameter.Settings.TemplatesNames)
                {
                    this.GenerateCode(parameter, templateEngine, templateName, ref genratedCount, ref errorCount, ref progressCount, asyncOp);
                }
            }
            catch (Exception ex)
            {
                logger.Error("", ex);
            }

            this.CompletionMethod(null, IsTaskCanceled(asyncOp.UserSuppliedState), asyncOp);
        }
Exemplo n.º 3
0
        public virtual void GenerateAsync(GenerationParameter parameter, object taskId)
        {
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(taskId);
            lock (userStateToLifetime.SyncRoot)
            {
                if (userStateToLifetime.Contains(taskId))
                    throw new ArgumentException("Task ID parameter must be unique", "taskId");

                userStateToLifetime[taskId] = asyncOp;
            }

            WorkerEventHandler workerDelegate = new WorkerEventHandler(GenerateWorker);
            workerDelegate.BeginInvoke(parameter, asyncOp, null, null);
        }
Exemplo n.º 4
0
        public static void GenerateCode(out string entitys, TreeView treeView, string[] templateNames, string tablePrefix, bool isOmitTablePrefix, bool isCamelCaseName)
        {
            entitys = "";
            GenerationParameter parameter = new GenerationParameter(
                ModelManager.Clone(),
                GetGenerationObjects(treeView),
                GetGenerationSettings(templateNames, tablePrefix, isOmitTablePrefix, isCamelCaseName));

            foreach (string modelId in parameter.GenerationObjects.Keys)
            {
                foreach (string objId in parameter.GenerationObjects[modelId])
                {
                    IMetaData    modelObject  = ModelManager.GetModelObject(parameter.Models[modelId], objId);
                    TemplateData templateData = TemplateDataBuilder.Build(modelObject, parameter.Settings,
                                                                          "default", parameter.Models[modelId].Database, modelId);

                    if (templateData == null)
                    {
                        throw new ArgumentNullException("Can not create template data!");
                    }
                    string currentCodeFileName = templateData == null ? string.Empty : templateData.CodeFileName;

                    if (modelObject is Table)
                    {
                        Table table = modelObject as Table;
                        foreach (var column in table.Columns.Values)
                        {
                            string langType     = column.LanguageType;
                            string defaultValue = column.LanguageDefaultValue;
                            string typeAlias    = column.LanguageTypeAlias;

                            entitys += string.Format("public {0} {1} {{get;set;}}\r\n\r\n\t\t", langType, column.DisplayName);
                        }
                    }
                    else if (modelObject is PhysicalDataModel.View)
                    {
                        PhysicalDataModel.View view = modelObject as PhysicalDataModel.View;
                        foreach (var column in view.Columns.Values)
                        {
                            string langType     = column.LanguageType;
                            string defaultValue = column.LanguageDefaultValue;
                            string typeAlias    = column.LanguageTypeAlias;
                            entitys += string.Format("public {0} {1} {{get;set;}}}\r\n\r\n\t\t", langType, column.DisplayName);
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        public virtual void GenerateAsync(GenerationParameter parameter, object taskId)
        {
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(taskId);

            lock (userStateToLifetime.SyncRoot)
            {
                if (userStateToLifetime.Contains(taskId))
                {
                    throw new ArgumentException(Resources.TaskId, "taskId");
                }

                userStateToLifetime[taskId] = asyncOp;
            }
            WorkerEventHandler workerDelegate = new WorkerEventHandler(GenerateWorker);

            workerDelegate.BeginInvoke(parameter, asyncOp, null, null);
        }
Exemplo n.º 6
0
        private void GenerateWorker(GenerationParameter parameter, AsyncOperation asyncOp)
        {
            if (IsTaskCanceled(asyncOp.UserSuppliedState)) return;

            int genratedCount = 0;
            int errorCount = 0;
            int progressCount = 0;

            try
            {
                string adapterTypeName = ConfigManager.SettingsSection.TemplateEngines[parameter.Settings.TemplateEngine].Adapter;
                ITemplateEngine templateEngine = (ITemplateEngine)Activator.CreateInstance(Type.GetType(adapterTypeName));

                foreach (string templateName in parameter.Settings.TemplatesNames)
                {
                    this.GenerateCode(parameter, templateEngine, templateName, ref genratedCount, ref errorCount, ref progressCount, asyncOp);
                }
            }
            catch (Exception ex)
            {
                logger.Error("", ex);
            }

            this.CompletionMethod(null, IsTaskCanceled(asyncOp.UserSuppliedState), asyncOp);
        }
Exemplo n.º 7
0
        private void GenerateCode(GenerationParameter parameter, ITemplateEngine templateEngine, string templateName,
            ref int genratedCount, ref int errorCount, ref int progressCount, AsyncOperation asyncOp)
        {
            foreach (string modelId in parameter.GenerationObjects.Keys)
            {
                string codeFileNamePath = PathHelper.GetCodeFileNamePath(ConfigManager.GenerationCodeOuputPath,
                    ConfigManager.SettingsSection.Languages[parameter.Settings.Language].Alias,
                    ConfigManager.SettingsSection.TemplateEngines[parameter.Settings.TemplateEngine].Name,
                    ConfigManager.TemplateSection.Templates[templateName].DisplayName, parameter.Settings.Package, modelId);
                PathHelper.CreateCodeFileNamePath(codeFileNamePath);

                foreach (string objId in parameter.GenerationObjects[modelId])
                {
                    IMetaData modelObject = ModelManager.GetModelObject(parameter.Models[modelId], objId);
                    TemplateData templateData = TemplateDataBuilder.Build(modelObject, parameter.Settings,
                            templateName, parameter.Models[modelId].Database, modelId);

                    if (templateData == null || !templateEngine.Run(templateData)) errorCount++; else genratedCount++;
                    string currentCodeFileName = templateData == null ? string.Empty : templateData.CodeFileName;

                    var args = new GenerationProgressChangedEventArgs(genratedCount,
                            errorCount, currentCodeFileName, ++progressCount, asyncOp.UserSuppliedState);
                    asyncOp.Post(this.onProgressReportDelegate, args);
                }
            }
        }