/// <summary>
    /// Adds environment variables that allow the program to make calls back to Zero Install.
    /// </summary>
    public static IEnvironmentBuilder SetCallbackEnvironmentVariables(this IEnvironmentBuilder builder)
    {
        void TryAdd(string envName, ProcessStartInfo?startInfo)
        {
            if (startInfo == null)
            {
                return;
            }

            try
            {
                builder.SetEnvironmentVariable(envName, startInfo.ToCommandLine());
            }
            catch (FileNotFoundException)
            {
                // Zero Install may be embedded as a library rather than called as an executable
            }
        }

        TryAdd(ZeroInstallEnvironment.CliName, ProgramUtils.CliStartInfo());
        TryAdd(ZeroInstallEnvironment.GuiName, ProgramUtils.GuiStartInfo());
        TryAdd(ZeroInstallEnvironment.ExternalFetcherName, ProgramUtils.CliStartInfo(Fetch.Name));

        return(builder);
    }
예제 #2
0
        /// <summary>
        /// Sets the current active log level for the configured logger
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="level">the log level to set</param>
        /// <returns></returns>
        public static IEnvironmentBuilder WithLogLevel(this IEnvironmentBuilder configuration,
                                                       LogLevel level)
        {
            if (configuration.Configuration.HasValue(typeof(ILoggerFacade).FullName))
            {
                if (configuration.Configuration.GetValue <ILoggerFacade>(typeof(ILoggerFacade).FullName) is ILoggerFacade facade)
                {
                    facade.LogLevel = level;
                }
            }

            return(configuration);
        }
예제 #3
0
 /// <summary>
 /// Adds the xml file to the pipe
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="xPath">the xpath to retrieve</param>
 /// <param name="file">the optional file to use</param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithXml(this IEnvironmentBuilder builder, string xPath, string file = null)
 {
     return(builder.WithSource(cfg =>
     {
         if (!cfg.HasValue(typeof(XmlFileParser).FullName))
         {
             return null;//no xml file parser was registered
         }
         var reqType = cfg.GetBuildType();
         var parser = cfg.GetValue <XmlFileParser>(typeof(XmlFileParser).FullName);
         return parser.Value(xPath, reqType, file);
     }, cfg => cfg.WithTrace(xPath, "xml")));
 }
