Skip to content

Commit f2d9a79

Browse files
committed
fixed format strings and logging exceptions
1 parent 2a3855a commit f2d9a79

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

Umbraco.CodeGen.Integration/ApplicationEvents.cs

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Linq.Expressions;
56
using System.Web;
67
using System.Xml.Linq;
78
using umbraco.cms.businesslogic.datatype.controls;
@@ -36,38 +37,45 @@ public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication,
3637

3738
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
3839
{
39-
var uSyncConfigurationProvider = new USyncConfigurationProvider(HttpContext.Current.Server.MapPath("~/config/uSyncSettings.config"), new HttpContextPathResolver());
40-
var configurationProvider = new CodeGeneratorConfigurationFileProvider(HttpContext.Current.Server.MapPath("~/config/CodeGen.config"));
40+
try
41+
{
42+
var uSyncConfigurationProvider = new USyncConfigurationProvider(HttpContext.Current.Server.MapPath("~/config/uSyncSettings.config"), new HttpContextPathResolver());
43+
var configurationProvider = new CodeGeneratorConfigurationFileProvider(HttpContext.Current.Server.MapPath("~/config/CodeGen.config"));
4144

42-
uSyncConfiguration = uSyncConfigurationProvider.GetConfiguration();
45+
uSyncConfiguration = uSyncConfigurationProvider.GetConfiguration();
4346

44-
dataTypesProvider = new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
47+
dataTypesProvider = new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
4548

46-
dataTypes = dataTypesProvider.GetDataTypes();
47-
configuration = configurationProvider.GetConfiguration();
48-
49-
generatorFactory = CreateFactory<CodeGeneratorFactory>(configuration.GeneratorFactory);
50-
parserFactory = CreateFactory<ParserFactory>(configuration.ParserFactory);
51-
52-
paths.Add("DocumentType", HttpContext.Current.Server.MapPath(configuration.DocumentTypes.ModelPath));
53-
paths.Add("MediaType", HttpContext.Current.Server.MapPath(configuration.MediaTypes.ModelPath));
54-
55-
XmlDoc.Saved += OnDocumentTypeSaved;
56-
57-
if (configuration.DocumentTypes.GenerateXml)
58-
{
59-
globalStart = DateTime.Now;
60-
LogHelper.Info<CodeGenerator>(() => "Parsing typed documenttype models");
61-
GenerateXml(configuration.DocumentTypes);
62-
LogHelper.Info<CodeGenerator>(() => String.Format("Parsing typed documenttype models done. Took {0:MM:ss}", DateTime.Now - globalStart));
63-
}
64-
if (configuration.MediaTypes.GenerateXml)
65-
{
66-
globalStart = DateTime.Now;
67-
LogHelper.Info<CodeGenerator>(() => "Parsing typed mediatype models");
68-
GenerateXml(configuration.MediaTypes);
69-
LogHelper.Info<CodeGenerator>(() => String.Format("Parsing typed mediatype models done. Took {0:MM:ss}", DateTime.Now - globalStart));
70-
}
49+
dataTypes = dataTypesProvider.GetDataTypes();
50+
configuration = configurationProvider.GetConfiguration();
51+
52+
generatorFactory = CreateFactory<CodeGeneratorFactory>(configuration.GeneratorFactory);
53+
parserFactory = CreateFactory<ParserFactory>(configuration.ParserFactory);
54+
55+
paths.Add("DocumentType", HttpContext.Current.Server.MapPath(configuration.DocumentTypes.ModelPath));
56+
paths.Add("MediaType", HttpContext.Current.Server.MapPath(configuration.MediaTypes.ModelPath));
57+
58+
XmlDoc.Saved += OnDocumentTypeSaved;
59+
60+
if (configuration.DocumentTypes.GenerateXml)
61+
{
62+
globalStart = DateTime.Now;
63+
LogHelper.Info<CodeGenerator>(() => "Parsing typed documenttype models");
64+
GenerateXml(configuration.DocumentTypes);
65+
LogHelper.Info<CodeGenerator>(() => String.Format("Parsing typed documenttype models done. Took {0:MM\\:ss}", DateTime.Now - globalStart));
66+
}
67+
if (configuration.MediaTypes.GenerateXml)
68+
{
69+
globalStart = DateTime.Now;
70+
LogHelper.Info<CodeGenerator>(() => "Parsing typed mediatype models");
71+
GenerateXml(configuration.MediaTypes);
72+
LogHelper.Info<CodeGenerator>(() => String.Format("Parsing typed mediatype models done. Took {0:MM\\:ss}", DateTime.Now - globalStart));
73+
}
74+
}
75+
catch(Exception ex)
76+
{
77+
LogHelper.Error<CodeGenerator>("Parsing typed models failed", ex);
78+
}
7179
}
7280

