}
@Override
public void writeDefinitions(Set<DefDescriptor<?>> dependencies, Writer out)
throws IOException, QuickFixException {
AuraContext context = Aura.getContextService().getCurrentContext();
Mode mode = context.getMode();
final boolean minify = !mode.prettyPrint();
final String mKey = minify ? "MIN:" : "DEV:";
//
// create a temp buffer in case anything bad happens while we're processing this.
// don't want to end up with a half a JS init function
//
// TODO: get rid of this buffering by adding functionality to Json.serialize that will help us
// make sure serialized JS is valid, non-error-producing syntax if an exception happens in the
// middle of serialization.
//
context.setPreloading(true);
DefDescriptor<?> applicationDescriptor = context.getLoadingApplicationDescriptor();
final String uid = context.getUid(applicationDescriptor);
final String key = "JS:" + mKey + uid;
String cached = context.getDefRegistry().getCachedString(uid, applicationDescriptor, key);
if (cached == null) {
StringBuilder sb = new StringBuilder();
sb.append("$A.clientService.initDefs({");
// append component definitions
sb.append("componentDefs:");
Collection<BaseComponentDef> defs = filterAndLoad(BaseComponentDef.class, dependencies, null);
Aura.getSerializationService().writeCollection(defs, BaseComponentDef.class, sb, "JSON");
sb.append(",");
// append event definitions
sb.append("eventDefs:");
Collection<EventDef> events = filterAndLoad(EventDef.class, dependencies, null);
Aura.getSerializationService().writeCollection(events, EventDef.class, sb, "JSON");
sb.append(",");
// append library definitions
sb.append("libraryDefs:");
Collection<LibraryDef> libraries = filterAndLoad(LibraryDef.class, dependencies, null);
Aura.getSerializationService().writeCollection(libraries, LibraryDef.class, sb, "JSON");
sb.append(",");
//
// append controller definitions
// Dunno how this got to be this way. The code in the Format adaptor was twisted and stupid,
// as it walked the namespaces looking up the same descriptor, with a string.format that had
// the namespace but did not use it. This ends up just getting a single controller.
//
sb.append("controllerDefs:");
Collection<ControllerDef> controllers = filterAndLoad(ControllerDef.class, dependencies, ACF);
Aura.getSerializationService().writeCollection(controllers, ControllerDef.class, sb, "JSON");
sb.append("});");
cached = sb.toString();
// only use closure compiler in prod mode, due to compile cost
if (minify) {
StringWriter sw = new StringWriter();
List<JavascriptProcessingError> errors = JavascriptWriter.CLOSURE_SIMPLE.compress(cached, sw, key);
if (errors == null || errors.isEmpty()) {
// For now, just use the non-compressed version if we can't get
// the compression to work.
cached = sw.toString();
} else {
// if unable to compress, add error comments to the end.
// ONLY if not production instance
if (!Aura.getConfigAdapter().isProduction()) {
sb.append(commentedJavascriptErrors(errors));
}
cached = sb.toString();
}
}
context.getDefRegistry().putCachedString(uid, applicationDescriptor, key, cached);
}
out.append(cached);
}