Beispiel #1
0
        internal void Process(string t4Template, string outputFilePath, ITemplateDefinition definition, ProcessorParameters parameters)
        {
            var ttParams = new TextTemplatingParameters(
                t4Template,
                outputFilePath,
                definition.OverrideFileIfExists,
                definition.AddToProject);

            string tmpOutputFilePath;

            try
            {
                var state = this.TextTemplatingEngine.Process(ttParams, out tmpOutputFilePath);

                if (state == ProcessStateEnum.Processed)
                {
                    // SQL scripts...

                    if (definition.ExecuteSqlScript)
                    {
                        bool canExecuted = true;
                        if (definition is TemplateDefinitions.Database.InsertAllStoredProceduresGeneratedSqlTemplateDefinition && !parameters.WithSqlProcedureIntegration)
                        {
                            canExecuted = false;
                        }

                        if (canExecuted)
                        {
                            this.PublishProcessMessage("Executing {0}...", Path.GetFileName(tmpOutputFilePath));

                            try
                            {
                                SmoHelper.RunSqlScript(tmpOutputFilePath, this.SmoContext);
                            }
                            catch
                            {
                                // Create a copy of the tmpOutputFilePath file (for debug) because it will be deleted at the end of the whole process...
                                FileHelper.TryCopy(tmpOutputFilePath, string.Concat(tmpOutputFilePath, ".err"), withOverwrite: true);

                                throw;
                            }
                        }
                    }
                }

                this.HasErrors |= state == ProcessStateEnum.Failed;
            }
            catch (Exception x)
            {
                this.PublishErrorMessage("Failed while processing {0} -> {1}", Path.GetFileName(t4Template), x.Message);
            }

            foreach (CompilerError error in this.TextTemplatingEngine.Errors)
            {
                this.PublishErrorMessage(
                    "Template error: {0}({1},{2}): error {3}: {4}",
                    Path.GetFileName(this.TextTemplatingEngine.Errors.First().FileName), error.Line, error.Column, error.ErrorNumber, error.ErrorText);
            }
        }
        /// <summary>
        /// Execute the process.
        /// </summary>
        ///
        /// <param name="parameters">
        /// Parameters.
        /// </param>
        ///
        /// <param name="tmpOutputFilePath">
        /// Path of the output file generated by the call.
        /// </param>
        ///
        /// <returns>
        /// The ProcessStateEnum state.
        /// </returns>
        public ProcessStateEnum Process(TextTemplatingParameters parameters, out string tmpOutputFilePath)
        {
            tmpOutputFilePath = null;

            if (!parameters.OverrideIfExists && File.Exists(parameters.OutputFilePath))
            {
                if (this.OnNotProcessed != null)
                {
                    this.OnNotProcessed(this, new TextTemplatingEngineTemplateEventArgs(parameters.TemplateFilePath));
                }

                return(ProcessStateEnum.NotProcessed);
            }

            if (this.OnProcessing != null)
            {
                this.OnProcessing(this, new TextTemplatingEngineTemplateEventArgs(parameters.TemplateFilePath));
            }

            _host.TemplateFile = parameters.TemplateFilePath;

            string input  = File.ReadAllText(parameters.TemplateFilePath);
            string output = _engine.ProcessTemplate(input, _host);

            if (this.Errors.Count() != 0 || output.Equals("ErrorGeneratingOutput", StringComparison.InvariantCultureIgnoreCase))
            {
                if (this.OnProcessFailed != null)
                {
                    this.OnProcessFailed(this, new TextTemplatingEngineTemplateEventArgs(parameters.TemplateFilePath));
                }

                return(ProcessStateEnum.Failed);
            }

            var fileDefinition = new TextTemplatingOutputFileDefinition(parameters.OutputFilePath, parameters.AddToProject);

            _generatedFiles.Add(fileDefinition);

            File.WriteAllText(fileDefinition.TempPath, output.TrimStart('\r', '\n'), _host.FileEncoding);

            tmpOutputFilePath = fileDefinition.TempPath;

            if (this.OnProcessed != null)
            {
                this.OnProcessed(this, new TextTemplatingEngineTemplateEventArgs(parameters.TemplateFilePath));
            }

            return(ProcessStateEnum.Processed);
        }