7381
private T CreateFactory<T>(string typeName)
@@ -103,7 +111,7 @@ private void GenerateXml(ContentTypeConfiguration contentTypeConfiguration)
103111
if (contentType != null)
104112
documents.Add(XDocument.Parse(serializer.Serialize(contentType)));
105113
}
106-
LogHelper.Debug<CodeGenerator>(() => String.Format("Parsing file {0} done. Took {1:MM:ss}", file, DateTime.Now - itemStart));
114+
LogHelper.Debug<CodeGenerator>(() => String.Format("Parsing file {0} done. Took {1:MM\\:ss}", file, DateTime.Now - itemStart));
107115
}
108116

109117
var documentTypeRoot = Path.Combine(uSyncConfiguration.USyncFolder, contentTypeConfiguration.ContentTypeName);
@@ -112,7 +120,7 @@ private void GenerateXml(ContentTypeConfiguration contentTypeConfiguration)
112120
itemStart = DateTime.Now;
113121
LogHelper.Info<CodeGenerator>(() => "Writing uSync definitions");
114122
WriteDocuments(contentTypeConfiguration, documents, documentTypeRoot, "");
115-
LogHelper.Info<CodeGenerator>(() => String.Format("Writing uSync definitions done. Took {0:MM:ss}", DateTime.Now - itemStart));
123+
LogHelper.Info<CodeGenerator>(() => String.Format("Writing uSync definitions done. Took {0:MM\\:ss}", DateTime.Now - itemStart));
116124
}
117125

118126
private void WriteDocuments(
@@ -143,38 +151,48 @@ string baseClass
143151

144152
private void OnDocumentTypeSaved(XmlDocFileEventArgs e)
145153
{
146-
if (!e.Path.Contains("DocumentType") && !e.Path.Contains("MediaType"))
147-
return;
154+
try
155+
{
156+
if (!e.Path.Contains("DocumentType") && !e.Path.Contains("MediaType"))
157+
return;
148158

149-
var typeConfig = e.Path.Contains("DocumentType")
150-
? configuration.DocumentTypes
151-
: configuration.MediaTypes;
159+
var typeConfig = e.Path.Contains("DocumentType")
160+
? configuration.DocumentTypes
161+
: configuration.MediaTypes;
152162

153-
if (!typeConfig.GenerateClasses)
154-
return;
163+
if (!typeConfig.GenerateClasses)
164+
return;
155165

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

159-
ContentType contentType;
160-
using (var reader = File.OpenText(e.Path))
161-
contentType = serializer.Deserialize(reader);
169+
ContentType contentType;
170+
using (var reader = File.OpenText(e.Path))
171+
contentType = serializer.Deserialize(reader);
162172

163-
LogHelper.Info<CodeGenerator>(() => String.Format("Generating typed model for {0}", contentType.Alias));
164-
165-
var modelPath = paths[typeConfig.ContentTypeName];
166-
if (!Directory.Exists(modelPath))
167-
Directory.CreateDirectory(modelPath);
168-
var path = Path.Combine(modelPath, contentType.Info.Alias.PascalCase() + ".cs");
169-
if (configuration.OverwriteReadOnly && File.Exists(path))
170-
File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
173+
LogHelper.Info<CodeGenerator>(() => String.Format("Generating typed model for {0}", contentType.Alias));
174+
175+
var modelPath = paths[typeConfig.ContentTypeName];
176+
if (!Directory.Exists(modelPath))
177+
Directory.CreateDirectory(modelPath);
178+
var path = Path.Combine(modelPath, contentType.Info.Alias.PascalCase() + ".cs");
179+
if (configuration.OverwriteReadOnly && File.Exists(path))
180+
File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
171181

172-
var classGenerator = new CodeGenerator(typeConfig, dataTypesProvider, generatorFactory);
173-
using (var stream = File.CreateText(path))
174-
classGenerator.Generate(contentType, stream);
182+
var classGenerator = new CodeGenerator(typeConfig, dataTypesProvider, generatorFactory);
183+
using (var stream = File.CreateText(path))
184+
classGenerator.Generate(contentType, stream);
175185

176-
LogHelper.Debug<CodeGenerator>(() => String.Format("Typed model for {0} generated. Took {1:MM:ss}", contentType.Alias, DateTime.Now - itemStart));
177-
}
186+
LogHelper.Debug<CodeGenerator>(
187+
() =>
188+
String.Format("Typed model for {0} generated. Took {1:MM\\:ss}", contentType.Alias,
189+
DateTime.Now - itemStart));
190+
}
191+
catch (Exception ex)
192+
{
193+
LogHelper.Error<CodeGenerator>("Generating typed model failed", ex);
194+
}
195+
}
178196

179197
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
180198
{

0 commit comments

Comments
 (0)