예제 #1
0
        protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject)
        {
            errors.Clear();
            warnings.Clear();

            return(base.CreateProject(targetProjectDirectory, sourceProject));
        }
        public CompiledTemplate CompileTemplate(string content)
        {
            if (String.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }

            errors.Clear();
            encoding = Encoding.UTF8;

            AppDomain        appdomain = ProvideTemplatingAppDomain(content);
            TemplatingEngine engine;

            if (appdomain != null)
            {
                engine = (TemplatingEngine)
                         appdomain.CreateInstanceAndUnwrap(typeof(TemplatingEngine).Assembly.FullName,
                                                           typeof(TemplatingEngine).FullName);
            }
            else
            {
                engine = new TemplatingEngine();
            }

            return(engine.CompileTemplate(content, this));
        }
예제 #3
0
        public CompiledTemplate CompileTemplate(string content)
        {
            if (String.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }

            errors.Clear();
            encoding = Encoding.UTF8;

            return(Engine.CompileTemplate(content, this));
        }
        public bool ProcessTemplate(string inputFile, string outputFile)
        {
            if (String.IsNullOrEmpty(inputFile))
            {
                throw new ArgumentNullException("inputFile");
            }
            if (String.IsNullOrEmpty(outputFile))
            {
                throw new ArgumentNullException("outputFile");
            }

            errors.Clear();
            encoding        = Encoding.UTF8;
            this.outputFile = outputFile;
            this.inputFile  = inputFile;

            return(ProcessCurrent());
        }
예제 #5
0
            private bool Parse(TextReader input)
            {
                m_errors.Clear();

                var lexer  = new Man.UnitsOfMeasurement.Lexer(input, LogError);
                var parser = new Man.UnitsOfMeasurement.Parser(lexer, m_units, m_scales);

                parser.Parse();

                return(!m_errors.HasErrors);
            }
