/*
 * Copyright 2015 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp.gwt.linker;

import com.google.common.base.Preconditions;
import com.google.gwt.core.ext.LinkerContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.linker.AbstractLinker;
import com.google.gwt.core.ext.linker.ArtifactSet;
import com.google.gwt.core.ext.linker.CompilationResult;
import com.google.gwt.core.ext.linker.LinkerOrder;
import com.google.gwt.core.ext.linker.LinkerOrder.Order;
import com.google.gwt.core.linker.SymbolMapsLinker;
import com.google.gwt.dev.util.Util;

/**
 * Simple single-script linker that doesn't add any dependence on the browser.
 * This is intended to generate server-side runnable JS.  It doesn't support
 * permutations, nor does it allow late-loading code.
 */
@LinkerOrder(Order.PRIMARY)
public class MinimalLinker extends AbstractLinker {

  private static final String PREFIX = "(function(){var $wnd=this;var $doc={};";
  private static final String SUFFIX = "typeof gwtOnLoad==='function'&&gwtOnLoad()})();";

  @Override
  public String getDescription() {
    return "Minimal";
  }

  @Override
  public ArtifactSet link(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
      throws UnableToCompleteException {
    ArtifactSet toReturn = link(logger, context, artifacts, true);
    toReturn = link(logger, context, toReturn, false);
    return toReturn;
  }

  @Override
  public ArtifactSet link(
      TreeLogger logger, LinkerContext context, ArtifactSet artifacts, boolean onePermutation)
      throws UnableToCompleteException {
    ArtifactSet toReturn = new ArtifactSet(artifacts);
    ArtifactSet writableArtifacts = new ArtifactSet(artifacts);

    for (CompilationResult result : toReturn.find(CompilationResult.class)) {
      String[] js = result.getJavaScript();
      Preconditions.checkArgument(js.length == 1, "MinimalLinker doesn't support GWT.runAsync");

      byte[] primary = Util.getBytes(PREFIX + js[0] + SUFFIX);
      toReturn.add(emitBytes(logger, primary, context.getModuleName() + ".js"));
    }

    for (SymbolMapsLinker.ScriptFragmentEditsArtifact ea :
        writableArtifacts.find(SymbolMapsLinker.ScriptFragmentEditsArtifact.class)) {
      toReturn.add(ea);
    }
    return toReturn;
  }

  @Override
  public boolean supportsDevModeInJunit(LinkerContext context) {
    return false;
  }
}

@@ -1508,16 +1508,338 @@
 nker %7B%0A%0A
+  /*%0A   * Call gwtLoad as per the normal GWT linker (provided in %7B@code ModuleUtils%7D), this is in order%0A   * to invoke the %7B@code onModuleLoad%7D methods of loaded modules. The gwtOnLoad method then sets%0A   * %7B@code $moduleName%7D and %7B@code $moduleBase%7D, shadow them here so they don't leak into the%0A   * global scope.%0A   */%0A
   privat
@@ -1868,16 +1868,22 @@
 PREFIX =
+%0A     
  %22(funct
@@ -1902,16 +1902,17 @@
 nd=this;
+ 
 var $doc
@@ -1915,16 +1915,46 @@
 $doc=%7B%7D;
+ var $moduleName, $moduleBase;
 %22;%0A  pri

Update MinimalLinker to shadow some variables emitted by ModuleUtils. Add explanation of the prefix/suffix changes. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=126865390