Skip to content

Commit f6e0e93

Browse files
committed
Merge branch 'master'
2 parents 759c7fb + f2d9a79 commit f6e0e93

File tree

1 file changed

+75
-61
lines changed

1 file changed

+75
-61
lines changed

Umbraco.CodeGen.Integration/ApplicationEvents.cs

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,50 @@ public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication,
3636

3737
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
3838
{
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"));
39+
try
40+
{
41+
var uSyncConfigurationProvider = new USyncConfigurationProvider(HttpContext.Current.Server.MapPath("~/config/uSyncSettings.config"), new HttpContextPathResolver());
42+
var configurationProvider = new CodeGeneratorConfigurationFileProvider(HttpContext.Current.Server.MapPath("~/config/CodeGen.config"));
4143

42-
uSyncConfiguration = uSyncConfigurationProvider.GetConfiguration();
44+
uSyncConfiguration = uSyncConfigurationProvider.GetConfiguration();
4345

44-
dataTypesProvider = new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
46+
dataTypesProvider = new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
4547

46-
if (!dataTypesProvider.GetDataTypes().Any())
47-
return;
48-
//new UmbracoDataTypeProvider();
49-
//new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
48+
if (!dataTypesProvider.GetDataTypes().Any())
49+
return;
50+
//new UmbracoDataTypeProvider();
51+
//new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
5052

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

7885
private T CreateFactory<T>(string typeName)
@@ -108,7 +115,7 @@ private void GenerateXml(ContentTypeConfiguration contentTypeConfiguration)
108115
if (contentType != null)
109116
documents.Add(XDocument.Parse(serializer.Serialize(contentType)));
110117
}
111-
LogHelper.Debug<CodeGenerator>(() => String.Format("Parsing file {0} done. Took {1:MM:ss}", file, DateTime.Now - itemStart));
118+
LogHelper.Debug<CodeGenerator>(() => String.Format("Parsing file {0} done. Took {1:MM\\:ss}", file, DateTime.Now - itemStart));
112119
}
113120

114121
var documentTypeRoot = Path.Combine(uSyncConfiguration.USyncFolder, contentTypeConfiguration.ContentTypeName);
@@ -117,8 +124,8 @@ private void GenerateXml(ContentTypeConfiguration contentTypeConfiguration)
117124
itemStart = DateTime.Now;
118125
LogHelper.Info<CodeGenerator>(() => "Writing uSync definitions");
119126
WriteDocuments(contentTypeConfiguration, documents, documentTypeRoot, "");
120-
LogHelper.Info<CodeGenerator>(() => String.Format("Writing uSync definitions done. Took {0:MM:ss}", DateTime.Now - itemStart));
121-
}
127+
LogHelper.Info<CodeGenerator>(() => String.Format("Writing uSync definitions done. Took {0:MM\\:ss}", DateTime.Now - itemStart));
128+
}
122129

123130
private void WriteDocuments(
124131
ContentTypeConfiguration contentTypeConfiguration,
@@ -148,37 +155,44 @@ string baseClass
148155

149156
private void OnDocumentTypeSaved(XmlDocFileEventArgs e)
150157
{
151-
if (!e.Path.Contains("DocumentType") && !e.Path.Contains("MediaType"))
152-
return;
158+
try
159+
{
160+
if (!e.Path.Contains("DocumentType") && !e.Path.Contains("MediaType"))
161+
return;
153162

154-
var typeConfig = e.Path.Contains("DocumentType")
155-
? configuration.DocumentTypes
156-
: configuration.MediaTypes;
163+
var typeConfig = e.Path.Contains("DocumentType")
164+
? configuration.DocumentTypes
165+
: configuration.MediaTypes;
157166

158-
if (!typeConfig.GenerateClasses)
159-
return;
167+
if (!typeConfig.GenerateClasses)
168+
return;
160169

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

164-
ContentType contentType;
165-
using (var reader = File.OpenText(e.Path))
166-
contentType = serializer.Deserialize(reader);
173+
ContentType contentType;
174+
using (var reader = File.OpenText(e.Path))
175+
contentType = serializer.Deserialize(reader);
167176

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

170-
var modelPath = paths[typeConfig.ContentTypeName];
171-
if (!Directory.Exists(modelPath))
172-
Directory.CreateDirectory(modelPath);
173-
var path = Path.Combine(modelPath, contentType.Info.Alias.PascalCase() + ".cs");
174-
if (configuration.OverwriteReadOnly && File.Exists(path))
175-
File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
176-
177-
var classGenerator = new CodeGenerator(typeConfig, dataTypesProvider, generatorFactory);
178-
using (var stream = File.CreateText(path))
179-
classGenerator.Generate(contentType, stream);
180-
181-
LogHelper.Debug<CodeGenerator>(() => String.Format("Typed model for {0} generated. Took {1:MM:ss}", contentType.Alias, DateTime.Now - itemStart));
179+
var modelPath = paths[typeConfig.ContentTypeName];
180+
if (!Directory.Exists(modelPath))
181+
Directory.CreateDirectory(modelPath);
182+
var path = Path.Combine(modelPath, contentType.Info.Alias.PascalCase() + ".cs");
183+
if (configuration.OverwriteReadOnly && File.Exists(path))
184+
File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
185+
186+
var classGenerator = new CodeGenerator(typeConfig, dataTypesProvider, generatorFactory);
187+
using (var stream = File.CreateText(path))
188+
classGenerator.Generate(contentType, stream);
189+
190+
LogHelper.Debug<CodeGenerator>(() => String.Format("Typed model for {0} generated. Took {1:MM\\:ss}", contentType.Alias, DateTime.Now - itemStart));
191+
}
192+
catch (Exception ex)
193+
{
194+
LogHelper.Error<CodeGenerator>("Generating typed model failed", ex);
195+
}
182196
}
183197

184198
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

0 commit comments

Comments
 (0)