Beispiel #1
0
        private void ProcessTemplate(params string[] args)
        {
            string templateFileName = null;

            if (args.Length == 0)
            {
                throw new System.Exception("you must provide a text template file path");
            }

            templateFileName = args[0];

            if (templateFileName == null)
            {
                throw new ArgumentNullException("the file name cannot be null");
            }

            if (!File.Exists(templateFileName))
            {
                throw new FileNotFoundException("the file cannot be found");
            }

            OnShowWorkInfoEventHandler(this, new WorkEventArgs(WorkStage.InitializeWork, string.Format("processing template: {0}", Path.GetFileName(templateFileName))));

            TextTemplatingSession session = new TextTemplatingSession();
            session.Add("t4Parameter", T4Parameters.Default);
            CustomCmdLineHost host = new CustomCmdLineHost();
            host.TemplateFileValue = templateFileName;
            host.Session = session;
            Engine engine = new Engine();
            //Read the text template.
            string input = File.ReadAllText(templateFileName);
            input = input.Replace(@"$(ProjectDir)\", "");
            //Transform the text template.
            string output = engine.ProcessTemplate(input, host);
            foreach (CompilerError error in host.Errors)
            {
                Console.WriteLine(error.ToString());
            }

            lock (synObject)
            {
                int prograssBarValue = OnGetValueEventHandler(this, null);
                prograssBarValue++;
                OnDoWorkEventHandler(this, new WorkEventArgs(WorkStage.DoWork, string.Format("{0} has been processed...", templateFileName), prograssBarValue){ TemplateName=Path.GetFileName(templateFileName)});
            }
        }
Beispiel #2
0
        private void GenerateFile(ThreadStateObject threadStateObject)
        {
            try
            {
                SetLabelText(string.Format("正在生成 {0} ", threadStateObject.TemplateName.ToString()), threadStateObject.LblTip);
                string templateFolder = string.Format(@"{0}\Templates\", Environment.CurrentDirectory);
                TextTemplatingSession session = new TextTemplatingSession();
                session.Add("t4Parameter", T4Parameters.Default);

                CustomCmdLineHost host = new CustomCmdLineHost();
                host.TemplateFileValue = threadStateObject.TemplateName.ToString();
                host.Session = session;
                Engine engine = new Engine();
                //Read the text template.
                string input = File.ReadAllText(templateFolder + threadStateObject.TemplateName);
                input = input.Replace(@"$(ProjectDir)\", "");
                //Transform the text template.
                string output = engine.ProcessTemplate(input, host);
                foreach (CompilerError error in host.Errors)
                {
                    Console.WriteLine(error.ToString());
                }
            }
            catch
            {

            }
            lock (synObj)
            {
                taskFinishedCount = taskFinishedCount + 1;
            }
        }