use of com.google.javascript.jscomp.JSModule 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));
}
}
use of com.google.javascript.jscomp.JSModule in project ow by vtst.
the class MainForDebug method measureTime.
public static void measureTime() {
final ArrayList<File> listFiles = new ArrayList<File>();
final ArrayList<JSSourceFile> listSourceFiles = new ArrayList<JSSourceFile>();
final ArrayList<AstFactoryFromModifiable> listAsts = new ArrayList<AstFactoryFromModifiable>();
FileTreeVisitor.Simple<RuntimeException> visitor = new FileTreeVisitor.Simple<RuntimeException>() {
public void visitFile(File file) {
if (!CompilerUtils.isJavaScriptFile(file))
return;
listFiles.add(file);
JSSourceFile sourceFile = JSSourceFile.fromFile(file);
listSourceFiles.add(sourceFile);
// listAsts.add(new JsAstFactoryFromFile(file));
}
};
visitor.visit(new File("/home/vtst/test/out/goog"));
System.out.println(listAsts.size());
long t0 = System.nanoTime();
long t1 = 0;
for (int i = 0; i < 10; ++i) {
if (i == 1)
t1 = System.nanoTime();
JSModule module = new JSModule("main");
for (AstFactoryFromModifiable ast : listAsts) {
module.add(new CompilerInput(ast.getClone(false)));
}
// for (JSSourceFile sourceFile: listSourceFiles) {
// module.add(new CompilerInput(sourceFile));
// }
// for (File file: listFiles) {
// module.add(new CompilerInput(JSSourceFile.fromFile(file)));
// }
Compiler compiler = CompilerUtils.makeCompiler(CompilerUtils.makePrintingErrorManager(System.out));
CompilerOptions options = CompilerUtils.makeOptionsForParsingAndErrorReporting();
options.checkTypes = true;
compiler.compile(new JSSourceFile[] {}, new JSModule[] { module }, options);
System.out.println(compiler.toSource().length());
}
long tf = System.nanoTime();
System.out.println((tf - t0) * 1e-9);
System.out.println((tf - t1) * 1e-9);
System.out.println("DONE");
}
use of com.google.javascript.jscomp.JSModule in project ow by vtst.
the class CompilerRun method compile.
/**
* Run the initial compilation.
*/
private void compile(ErrorManager errorManager) {
// Set-up the compiler
compiler = CompilerUtils.makeCompiler(errorManager);
compiler.initOptions(options);
passes = new DefaultPassConfig(options);
CompilerUtils.addCustomCompilerPass(options, new NamespaceProvidersPass(compiler, namespaceToScriptNode), CustomPassExecutionTime.BEFORE_CHECKS);
compiler.setPassConfig(passes);
// Store the modification info
lastModifiedMapForFullCompile = buildLastModifiedMap(sortedUnits);
lastModifiedMapForFastCompile = Maps.newHashMap(lastModifiedMapForFullCompile);
// Run compilation
JSModule module = buildJSModule();
// For avoiding the magic, we could do:
// compiler.compileModules(
// Collections.<SourceFile> emptyList(), Lists.newArrayList(DefaultExternsProvider.getAsModule(), module), options);
// but this would be less efficient.
MagicCompiler.compile(compiler, getExternsAsCompilerInputs(), module, options);
// Set-up post-compilation state
if (keepCompilationResultsInMemory) {
scopeCreator = new MagicScopeCreator(compiler);
} else {
compiler = null;
passes = null;
options = null;
}
}
Aggregations