public async void Generate()
        {
            if (Project == null)
            {
                Helper.Notify("当前项目为空,无法生成代码,请先通过菜单“项目-项目管理”加载项目", NotificationType.Error);
                return;
            }
            CodeProject project = null;

            CodeTemplate[] templates = new CodeTemplate[0];
            _provider.ExecuteScopedWork(provider =>
            {
                IDataContract contract = provider.GetRequiredService <IDataContract>();
                project   = contract.GetCodeProject(m => m.Name == Project.Name).FirstOrDefault();
                templates = contract.CodeProjectTemplates.Where(m => m.ProjectId == project.Id && !m.IsLocked).Select(m => m.Template).ToArray();
            });
            if (project == null)
            {
                Helper.Notify($"名称为“{Project.Name}”的项目信息不存在", NotificationType.Error);
                return;
            }

            if (templates.Length == 0)
            {
                Helper.Notify($"项目“{project.GetName()}”的模板数量为0,请先通过菜单“项目-项目模板管理”添加模板", NotificationType.Error);
                return;
            }

            CodeFile[] codeFiles;
            var        progress = await Helper.Main.ShowProgressAsync("请稍候", "正在生成代码,请稍候");

            await Task.Run(async() =>
            {
                try
                {
                    ICodeGenerator generator = _provider.GetRequiredService <ICodeGenerator>();
                    codeFiles = await generator.GenerateCodes(templates, project);
                    await progress.CloseAsync();
                }
                catch (Exception ex)
                {
                    await progress.CloseAsync();
                    Helper.Notify($"代码生成失败:{ex.Message}", NotificationType.Error);
                    return;
                }

                SaveToFiles(codeFiles);
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the code generation workflow.
        /// </summary>
        public CodeWriterOutput GenerateCode(Options.CodeGenOptions options)
        {
            ICodeGeneratorContext codeGeneratorContext = buildCodeGeneratorContext(options);
            ICodeGenerator        codeGenerator        = this.buildCodeGenerator(options);

            CodeWriterOptions writeOptions = CodeGenOptionsParser.GetCodeWriterOptions(options);

            if (options.OnlyUseDataContractSerializer)
            {
                CodeCompileUnit targetCodeCompileUnit = codeGenerator.GenerateDataContractCode(codeGeneratorContext);
                return(CodeWriter.CodeWriter.Write(targetCodeCompileUnit, writeOptions));
            }
            else if (options.GenerateSeparateFilesEachXsd)
            {
                CodeNamespace[] targetCodeNamespaces = codeGenerator.GenerateCodes(codeGeneratorContext);
                return(CodeWriter.CodeWriter.Write(targetCodeNamespaces, writeOptions));
            }
            else
            {
                CodeNamespace targetCodeNamespace = codeGenerator.GenerateCode(codeGeneratorContext);
                return(CodeWriter.CodeWriter.Write(targetCodeNamespace, writeOptions));
            }
        }