def build(session, config, cdnPrefix="asset", compressionLevel=2, formattingLevel=0, kernelName="unify.Kernel"): name = config.get("name") assetManager = AssetManager(session) outputManager = OutputManager(session, assetManager, compressionLevel, formattingLevel) fileManager = FileManager(session) session.setField("unify.application.namespace", name) # Assets assetManager.addBuildProfile(cdnPrefix) assetManager.deploy(Resolver(session).addClassName("%s.Application" % name).getIncludedClasses()) # Store loader script if jasy.__version__ < "1.1": outputManager.storeKernel("$prefix/script/kernel.js", classes=[kernelName]) else: outputManager.storeKernel("$prefix/script/kernel.js", kernelName) # Copy files from source fileManager.updateFile("source/index.html", "$prefix/index.html") # Process every possible permutation for permutation in session.permutate(): # Resolving dependencies resolver = Resolver(session).addClassName("%s.Application" % name) # Compressing classes outputManager.storeCompressed(resolver.getSortedClasses(), "$prefix/script/%s-$permutation.js" % name, "unify.core.Init.startUp();")
def source(session, config, kernelName="unify.Kernel"): # Check for new jasy replacement system (1.1.0-rc4) if session.expandFileName("{{id}}") != "{{id}}": PREFIX = "{{prefix}}" HASH = "{{id}}" else: PREFIX = "$prefix" HASH = "$permutation" name = config.get("name") assetManager = AssetManager(session) outputManager = OutputManager(session, assetManager, 0, 1) session.setField("unify.application.namespace", name) assetManager.addSourceProfile() if jasy.__version__ < "1.1": outputManager.storeKernel(PREFIX + "/script/kernel.js", classes=[kernelName]) else: outputManager.storeKernel(PREFIX + "/script/kernel.js", kernelName) for permutation in session.permutate(): # Resolving dependencies resolver = Resolver(session).addClassName("%s.Application" % name) # Building class loader outputManager.storeLoader( resolver.getSortedClasses(), (PREFIX + "/script/%s-" + HASH + ".js") % name, "unify.core.Init.startUp();" )
def storeKernel(fileName, session, assets=None, translations=None, optimization=None, formatting=None, debug=False): """ Writes a so-called kernel script to the given location. This script contains data about possible permutations based on current session values. It optionally might include asset data (useful when boot phase requires some assets) and localization data (if only one locale is built). Optimization of the script is auto-enabled when no other information is given. This method returns the classes which are included by the script so you can exclude it from the real other generated output files. """ # Auto optimize kernel with basic compression features if optimization is None: optimization = Optimization("variables", "declarations", "blocks") # This exports all field values from the session fields = session.exportFields() # This permutation injects data in the core classes and configures debugging as given by parameter # # - fields => core.Env # - assets => core.Asset # - translations => core.Locale permutation = Permutation({ "debug" : debug, "fields" : fields, "assets" : assets, "translations" : translations }) # Build resolver # We need the permutation here because the field configuration might rely on detection classes resolver = Resolver(session.getProjects(), permutation) # Include classes for value injection if fields is not None: resolver.addClassName("core.Env") if assets is not None: resolver.addClassName("core.io.Asset") if translations is not None: resolver.addClassName("core.locale.Translate") # Include IO classes resolver.addClassName("core.io.Queue") # Sort resulting class list classes = Sorter(resolver, permutation).getSortedClasses() storeCompressed(fileName, classes, permutation=permutation, optimization=optimization, formatting=formatting) return classes
def source(session, config, kernelName="unify.Kernel"): name = config.get("name") assetManager = AssetManager(session) outputManager = OutputManager(session, assetManager, 0, 1) session.setField("unify.application.namespace", name) assetManager.addSourceProfile() if jasy.__version__ < "1.1": outputManager.storeKernel("$prefix/script/kernel.js", classes=[kernelName]) else: outputManager.storeKernel("$prefix/script/kernel.js", kernelName) for permutation in session.permutate(): # Resolving dependencies resolver = Resolver(session).addClassName("%s.Application" % name) # Building class loader outputManager.storeLoader(resolver.getSortedClasses(), "$prefix/script/%s-$permutation.js" % name, "unify.core.Init.startUp();")
def deployAssets(self, classes, assetFolder=None): """ Deploys assets for the given classes and all their dependencies :param classes: List of classes to deploy assets for :type classes: list :param assetFolder: Destination folder of assets (defaults to $prefix/asset) :type assetFolder: string """ Console.info("Deploying assets...") Console.indent() resolver = Resolver(self.__session) for className in classes: resolver.addClassName(className) self.__assetManager.deploy(resolver.getIncludedClasses(), assetFolder=assetFolder) Console.outdent()
def api(): """Generates the API viewer for the current project""" sourceFolder = session.getProjectByName("core").getPath() + "/source/" # Configure fields session.setField("debug", False) session.permutateField("es5") # Initialize shared objects assetManager = AssetManager(session).addBuildProfile() outputManager = OutputManager(session, assetManager, compressionLevel=2) fileManager = FileManager(session) # Deploy assets outputManager.deployAssets(["core.apibrowser.Browser"]) # Write kernel script outputManager.storeKernel("{{prefix}}/script/kernel.js", bootCode="core.apibrowser.Kernel.init();") # Copy files from source fileManager.updateFile(sourceFolder + "/apibrowser.html", "{{prefix}}/index.html") # Rewrite template as jsonp for tmpl in ["main", "error", "entry", "type", "params", "info", "origin", "tags"]: jsonTemplate = json.dumps({ "template" : open(sourceFolder + "/tmpl/apibrowser/%s.mustache" % tmpl).read() }) fileManager.writeFile("{{prefix}}/tmpl/%s.js" % tmpl, "apiload(%s, '%s.mustache')" % (jsonTemplate, tmpl)) # Process every possible permutation for permutation in session.permutate(): # Resolving dependencies resolver = Resolver(session).addClassName("core.apibrowser.Browser") # Compressing classes outputManager.storeCompressed(resolver.getSortedClasses(), "{{prefix}}/script/apibrowser-{{id}}.js", "new core.apibrowser.Browser;") # Write API data ApiWriter(session).write("{{prefix}}/data")
def build(session, config, cdnPrefix="asset", compressionLevel=2, formattingLevel=0, kernelName="unify.Kernel"): # Check for new jasy replacement system (1.1.0-rc4) if session.expandFileName("{{id}}") != "{{id}}": PREFIX = "{{prefix}}" HASH = "{{id}}" else: PREFIX = "$prefix" HASH = "$permutation" name = config.get("name") assetManager = AssetManager(session) outputManager = OutputManager(session, assetManager, compressionLevel, formattingLevel) fileManager = FileManager(session) session.setField("unify.application.namespace", name) # Assets assetManager.addBuildProfile(cdnPrefix) assetManager.deploy(Resolver(session).addClassName("%s.Application" % name).getIncludedClasses()) # Store loader script if jasy.__version__ < "1.1": outputManager.storeKernel(PREFIX + "/script/kernel.js", classes=[kernelName]) else: outputManager.storeKernel(PREFIX + "/script/kernel.js", kernelName) # Copy files from source fileManager.updateFile("source/index.html", PREFIX + "/index.html") # Process every possible permutation for permutation in session.permutate(): # Resolving dependencies resolver = Resolver(session).addClassName("%s.Application" % name) # Compressing classes outputManager.storeCompressed( resolver.getSortedClasses(), (PREFIX + "/script/%s-" + HASH + ".js") % name, "unify.core.Init.startUp();" )
def storeKernel(fileName, debug=False): """ Writes a so-called kernel script to the given location. This script contains data about possible permutations based on current session values. It optionally might include asset data (useful when boot phase requires some assets) and localization data (if only one locale is built). Optimization of the script is auto-enabled when no other information is given. This method returns the classes which are included by the script so you can exclude it from the real other generated output files. """ header("Storing kernel...") # This exports all field values from the session fields = session.exportFields() # This permutation injects data in the core classes and configures debugging as given by parameter setPermutation(Permutation({ "debug" : debug, "fields" : fields })) # Build resolver # We need the permutation here because the field configuration might rely on detection classes resolver = Resolver() resolver.addClassName("core.Env") resolver.addClassName("core.io.Asset") resolver.addClassName("core.io.Queue") # Sort resulting class list classes = resolver.getSortedClasses() storeCompressed(classes, fileName) setPermutation(None) return classes
def cacheManifest(session, startClassName, scripts=["script/application-%s.js"], htmlfile="index.html", kernel="script/kernel.js", ignoreAssets=False): # Check for new jasy replacement system (1.1.0-rc4) if session.expandFileName("{{id}}") != "{{id}}": PREFIX = "{{prefix}}" HASH = "{{id}}" else: PREFIX = "$prefix" HASH = "$permutation" timestamp = time.strftime("%Y-%m-%d %H:%M:%S") appcache = """CACHE MANIFEST # Jasy AppCache Manifest file # Version: {version} CACHE: {htmlfile} {kernel} {scripts} {assets} NETWORK: *""" htmlcache = '<!DOCTYPE html><html manifest="%s"></html>' assetManager = AssetManager(session).addBuildProfile() outputManager = OutputManager(session, assetManager) fileManager = FileManager(session) # Create an application cache file for each permutation for permutation in session.permutate(): if ignoreAssets: assets = [] else: classes = Resolver(session).addClassName( startClassName).getSortedClasses() assetConfig = json.loads(assetManager.export(classes)) assets = filenamesFromAsset("", assetConfig["assets"], assetConfig["profiles"]) # Set options if hasattr(permutation, "getId"): checksum = permutation.getId() else: checksum = session.expandFileName( HASH) #instead of permutation.getChecksum() scriptFiles = [] for script in scripts: scriptFiles.append(script % checksum) manifestFilename = "appcache-%s.manifest" % (checksum) fileManager.writeFile( PREFIX + "/" + manifestFilename, appcache.format(version=timestamp, htmlfile=htmlfile, kernel=kernel, scripts="\n".join(scriptFiles), assets="\n".join(assets))) fileManager.writeFile(PREFIX + "/index-%s.html" % (checksum), htmlcache % manifestFilename)
def storeKernel(self, fileName, classes=None, debug=False): """ Writes a so-called kernel script to the given location. This script contains data about possible permutations based on current session values. It optionally might include asset data (useful when boot phase requires some assets) and localization data (if only one locale is built). Optimization of the script is auto-enabled when no other information is given. This method returns the classes which are included by the script so you can exclude it from the real other generated output files. """ Console.info("Storing kernel...") Console.indent() # Use a new permutation based on debug settings and statically configured fields self.__session.setStaticPermutation(debug=debug) # Build resolver # We need the permutation here because the field configuration might rely on detection classes resolver = Resolver(self.__session) detectionClasses = self.__session.getFieldDetectionClasses() for className in detectionClasses: resolver.addClassName(className) # Jasy client side classes to hold data resolver.addClassName("jasy.Env") resolver.addClassName("jasy.Asset") resolver.addClassName("jasy.Translate") # Allow kernel level mass loading of scripts (required for source, useful for build) resolver.addClassName("core.io.Script") resolver.addClassName("core.io.Queue") if classes: for className in classes: resolver.addClassName(className) # Generate boot code bootCode = "jasy.Env.setFields(%s);" % self.__session.exportFields() if self.__compressGeneratedCode: bootCode = packCode(bootCode) # Sort resulting class list sortedClasses = resolver.getSortedClasses() self.storeCompressed(sortedClasses, fileName, bootCode) # Remember classes for filtering in storeLoader/storeCompressed self.__kernelClasses = set(sortedClasses) # Reset static permutation self.__session.resetCurrentPermutation() Console.outdent()
def __buildClassList(self, classes, bootCode=None, filterBy=None, inlineTranslations=False): session = self.__session # 1. Add given set of classes resolver = Resolver(session) for classItem in classes: resolver.addClass(classItem) # 2. Add optional boot code if bootCode: bootClassItem = session.getVirtualItem("jasy.generated.BootCode", ClassItem, "(function(){%s})();" % bootCode, ".js") resolver.addClass(bootClassItem) # 3. Check for asset usage includedClasses = resolver.getIncludedClasses() usesAssets = False for classItem in includedClasses: if classItem.getId() == "jasy.Asset": usesAssets = True break # 4. Add asset data if needed if usesAssets: assetData = self.__assetManager.export(includedClasses) assetClassItem = session.getVirtualItem("jasy.generated.AssetData", ClassItem, "jasy.Asset.addData(%s);" % assetData, ".js") resolver.addClass(assetClassItem, prepend=True) # 5. Add translation data if not inlineTranslations: translationBundle = self.__session.getCurrentTranslationBundle() if translationBundle: translationData = translationBundle.export(includedClasses) if translationData: translationClassItem = session.getVirtualItem("jasy.generated.TranslationData", ClassItem, "jasy.Translate.addData(%s);" % translationData, ".js") resolver.addClass(translationClassItem, prepend=True) # 6. Sorting classes sortedClasses = resolver.getSortedClasses() # 7. Apply filter if filterBy: filteredClasses = [] for classObj in sortedClasses: if not classObj in filterBy: filteredClasses.append(classObj) sortedClasses = filteredClasses return sortedClasses