Ejemplo n.º 1
0
        protected override int Execute()
        {
            var baseResult = base.Execute();

            if (baseResult != 0)
            {
                return(baseResult);
            }

            var metadata = new GenymanMetadata();
            var version  = GetVersion();

            Log.Information($"Executing new command for {metadata.PackageId} - Version {version}");

            var sw = Stopwatch.StartNew();

            var configuration = new GenymanConfiguration <TConfiguration>
            {
                Genyman       = metadata,
                Configuration = new TTemplate(),
            };

            var output    = "";
            var extension = "json";

            if (JsonOption.HasValue())
            {
                output    = configuration.ToJsonString();
                extension = "json";
            }
            else             //Later support more formats!
            {
                // DEFAULT is json
                output    = configuration.ToJsonString();
                extension = "json";
            }

            var fileName = !string.IsNullOrEmpty(FileNameOption.Value()) ? FileNameOption.ParsedValue : $"gm-{metadata.Identifier.ToLower()}";

            fileName = $"{fileName}.{extension}";

            var fullFileName = Path.Combine(Environment.CurrentDirectory, fileName);

            if (File.Exists(fileName) && !Overwrite.HasValue())
            {
                Log.Error($"File {fullFileName} already exists. Specify --overwrite if you want to overwrite files");
                return(-1);
            }

            File.WriteAllText(fullFileName, output);
            Log.Information($"Configuration file {fileName} was written");

            Log.Information($"Finished ({sw.ElapsedMilliseconds}ms)");

            return(0);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// JsonObject constructor by a given key-value pair of items.
 /// </summary>
 /// <param name="keys">Key or keys</param>
 /// <param name="values">Value or values</param>
 /// <param name="nesting">Apply nesting behaviour if key input is a single string concatenated by dots, representing the desired nested structure</param>
 /// <param name="jsonOption">Options to modify a JsonObject</param>
 internal JsonObject(List <string> keys, List <object> values, bool nesting, JsonOption jsonOption)
 {
     if (nesting)
     {
         List <KeyValuePair <string, object> > pair = keys.Zip(values, (k, v) => new KeyValuePair <string, object>(k, v)).ToList();
         pair.ForEach(p => AddRecursive(this, p.Key, p.Value, jsonOption));
     }
     else
     {
         List <KeyValuePair <string, object> > pair = keys.Zip(values, (k, v) => new KeyValuePair <string, object>(k, v)).ToList();
         pair.ForEach(p => AddInternal(this, p.Key, p.Value, jsonOption));
     }
 }
Ejemplo n.º 3
0
 public static JsonObject ByKeysAndValues([ArbitraryDimensionArrayImport] List <string> keys,
                                          [KeepReference][ArbitraryDimensionArrayImport] List <object> values,
                                          [DefaultArgument("true")] bool nesting,
                                          [DefaultArgument("JsonOption.None")] JsonOption jsonOption)
 {
     if (keys == null)
     {
         throw new ArgumentNullException("keys");
     }
     if (values == null)
     {
         throw new ArgumentNullException("values");
     }
     if (keys.Count != values.Count)
     {
         string error = String.Format("Keys and values need to be of same size.\nKeys: {0}\nValues: {1}", keys.Count, values.Count);
         throw new ArgumentException(error);
     }
     if (values.Contains(null))
     {
         throw new ArgumentNullException("values", "Values' input contains one or more null elements.");
     }
     return(new JsonObject(keys, values, nesting, jsonOption));
 }
Ejemplo n.º 4
0
        public static JsonObject Merge(JsonObject jsonObject, List <JsonObject> others, [DefaultArgument("JsonOption.None")] JsonOption jsonOption)
        {
            Dictionary <string, object> dict = new Dictionary <string, object>();
            List <string> keys   = new List <string>();
            List <object> values = new List <object>();

            foreach (KeyValuePair <string, object> x in jsonObject.dict.ToList())
            {
                keys.Add(x.Key);
                values.Add(x.Value);
            }
            foreach (var other in others)
            {
                foreach (KeyValuePair <string, object> y in other.dict.ToList())
                {
                    keys.Add(y.Key);
                    values.Add(y.Value);
                }
            }

            return(new JsonObject(keys, values, true, jsonOption));
        }
Ejemplo n.º 5
0
        public static JsonObject Add(JsonObject jsonObject, List <string> key, List <object> value, bool nesting, JsonOption jsonOption)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            List <string> keys   = jsonObject.Keys.Concat(key).ToList();
            List <object> values = jsonObject.Values.Concat(value).ToList();

            return(new JsonObject(keys, values, nesting, jsonOption));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Method to add values recursively to a JsonObject.
        /// </summary>
        /// <param name="json"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="jsonOption"></param>
        /// <returns></returns>
        internal static JsonObject AddRecursive(JsonObject json, string key, object value, JsonOption jsonOption)
        {
            //If key doens't contain a dot, recursiveness is not applied.
            if (!key.Contains("."))
            {
                json = AddInternal(json, key, value, jsonOption);
            }
            else
            {
                string[] keys        = key.Split('.');
                string   k           = keys.First();
                string   restOfKeys  = String.Join(".", keys.Skip(1).ToArray());
                bool     containsKey = json.dict.ContainsKey(k);

                //If contains key, the associated value will be a JsonObject.
                if (containsKey)
                {
                    JsonObject newJson       = json.dict[k] as JsonObject;
                    JsonObject jsonRecursive = AddRecursive(newJson, restOfKeys, value, jsonOption);
                    json.dict[k] = jsonRecursive;
                }
                else
                {
                    JsonObject newJson       = new JsonObject();
                    JsonObject jsonRecursive = AddRecursive(newJson, restOfKeys, value, jsonOption);
                    json = AddInternal(json, k, jsonRecursive, jsonOption);
                }
            }
            return(json);
        }
Ejemplo n.º 7
0
        internal static JsonObject AddInternal(JsonObject json, string key, object value, JsonOption jsonOption)
        {
            if (json.dict.ContainsKey(key))
            {
                switch (jsonOption)
                {
                case JsonOption.Update:
                    json.dict[key] = value;
                    break;

                case JsonOption.Combine:
                    if (json.dict[key] is IList <object> && json.dict[key].GetType().IsGenericType)
                    {
                        List <object> listValue = json.dict[key] as List <object>;
                        listValue.Add(value);
                        json.dict[key] = listValue;
                    }
                    else
                    {
                        json.dict[key] = new List <object>()
                        {
                            json.dict[key], value
                        };
                    }
                    break;

                default:
                    json.dict.Add(key, value);
                    break;
                }
            }
            else
            {
                json.dict.Add(key, value);
            }
            return(json);
        }
Ejemplo n.º 8
0
 public JsonParser AddOption(JsonOption option)
 {
     this.options.Add(option);
     return(this);
 }