Search in sources :

Example 1 with JSProject

use of net.vtst.ow.closure.compiler.deps.JSProject in project ow by vtst.

the class ClosureCompilerLaunchConfigurationDelegate method launch.

@Override
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    monitor.beginTask(config.getName(), 1);
    monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_prepareCompiler"));
    IReadOnlyStore store = new LaunchConfigurationReadOnlyStore(config);
    List<IResource> resources = record.inputResources.get(store);
    if (resources.isEmpty())
        return;
    // Getting the stores for project configurations
    IProject project = null;
    if (record.useProjectPropertiesForChecks.get(store) || record.useProjectPropertiesForIncludes.get(store)) {
        project = ClosureCompiler.getCommonProject(resources);
        if (project == null)
            throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, messages.getString("ClosureCompilerLaunchConfigurationDelegate_differentProjects")));
    }
    IReadOnlyStore storeForChecks = record.useProjectPropertiesForChecks.get(store) ? new ResourcePropertyStore(project, OwJsClosurePlugin.PLUGIN_ID) : store;
    IReadOnlyStore storeForIncludes = record.useProjectPropertiesForIncludes.get(store) ? new ResourcePropertyStore(project, OwJsClosurePlugin.PLUGIN_ID) : store;
    // Get the output file
    IFile outputFile = getOutputFile(store, resources);
    // Create and configure the compiler
    ClosureCompilerProcess process = new ClosureCompilerProcess(launch);
    Compiler compiler = CompilerUtils.makeCompiler(process.getErrorManager());
    CompilerOptions options = ClosureCompilerOptions.makeForLaunch(storeForChecks, store);
    compiler.initOptions(options);
    // Get the files to compile
    Set<IFile> allFiles, rootFiles;
    List<AbstractJSProject> libraries;
    if (record.manageClosureDependencies.get(store)) {
        // If dependencies are managed, we take all projects containing selected resources,
        // then all their referenced projects.
        // TODO: It should not be allowed to customize the includes in this case, we should always
        // use the project ones.
        Collection<IProject> projects = getProjects(resources);
        Comparator<IProject> comparator = OwJsClosurePlugin.getDefault().getProjectOrderManager().get().reverseOrderComparator();
        ArrayList<IProject> allProjects = ClosureCompiler.getReferencedJavaScriptProjectsRecursively(projects, comparator);
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_loadLibraries"));
        libraries = includesProvider.getLibraries(compiler, monitor, allProjects);
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_prepareCompiler"));
        allFiles = ClosureCompiler.getJavaScriptFilesOfProjects(allProjects);
        for (IResource resource : resources) {
            if (!(resource instanceof IProject))
                allFiles.addAll(ClosureCompiler.getJavaScriptFiles(resource));
        }
        rootFiles = Utils.getAllContainedFilesWhichAreInSet(resources, allFiles);
    } else {
        // If dependencies are not managed, we take only what has been selected.
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_loadLibraries"));
        libraries = includesProvider.getLibraries(compiler, monitor, storeForIncludes);
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_prepareCompiler"));
        allFiles = ClosureCompiler.getJavaScriptFiles(resources);
        rootFiles = allFiles;
    }
    // Build the project to compile
    File closureBasePath = ClosureCompiler.getPathOfClosureBase(storeForIncludes);
    Map<IFile, JSUnit> units = makeJSUnits(closureBasePath, allFiles);
    List<JSUnit> rootUnits = new ArrayList<JSUnit>(rootFiles.size());
    for (IFile selectedJsFile : rootFiles) rootUnits.add(units.get(selectedJsFile));
    try {
        JSProject jsProject = makeJSProject(compiler, Lists.newArrayList(units.values()), libraries, closureBasePath);
        List<JSUnit> rootUnitsWithTheirDependencies = jsProject.getSortedDependenciesOf(rootUnits);
        JSModule module = new JSModule("main");
        for (JSUnit unit : rootUnitsWithTheirDependencies) module.add(new CompilerInput(unit.getAst(false)));
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_runCompiler"));
        compiler.compileModules(getExterns(compiler, monitor, storeForIncludes), Collections.singletonList(module), options);
        if (outputFile.exists()) {
            outputFile.setContents(new ByteArrayInputStream(compiler.toSource().getBytes("UTF-8")), false, false, monitor);
        } else {
            outputFile.create(new ByteArrayInputStream(compiler.toSource().getBytes("UTF-8")), false, monitor);
        }
        outputFile.setCharset("UTF-8", monitor);
        ClosureFilePropertyRecord.getInstance().generatedByCompiler.set(new ResourcePropertyStore(outputFile, OwJsClosurePlugin.PLUGIN_ID), true);
        process.setTerminated();
        monitor.done();
    } catch (CircularDependencyException e) {
        throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, e.getLocalizedMessage(), e));
    } catch (IOException e) {
        throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, e.getLocalizedMessage(), e));
    }
}
Also used : AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) JSProject(net.vtst.ow.closure.compiler.deps.JSProject) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) LaunchConfigurationReadOnlyStore(net.vtst.eclipse.easy.ui.properties.stores.LaunchConfigurationReadOnlyStore) CircularDependencyException(com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException) Status(org.eclipse.core.runtime.Status) AbstractCompiler(com.google.javascript.jscomp.AbstractCompiler) ClosureCompiler(net.vtst.ow.eclipse.js.closure.compiler.ClosureCompiler) Compiler(com.google.javascript.jscomp.Compiler) IOException(java.io.IOException) JSUnit(net.vtst.ow.closure.compiler.deps.JSUnit) IProject(org.eclipse.core.resources.IProject) IReadOnlyStore(net.vtst.eclipse.easy.ui.properties.stores.IReadOnlyStore) CompilerInput(com.google.javascript.jscomp.CompilerInput) CoreException(org.eclipse.core.runtime.CoreException) AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourcePropertyStore(net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) ClosureCompilerOptions(net.vtst.ow.eclipse.js.closure.compiler.ClosureCompilerOptions) JSModule(com.google.javascript.jscomp.JSModule) IFile(org.eclipse.core.resources.IFile) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Example 2 with JSProject

