/*
         * Compile a web handler file into a Type.  Result is cached.
         */
        /// <include file='doc\SimpleWebHandlerParser.uex' path='docs/doc[@for="SimpleWebHandlerParser.GetCompiledTypeFromCache"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        protected Type GetCompiledTypeFromCache()
        {
            _sourceCompiler = new SourceCompiler(_context, _virtualPath);

            // First, see if it's cached
            Type t = _sourceCompiler.GetTypeFromCache();

            if (t != null)
            {
                return(t);
            }

            CompilationLock.GetLock();

            try {
                // Try the cache again
                t = _sourceCompiler.GetTypeFromCache();
                if (t != null)
                {
                    return(t);
                }

                return(GetCompiledType());
            }
            finally {
                CompilationLock.ReleaseLock();
            }
        }
        /*
         * Compile a source file into an assembly, and import it
         */
        private void ImportSourceFile(string virtualPath)
        {
            // Get a full path to the source file
            string baseVirtualDir  = UrlPath.GetDirectory(_virtualPath);
            string fullVirtualPath = UrlPath.Combine(baseVirtualDir, virtualPath);
            string physicalPath    = _context.Request.MapPath(fullVirtualPath, null, false /*allowCrossAppMapping*/);

            // Add the source file to the list of files we depend on
            AddSourceDependency(physicalPath);

            CompilerInfo compilerInfo = CompilationConfiguration.GetCompilerInfoFromFileName(
                _context, physicalPath);

            CompilerParameters compilParams = compilerInfo.CompilParams;

            // Compile it into an assembly

            Assembly a = SourceCompiler.GetAssemblyFromSourceFile(_context, fullVirtualPath,
                                                                  null /*assemblies*/, compilerInfo.CompilerType, compilParams);

            // Add a dependency to the assembly
            AddAssemblyDependency(a);
        }
