Search in sources :

Example 1 with VersionedFile

use of com.google.template.soy.shared.SoyAstCache.VersionedFile in project closure-templates by google.

the class SoyAstCacheTest method testGetSet.

@Test
public void testGetSet() {
    // Matching version.
    cache.put("foo", VersionedFile.of(fileNode1, version2));
    VersionedFile versionedFile = cache.get("foo", version2);
    assertThat(versionedFile.file().getId()).isEqualTo(0xdeadbeef);
    assertThat(versionedFile.file()).isNotSameAs(fileNode1);
    assertThat(versionedFile.version()).isEqualTo(version2);
    assertThat(cache.get("bar", version1)).isNull();
    versionedFile = cache.get("foo", version2);
    assertThat(versionedFile.file().getId()).isEqualTo(0xdeadbeef);
    assertThat(versionedFile.file()).isNotSameAs(fileNode1);
    assertThat(versionedFile.version()).isEqualTo(version2);
    // Non matching version.
    cache.put("foo", VersionedFile.of(fileNode1, version1));
    assertThat(cache.get("foo", version2)).isNull();
    assertThat(cache.get("bar", version1)).isNull();
}
Also used : VersionedFile(com.google.template.soy.shared.SoyAstCache.VersionedFile) Test(org.junit.Test)

Example 2 with VersionedFile

use of com.google.template.soy.shared.SoyAstCache.VersionedFile in project closure-templates by google.

the class SoyFileSetParser method parseWithVersions.

/**
 * Parses a set of Soy files, returning a structure containing the parse tree and template
 * registry.
 */
private ParseResult parseWithVersions() throws IOException {
    IdGenerator nodeIdGen = (cache() != null) ? cache().getNodeIdGenerator() : new IncrementingIdGenerator();
    SoyFileSetNode soyTree = new SoyFileSetNode(nodeIdGen.genId(), nodeIdGen);
    boolean filesWereSkipped = false;
    // generator but fail to lock on it.  Eliminate the id system to avoid this whole issue.
    synchronized (nodeIdGen) {
        // Avoid using the same ID generator in multiple threads.
        for (SoyFileSupplier fileSupplier : soyFileSuppliers().values()) {
            SoyFileSupplier.Version version = fileSupplier.getVersion();
            VersionedFile cachedFile = cache() != null ? cache().get(fileSupplier.getFilePath(), version) : null;
            SoyFileNode node;
            if (cachedFile == null) {
                node = parseSoyFileHelper(fileSupplier, nodeIdGen, typeRegistry());
                // a malformed parse tree.
                if (node == null) {
                    filesWereSkipped = true;
                    continue;
                }
                // Run passes that are considered part of initial parsing.
                passManager().runSingleFilePasses(node, nodeIdGen);
                // Run passes that check the tree.
                if (cache() != null) {
                    cache().put(fileSupplier.getFilePath(), VersionedFile.of(node, version));
                }
            } else {
                node = cachedFile.file();
            }
            soyTree.addChild(node);
        }
        TemplateRegistry registry;
        // Run passes that check the tree iff we successfully parsed every file.
        if (!filesWereSkipped) {
            registry = passManager().runWholeFilesetPasses(soyTree);
        } else {
            registry = new TemplateRegistry(soyTree, errorReporter());
        }
        return ParseResult.create(soyTree, registry);
    }
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IdGenerator(com.google.template.soy.base.internal.IdGenerator) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) VersionedFile(com.google.template.soy.shared.SoyAstCache.VersionedFile)

Aggregations

VersionedFile (com.google.template.soy.shared.SoyAstCache.VersionedFile)2 IdGenerator (com.google.template.soy.base.internal.IdGenerator)1 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)1 SoyFileSupplier (com.google.template.soy.base.internal.SoyFileSupplier)1 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1 Test (org.junit.Test)1