Ejemplo n.º 1
0
        private void OnDocumentTypeSaved(XmlDocFileEventArgs e)
        {
            try
            {
                if (!e.Path.Contains("DocumentType") && !e.Path.Contains("MediaType"))
                {
                    return;
                }

                var typeConfig = e.Path.Contains("DocumentType")
                            ? configuration.DocumentTypes
                            : configuration.MediaTypes;

                if (!typeConfig.GenerateClasses)
                {
                    return;
                }

                itemStart = DateTime.Now;
                LogHelper.Debug <CodeGenerator>(() => String.Format("Content type {0} saved, generating typed model", e.Path));

                ContentType contentType;
                using (var reader = File.OpenText(e.Path))
                    contentType = serializer.Deserialize(reader);

                LogHelper.Info <CodeGenerator>(() => String.Format("Generating typed model for {0}", contentType.Alias));

                var modelPath = paths[typeConfig.ContentTypeName];
                if (!Directory.Exists(modelPath))
                {
                    Directory.CreateDirectory(modelPath);
                }
                var path = Path.Combine(modelPath, contentType.Info.Alias.PascalCase() + ".cs");
                if (configuration.OverwriteReadOnly && File.Exists(path))
                {
                    File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
                }

                var classGenerator = new CodeGenerator(typeConfig, dataTypesProvider, generatorFactory);
                using (var stream = File.CreateText(path))
                    classGenerator.Generate(contentType, stream);

                LogHelper.Debug <CodeGenerator>(
                    () =>
                    String.Format("Typed model for {0} generated. Took {1:MM\\:ss}", contentType.Alias,
                                  DateTime.Now - itemStart));
            }
            catch (Exception ex)
            {
                LogHelper.Error <CodeGenerator>("Generating typed model failed", ex);
            }
        }
        private void OnDocumentTypeSaved(XmlDocFileEventArgs e)
        {
            var inputPath = e.Path;

            try
            {
                if (inputPath.Contains("DocumentType") || inputPath.Contains("MediaType"))
                {
                    GenerateModels(inputPath);
                }

                if (inputPath.Contains("DataTypeDefinition"))
                {
                    EnsureReloadDataTypes();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error <CodeGenerator>("Generating typed model failed", ex);
            }
        }