예제 #4
0
        /// <summary>
        /// Adds the json file to the pipeline
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="jPath">the json path to extract</param>
        /// <returns></returns>
        public static IEnvironmentBuilder WithJson(this IEnvironmentBuilder builder, string jPath)
        {
            //next line wil trigger an exception on malformed syntax
            var expr = JSegment.Parse(jPath).ToList();

            return(builder.WithSource(cfg =>
            {
                if (!cfg.HasValue(typeof(JsonFileParser).FullName))
                {
                    return null;//no json file parser was registered
                }
                var reqType = cfg.GetBuildType();
                var parser = cfg.GetValue <JsonFileParser>(typeof(JsonFileParser).FullName);
                return parser.Value(expr, reqType);
            }, cfg => cfg.WithTrace(jPath, "json")));
        }
        // this method will be used to get the next randomly incremented number
        public static IEnvironmentBuilder Random(this IEnvironmentBuilder builder)
        {
            if (!builder.Configuration.HasValue(SaverKey))
            {
                builder.WithConfiguration(cfg => cfg.SetValue(SaverKey, new NumberSaver()));
            }

            return(builder.WithSource(config =>
            {
                //just a simple logic to increment and save values
                var nextValue = config.GetFactoryValue <int>(RngKey);
                var saver = config.GetValue <NumberSaver>(SaverKey);
                var oldKey = saver.Prev;
                saver.Prev += nextValue;
                return oldKey;
            }, config => config.WithTrace("my random value", "my random source")));
        }
        /// <summary>
        /// Gets the formatted string containing the descriptions of the bundles and the utility.
        /// </summary>
        /// <param name="builder"></param>
        /// <returns>the formatted help string</returns>
        public static string GetHelp(this IEnvironmentBuilder builder)
        {
            var  help            = $"{{0}}{Environment.NewLine}{Environment.NewLine}{{1}}";
            var  parameter0      = builder.GetDescription();
            bool parameter0valid = !string.IsNullOrEmpty(parameter0?.Trim());

            string tabFormat    = "\t";
            var    descriptions = builder.Bundles.GetDescriptions().ToArray();
            var    traces       = builder.Bundles.Select(x => x.Sources.Select(y => y.GetTrace()).ToArray()).ToArray();
            var    range        = Math.Max(descriptions.Length, traces.Length);
            var    combined     = Enumerable.Range(0, range)
                                  .Select(x =>
            {
                StringBuilder sb = new StringBuilder();
                if (x < traces.Length)
                {
                    sb.AppendLine($"- {(string.Join(", ", traces[x]))}");
                }
                else
                {
                    sb.AppendLine($"- [unknown]");
                }


                if (x < descriptions.Length)
                {
                    sb.AppendLine(tabFormat + descriptions[x]);
                }
                else
                {
                    sb.AppendLine();
                }

                return(sb.ToString());
            }).ToArray();
            var parameter1 = string.Join(string.Empty, combined);

            //var parameter1 = string.Join(Environment.NewLine ,
            //    builder.Bundles.GetDescriptions()
            //        .Select(x=>$"{(parameter0valid?tabFormat:string.Empty)}{x??string.Empty}").ToArray()
            //);
            return(parameter0valid ? string.Format(help, parameter0, parameter1) : parameter1);
        }
        /// <summary>
        /// Adds the environment variable source to te pipe
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="name">the name of the variable</param>
        /// <param name="configuration">the scoped configuration to add</param>
        /// <returns></returns>
        public static IEnvironmentBuilder WithEnvironmentVariable(this IEnvironmentBuilder builder, string name, Action <IEnvironmentConfiguration> configuration = null)
        {
            if (!builder.Configuration.HasValue(typeof(EnvironmentVariableParser).FullName))
            {
                builder.WithConfiguration(cfg => cfg.SetValue(typeof(EnvironmentVariableParser).FullName,
                                                              new EnvironmentVariableParser()));
            }

            return(builder.WithSource(cfg =>
            {
                var parser = cfg.GetValue <EnvironmentVariableParser>(typeof(EnvironmentVariableParser).FullName);
                var requiredType = cfg.GetBuildType();
                var target = cfg.GetEnvironmentTarget();
                var prefix = cfg.GetEnvironmentVariablePrefix();
                return parser.Value(name, requiredType, target, prefix);
            }, cfg =>
            {
                configuration?.Invoke(cfg);
                cfg.WithTrace(name, "environment");
            }));
        }
        public static Resolvable <T> When <T, TR>(this IEnvironmentBuilder bundle, TR match, Func <T> segment)
        {
            var wrapper = bundle.Bundle();

            return(new Resolvable <T>(() =>
            {
                var resolvedValue = wrapper.Build <TR>();
                if ((resolvedValue != null && resolvedValue.Equals(match)) ||
                    (match != null && match.Equals(resolvedValue)))
                {
                    return segment == null
                        ? default
                        : segment.Invoke();
                }

                return default;
            })
            {
                Scope = new FlowScope
                {
                    PreviousState = wrapper
                }
            });
 /// <summary>
 /// Adds the default value source to the pipe
 /// </summary>
 /// <typeparam name="T">the type of value</typeparam>
 /// <param name="builder"></param>
 /// <param name="value">the value to add</param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithDefaultValue <T>(this IEnvironmentBuilder builder, T value)
 {
     return(builder.WithSource(_ => value, cfg => cfg.WithTrace(value?.ToString(), "default")));
 }
 /// <summary>
 /// Gets the global description or null if none exist
 /// </summary>
 /// <param name="builder">the builder</param>
 /// <returns>the description</returns>
 public static string GetDescription(this IEnvironmentBuilder builder)
 {
     return(builder.Configuration.GetValue(Constants.GlobalDescriptionValueKey));
 }
 /// <summary>
 /// Sets the scoped description for the current bundle. This value will be used to print out the info for the current bundle.
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="description"></param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithDescription(this IEnvironmentBuilder builder, string description)
 {
     return(builder.WithConfiguration(config =>
                                      config.SetValue(Constants.SourceDescriptionValueKey, description)));
 }
예제 #12
0
 private Flow(IEnvironmentBuilder builder)
 {
     _builder = builder;
 }
예제 #13
0
 /// <summary>
 /// This is a shorthand for the "WithXml"
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="xPath">the xpath to retrieve</param>
 /// <param name="file">the optional file to use</param>
 /// <returns></returns>
 public static IEnvironmentBuilder Xml(this IEnvironmentBuilder builder, string xPath, string file = null)
 {
     return(builder.WithXml(xPath, file));
 }
예제 #14
0
 /// <summary>
 /// Shorthand alias for "WithArgument" using the common key set beforehand
 /// See also "CommonExtensions.WithCommonKey"
 /// </summary>
 /// <param name="builder"></param>
 /// <returns></returns>
 public static IEnvironmentBuilder Arg(this IEnvironmentBuilder builder)
 {
     return(builder.WithArgument(builder.Configuration.GetCommonKey()));
 }
 /// <summary>
 /// This is a shorthand for <see cref="WithCommonKey"/>
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="key">the key name</param>
 /// <returns></returns>
 public static IEnvironmentBuilder With(this IEnvironmentBuilder builder, string key)
 {
     return(builder.WithCommonKey(key));
 }
 /// <summary>
 /// Shorthand alias for <see cref="WithException"/>
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="message">the message to throw</param>
 /// <returns></returns>
 public static IEnvironmentBuilder Throw(this IEnvironmentBuilder builder, string message = null)
 {
     return(builder.WithException(message));
 }
예제 #17
0
 /// <summary>
 /// Logs the fatal message to the logger
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="message">the message to log</param>
 public static void LogFatal(this IEnvironmentBuilder configuration, object message)
 {
     configuration.WithConfiguration(cfg => cfg.Log(LogLevel.Fatal, message));
 }
 public static Resolvable <T> As <T>(this IEnvironmentBuilder bundle)
 {
     return(new Resolvable <T>(bundle.Bundle().Build <T>));
 }
 /// <summary>
 /// Shorthand alias for <see cref="WithDefaultValue{T}"/>
 /// </summary>
 /// <typeparam name="T">the type of value</typeparam>
 /// <param name="builder"></param>
 /// <param name="value">the value to add</param>
 /// <returns></returns>
 public static IEnvironmentBuilder Default <T>(this IEnvironmentBuilder builder, T value)
 {
     return(builder.WithDefaultValue(value));
 }
 /// <summary>
 /// Shorthand alias for "WithEnvironmentVariable" using the common key set beforehand
 /// "CommonExtensions.WithCommonKey"
 /// </summary>
 /// <param name="builder"></param>
 /// <returns></returns>
 public static IEnvironmentBuilder Env(this IEnvironmentBuilder builder, Action <IEnvironmentConfiguration> configuration = null)
 {
     return(builder.WithEnvironmentVariable(builder.Configuration.GetCommonKey(), configuration));
 }
 /// <summary>
 /// Adds the throwable source to the pipe. Throws an ArgumentException if hit
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="message">the message to throw</param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithException(this IEnvironmentBuilder builder, string message)
 {
     return(builder.WithSource <object>(_ => throw new ArgumentException(message ?? "Missing required variable"), cfg => cfg.WithTrace(message, "exception")));
 }
예제 #22
0
 public EnvironmentBundle(IEnvironmentBuilder builder)
 {
     this._builder = builder;
 }
 /// <summary>
 /// Adds the common key to the configuration to be consumed by other types
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="key">the key name</param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithCommonKey(this IEnvironmentBuilder builder, string key)
 {
     return(builder.WithConfiguration(cfg => cfg.SetValue(Constants.SourceCommonKeyKey, key)));
 }
예제 #24
0
 /// <summary>
 /// This is a shorthand for the "WithJson"
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="jPath">the json path to extract</param>
 /// <returns></returns>
 public static IEnvironmentBuilder Json(this IEnvironmentBuilder builder, string jPath)
 {
     return(builder.WithJson(jPath));
 }
예제 #25
0
        private void GenerateEnvironment(IEnvironment env, ITexturePack textures)
        {
            IEnvironmentBuilder dungeonBuilder = GetComponent <EnvironmentBuilder> ();

            dungeonBuilder.Generate(env, textures);
        }
 /// <summary>
 /// Shorthand for "WithEnvironmentVariable"
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="name">the name of the variable</param>
 /// <param name="configuration">the configuration to use</param>
 /// <returns></returns>
 public static IEnvironmentBuilder Env(this IEnvironmentBuilder builder, string name, Action <IEnvironmentConfiguration> configuration = null)
 {
     return(builder.WithEnvironmentVariable(name, configuration));
 }