Skip to content

Commit

Permalink
Merge pull request #1656 from briandipalma/feat/inline-app-meta-require
Browse files Browse the repository at this point in the history
refactor: Inline app-metadata require
  • Loading branch information
Aivaras committed Feb 15, 2016
2 parents f897565 + 03a56eb commit 95ae3d5
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bladerunnerjs.api.Bladeset;
import org.bladerunnerjs.api.appserver.ApplicationServer;
import org.bladerunnerjs.api.spec.engine.SpecTest;
import org.bladerunnerjs.plugin.plugins.require.AppMetaDataSourceModule;
import org.bladerunnerjs.api.DirNode;
import org.bladerunnerjs.api.BladeWorkbench;
import org.junit.After;
Expand Down Expand Up @@ -553,8 +554,12 @@ public void defaultAppLocaleTokensIsUsedForBundles() throws Exception
.and(app).containsFileWithContents("app.conf", "localeCookieName: BRJS.LOCALE\n"
+ "locales: en,de\n"
+ "requirePrefix: appns")
.and(defaultAspect).containsFileWithContents("src/App.js", "@BRJS.APP.LOCALE@")
.and(defaultAspect).indexPageHasContent("<@js.bundle@/>\n"+"require('appns/App');")
.and(aspect).containsFileWithContents(
"src/App.js",
"@BRJS.APP.LOCALE@" +
"require('" + AppMetaDataSourceModule.PRIMARY_REQUIRE_PATH + "');"
)
.and(aspect).indexPageHasContent("<@js.bundle@/>\n" + "require('appns/App');")
.and(brjs).hasVersion("123")
.and(appServer).started();
then(appServer).requestForUrlContains("/app/v/123/js/prod/combined/bundle.js", "en");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bladerunnerjs.api.model.exception.request.ResourceNotFoundException;
import org.bladerunnerjs.api.spec.engine.SpecTest;
import org.bladerunnerjs.api.BladeWorkbench;
import org.bladerunnerjs.plugin.bundlers.appmeta.AppMetadataContentPlugin;
import org.bladerunnerjs.spec.brjs.appserver.MockTagHandler;
import org.bladerunnerjs.testing.utility.MockContentPlugin;
import org.junit.Before;
Expand All @@ -29,7 +28,6 @@ public void initTestObjects() throws Exception
.and(brjs).automaticallyFindsRequirePlugins()
.and(brjs).hasTagHandlerPlugins(new MockTagHandler("tagToken", "dev replacement", "prod replacement", false), new MockTagHandler("localeToken", "", "", true))
.and(brjs).hasContentPlugins(new MockContentPlugin())
.and(brjs).hasContentPlugins(new AppMetadataContentPlugin())
.and(brjs).hasBeenCreated();
app = brjs.app("app1");
appConf = app.appConf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void theBrjsConfIsWrittenOnPopulate() throws Exception {
" ContentPlugin:",
" - I18nContentPlugin",
" - ThirdpartyContentPlugin",
" - AppMetadataContentPlugin",
" - CommonJsContentPlugin",
" - NamespacedJsContentPlugin",
" - '*'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void bladerunnerConfWillHaveSensibleDefaultsIfItDoesntAlreadyExist() thro
" ContentPlugin:",
" - I18nContentPlugin",
" - ThirdpartyContentPlugin",
" - AppMetadataContentPlugin",
" - CommonJsContentPlugin",
" - NamespacedJsContentPlugin",
" - '*'");
Expand Down Expand Up @@ -63,7 +62,6 @@ public void bladerunnerConfThatAlreadyExistsCanBeReadAndModified() throws Except
" ContentPlugin:",
" - I18nContentPlugin",
" - ThirdpartyContentPlugin",
" - AppMetadataContentPlugin",
" - CommonJsContentPlugin",
" - NamespacedJsContentPlugin",
" - '*'");
Expand Down
4 changes: 2 additions & 2 deletions brjs-core/src/main/java/org/bladerunnerjs/api/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public interface Asset {
boolean isScopeEnforced();

/**
* BRJS distinguishes between Assets that may be required and those they may not. Those that may not include Assets on the
* BRJS distinguishes between Assets that may be required and those that may not. Those that may not include Assets on the
* root directory level or directories. The method returns true if the current Asset may be required and false otherwise.
*
* @return a boolean object stating whether the Asset may be required by another one.
* @return a boolean stating whether the Asset may be required by another one.
*/
boolean isRequirable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private LinkedHashMap<String,List<String>> getDefaultOrderedPlugins() {

addDefaultActivePlugin(activePlugins, ContentPlugin.class, "org.bladerunnerjs.plugin.bundlers.i18n.I18nContentPlugin");
addDefaultActivePlugin(activePlugins, ContentPlugin.class, "org.bladerunnerjs.plugin.bundlers.thirdparty.ThirdpartyContentPlugin");
addDefaultActivePlugin(activePlugins, ContentPlugin.class, "org.bladerunnerjs.plugin.bundlers.appmeta.AppMetadataContentPlugin");
addDefaultActivePlugin(activePlugins, ContentPlugin.class, "org.bladerunnerjs.plugin.bundlers.commonjs.CommonJsContentPlugin");
addDefaultActivePlugin(activePlugins, ContentPlugin.class, "org.bladerunnerjs.plugin.bundlers.namespacedjs.NamespacedJsContentPlugin");
addDefaultActivePlugin(activePlugins, ContentPlugin.class, "*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ org.bladerunnerjs.plugin.plugins.bundlers.i18n.I18nContentPlugin
org.bladerunnerjs.plugin.plugins.bundlers.css.CssContentPlugin
org.bladerunnerjs.plugin.plugins.bundlers.cssresource.CssResourceContentPlugin
org.bladerunnerjs.plugin.plugins.bundlers.unbundledresources.UnbundledResourcesContentPlugin
org.bladerunnerjs.plugin.plugins.bundlers.appmeta.AppMetadataContentPlugin
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"use strict";

// use a variable to store this so the modle doesn't try to find the source module that represents it during the dependency analysis
var metaDataRequirePath = "app-meta!$data";
var metaData;

/**
* @module br/services/bundlepath/BRAppMetaService
*/

var metaData = require("app-meta!$app-metadata");

var br = require('br/Core');
var AppMetaService = require('br/services/AppMetaService');

Expand Down Expand Up @@ -54,5 +52,3 @@ function getBundlePath(prefix, bundlePath) {
}

module.exports = BRAppMetaService;

metaData = require(metaDataRequirePath);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.bladerunnerjs.plugin.plugins.require;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.bladerunnerjs.api.App;
import org.bladerunnerjs.api.Asset;
import org.bladerunnerjs.api.BRJS;
import org.bladerunnerjs.api.BundlableNode;
import org.bladerunnerjs.api.memoization.MemoizedFile;
import org.bladerunnerjs.api.model.exception.ConfigException;
import org.bladerunnerjs.api.model.exception.ModelOperationException;
import org.bladerunnerjs.model.AssetContainer;
import org.bladerunnerjs.plugin.bundlers.commonjs.CommonJsSourceModule;
import org.bladerunnerjs.utility.AppMetadataUtility;

import com.google.common.base.Joiner;

public class AppMetaDataSourceModule implements CommonJsSourceModule
{

public static final String PRIMARY_REQUIRE_PATH = "app-meta!$app-metadata";
private BRJS brjs;
private BundlableNode bundlableNode;

public AppMetaDataSourceModule(BRJS brjs, BundlableNode bundlableNode) {
this.brjs = brjs;
this.bundlableNode = bundlableNode;
}

@Override
public boolean isEncapsulatedModule() {
return true;
}

@Override
public boolean isGlobalisedModule() {
return false;
}

@Override
public List<Asset> getPreExportDefineTimeDependentAssets(BundlableNode bundlableNode)
throws ModelOperationException {
return Collections.emptyList();
}

@Override
public List<Asset> getPostExportDefineTimeDependentAssets(BundlableNode bundlableNode)
throws ModelOperationException {
return Collections.emptyList();
}

@Override
public List<Asset> getUseTimeDependentAssets(BundlableNode bundlableNode) throws ModelOperationException {
return Collections.emptyList();
}

@Override
public List<Asset> getDependentAssets(BundlableNode bundlableNode) throws ModelOperationException {
return Collections.emptyList();
}

@Override
public void addImplicitDependencies(List<Asset> implicitDependencies) {
}

@Override
public Reader getReader() throws IOException {
try {
App app = bundlableNode.app();
String appLocales = Joiner.on("':true, '").join(app.appConf().getLocales());
String localCookieName = app.appConf().getLocaleCookieName();
String version = brjs.root().getAppVersionGenerator().getVersion();
String bundlePath = AppMetadataUtility.getRelativeVersionedBundlePath(app, version, "");

return new StringReader(
String.format(COMMONJS_DEFINE_BLOCK_HEADER, getPrimaryRequirePath()) + "\n" +
"// these variables should not be used directly but accessed via the 'br.app-meta-service' instead\n" +
"module.exports.APP_VERSION = '" + version + "';\n" +
"module.exports.VERSIONED_BUNDLE_PATH = '" + bundlePath + "';\n" +
"module.exports.LOCALE_COOKIE_NAME = '" + localCookieName + "';\n" +
"module.exports.APP_LOCALES = {'" + appLocales + "':true};" +
COMMONJS_DEFINE_BLOCK_FOOTER
);
} catch (ConfigException e) {
throw new IOException(e);
}
}

@Override
public MemoizedFile file() {
return bundlableNode.dir();
}

@Override
public String getAssetName() {
return getPrimaryRequirePath();
}

@Override
public String getAssetPath() {
return getPrimaryRequirePath();
}

@Override
public List<String> getRequirePaths() {
return Arrays.asList(getPrimaryRequirePath());
}

@Override
public String getPrimaryRequirePath() {
return PRIMARY_REQUIRE_PATH;
}

@Override
public AssetContainer assetContainer() {
return bundlableNode;
}

@Override
public boolean isScopeEnforced() {
return false;
}

@Override
public boolean isRequirable() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.bladerunnerjs.plugin.require;

import org.bladerunnerjs.api.Asset;
import org.bladerunnerjs.api.BRJS;
import org.bladerunnerjs.api.BundlableNode;
import org.bladerunnerjs.api.model.exception.RequirePathException;
import org.bladerunnerjs.api.plugin.RequirePlugin;
import org.bladerunnerjs.api.plugin.base.AbstractRequirePlugin;
import org.bladerunnerjs.plugin.plugins.require.AppMetaDataSourceModule;

public class AppMetaDataRequirePlugin extends AbstractRequirePlugin implements RequirePlugin
{

private BRJS brjs;
private RequirePlugin defaultRequirePlugin;

@Override
public void setBRJS(BRJS brjs) {
this.brjs = brjs;
this.defaultRequirePlugin = brjs.plugins().requirePlugin("default");
}

@Override
public String getPluginName() {
return "app-meta";
}

@Override
public Asset getAsset(BundlableNode bundlableNode, String requirePathSuffix) throws RequirePathException {
String requirePath = getPluginName() + "!" + requirePathSuffix;

if (requirePath.equals(AppMetaDataSourceModule.PRIMARY_REQUIRE_PATH)) {
return new AppMetaDataSourceModule(brjs, bundlableNode);
}

return defaultRequirePlugin.getAsset(bundlableNode, requirePath);
}

}
Loading

0 comments on commit 95ae3d5

Please sign in to comment.