com.google.gwt.core.ext.linker.ArtifactSet Java Examples

The following examples show how to use com.google.gwt.core.ext.linker.ArtifactSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientEngineLinker.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<Artifact<?>> doEmitCompilation(TreeLogger logger,
        LinkerContext context, CompilationResult result,
        ArtifactSet artifacts) throws UnableToCompleteException {

    String[] js = result.getJavaScript();
    if (js.length != 1) {
        logger.branch(TreeLogger.ERROR,
                "The module must not have multiple fragments when using the "
                        + getDescription() + " Linker.",
                null);
        throw new UnableToCompleteException();
    }

    ArrayList<Artifact<?>> toReturn = new ArrayList<>();
    toReturn.add(new Script(result.getStrongName(), js[0]));
    toReturn.addAll(
            emitSelectionInformation(result.getStrongName(), result));
    return toReturn;
}
 
Example #2
Source File: AppcacheLinker.java    From gwt-appcache with Apache License 2.0 6 votes vote down vote up
@Override
public ArtifactSet link( @Nonnull final TreeLogger logger,
                         @Nonnull final LinkerContext context,
                         @Nonnull final ArtifactSet artifacts,
                         final boolean onePermutation )
  throws UnableToCompleteException
{
  if ( onePermutation )
  {
    return perPermutationLink( logger, context, artifacts );
  }
  else
  {
    return perCompileLink( logger, context, artifacts );
  }
}
 
Example #3
Source File: AppcacheLinker.java    From gwt-appcache with Apache License 2.0 6 votes vote down vote up
@Nonnull
final ArtifactSet perPermutationLink( @Nonnull final TreeLogger logger,
                                      @Nonnull final LinkerContext context,
                                      @Nonnull final ArtifactSet artifacts )
  throws UnableToCompleteException
{
  final Permutation permutation = calculatePermutation( logger, context, artifacts );
  if ( null == permutation )
  {
    logger.log( Type.ERROR, "Unable to calculate permutation " );
    throw new UnableToCompleteException();
  }

  final ArtifactSet results = new ArtifactSet( artifacts );
  results.add( new PermutationArtifact( AppcacheLinker.class, permutation ) );
  return results;
}
 
Example #4
Source File: AppcacheLinkerTest.java    From gwt-appcache with Apache License 2.0 6 votes vote down vote up
@Test
public void getArtifactsForCompilation()
{
  final AppcacheLinker linker = new AppcacheLinker();
  final ArtifactSet artifacts1 = new ArtifactSet();
  final PermutationArtifact artifact1 = new PermutationArtifact( AppcacheLinker.class, new Permutation( "1" ) );
  final PermutationArtifact artifact2 = new PermutationArtifact( AppcacheLinker.class, new Permutation( "2" ) );
  artifacts1.add( artifact1 );
  artifacts1.add( new StandardGeneratedResource( "path1", new byte[ 0 ] ) );
  artifacts1.add( new StandardGeneratedResource( "path2", new byte[ 0 ] ) );
  final StandardGeneratedResource resource =
    new StandardGeneratedResource( "path3", new byte[ 0 ] );
  resource.setVisibility( Visibility.Private );
  artifacts1.add( resource );
  artifacts1.add( new StandardGeneratedResource( "compilation-mappings.txt", new byte[ 0 ] ) );
  artifacts1.add( new StandardGeneratedResource( "myapp.devmode.js", new byte[ 0 ] ) );
  artifacts1.add( artifact2 );
  final LinkerContext linkerContext = mock( LinkerContext.class );
  when( linkerContext.getModuleName() ).thenReturn( "myapp" );
  final Set<String> files =
    linker.getArtifactsForCompilation( linkerContext, artifacts1 );
  assertEquals( files.size(), 2 );
  assertTrue( files.contains( "myapp/path1" ) );
  assertTrue( files.contains( "myapp/path2" ) );
}
 
Example #5
Source File: GadgetLinker.java    From shortyz with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ArtifactSet link(TreeLogger logger, LinkerContext context,
    ArtifactSet artifacts) throws UnableToCompleteException {
    ArtifactSet toLink = new ArtifactSet(artifacts);

    // Mask the stub manifest created by the generator
    for (EmittedArtifact res : toLink.find(EmittedArtifact.class)) {
        if (res.getPartialPath().endsWith(".gadget.xml")) {
            manifestArtifact = res;
            toLink.remove(res);

            break;
        }
    }

    if (manifestArtifact == null) {
        if (artifacts.find(CompilationResult.class).isEmpty()) {
            // Maybe hosted mode or junit, defer to XSLinker.
            return new XSLinker().link(logger, context, toLink);
        } else {
            // When compiling for web mode, enforce that the manifest is present.
            logger.log(TreeLogger.ERROR,
                "No gadget manifest found in ArtifactSet.");
            throw new UnableToCompleteException();
        }
    }

    return super.link(logger, context, toLink);
}
 
