All files / universal/gpii/node_modules/lifecycleManager/src Resolvers.js

95% Statements 19/20
100% Branches 10/10
87.5% Functions 7/8
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                    3x 3x   3x       3x                           3x                                                                   3x 292x     3x   1494x                                         3x 514x 319x 319x                 3x 1716x 244x 244x                         3x             3x          
/*!
 * Lifecycle Manager Resolvers
 *
 * Copyright 2012 Antranig Basman
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * The research leading to these results has received funding from the European Union's
 * Seventh Framework Programme (FP7/2007-2013)
 * under grant agreement no. 289016.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */
 
"use strict";
 
var fluid = fluid || require("infusion");
var gpii = fluid.registerNamespace("gpii");
 
(function () {
    // A standard interception point so that the process of resolving names onto
    // settings handlers and actions can be mocked for integration tests
 
    fluid.defaults("gpii.lifecycleManager.nameResolver", {
        gradeNames: ["fluid.component"],
        invokers: {
            resolveName: {
                funcName: "fluid.identity"
            }
        }
    });
 
    /** The central machinery in the Lifecycle Manager which manages the process
      * of resolving contextual expressions such as ${{environment}.WINDIR} onto
      * strings by interpolation.
      */
 
    fluid.defaults("gpii.lifecycleManager.variableResolver", {
        gradeNames: ["fluid.component"],
        components: {
            resolverConfig: {
                type: "gpii.lifecycleManager.standardResolverConfig"
            }
        },
        members: {
            resolvers: {
                expander: {
                    func: "gpii.lifecycleManager.variableResolver.computeResolvers",
                    args: "{that}.resolverConfig.options.resolvers"
                }
            },
            fetcher: {
                expander: {
                    func: "gpii.resolversToFetcher",
                    args: "{that}.resolvers"
                }
            }
        },
        invokers: {
            /** Resolves interpolated variables within some options material (argument 0).
             * This is resolved first against the `builtin fetcher` which is computed by pooling
             * environmental fetchers from the platform-specific repositories, together with
             * a fetcher resolving onto variables in the user's current session (argument 1)
             */
            resolve: {
                funcName: "gpii.lifecycleManager.variableResolver.resolve",
                args: ["{arguments}.0", "{that}.fetcher", "{arguments}.1"]
            }
        }
    });
 
    gpii.lifecycleManager.variableResolver.computeResolvers = function (resolvers) {
        return fluid.transform(resolvers, fluid.getGlobalValue);
    };
 
    gpii.lifecycleManager.variableResolver.resolve = function (material, fetcher, extraFetcher) {
        // TODO: This relies on a horribly undocumented and unstable Infusion API
        return fluid.expand(material, {
            bareContextRefs: false,
            // TODO: FLUID-4932 - the framework currently has no wildcard support in mergePolicy.
            mergePolicy: {
                0: {
                    capabilitiesTransformations: {
                        "*": {
                            noexpand: true
                        }
                    }
                }
            },
            fetcher: gpii.combineFetchers(fetcher, extraFetcher)
        });
    };
 
    /** Converts a free hash of "resolvers" (keyed by resolver name) into a
     * "fetcher" suitable for being operated by Infusion's expansion machinery resolving
     * expressions such as ${{environment}.WINDIR} when they are seen within strings for interpolation.
     */
 
    gpii.resolversToFetcher = function (resolvers) {
        return function (parsed) {
            var resolver = resolvers[parsed.context];
            return !resolver ? undefined : (
                typeof(resolver) === "function" ?
                    resolver(parsed.path) : fluid.get(resolver, parsed.path));
        };
    };
 
    /** A hacked and unsatisfactory method to compose two fetchers (the second of which is optional)
     * into a single one
     */
    gpii.combineFetchers = function (main, fallback) {
        return fallback ? function (parsed) {
            var fetched = main(parsed);
            return fetched === undefined ? fallback(parsed) : fetched;
        } : main;
    };
 
    /** Central standard repository for "resolvers" mapping some environmental material onto
     * resolvable expressions. This is targetted, e.g., from the windows repository by a
     * grade linkage registering the Windows Registry resolver.
     * It contains an option named `resolvers` which maps resolver names (which will act as context names,
     * in expressions such as ${{environment}.WINDIR}) onto global names, which are resolvable via
     * `fluid.getGlobalValue` to unary functions resolving the path expressions - these unary
     * functions mapping Strings to Strings are named (variable) `resolvers`.
     */
 
    fluid.defaults("gpii.lifecycleManager.standardResolverConfig", {
        gradeNames: "fluid.component",
        resolvers: {
            environment: "gpii.lifecycleManager.environmentResolver"
        }
    });
 
    gpii.lifecycleManager.environmentResolver = function (name) {
        return process.env[name];
    };
 
})();