Example #1
0
		public void Load(ProjectItem item)
		{
			// Split the logic of this method into smaller pieces
			// Wrap with Try/Catch and print any exceptions (if debug build etc)
			// Handle missing references/includes better - print out what is being loaded and if some path is not found

			

			if (item == null)
				throw new ArgumentNullException("item");
			
			// If this is public then check if it is a Jiss file here ... else make private ?
			// Right now every method that calls it checks it instead ?

			// Check if item is ProjectFile or IsJiss here ?
			//if (!item.IsProjectFile() or IsJiss())
			//    return;



			var filename = item.FileNames[0];	// create extension method GetFullName ?

			// Load the file and create a reader
			JissFile script;
			IJissReader reader;
			try
			{
				script = JissFile.Create(filename);
				reader = new JissReader(script);		// Should reader take in filename and store a target property to get Language etc ? or just TargetLanguage property?
			}
			catch (Exception)
			{
				Report("Error occured when trying to load file: " + filename);		// Could be one of the includes - better error handling
				return;
			}

			// Create Provider/Compiler
			CompilerResults result;
			using (var provider = CompilerHelper.CreateProvider(script.Language, CompilerVersion.v40))
			{
				// Handle references
				var references = Helper.GetDefaultReferences().ToList();
				references.AddRange(reader.GetGacs());
				references.AddRange(reader.GetReferences());

				// Create parameters
				var parameters = CompilerHelper.CreateParameters(references, true, false, false);
				parameters.SetGenerateInMemory();

				// Compile
				result = provider.CompileAssemblyFromSource(parameters, reader.GetSources().ToArray());
			}

			// What about warnings ?
			if (result.Errors.HasErrors)
			{
				string errors = "";

				foreach (CompilerError error in result.Errors)
				{
					errors += error.ErrorText + Environment.NewLine;
				}

				Report(errors);	// Show errors in the Error List instead !?
				
				return;
			}



			// Create the resolver for the referenced assemblies

			var assemblyReferences = result.CompiledAssembly.GetReferencedAssemblies();
			var readerReferences = reader.GetReferences();

			Dictionary<string, string> assemblyNames = new Dictionary<string, string>();	// Contains AssemblyName.FullName / Full reference
			foreach (var reference in readerReferences)
			{
				var assName = AssemblyName.GetAssemblyName(reference);

				foreach (var assReference in assemblyReferences)
				{
					if (assName.FullName == assReference.FullName)
						assemblyNames.Add(assName.FullName, reference);
				}
			}

			var jissAssName = typeof(IEventScript).Assembly.FullName;	// add to assemblyNames + reference ?
			var resolver = new ResolveEventHandler((obj, args) =>
												   {
													   if (args.RequestingAssembly == result.CompiledAssembly)
													   {
														   string fullReference;
														   if (assemblyNames.TryGetValue(args.Name, out fullReference))
															   return Assembly.LoadFrom(fullReference);

														   // Move this into seperate global resolver ?
														   if (args.Name == jissAssName)
															   return Assembly.Load(args.Name);	// use AssemblyName instead ?
													   }

													   return null;
												   });

			// Only attach (and create) resolver if readerReferences.Any() ? requires seperate resolver for Dynamo.Jiss assembly then...
			AppDomain.CurrentDomain.AssemblyResolve += resolver;



			// Get all types/classes which implement the IEventScript interface
			var types = result.CompiledAssembly.GetTypesAssignableTo<IEventScript>();

			// Create an instance for each type found and call setup
			var eventModel = new EventModel();
			var instances = new List<IEventScript>();
			foreach (var type in types)
			{
				IEventScript instance;
				try
				{
					instance = (IEventScript)Activator.CreateInstance(type);
				}
				catch (Exception)
				{
					Report("Error occured when trying to create an instance of type: " + type.Name + Environment.NewLine + "Please check that the type have an empty public constructor.");
					continue;
				}

				try
				{
					instance.Setup(item, eventModel);
				}
				catch (Exception)
				{
					Report("Error occured in the Setup() method for type: " + type.Name);
					continue;
				}

				instances.Add(instance);
			}

			// Add to index if any instances was created
			if (instances.Any())
			{
				_index.Add(item, eventModel, instances, resolver);
			}
			else
			{
				AppDomain.CurrentDomain.AssemblyResolve -= resolver;
			}
		}
Example #2
0
		private static void Main(string[] args)
		{
			//// Trying with relative path
			//references.Add(@"C:\Users\Martin\Projects\Dynamo.Jiss\Dynamo.Jiss.Console\bin\..\bin\Debug\Dynamo.Jiss.dll");	// would this work ?



			var targetFilename = @"C:\Users\Martin\Desktop\Mess\TestMvcApplication\TestMvcApplication\Whatever.jiss.cs";

			var jiss = JissFile.Create(targetFilename);
			var reader = new JissReader(jiss);
			var test1 = reader.GetReferences();
			var test2 = reader.GetSources();

			CompilerResults result;
			using (var provider = CompilerHelper.CreateProvider(jiss.Language, CompilerVersion.v40))
			{
				// Handle references
				var references = ReferenceHelper.GetFrameworkReferences().ToList();
				//references.AddRange(ReferenceHelper.GetVisualStudioReferences());
				references.Add(typeof(IEventScript).Assembly.Location);

				references.AddRange(reader.GetReferences());	// ?

				// Create helper that takes parameters in when creating the provider ?
				var parameters = CompilerHelper.CreateParameters(references.ToArray());	// take in IEnumerable<string> ?
				parameters.SetGenerateInMemory();
				
				result = provider.CompileAssemblyFromSource(parameters, reader.GetSources().ToArray());	// Read all the sources output as string[] or IEnumerable<string> !
			}

			if (result.Errors.HasErrors)
			{
				string errors = "";

				foreach (CompilerError error in result.Errors)
				{
					// Show errors in the Error List !???
					errors += error.ErrorText + Environment.NewLine; // Include more options - should show in ErrorList?
				}

				Console.WriteLine(errors);
				return;
			}



			//var ref1 = result.CompiledAssembly.GetReferencedAssemblies();
			//var what2 = Assembly.GetEntryAssembly();



			var types = result.CompiledAssembly.GetTypesAssignableTo<IEventScript>();
			foreach (var type in types)
			{
				var instance = (IEventScript)Activator.CreateInstance(type);
				instance.Setup(null, null);
			}



			System.Console.ReadLine();
		}