示例#3
0
 internal SourceCodeGenerator(ClassLoader parentClassLoader, Configuration configuration, SourceCompiler compiler) : base(parentClassLoader)
 {
     this._configuration = configuration;
     this._compiler      = compiler;
 }
 public void SetUp()
 {
     _compiler = new SourceCompiler();
 }
        private IProject[] buildProject(IProgressMonitor monitor)
        {
            Environment.trace(this, "Full build");
            var t0 = System.nanoTime();

            try {
                monitor.beginTask(Messages.fullBuildTaskName, 4);

                var libs      = projectManager.Properties.Libraries.where (p => p.Enabled);
                var libFiles  = libs.select(p => projectManager.Project.getFile(Path.fromPortableString(p.Path))).where (p => p.exists()).toList();
                int i         = libFiles.size();
                var classPath = new String[i];
                foreach (var file in libFiles)
                {
                    classPath[--i] = file.getLocation().toOSString();
                }

                var parameters = new SourceCompilerParameters {
                    FullBuild = true, ClassPath = classPath
                };
                parameters.AllFiles.addAll(projectManager.getSourceFiles());
                foreach (var s in projectManager.Properties.PreprocessorSymbols)
                {
                    parameters.PreprocessorSymbols.add(s);
                }

                var compiler = new SourceCompiler();
                var results  = compiler.compile(parameters, new SubProgressMonitor(monitor, 3));
                if (results.Failed)
                {
                    if (results.MissingType != null)
                    {
                        projectManager.DependencyInfo = null;
                        IMarker marker = getProject().getFile(".stabproperties").createMarker(IMarker.PROBLEM);
                        marker.setAttribute(IMarker.MESSAGE, results.MissingType);
                        marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                        marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
                    }
                    else
                    {
                        projectManager.FilesToCompile = results.CompiledFiles;
                    }
                }
                else
                {
                    var t1 = System.nanoTime();

                    var outputFolder = getProject().getFolder(projectManager.Properties.OutputPath);
                    if (outputFolder.exists())
                    {
                        outputFolder.accept(p => {
                            if (p.getType() == IResource.FILE)
                            {
                                if (p.getName().endsWith(".class"))
                                {
                                    p.requestResource().delete(IResource.FORCE, null);
                                }
                                return(false);
                            }
                            return(true);
                        }, IResource.NONE);
                    }
                    else
                    {
                        outputFolder.create(IResource.FORCE, true, null);
                    }
                    foreach (var e in results.ClassFiles.entrySet())
                    {
                        var fileName = e.Key.replace('.', '/') + ".class";
                        var file     = outputFolder.getFile(fileName);
                        EclipseHelper.createFolders(file);
                        file.create(new ByteArrayInputStream(e.Value), IResource.FORCE, null);
                    }

                    Environment.trace(this, results.ClassFiles.size() + " .class files saved in " + ((System.nanoTime() - t1) / 1e6) + "ms");

                    projectManager.DependencyInfo      = results.DependencyInfo;
                    projectManager.ClassPath           = classPath;
                    projectManager.TypeSystem          = results.TypeSystem;
                    projectManager.AnnotatedTypeSystem = results.AnnotatedTypeSystem;
                    projectManager.CompilationUnits    = results.CompilationUnits;
                    projectManager.FilesToCompile      = Query.empty();

                    Environment.fireProjectBuildEvent(projectManager);
                }
                updateMarkers(results.CodeErrors, parameters.AllFiles);

                monitor.worked(1);
            } catch (InterruptedException) {
            } catch (Exception e) {
                org.eclipse.ui.PlatformUI.getWorkbench().getDisplay().syncExec(
                    () => MessageDialog.openError(org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                                                  "Build error", e.getMessage()));
            } finally {
                monitor.done();
            }

            Environment.trace(this, "Full build done in " + ((System.nanoTime() - t0) / 1e6) + "ms");
            return(null);
        }
        private IProject[] buildDelta(IResourceDelta delta, IProgressMonitor monitor)
        {
            Environment.trace(this, "Incremental build");
            var t0 = System.nanoTime();

            try {
                monitor.beginTask("", 5);

                var filesToCompile = EclipseHelper.getModifiedFiles(delta, Query.singleton("stab"), Query.singleton("bin"));
                var deletedFiles   = filesToCompile.where (p => !p.exists()).select(p => p.getProjectRelativePath().toPortableString());
                Environment.trace(this, filesToCompile.count() + " files to compile");

                var parameters = new SourceCompilerParameters {
                    ProgressiveBuild = true,
                    ClassPath        = projectManager.ClassPath,
                    TypeSystem       = projectManager.TypeSystem,
                    DependencyInfo   = projectManager.DependencyInfo
                };
                parameters.AllFiles.addAll(projectManager.getSourceFiles());
                parameters.AllFiles.addAll(filesToCompile);
                foreach (var f in filesToCompile)
                {
                    parameters.FilesToCompile.add(parameters.AllFiles.getProjectRelativeName(f));
                }
                foreach (var f in projectManager.FilesToCompile)
                {
                    parameters.FilesToCompile.add(f);
                }
                foreach (var s in projectManager.Properties.PreprocessorSymbols)
                {
                    parameters.PreprocessorSymbols.add(s);
                }

                var compiler = new SourceCompiler();
                var results  = compiler.compile(parameters, new SubProgressMonitor(monitor, 3));
                if (results.Failed)
                {
                    if (results.MissingType != null)
                    {
                        projectManager.DependencyInfo = null;
                        IMarker marker = getProject().getFile(".stabproperties").createMarker(IMarker.PROBLEM);
                        marker.setAttribute(IMarker.MESSAGE, results.MissingType);
                        marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                        marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
                    }
                    else
                    {
                        projectManager.FilesToCompile = projectManager.FilesToCompile.union(results.CompiledFiles).toList();
                    }
                }
                else
                {
                    // Update the .class files
                    var outputFolder = getProject().getFolder(projectManager.Properties.OutputPath);
                    foreach (var typeName in results.CompiledFiles.selectMany(p => results.DependencyInfo.getFileContents(p)))
                    {
                        var file = outputFolder.getFile(typeName + ".class");
                        if (!file.exists())
                        {
                            continue;
                        }
                        var type = JvmTypeSystemHelper.getType(results.TypeSystem, typeName);
                        var declaringTypeName = type.Name;
                        var declaringType     = type;
                        while ((declaringType = declaringType.DeclaringType) != null)
                        {
                            declaringTypeName = declaringType.Name + "$" + declaringTypeName;
                        }
                        // Deletes all the nested types of the declaring type
                        file.getParent().accept(p => {
                            if (p.getType() == IResource.FILE)
                            {
                                if (p.getName().startsWith(declaringTypeName + "$"))
                                {
                                    p.requestResource().delete(IResource.FORCE, null);
                                }
                                return(false);
                            }
                            return(true);
                        }, IResource.NONE);
                        file.delete(IResource.FORCE, null);
                    }
                    foreach (var e in results.ClassFiles.entrySet())
                    {
                        var fileName = e.Key.replace('.', '/') + ".class";
                        var file     = outputFolder.getFile(fileName);
                        EclipseHelper.createFolders(file);
                        file.create(new ByteArrayInputStream(e.Value), IResource.FORCE, null);
                    }

                    // TODO: delete the .class generated from the types contained in deletedFiles

                    projectManager.DependencyInfo      = results.DependencyInfo;
                    projectManager.TypeSystem          = results.TypeSystem;
                    projectManager.AnnotatedTypeSystem = results.AnnotatedTypeSystem;
                    projectManager.FilesToCompile      = Query.empty();

                    var compilationUnits = new HashMap <String, CompilationUnitNode>(projectManager.CompilationUnits);
                    foreach (var e in results.CompilationUnits.entrySet())
                    {
                        compilationUnits[e.Key] = e.Value;
                    }
                    foreach (var s in deletedFiles)
                    {
                        compilationUnits.remove(s);
                    }
                    projectManager.CompilationUnits = compilationUnits;

                    Environment.fireProjectBuildEvent(projectManager);
                }
                updateMarkers(results.CodeErrors, parameters.AllFiles);

                monitor.worked(1);
            } catch (InterruptedException) {
            } finally {
                monitor.done();
            }

            Environment.trace(this, "Incremental build done in " + ((System.nanoTime() - t0) / 1e6) + "ms");
            return(null);
        }