Example #6
Source File: GadgetLinker.java    From shortyz with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected EmittedArtifact emitSelectionScript(TreeLogger logger,
    LinkerContext context, ArtifactSet artifacts)
    throws UnableToCompleteException {
    logger = logger.branch(TreeLogger.DEBUG, "Building gadget manifest",
            null);

    String bootstrap = "<script>" +
        context.optimizeJavaScript(logger,
            generateSelectionScript(logger, context, artifacts)) +
        "</script>\n" + "<div id=\"__gwt_gadget_content_div\"></div>";

    // Read the content
    StringBuffer manifest = new StringBuffer();

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(
                    manifestArtifact.getContents(logger)));

        for (String line = in.readLine(); line != null;
                line = in.readLine()) {
            manifest.append(line).append("\n");
        }

        in.close();
    } catch (IOException e) {
        logger.log(TreeLogger.ERROR, "Unable to read manifest stub", e);
        throw new UnableToCompleteException();
    }

    replaceAll(manifest, "__BOOTSTRAP__", bootstrap);

    return emitString(logger, manifest.toString(),
        manifestArtifact.getPartialPath());
}
 
Example #7
Source File: GadgetLinker.java    From shortyz with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected String generateSelectionScript(TreeLogger logger,
    LinkerContext context, ArtifactSet artifacts)
    throws UnableToCompleteException {
    StringBuffer scriptContents = new StringBuffer(super.generateSelectionScript(
                logger, context, artifacts));

    // Add a substitution for the GWT major release number. e.g. "1.6"
    int[] gwtVersions = getVersionArray();
    replaceAll(scriptContents, "__GWT_MAJOR_VERSION__",
        gwtVersions[0] + "." + gwtVersions[1]);

    return scriptContents.toString();
}
 
Example #8
Source File: ClientEngineLinker.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
protected String fillSelectionScriptTemplate(StringBuffer selectionScript,
        TreeLogger logger, LinkerContext context, ArtifactSet artifacts,
        CompilationResult result) throws UnableToCompleteException {
    String computeScriptBase;
    String processMetas;
    try {
        computeScriptBase = Utility
                .getFileFromClassPath(COMPUTE_SCRIPT_BASE_DOT_JS);
        processMetas = Utility.getFileFromClassPath(PROCESS_METAS_JS);
    } catch (IOException e) {
        logger.log(TreeLogger.ERROR,
                "Unable to read selection script template", e);
        throw new UnableToCompleteException();
    }
    replaceAll(selectionScript, "__COMPUTE_SCRIPT_BASE__",
            computeScriptBase);

    replaceAll(selectionScript, "__PROCESS_METAS__", processMetas);

    ResourceInjectionUtil.injectResources(selectionScript, artifacts);
    permutationsUtil.addPermutationsJs(selectionScript, logger, context);

    replaceAll(selectionScript, "__MODULE_FUNC__",
            context.getModuleFunctionName());
    replaceAll(selectionScript, "__MODULE_NAME__", context.getModuleName());
    replaceAll(selectionScript, "__HOSTED_FILENAME__", getHostedFilename());

    return selectionScript.toString();
}
 
Example #9
Source File: ClientEngineLinker.java    From flow with Apache License 2.0 5 votes vote down vote up
private Script getScript(TreeLogger logger, ArtifactSet artifacts)
        throws UnableToCompleteException {
    Set<Script> results = artifacts.find(Script.class);
    if (results.size() != 1) {
        logger.log(TreeLogger.ERROR,
                "The module must have exactly one distinct"
                        + " permutation when using the " + getDescription()
                        + " Linker; found " + results.size(),
                null);
        throw new UnableToCompleteException();
    }
    return results.iterator().next();
}
 
Example #10
Source File: ClientEngineLinker.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public ArtifactSet link(TreeLogger logger, LinkerContext context,
        ArtifactSet artifacts, boolean onePermutation)
                throws UnableToCompleteException {
    ArtifactSet result = super.link(logger, context, artifacts,
            onePermutation);
    if (!onePermutation) {
        result.add(emitStrongNamePropertyFile(logger, context, artifacts));
    }
    return result;
}
 
Example #11
Source File: ClientEngineLinker.java    From flow with Apache License 2.0 5 votes vote down vote up
private Artifact<?> emitStrongNamePropertyFile(TreeLogger logger,
        LinkerContext context, ArtifactSet artifacts)
                throws UnableToCompleteException {
    Script result = getScript(logger, artifacts);

    String contents = "jsFile=" + getJsFilename(context, result);
    return emitString(logger, contents, "compile.properties");
}
 
