コード例 #1
0
        public static ItemGenerationStatusEventArgs CreateError(Template template, TemplateOutputDefinitionFilenameResult result, Exception error)
        {
            if (template == null)
                throw new ArgumentNullException("template");

            if (result == null)
                throw new ArgumentNullException("result");

            if (error == null)
                throw new ArgumentNullException("error");

            return new ItemGenerationStatusEventArgs(template, result, GenerationStatus.Error, error);
        }
コード例 #2
0
ファイル: Generator.cs プロジェクト: VijayMVC/CodeGenerator
        /// <summary>Processes a single template to one or more output files using the provided project XML.</summary>
        private Task GenerateOutputAsync(string projectXml, Template template, XslCompiledTransform compiledTransform, TemplateOutputDefinitionFilenameResult output)
        {
            // Report status and do sanity checks upfront.
            OnItemGenerationStatus(ItemGenerationStatusEventArgs.CreateGenerating(template, output));

            if (File.Exists(output.Value))
            {
                try
                {
                    File.Delete(output.Value);
                }
                catch (Exception ex)
                {
                    Generator.Logger.Error(ex);
                    OnItemGenerationStatus(ItemGenerationStatusEventArgs.CreateError(template, output, new ApplicationException("Unable to delete existing file.", ex)));
                    return(Task.CompletedTask);
                }
            }

            try {
                Directory.CreateDirectory(Path.GetDirectoryName(output.Value));
            }
            catch (Exception ex) {
                Generator.Logger.Error(ex);
                OnItemGenerationStatus(ItemGenerationStatusEventArgs.CreateError(template, output, new ApplicationException("Unable to create output directory.", ex)));
                return(Task.CompletedTask);
            }

            // Perform the actual processing async.
            return(Task.Run(() => {
                try {
                    using (StringReader stringReader = new StringReader(projectXml)) {
                        XmlReaderSettings xmlReaderSettings = new XmlReaderSettings()
                        {
                            DtdProcessing = DtdProcessing.Parse
                        };

                        using (XmlReader xmlReader = XmlReader.Create(stringReader, xmlReaderSettings)) {
                            XsltArgumentList arguments = new XsltArgumentList();

                            arguments.AddParam(Template.Param_Name, string.Empty, template.Name);

                            if (!output.ElementName.IsNullOrEmpty())
                            {
                                arguments.AddParam(TemplateOutputDefinitionFilenameResult.Param_ElementName, string.Empty, output.ElementName);
                            }

                            using (FileStream outputStream = File.Create(output.Value))
                                using (XmlWriter xmlWriter = XmlWriter.Create(outputStream, compiledTransform.OutputSettings)) {
                                    compiledTransform.Transform(xmlReader, arguments, xmlWriter);
                                }
                        }
                    }

                    PerformPostProcessing(output.Value);
                }
                catch (Exception ex) {
                    Generator.Logger.Error(ex);
                    OnItemGenerationStatus(ItemGenerationStatusEventArgs.CreateError(template, output, new ApplicationException("Unable to generate the output. Error writing to the specified path or the template or input XML is not valid.", ex)));
                    return;
                }

                OnItemGenerationStatus(ItemGenerationStatusEventArgs.CreateComplete(template, output));
            }));
        }
コード例 #3
0
        public static ItemGenerationStatusEventArgs CreateComplete(Template template, TemplateOutputDefinitionFilenameResult result)
        {
            if (template == null)
                throw new ArgumentNullException("template");

            if (result == null)
                throw new ArgumentNullException("result");

            return new ItemGenerationStatusEventArgs(template, result, GenerationStatus.Complete);
        }
コード例 #4
0
 protected ItemGenerationStatusEventArgs(Template template, TemplateOutputDefinitionFilenameResult result, GenerationStatus status, Exception error = null)
     : base(template, status, error)
 {
     this.Result = result;
 }