Ejemplo n.º 1
0
            public void Add(ISourceCodeGenerator generator)
            {
                string typeName = generator.SourceObjectType.FullName;

                if (codeGens.ContainsKey(typeName))
                {
                    throw new Exception("A source code generator is already registered for this type: "
                                        + typeName);
                }
                codeGens.Add(typeName, generator);
            }
Ejemplo n.º 2
0
        private static void GenerateResolvers(
            IEnumerable <IDelegateDescriptor> resolverDescriptors,
            StringBuilder source)
        {
            var i = 0;

            foreach (IDelegateDescriptor resolverDescriptor in
                     resolverDescriptors)
            {
                ISourceCodeGenerator generator = _generators.First(
                    t => t.CanHandle(resolverDescriptor));
                source.AppendLine(generator.Generate(
                                      GetDelegateName(i++), resolverDescriptor));
            }
        }
Ejemplo n.º 3
0
 public bool Remove(ISourceCodeGenerator generator)
 {
     return(Remove(generator.SourceObjectType.FullName));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Compiles given source file.
        /// </summary>
        /// <param name="scriptFileObject">The source file to be compiled.</param>
        public CompiledUnit Compile(string filePath, object scriptFileObject)
        {
            if (filePath == null)
            {
                throw new Exception("No file path specified!");
            }
            if (scriptFileObject == null)
            {
                throw new Exception("No script file object specified!");
            }

            ISourceCodeGenerator gen = codeGenList[scriptFileObject.GetType()];

            counter++;

            // compile the code

            SourceCodeData sourceCode = gen.CreateSourceContent(filePath, scriptFileObject, "ScriptedNameSpace" + counter, "ScriptedClass" + counter);

            // extract all using-information

            HashSet <string> usings = new HashSet <string>();

            foreach (ISourceCodeLine line in sourceCode)
            {
                string s = line.LineText.Trim();
                if (!s.StartsWith("using"))
                {
                    continue;
                }
                if (!s.EndsWith(";"))
                {
                    continue;
                }
                s = s.Substring("using".Length);
                if (!char.IsWhiteSpace(s[0]))
                {
                    continue;
                }
                s = s.Trim();
                s = s.Substring(0, s.Length - 1);
                if (!NameSpaceReference.IsMatch(s))
                {
                    continue;
                }
                usings.Add(s);
            }

            // prepare compilation

            CompilerParameters compilerParameters = new CompilerParameters {
                GenerateExecutable = false,
                GenerateInMemory   = true,
                // CompilerOptions = "/o+",
                IncludeDebugInformation = true,
                CompilerOptions         = " /debug:pdbonly",
                OutputAssembly          = tempDir2 + "ScriptedAssembly" + counter + ".dll",
            };

            // add additional assemblies

            List <string> referencedAssemblies = new List <string>();

            foreach (AssemblyManager.NamespaceAssemblyInfo ai in AssemblyManager.GetNamespaceAssemblyInfos(usings))
            {
                compilerParameters.ReferencedAssemblies.Add(ai.Path);
                referencedAssemblies.Add(ai.Path);
            }

            /*
             * ReferencedAssemblyList referencedAssemblyList = new ReferencedAssemblyList();
             * if (gen.ReferencedAssemblies != null) {
             *      referencedAssemblyList.AddLoadAssemblies(gen.ReferencedAssemblies);
             * }
             * compilerParameters.ReferencedAssemblies.AddRange(referencedAssemblyList.ToArray());
             */

            /* does not work
             * foreach (string assemblyPath in compilerParameters.ReferencedAssemblies) {
             *      Assembly.LoadFrom(assemblyPath);
             * }			*/

            // check compilation result

            CompilerResults results = csharpCompiler.CompileAssemblyFromSource(compilerParameters, sourceCode.ToString());

            if (results.Errors.Count > 0)
            {
                return(new CompiledUnit(sourceSpec, filePath, referencedAssemblies.ToArray(), __TransformErrors(results.Errors, sourceCode)));
            }
            else
            if (results.CompiledAssembly == null)
            {
                throw new InvalidOperationException("Unable to compile scripts: No result assembly from compiler!");
            }

            // try to get main class

            Assembly compiledAssembly = results.CompiledAssembly;
            Type     scriptClass      = compiledAssembly.GetType("ScriptedNameSpace" + counter + ".ScriptedClass" + counter);

            if (scriptClass == null)
            {
                throw new InvalidOperationException("Unable to compile scripts: scripted class not found!");
            }

            return(new CompiledUnit(sourceSpec, filePath, referencedAssemblies.ToArray(), compiledAssembly, scriptClass));
        }
Ejemplo n.º 5
0
 public UniqueSourceCodeGenerator(ISourceCodeRepository repository, ISourceCodeGenerator generator)
 {
     _repository = repository;
     _generator  = generator;
 }