Example #12
Source File: AppcacheLinker.java    From gwt-appcache with Apache License 2.0 5 votes vote down vote up
@Nonnull
final Set<String> getArtifactsForCompilation( @Nonnull final LinkerContext context,
                                              @Nonnull final ArtifactSet artifacts )
{
  final Set<String> artifactNames = new HashSet<>();
  for ( final EmittedArtifact artifact : artifacts.find( EmittedArtifact.class ) )
  {
    if ( Visibility.Public == artifact.getVisibility() && shouldAddToManifest( artifact.getPartialPath() ) )
    {
      artifactNames.add( context.getModuleName() + "/" + artifact.getPartialPath() );
    }
  }
  return artifactNames;
}
 
Example #13
Source File: ClientEngineLinker.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
protected EmittedArtifact emitSelectionScript(TreeLogger logger,
        LinkerContext context, ArtifactSet artifacts)
                throws UnableToCompleteException {

    // Find the single Script result
    Script result = getScript(logger, artifacts);

    DefaultTextOutput out = new DefaultTextOutput(true);

    // Emit the selection script.
    String bootstrap = generateSelectionScript(logger, context, artifacts);
    bootstrap = context.optimizeJavaScript(logger, bootstrap);
    out.print(bootstrap);
    out.newlineOpt();

    // Emit the module's JS a closure.
    out.print("(function () {");
    out.newlineOpt();
    out.print("var $gwt_version = \"" + About.getGwtVersionNum() + "\";");
    out.newlineOpt();
    out.print("var $wnd = window;");
    out.newlineOpt();
    out.print("var $doc = $wnd.document;");
    out.newlineOpt();
    out.print("var $moduleName, $moduleBase;");
    out.newlineOpt();
    out.print(
            "var $stats = $wnd.__gwtStatsEvent ? function(a) {$wnd.__gwtStatsEvent(a)} : null;");
    out.newlineOpt();

    out.print("var $strongName = '" + result.getStrongName() + "';");
    out.newlineOpt();

    out.print(result.getJavaScript());

    // Generate the call to tell the bootstrap code that we're ready to go.
    out.newlineOpt();
    out.print("if (" + context.getModuleFunctionName() + ") "
            + context.getModuleFunctionName()
            + ".onScriptLoad(gwtOnLoad);");
    out.newlineOpt();
    out.print("})();");
    out.newlineOpt();

    return emitString(logger, out.toString(),
            getJsFilename(context, result));
}
 
Example #14
Source File: AppcacheLinker.java    From gwt-appcache with Apache License 2.0 4 votes vote down vote up
@Nonnull
private ArtifactSet perCompileLink( @Nonnull final TreeLogger logger,
                                    @Nonnull final LinkerContext context,
                                    @Nonnull final ArtifactSet artifacts )
  throws UnableToCompleteException
{
  final ArrayList<PermutationArtifact> permutationArtifacts =
    new ArrayList<>( artifacts.find( PermutationArtifact.class ) );
  if ( 0 == permutationArtifacts.size() )
  {
    // hosted mode
    return new ArtifactSet( artifacts );
  }

  final Set<String> allPermutationFiles = getAllPermutationFiles( permutationArtifacts );

  // get all artifacts
  final Set<String> allArtifacts = getArtifactsForCompilation( context, artifacts );

  final ArtifactSet results = new ArtifactSet( artifacts );
  for ( final PermutationArtifact permutation : permutationArtifacts )
  {
    // make a copy of all artifacts
    final HashSet<String> filesForCurrentPermutation = new HashSet<>( allArtifacts );
    // remove all permutations
    filesForCurrentPermutation.removeAll( allPermutationFiles );
    // add files of the one permutation we are interested in
    // leaving the common stuff for all permutations in...
    for ( final String file : permutation.getPermutation().getPermutationFiles() )
    {
      if ( allArtifacts.contains( file ) )
      {
        filesForCurrentPermutation.add( file );
      }
    }

    // build manifest
    final Set<String> externalFiles = getConfigurationValues( context, STATIC_FILES_CONFIGURATION_PROPERTY_NAME );
    final Map<String, String> fallbackFiles =
      parseFallbackResources( logger, getConfigurationValues( context, FALLBACK_FILES_CONFIGURATION_PROPERTY_NAME ) );
    final String maniFest = writeManifest( logger, externalFiles, fallbackFiles, filesForCurrentPermutation );
    final String filename =
      permutation.getPermutation().getPermutationName() + Permutation.PERMUTATION_MANIFEST_FILE_ENDING;
    results.add( emitString( logger, maniFest, filename ) );
  }

  results.add( createPermutationMap( logger, permutationArtifacts ) );
  return results;
}
 
Example #15
Source File: AppcacheLinker.java    From gwt-appcache with Apache License 2.0 4 votes vote down vote up
/**
 * Return the permutation for a single link step.
 */
