Exemplo n.º 1
0
        public SourceCompilerResults compile(SourceCompilerParameters parameters, IProgressMonitor monitor)
        {
            this.parameters = parameters;
            if (parameters.FullBuild)
            {
                return(compileCore(monitor));
            }

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

                var results = compileCore(new SubProgressMonitor(monitor, 2));
                if (results.Failed)
                {
                    if (results.MissingType == null)
                    {
                        Environment.trace(this, "Errors: all dependent types rebuild required");
                        // Try to build with all the dependencies
                        parameters.ProgressiveBuild = false;
                        results = compileCore(new SubProgressMonitor(monitor, 3));
                    }
                    else
                    {
                        Environment.trace(this, "Missing type '" + results.MissingType + "': full rebuild");
                        parameters.FullBuild = true;
                        results = compileCore(new SubProgressMonitor(monitor, 3));
                    }
                }
                else
                {
                    // Check for type structural modifications
                    var oldDependencyInfo = parameters.DependencyInfo;
                    var newDependencyInfo = results.DependencyInfo;
                    var oldTypes          = results.CompiledFiles.selectMany(p => oldDependencyInfo.getFileContents(p)).toList();
                    var newTypes          = results.CompiledFiles.selectMany(p => newDependencyInfo.getFileContents(p)).toList();
                    var typesToRebuild    = Query.empty <String>();
                    foreach (var type in oldTypes.except(newTypes))
                    {
                        typesToRebuild = typesToRebuild.concat(oldDependencyInfo.getReferencingTypes(type));
                    }
                    foreach (var type in oldTypes.intersect(newTypes))
                    {
                        if (!JvmTypeSystemHelper.isStructurallyEqual(JvmTypeSystemHelper.getType(parameters.TypeSystem, type),
                                                                     JvmTypeSystemHelper.getType(results.TypeSystem, type)))
                        {
                            Environment.trace(this, "Structurally changed: " + type);
                            typesToRebuild = typesToRebuild.concat(oldDependencyInfo.getReferencingTypes(type));
                        }
                    }
                    var filesToRebuild = typesToRebuild.selectMany(p => oldDependencyInfo.getTypeLocations(p));
                    filesToRebuild = filesToRebuild.distinct().except(results.CompiledFiles).toList();
                    monitor.worked(1);

                    if (filesToRebuild.any())
                    {
                        Environment.trace(this, "Structural changes: referencing types rebuild required (" + filesToRebuild.count() + " files)");

                        parameters.ProgressiveBuild = false;
                        results = compileCore(new SubProgressMonitor(monitor, 3));

                        /* The compilation units are not using the current type system
                         *
                         * // If some types have changed, rebuild the referencing types
                         * var oldResult = results;
                         * this.parameters = new SourceCompilerParameters();
                         * foreach (var f in filesToRebuild) {
                         *      this.parameters.getFilesToCompile().add(f);
                         * }
                         * this.parameters.AllFiles.addAll(parameters.AllFiles.getAllResources());
                         * this.parameters.DependencyInfo = newDependencyInfo;
                         * this.parameters.ClassPath = parameters.ClassPath;
                         * this.parameters.TypeSystem = results.TypeSystem;
                         *
                         * results = compileCore(new SubProgressMonitor(monitor, 2));
                         *
                         * foreach (var e in oldResult.CompilationUnits.entrySet()) {
                         *      results.CompilationUnits[e.Key] = e.Value;
                         * }
                         * foreach (var e in oldResult.ClassFiles.entrySet()) {
                         *      results.ClassFiles[e.Key] = e.Value;
                         * }
                         * results.CompiledFiles.addAll(oldResult.CompiledFiles);
                         */
                    }
                    else
                    {
                        monitor.worked(2);
                    }
                }
                return(results);
            } finally {
                monitor.done();
            }
        }
        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);
        }
        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);
        }
		public SourceCompilerResults compile(SourceCompilerParameters parameters, IProgressMonitor monitor) {
			this.parameters = parameters;
			if (parameters.FullBuild) {
				return compileCore(monitor);
			}
			
			try {
				monitor.beginTask("", 5);
				
				var results = compileCore(new SubProgressMonitor(monitor, 2));
				if (results.Failed) {
					if (results.MissingType == null) {
						Environment.trace(this, "Errors: all dependent types rebuild required");
						// Try to build with all the dependencies
						parameters.ProgressiveBuild = false;
						results = compileCore(new SubProgressMonitor(monitor, 3));
					} else {
						Environment.trace(this, "Missing type '" + results.MissingType + "': full rebuild");
						parameters.FullBuild = true;
						results = compileCore(new SubProgressMonitor(monitor, 3));
					}
				} else {
					// Check for type structural modifications
					var oldDependencyInfo = parameters.DependencyInfo;
					var newDependencyInfo = results.DependencyInfo;
					var oldTypes = results.CompiledFiles.selectMany(p => oldDependencyInfo.getFileContents(p)).toList();
					var newTypes = results.CompiledFiles.selectMany(p => newDependencyInfo.getFileContents(p)).toList();
					var typesToRebuild = Query.empty<String>();
					foreach (var type in oldTypes.except(newTypes)) {
						typesToRebuild = typesToRebuild.concat(oldDependencyInfo.getReferencingTypes(type));
					}
					foreach (var type in oldTypes.intersect(newTypes)) {
						if (!JvmTypeSystemHelper.isStructurallyEqual(JvmTypeSystemHelper.getType(parameters.TypeSystem, type),
									JvmTypeSystemHelper.getType(results.TypeSystem, type))) {
							Environment.trace(this, "Structurally changed: " + type);
							typesToRebuild = typesToRebuild.concat(oldDependencyInfo.getReferencingTypes(type));
						}
					}
					var filesToRebuild = typesToRebuild.selectMany(p => oldDependencyInfo.getTypeLocations(p));
					filesToRebuild = filesToRebuild.distinct().except(results.CompiledFiles).toList();
					monitor.worked(1);
					
					if (filesToRebuild.any()) {
						Environment.trace(this, "Structural changes: referencing types rebuild required (" + filesToRebuild.count() + " files)");

						parameters.ProgressiveBuild = false;
						results = compileCore(new SubProgressMonitor(monitor, 3));
						
						/* The compilation units are not using the current type system
						
						// If some types have changed, rebuild the referencing types
						var oldResult = results;
						this.parameters = new SourceCompilerParameters();
						foreach (var f in filesToRebuild) {
							this.parameters.getFilesToCompile().add(f);
						}
						this.parameters.AllFiles.addAll(parameters.AllFiles.getAllResources());
						this.parameters.DependencyInfo = newDependencyInfo;
						this.parameters.ClassPath = parameters.ClassPath;
						this.parameters.TypeSystem = results.TypeSystem;
						
						results = compileCore(new SubProgressMonitor(monitor, 2));
						
						foreach (var e in oldResult.CompilationUnits.entrySet()) {
							results.CompilationUnits[e.Key] = e.Value;
						}
						foreach (var e in oldResult.ClassFiles.entrySet()) {
							results.ClassFiles[e.Key] = e.Value;
						}
						results.CompiledFiles.addAll(oldResult.CompiledFiles);
						*/
					} else {
						monitor.worked(2);
					}
				}
				return results;
			} finally {
				monitor.done();
			}
		}