Example #1
0
        private Type CompileTypeFromFile(OpenedFile file)
        {
            string sourceCode = file.GetContentsAsString();

            var projectFile = _extensionHost.CurrentSolution.FindProjectFile(file.FilePath);

            if (projectFile == null)
            {
                throw new FileNotFoundException("Cannot find project file " + file.FilePath.FullPath);
            }

            CompilerParameters parameters = new CompilerParameters()
            {
                GenerateInMemory   = true,
                GenerateExecutable = false,
            };

            // standard references.
            parameters.ReferencedAssemblies.AddRange(new string[] { "mscorlib.dll", "System.dll", "System.Drawing.dll", "System.Windows.Forms.dll" });

            CodeDomProvider provider = Language.CodeProvider;

            // validate source
            var sourceSnapshot = Language.CreateSourceSnapshot(sourceCode) as NetSourceSnapshot;

            if (sourceSnapshot.Types.Length != 1)
            {
                throw new ArgumentException("Source must contain exactly one type.");
            }

            string @namespace;

            if (sourceSnapshot.Namespaces.Length == 0)
            {
                // TODO: add temp namespace ....
                throw new NotImplementedException("Cannot handle classes without a namespace yet.");
            }
            else
            {
                @namespace = sourceSnapshot.Namespaces[0].Name;
            }

            // create stub class
            var writer    = new StringWriter();
            var stubClass = new CodeTypeDeclaration(sourceSnapshot.Types[0].Name)
            {
                IsPartial = true
            };

            stubClass.Members.Add(GenerateConstructor());

            // check if snapshot class doesn't have a base type.
            if (string.IsNullOrEmpty(sourceSnapshot.Types[0].ValueType))
            {
                // try to find in dependencies
                foreach (var dependency in file.Dependencies)
                {
                    if (Path.GetExtension(dependency) == file.FilePath.Extension)
                    {
                        var dependentFile      = _extensionHost.FileService.OpenFile(projectFile.FilePath.ParentDirectory.Combine(dependency));
                        var dependencySnapshot = Language.CreateSourceSnapshot(dependentFile.GetContentsAsString()) as NetSourceSnapshot;
                        if (dependencySnapshot.Types[0].Name == sourceSnapshot.Types[0].Name && !string.IsNullOrEmpty(dependencySnapshot.Types[0].ValueType))
                        {
                            var baseType = dependencySnapshot.GetTypeByName(dependencySnapshot.Types[0].ValueType);
                            if (baseType != null)
                            {
                                stubClass.BaseTypes.Add(baseType);
                                break;
                            }
                        }
                    }
                }
            }

            // write stub class
            var stubNamespace = new CodeNamespace(@namespace);

            stubNamespace.Types.Add(stubClass);
            provider.GenerateCodeFromNamespace(stubNamespace, writer, new CodeGeneratorOptions());


            // compile.
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceCode, writer.GetStringBuilder().ToString());

            if (results.Errors.Count == 0)
            {
                // find target type.
                foreach (Type type in results.CompiledAssembly.GetTypes())
                {
                    if (type.IsBasedOn(typeof(Component)))
                    {
                        return(type);
                    }
                }

                throw new Exception("The type based on " + typeof(Component).FullName + " was not found.");
            }
            else
            {
                throw new BuildException("The compiler did not succeed in compiling the source.", BuildResult.FromCompilerResults(results));
            }
        }