Search in sources :

Example 1 with ResourcePropertyStore

use of net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore 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 ResourcePropertyStore

use of net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore in project ow by vtst.

the class ClosureLinterLaunchConfigurationDelegate method getProcessBuilder.

protected ProcessBuilder getProcessBuilder(ILaunchConfiguration config, Fixture fixture) throws CoreException {
    IReadOnlyStore store = new LaunchConfigurationReadOnlyStore(config);
    ProcessBuilder pb = super.getProcessBuilder(config, fixture);
    List<String> list = new ArrayList<String>();
    // Command
    boolean fixLintErrors = record.fixLintErrors.get(store);
    if (fixLintErrors) {
        list.add(record.fixjsstyleCommand.get(store));
    } else {
        list.add(record.gjslintCommand.get(store));
    }
    // Options
    List<IResource> resources = record.inputResources.get(store);
    if (resources.isEmpty()) {
        throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, messages.getString("ClosureLinterLaunchConfigurationDelegate_noInputResource")));
    }
    IReadOnlyStore storeForLinterOptions = store;
    if (record.useProjectPropertiesForLinterChecks.get(store)) {
        IProject project = ClosureCompiler.getCommonProject(resources);
        if (project == null)
            throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, messages.getString("ClosureLinterLaunchConfigurationDelegate_differentProjects")));
        store = new ResourcePropertyStore(project, OwJsClosurePlugin.PLUGIN_ID);
    }
    String customJsDocTags = record.linterChecks.customJsdocTags.get(storeForLinterOptions);
    if (customJsDocTags.length() > 0) {
        list.add("--custom_jsdoc_tags");
        list.add(customJsDocTags);
    }
    Set<String> lintErrorChecks = record.linterChecks.lintErrorChecks.get(storeForLinterOptions);
    for (String lintErrorCheck : lintErrorChecks) {
        list.add("--jslint_error");
        list.add(lintErrorCheck);
    }
    list.add(record.linterChecks.strictClosureStyle.get(storeForLinterOptions) ? "--strict" : "--nostrict");
    list.add(record.linterChecks.missingJsdoc.get(storeForLinterOptions) ? "--jsdoc" : "--nojsdoc");
    if (!fixLintErrors)
        // Otherwise we have a special character in the output
        list.add("--nobeep");
    // Files
    for (IFile file : ClosureCompiler.getJavaScriptFiles(resources)) {
        list.add(file.getLocation().toOSString());
        clearProblemMarkers(file);
    }
    pb.command(list);
    return pb;
}
Also used : Status(org.eclipse.core.runtime.Status) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) LaunchConfigurationReadOnlyStore(net.vtst.eclipse.easy.ui.properties.stores.LaunchConfigurationReadOnlyStore) IProject(org.eclipse.core.resources.IProject) IReadOnlyStore(net.vtst.eclipse.easy.ui.properties.stores.IReadOnlyStore) CoreException(org.eclipse.core.runtime.CoreException) ResourcePropertyStore(net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore) IResource(org.eclipse.core.resources.IResource)

Example 3 with ResourcePropertyStore

use of net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore in project ow by vtst.

the class AbstractJSIncludesProvider method getLibraries.

public List<AbstractJSProject> getLibraries(AbstractCompiler compiler, IProgressMonitor monitor, ArrayList<IProject> projects) throws CoreException {
    List<AbstractJSProject> result = new ArrayList<AbstractJSProject>();
    Set<JSLibraryKey> keys = new HashSet<JSLibraryKey>();
    for (int i = projects.size() - 1; i >= 0; --i) {
        addLibraries(compiler, monitor, new ResourcePropertyStore(projects.get(i), OwJsClosurePlugin.PLUGIN_ID), keys, result);
    }
    return result;
}
Also used : AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) ResourcePropertyStore(net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 4 with ResourcePropertyStore

use of net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore in project ow by vtst.

the class AbstractJSIncludesProvider method getExterns.

public List<JSExtern> getExterns(AbstractCompiler compiler, IProgressMonitor monitor, ArrayList<IProject> projects) throws CoreException {
    List<JSExtern> result = new ArrayList<JSExtern>();
    Set<File> keys = new HashSet<File>();
    for (int i = projects.size() - 1; i >= 0; --i) {
        addExterns(compiler, monitor, new ResourcePropertyStore(projects.get(i), OwJsClosurePlugin.PLUGIN_ID), keys, result);
    }
    return result;
}
Also used : JSExtern(net.vtst.ow.closure.compiler.deps.JSExtern) ResourcePropertyStore(net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore) ArrayList(java.util.ArrayList) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with ResourcePropertyStore

use of net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore in project ow by vtst.

the class ClosureCompiler method isJavaScriptFile.

/**
   * Test whether a file is a JavaScript file (by looking at its content type).
   * @param file  The file to test.
   * @return  true iif the given file is a JavaScript file.
   * @throws CoreException
   */
public static boolean isJavaScriptFile(IFile file) throws CoreException {
    IContentDescription contentDescription = file.getContentDescription();
    if (contentDescription == null)
        return false;
    IContentType contentType = contentDescription.getContentType();
    if (!contentType.isKindOf(jsContentType))
        return false;
    if (ClosureFilePropertyRecord.getInstance().generatedByCompiler.get(new ResourcePropertyStore(file, OwJsClosurePlugin.PLUGIN_ID)))
        return false;
    return true;
}
Also used : ResourcePropertyStore(net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Aggregations

ResourcePropertyStore (net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore)5 ArrayList (java.util.ArrayList)4 File (java.io.File)2 HashSet (java.util.HashSet)2 IReadOnlyStore (net.vtst.eclipse.easy.ui.properties.stores.IReadOnlyStore)2 LaunchConfigurationReadOnlyStore (net.vtst.eclipse.easy.ui.properties.stores.LaunchConfigurationReadOnlyStore)2 AbstractJSProject (net.vtst.ow.closure.compiler.deps.AbstractJSProject)2 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 Status (org.eclipse.core.runtime.Status)2 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 Compiler (com.google.javascript.jscomp.Compiler)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 CircularDependencyException (com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1