예제 #6
0
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="ttFile">tt模板路径</param>
        /// <param name="outputFileName">生成的文件的名称</param>
        /// <param name="fileNamePattern">需要替换的文件名</param>
        /// <param name="fileNamesDel">需要去掉的文件名前缀</param>
        /// <param name="outputPath">输出路径</param>
        /// <param name="nameSpace">命名空间</param>
        public static void CreateCode(ZippyCoder.Entity.Project project, ZippyCoder.Entity.Table table, string ttFile, string outputFileName, string fileNamePattern, string fileNamesDel, string outputPath, bool multiDir)
        {
            if (T4Errors != null)
            {
                T4Errors.Clear();
            }
            ZippyT4Host host = new ZippyT4Host();

            host.Project = project;
            host.Table   = table;

            Engine engine = new Engine();

            host.TemplateFile = ttFile;


            string input  = File.ReadAllText(ttFile);
            string output = engine.ProcessTemplate(input, host);

            string[] fileNameDels = fileNamesDel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string fileNameDel in fileNameDels)
            {
                if (outputFileName.StartsWith(fileNameDel))
                {
                    outputFileName = outputFileName.Remove(0, fileNameDel.Length);
                }
            }
            //大写文件名,java 会敏感,与类对应。
            outputFileName = ZippyCoder.Helper.ToJavaClassName(outputFileName);

            if (multiDir)
            {
                if (table != null)
                {
                    outputPath = Path.Combine(outputPath, table.Name);
                    if (!System.IO.Directory.Exists(outputPath))
                    {
                        System.IO.Directory.CreateDirectory(outputPath);
                    }
                }
            }

            outputFileName = string.Format(fileNamePattern, outputFileName);
            var utf8WithoutBom = new System.Text.UTF8Encoding(false);

            File.WriteAllText(Path.Combine(outputPath, outputFileName + host.FileExtension), output, utf8WithoutBom);

            T4Errors = host.Errors;
        }
        /// <summary>Compile supplementary (runtime) units/scales from a file or string</summary>
        /// <param name="definitions">definition file path --or-- definition string</param>
        /// <param name="parse">ParseFile | ParseString (parser methods)</param>
        /// <param name="outputAssemblyPath">path to the output DLL | null if the DLL is not required</param>
        /// <returns>assembly with supplementary (runtime) units/scales | null in case of errors</returns>
        private Assembly Compile(string definitions, Func <string, bool> parse, string outputAssemblyPath)
        {
            // Switch error collection back to the parser
            m_errors = m_parser.Errors;

            m_errors.Clear();

            // Initialize m_units & m_scales lists with compile-time definitions:
            m_decompiler.Decompile();

            int unitStartIndex  = m_units.Count;                    // start index for (possible) new units
            int scaleStartIndex = m_scales.Count;                   // start index for (possible) new scales
            int familyStartId   = m_decompiler.MaxFamilyFound + 1;  // start id for (possible) new families

            Assembly supplement = null;

            // Parse (append) new definitions into m_units & m_scales lists:
            if (parse(definitions))
            {
                // Generate source code (for the new definitions only):
                Generator generator = new Generator(familyStartId, m_units, unitStartIndex, m_scales, scaleStartIndex);
                string    source    = generator.TransformText();

                // References to assemblies containing units & scales that might be referenced from the source.
                // NOTE: references to measures appended previously from string (and not saved to a DLL) are not available!!!
                string[] references = Catalog.All
                                      .Select(m => m.Type.Assembly.Location).Distinct()
                                      .Where(location => !string.IsNullOrWhiteSpace(location)) // NOTE: assembly locations for measures appended from string are empty!!!
                                      .ToArray();

                // Compile the generated source code:
                if (m_compiler.CompileFromSource(source, references, outputAssemblyPath))
                {
                    supplement = m_compiler.Results.CompiledAssembly;
                }

                m_errors = m_compiler.Results.Errors;
            }
            return(supplement);
        }
        /// <summary>Compile supplementary (runtime) units/scales from a definition text file</summary>
        /// <param name="definitionsTextfile">definition file path</param>
        /// <returns>assembly with supplementary (runtime) units/scales, saved to an external DLL | null in case of errors</returns>
        private Assembly CompileFromFile(string definitionsTextfile)
        {
            string definitionsAssembly =
                string.Format("{0}\\{1}.dll",
                              Path.GetDirectoryName(definitionsTextfile),
                              Path.GetFileNameWithoutExtension(definitionsTextfile)
                              );

            FileSystemInfo textfile = new FileInfo(definitionsTextfile);
            FileSystemInfo assembly = new FileInfo(definitionsAssembly);

            if (textfile.LastWriteTime >= assembly.LastWriteTime)
            {
                return(Compile(definitionsTextfile, m_parser.ParseFile, outputAssemblyPath: definitionsAssembly));
            }
            else
            {
                m_errors = m_parser.Errors;
                m_errors.Clear();
                return(Assembly.LoadFrom(definitionsAssembly));
            }
        }
예제 #9
0
        /// <summary>
        /// Render the model and returns the results
        /// </summary>
        /// <param name="modelPath"></param>
        /// <param name="modelType"></param>
        /// <param name="ttPath"></param>
        public string RenderModel(string modelPath, string modelType, string ttPath, bool preProcess)
        {
            if (!string.IsNullOrEmpty(_basePath))
            {
                modelPath = modelPath.Replace("~", _basePath);
                ttPath    = ttPath.Replace("~", _basePath);
            }

            if (!File.Exists(modelPath) || !File.Exists(ttPath))
            {
                throw new Exception("Model file ('" + modelPath + "') or Template file ('" + ttPath + "') doesn't exist.");
            }

            string templateData = Properties.Resources.Header +
                                  File.ReadAllText(ttPath) +
                                  Properties.Resources.Footer.Replace("~~", Path.GetFullPath(modelPath));

            File.WriteAllText(ttPath + ".tmp", templateData);

            DynamicTemplateHost thost = new DynamicTemplateHost();
            string data = string.Empty;

            _errors.Clear();
            var results = thost.ProcessTemplate(ttPath + ".tmp", out data);

            foreach (var res in results)
            {
                _errors.Add(res as CompilerError);
            }

            try
            {
                File.Delete(ttPath + ".tmp");
            }
            catch { }

            return(data);
        }