Beispiel #1
0
		public static string GenerateText(TypeDeclaration type, AbstractDynamicCompilationExtension[] extensions)
		{
			var unit = new CompilationUnit();

			var namespaces = new HashSet<string>
			{
				typeof (AbstractViewGenerator).Namespace,
				typeof (Enumerable).Namespace,
				typeof (IEnumerable<>).Namespace,
				typeof (IEnumerable).Namespace,
				typeof (int).Namespace,
				typeof (LinqOnDynamic).Namespace
			};
			foreach (var extension in extensions)
			{
				foreach (var ns in extension.GetNamespacesToImport())
				{
					namespaces.Add(ns);
				}
			}

			foreach (var ns in namespaces)
			{
				unit.AddChild(new Using(ns));
			}

			unit.AddChild(type);
			var output = new CSharpOutputVisitor();
			unit.AcceptVisitor(output, null);

			return output.Text;
		}
Beispiel #2
0
 public DynamicViewCompiler(string name, IndexDefinition indexDefinition, AbstractDynamicCompilationExtension[] extensions)
 {
     this.indexDefinition = indexDefinition;
     this.extensions = extensions;
     this.name = MonoHttpUtility.UrlEncode(name);
     RequiresSelectNewAnonymousType = true;
 }
		public IndexDefinitionStorage(
			ITransactionalStorage  transactionalStorage,
			string path, 
			IEnumerable<AbstractViewGenerator> compiledGenerators, 
			AbstractDynamicCompilationExtension[] extensions)
		{
			this.extensions = extensions;// this is used later in the ctor, so it must appears first
			this.path = Path.Combine(path, IndexDefDir);

			if (Directory.Exists(this.path) == false)
				Directory.CreateDirectory(this.path);

            this.extensions = extensions;
            foreach (var index in Directory.GetFiles(this.path, "*.index"))
			{
				try
				{
					AddAndCompileIndex(
						MonoHttpUtility.UrlDecode(Path.GetFileNameWithoutExtension(index)),
						JsonConvert.DeserializeObject<IndexDefinition>(File.ReadAllText(index), new JsonEnumConverter())
						);
				}
				catch (Exception e)
				{
					logger.Warn("Could not compile index " + index + ", skipping bad index", e);
				}
			}

            //compiled view generators always overwrite dynamic views
		    foreach (var generator in compiledGenerators)
		    {
		        var copy = generator;
		        var displayNameAtt = TypeDescriptor.GetAttributes(copy)
		            .OfType<DisplayNameAttribute>()
		            .FirstOrDefault();

		        var name = displayNameAtt != null ? displayNameAtt.DisplayName : copy.GetType().Name;

                transactionalStorage.Batch(actions =>
                {
                    if (actions.Indexing.GetIndexesStats().Any(x => x.Name == name))
                        return;

                    actions.Indexing.AddIndex(name);
                });

		        var indexDefinition = new IndexDefinition
		        {
                    Map = "Compiled map function: " + generator.GetType().AssemblyQualifiedName,
                    // need to supply this so the index storage will create map/reduce index
                    Reduce = generator.ReduceDefinition == null ? null : "Compiled reduce function: " + generator.GetType().AssemblyQualifiedName,
		            Indexes = generator.Indexes,
		            Stores = generator.Stores,
                    IsCompiled = true
		        };
		        indexCache.AddOrUpdate(name, copy, (s, viewGenerator) => copy);
		        indexDefinitions.AddOrUpdate(name, indexDefinition, (s1, definition) => indexDefinition);
		    }
		}
		public DynamicViewCompiler(string name, IndexDefinition indexDefinition, AbstractDynamicCompilationExtension[] extensions, string basePath)
		{
			this.indexDefinition = indexDefinition;
			this.extensions = extensions;
			this.basePath = Path.Combine(basePath, "TemporaryIndexDefinitionsAsSource");
			if (Directory.Exists(this.basePath) == false)
				Directory.CreateDirectory(this.basePath);
			this.name = MonoHttpUtility.UrlEncode(name);
		    RequiresSelectNewAnonymousType = true;
		}
		public IndexDefinitionStorage(
            InMemoryRavenConfiguration configuration,
			ITransactionalStorage  transactionalStorage,
			string path, 
			IEnumerable<AbstractViewGenerator> compiledGenerators, 
			AbstractDynamicCompilationExtension[] extensions)
		{
		    this.configuration = configuration;
		    this.extensions = extensions;// this is used later in the ctor, so it must appears first
			this.path = Path.Combine(path, IndexDefDir);

			if (Directory.Exists(this.path) == false && configuration.RunInMemory == false)
				Directory.CreateDirectory(this.path);

            this.extensions = extensions;

            if (configuration.RunInMemory == false)
                ReadIndexesFromDisk();

            //compiled view generators always overwrite dynamic views
		    ReadIndexesFromCatalog(compiledGenerators, transactionalStorage);
		}
Beispiel #6
0
		public static Type Compile(string fileName, string name, string queryText, AbstractDynamicCompilationExtension[] extensions)
		{
			var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
			var assemblies = new HashSet<string>
			{
				typeof (AbstractViewGenerator).Assembly.Location,
				typeof (NameValueCollection).Assembly.Location,
				typeof (Enumerable).Assembly.Location,
				typeof (Binder).Assembly.Location,
			};
			foreach (var extension in extensions)
			{
				foreach (var assembly in extension.GetAssembliesToReference())
				{
					assemblies.Add(assembly);
				}
			}
			var compilerParameters = new CompilerParameters
			{
				GenerateExecutable = false,
				GenerateInMemory = false,
				IncludeDebugInformation = true,
			};
			foreach (var assembly in assemblies)
			{
				compilerParameters.ReferencedAssemblies.Add(assembly);
			}
			var compileAssemblyFromFile = provider.CompileAssemblyFromFile(compilerParameters, fileName);
			var results = compileAssemblyFromFile;

			if (results.Errors.HasErrors)
			{
				var sb = new StringBuilder()
					.AppendLine("Source code:")
					.AppendLine(queryText)
					.AppendLine();
				foreach (CompilerError error in results.Errors)
				{
					sb.AppendLine(error.ToString());
				}
				throw new InvalidOperationException(sb.ToString());
			}
			return results.CompiledAssembly.GetType(name);
		}
		public DynamicViewCompiler(string name, IndexDefinition indexDefinition, AbstractDynamicCompilationExtension[] extensions)
		{
			this.indexDefinition = indexDefinition;
			this.extensions = extensions;
			this.name = MonoHttpUtility.UrlEncode(name);
		}