Esempio n. 1
0
        public CodeDto GetPreview(int id)
        {
            var docType     = ApplicationContext.Services.ContentTypeService.GetAllContentTypes(id).Single();
            var contentPath = ApplicationContext.Services.ContentTypeService.GetAllContentTypes(docType.Path.Split(',').Select(p => Convert.ToInt32(p)).ToArray());
            var defPath     = "~/usync/" + "DocumentType/" + String.Join("/", contentPath.Select(c => c.Alias)) + "/def.config";
            var inputPath   = HttpContext.Current.Server.MapPath(defPath);

            var typeConfig = inputPath.Contains("DocumentType")
                ? Integration.Configuration.CodeGen.DocumentTypes
                : Integration.Configuration.CodeGen.MediaTypes;

            Definitions.ContentType contentType;
            using (var reader = File.OpenText(inputPath))
                contentType = serializer.Deserialize(reader);

            var generatorFactory = ApplicationEvents.CreateFactory <CodeGeneratorFactory>(Integration.Configuration.CodeGen.GeneratorFactory);
            var classGenerator   = new CodeGenerator(typeConfig, Integration.Configuration.DataTypesProvider, generatorFactory);
            var builder          = new StringBuilder();

            using (var stream = new StringWriter(builder))
                classGenerator.Generate(contentType, stream);

            return(new CodeDto {
                Name = docType.Name, Code = builder.ToString()
            });
        }
        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 static void TestDeserialize <T>(string filename, T expectedContentType)
            where T : ContentType
        {
            T   actual;
            var actualBuilder   = new StringBuilder();
            var expectedBuilder = new StringBuilder();

            using (var reader = File.OpenText(string.Format(@"..\..\TestFiles\{0}.xml", filename)))
            {
                var serializer = new ContentTypeSerializer();
                var content    = serializer.Deserialize(reader);
                actual = (T)content;
            }
            SerializationHelper.BclSerialize(actualBuilder, actual);
            SerializationHelper.BclSerialize(expectedBuilder, expectedContentType);
            Assert.AreEqual(expectedBuilder.ToString(), actualBuilder.ToString());
        }
        private void GenerateModels(string inputPath)
        {
            var typeConfig = inputPath.Contains("DocumentType")
                    ? Configuration.CodeGen.DocumentTypes
                    : Configuration.CodeGen.MediaTypes;

            if (!typeConfig.GenerateClasses)
            {
                return;
            }

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

            ContentType contentType;

            using (var reader = File.OpenText(inputPath))
                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.CodeGen.OverwriteReadOnly && File.Exists(path))
            {
                File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
            }

            var classGenerator = new CodeGenerator(typeConfig, Configuration.DataTypesProvider, generatorFactory);

            using (var stream = File.CreateText(path))
                classGenerator.Generate(contentType, stream);

            LogHelper.Debug <CodeGenerator>(
                () => String.Format("Typed model for {0} generated. Took {1}", contentType.Alias, DateTime.Now - itemStart));
        }