use of net.vtst.ow.closure.compiler.deps.JSProject in project ow by vtst.

the class ClosureCompilerLaunchConfigurationDelegate method makeJSProject.

private JSProject makeJSProject(AbstractCompiler compiler, List<JSUnit> units, List<AbstractJSProject> libraries, File closureBasePath) throws CircularDependencyException {
    JSProject jsProject = new JSProject();
    jsProject.setUnits(compiler, units);
    jsProject.setReferencedProjects(libraries);
    return jsProject;
}
Also used : AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) JSProject(net.vtst.ow.closure.compiler.deps.JSProject)

Example 3 with JSProject

use of net.vtst.ow.closure.compiler.deps.JSProject in project ow by vtst.

the class ResourceProperties method getOrCreateJSProject.

/**
 * Get the {@code JSProject} associated with a resource project, or create it if it does not exist.
 * Synchronized.
 */
public static JSProject getOrCreateJSProject(IProject project) throws CoreException {
    JSProject jsProject = getJSProject(project);
    if (jsProject == null) {
        synchronized (project) {
            jsProject = getJSProject(project);
            if (jsProject == null) {
                jsProject = new JSProject();
                setJSProject(project, jsProject);
            }
        }
    }
    return jsProject;
}
Also used : JSProject(net.vtst.ow.closure.compiler.deps.JSProject)

Example 4 with JSProject

use of net.vtst.ow.closure.compiler.deps.JSProject in project ow by vtst.

the class ClosureBuilder method incrementalBuild.

/**
 * Do an incremental build of a project.  Reverts to a full build if an incremental build
 * cannot be done.
 * @param monitor  This method reports two units of work.
 * @param project  The project to build.
 * @param delta  The delta since the last build.
 * @throws CoreException
 */
private void incrementalBuild(IProgressMonitor monitor, IProject project, IResourceDelta delta) throws CoreException {
    // If the project is not already known by the builder, a full build is required.
    JSProject jsProject = ResourceProperties.getJSProject(project);
    Collection<IFile> files = ResourceProperties.getJavaScriptFiles(project);
    if (jsProject == null || files == null) {
        fullBuild(monitor, project);
        return;
    }
    // Visit the deltas.
    ResourceDeltaVisitorForIncrementalBuild visitor = new ResourceDeltaVisitorForIncrementalBuild(files);
    delta.accept(visitor);
    if (visitor.fullBuildRequired()) {
        fullBuild(monitor, project);
    } else {
        monitor.worked(1);
        compileJavaScriptFiles(monitor, project, files, false);
    }
}
Also used : AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) JSProject(net.vtst.ow.closure.compiler.deps.JSProject) IFile(org.eclipse.core.resources.IFile)