@Nullable
final Permutation calculatePermutation( @Nonnull final TreeLogger logger,
                                        @Nonnull final LinkerContext context,
                                        @Nonnull final ArtifactSet artifacts )
  throws UnableToCompleteException
{
  Permutation permutation = null;

  for ( final SelectionInformation result : artifacts.find( SelectionInformation.class ) )
  {
    final String strongName = result.getStrongName();
    if ( null != permutation && !permutation.getPermutationName().equals( strongName ) )
    {
      throw new UnableToCompleteException();
    }
    if ( null == permutation )
    {
      permutation = new Permutation( strongName );
      final Set<String> artifactsForCompilation = getArtifactsForCompilation( context, artifacts );
      permutation.getPermutationFiles().addAll( artifactsForCompilation );
    }
    final List<BindingProperty> list = new ArrayList<>();
    for ( final SelectionProperty property : context.getProperties() )
    {
      if ( !property.isDerived() )
      {
        final String name = property.getName();
        final String value = result.getPropMap().get( name );
        if ( null != value )
        {
          list.add( new BindingProperty( name, value ) );
        }
      }
    }
    final SelectionDescriptor selection = new SelectionDescriptor( strongName, list );
    final List<SelectionDescriptor> selectors = permutation.getSelectors();
    if ( !selectors.contains( selection ) )
    {
      selectors.add( selection );
    }
  }
  if ( null != permutation )
  {
    logger.log( Type.DEBUG, "Calculated Permutation: " + permutation.getPermutationName() +
                            " Selectors: " + permutation.getSelectors() );
  }
  return permutation;
}
 
Example #16
Source File: AppcacheLinkerTest.java    From gwt-appcache with Apache License 2.0 4 votes vote down vote up
@Test
public void calculatePermutation()
  throws UnableToCompleteException
{
  final AppcacheLinker linker = new AppcacheLinker();
  final ArtifactSet artifacts1 = new ArtifactSet();
  artifacts1.add( new PermutationArtifact( AppcacheLinker.class, new Permutation( "1" ) ) );
  artifacts1.add( new StandardGeneratedResource( "myapp.devmode.js", new byte[ 0 ] ) );
  artifacts1.add( new StandardGeneratedResource( "file1.txt", new byte[ 0 ] ) );
  final TreeMap<String, String> configs2 = new TreeMap<>();
  configs2.put( "user.agent", "ie9" );
  configs2.put( "screen.size", "large" );
  configs2.put( "geolocationSupport", "maybe" );
  artifacts1.add( new SelectionInformation( "S2", 0, configs2 ) );
  final TreeMap<String, String> configs3 = new TreeMap<>();
  configs3.put( "user.agent", "ie9" );
  configs3.put( "screen.size", "small" );
  configs3.put( "geolocationSupport", "maybe" );
  artifacts1.add( new SelectionInformation( "S2", 1, configs3 ) );

  final LinkerContext linkerContext = mock( LinkerContext.class );
  when( linkerContext.getModuleName() ).thenReturn( "myapp" );

  final TreeSet<SelectionProperty> properties =
    new TreeSet<>( Comparator.comparing( SelectionProperty::getName ) );
  properties.add( new TestSelectionProperty( "user.agent", false ) );
  properties.add( new TestSelectionProperty( "screen.size", false ) );
  properties.add( new TestSelectionProperty( "geolocationSupport", true ) );

  when( linkerContext.getProperties() ).thenReturn( properties );

  final Permutation permutation = linker.calculatePermutation( TreeLogger.NULL, linkerContext, artifacts1 );

  assertEquals( permutation.getPermutationName(), "S2" );
  final Set<String> files = permutation.getPermutationFiles();
  assertEquals( files.size(), 1 );
  assertTrue( files.contains( "myapp/file1.txt" ) );

  final List<SelectionDescriptor> softPermutations = permutation.getSelectors();
  assertEquals( softPermutations.size(), 2 );
  final SelectionDescriptor s0 = softPermutations.get( 0 );
  final List<BindingProperty> bp0 = s0.getBindingProperties();
  final BindingProperty property01 = findProperty( "user.agent", bp0 );
  assertNotNull( property01 );
  assertEquals( property01.getValue(), "ie9" );

  final BindingProperty property02 = findProperty( "screen.size", bp0 );
  assertNotNull( property02 );
  assertEquals( property02.getValue(), "large" );

  final SelectionDescriptor s1 = softPermutations.get( 1 );
  final List<BindingProperty> bp1 = s1.getBindingProperties();
  final BindingProperty property11 = findProperty( "user.agent", bp1 );
  assertNotNull( property11 );
  assertEquals( property11.getValue(), "ie9" );

  final BindingProperty property12 = findProperty( "screen.size", bp1 );
  assertNotNull( property12 );
  assertEquals( property12.getValue(), "small" );
}