Example 5 with JSProject

use of net.vtst.ow.closure.compiler.deps.JSProject in project ow by vtst.

the class ClosureBuilder method fullBuild.

// **************************************************************************
// Full build
/**
 * Perform a full build of a project.
 * @param monitor  The project monitor.  This methods reports two units of work.
 * @param project  The project to build.
 * @throws CoreException, OperationCanceledException
 */
private void fullBuild(IProgressMonitor monitor, IProject project) throws CoreException {
    monitor.subTask(messages.format("build_prepare"));
    boolean forgetIfCanceled = true;
    Compiler compiler = CompilerUtils.makeCompiler(new ErrorManagerForProjectBuild(project));
    compiler.initOptions(CompilerUtils.makeOptionsForParsingAndErrorReporting());
    try {
        File pathOfClosureBase = ClosureCompiler.getPathOfClosureBase(project);
        if (pathOfClosureBase == null) {
            monitor.worked(1);
            return;
        }
        // Create or get the project
        JSProject jsProject = ResourceProperties.getOrCreateJSProject(project);
        boolean jsProjectUpdated = updateJSProjectIfNeeded(monitor, compiler, project, jsProject);
        // Set the compilation units
        Set<IFile> files = ClosureCompiler.getJavaScriptFilesOfProject(project);
        ResourceProperties.setJavaScriptFiles(project, files);
        List<CompilableJSUnit> units = new ArrayList<CompilableJSUnit>(files.size());
        for (IFile file : files) {
            Utils.checkCancel(monitor);
            CompilableJSUnit unit = ResourceProperties.getJSUnit(file);
            if (unit == null) {
                unit = new CompilableJSUnit(jsProject, file.getLocation().toFile(), pathOfClosureBase, new CompilationUnitProviderFromEclipseIFile(file));
                ResourceProperties.setJSUnit(file, unit);
            } else {
                if (jsProjectUpdated)
                    unit.clear();
            }
            units.add(unit);
        }
        try {
            jsProject.setUnits(compiler, units);
            monitor.worked(1);
            forgetIfCanceled = false;
            compileJavaScriptFiles(monitor, project, files, false);
        } catch (CircularDependencyException e) {
            CompilerUtils.reportError(compiler, JSError.make(CIRCULAR_DEPENDENCY_ERROR, e.getMessage()));
            forgetLastBuiltState();
        }
    } catch (OperationCanceledException e) {
        if (forgetIfCanceled)
            forgetLastBuiltState();
        throw e;
    } finally {
        compiler.getErrorManager().generateReport();
    }
}
Also used : ClosureCompiler(net.vtst.ow.eclipse.js.closure.compiler.ClosureCompiler) Compiler(com.google.javascript.jscomp.Compiler) AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) JSProject(net.vtst.ow.closure.compiler.deps.JSProject) IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) CompilableJSUnit(net.vtst.ow.closure.compiler.compile.CompilableJSUnit) IFile(org.eclipse.core.resources.IFile) File(java.io.File) CircularDependencyException(com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)

Aggregations

JSProject (net.vtst.ow.closure.compiler.deps.JSProject)5 AbstractJSProject (net.vtst.ow.closure.compiler.deps.AbstractJSProject)4 IFile (org.eclipse.core.resources.IFile)3 Compiler (com.google.javascript.jscomp.Compiler)2 CircularDependencyException (com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ClosureCompiler (net.vtst.ow.eclipse.js.closure.compiler.ClosureCompiler)2 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 CompilerInput (com.google.javascript.jscomp.CompilerInput)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 JSModule (com.google.javascript.jscomp.JSModule)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 IReadOnlyStore (net.vtst.eclipse.easy.ui.properties.stores.IReadOnlyStore)1 LaunchConfigurationReadOnlyStore (net.vtst.eclipse.easy.ui.properties.stores.LaunchConfigurationReadOnlyStore)1 ResourcePropertyStore (net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore)1 CompilableJSUnit (net.vtst.ow.closure.compiler.compile.CompilableJSUnit)1 JSUnit (net.vtst.ow.closure.compiler.deps.JSUnit)1