public static CommandLineApplication CommandDownloadNuget(this CommandLineApplication app)
        {
            var cmd = app.Command("download", config =>
            {
                config.Description = "Download nuget at " + Constants.urlNuget;
                config.HelpOption(HelpFlag);

                var validator = new GroupArgument(config, false);

                config.OnExecute(() =>
                {
                    if (validator.Evaluate() > 0)
                    {
                        return(2);
                    }

                    var uri          = new Uri(Constants.urlNuget);
                    BbClientHttp cli = new BbClientHttp(uri);


                    try
                    {
                        if (Constants.NugetFile.Exists)
                        {
                            Output.WriteLine("remove current version of nuget.exe in " + Constants.NugetFile.Directory.FullName);
                            Constants.NugetFile.Delete();
                        }
                    }
                    catch (Exception ex1)
                    {
                        throw new Exception("failed to remove current version of nuget.exe", ex1);
                    }

                    try
                    {
                        Output.WriteLine("download lastest version of nuget.exe in " + Constants.NugetFile.Directory.FullName);
                        BbClientHttp.DownloadAsync(uri, Constants.NugetFile.FullName).Wait();
                    }
                    catch (Exception ex2)
                    {
                        throw new Exception("failed to to downlod nuget.exe", ex2);
                    }

                    Output.WriteLine("download successfull");

                    Result = Constants.NugetFile;

                    return(0);
                });
            });

            return(app);
        }
Example #2
0
        public static CommandLineApplication CommandCountries(this CommandLineApplication app)
        {
            var cmd = app.Command("config", config1 =>
            {
                config1.Description = "manage country's configuration";
                config1.HelpOption(HelpFlag);

                var cmd2 = config1.Command("list", config2 =>
                {
                    config2.Description = "return the list of available countries";
                    config2.HelpOption(HelpFlag);

                    var validator = new GroupArgument(config2, false);

                    config2.OnExecute(() =>
                    {
                        if (validator.Evaluate() > 0)
                        {
                            return(2);
                        }

                        try
                        {
                            DownloadCountryList();
                            Output.WriteLine("list successfull downloaded");
                        }
                        catch (Exception)
                        {
                            Output.WriteLine("Fail to resolve remote list of configuration. Working with local list");
                            throw;
                        }


                        ConvertToDatatable.ConvertList <CountryParameter>(Helper.Parameters.Countries
                                                                          , "available countries parameters"
                                                                          , c => c.Name
                                                                          , c => c.Included
                                                                          , c => c.OnlineVersion
                                                                          , c => c.OnlineVersion
                                                                          )
                        .PrintList()
                        ;

                        return(0);
                    });
                });

                var cmd3 = config1.Command("add", config2 =>
                {
                    config2.Description = "add a new country in the local configuration";
                    config2.HelpOption(HelpFlag);

                    var validator = new GroupArgument(config2, false);

                    var countryName = validator.Argument("CountryName",
                                                         "name of the country must be added in the configuration. (this argument is required)"
                                                         , ValidatorExtension.EvaluateRequired
                                                         , ValidatorExtension.EvaluateCountryName
                                                         );

                    config2.OnExecute(() =>
                    {
                        if (Helper.Parameters.Countries.Count == 0)
                        {
                            GetDictionnaryCountry();
                            Output.WriteLine("list successfull downloaded");
                        }

                        if (validator.Evaluate() > 0)
                        {
                            return(2);
                        }

                        var country       = countryName.Value.ToLower();
                        var _country      = Helper.Parameters.Countries.FirstOrDefault(c => c.Name.ToLower() == country);
                        _country.Included = true;


                        var rootPath = GetTempFolder();
                        RefreshConfigCountry(_country, rootPath);
                        rootPath.DeleteDirectoryTree();


                        ConvertToDatatable.ConvertList <CountryParameter>(Helper.Parameters.Countries.Where(c => c.Included)
                                                                          , "available countries parameters"
                                                                          , c => c.Name
                                                                          , c => c.Included
                                                                          , c => c.OnlineVersion
                                                                          , c => c.OnlineVersion
                                                                          )
                        .PrintList()
                        ;

                        return(0);
                    });
                });

                var cmd4 = config1.Command("refresh", config2 =>
                {
                    config2.Description = "refresh list of country's configuration";
                    config2.HelpOption(HelpFlag);

                    var validator = new GroupArgument(config2, false);

                    var countryName = validator.Argument("CountryName",
                                                         "name of the country must be added in the configuration. (when argument is missing, all configuration are refreshed)"
                                                         //, ValidatorExtension.
                                                         );

                    config2.OnExecute(() =>
                    {
                        if (validator.Evaluate() > 0)
                        {
                            return(2);
                        }

                        var rootPath = GetTempFolder();

                        foreach (var item in Helper.Parameters.Countries.Where(c => c.Included))
                        {
                            if (item.LocalVersion != null && item.LocalVersion == item.OnlineVersion)
                            {
                                var target = new FileInfo(Path.Combine(Constants.RootPathConfig.FullName, item.Name) + ".json");
                                if (target.Exists)
                                {
                                    Output.WriteLine($"{item.Name} up to date in the version {item.LocalVersion}");
                                    continue;
                                }
                            }

                            RefreshConfigCountry(item, rootPath);
                        }


                        Output.WriteLine($"Clean");
                        rootPath.DeleteDirectoryTree();

                        Output.WriteLine("successfull updated");

                        return(0);
                    });
                });
            });

            return(app);
        }
Example #3
0
        public static CommandLineApplication CommandCompute(this CommandLineApplication app)
        {
            var cmd = app.Command("compute", (Action <CommandLineApplication>)(config1 =>
            {
                config1.Description = "compute country";
                config1.HelpOption(HelpFlag);

                var validator = new GroupArgument(config1, false);

                var outputName = validator.Argument("outputName",
                                                    "name of the output csv file"
                                                    , ValidatorExtension.EvaluateRequired
                                                    );

                var countYear = validator.Argument("CountYears",
                                                   "count of years must be computed. (this argument is required)"
                                                   , ValidatorExtension.EvaluateShort
                                                   );

                var countryName = validator.Argument("CountryName",
                                                     "name of the country must be computed. (if this argument is missing all config will be computed)"
                                                     , ValidatorExtension.EvaluateCountryName
                                                     );
                countryName.MultipleValues = true;



                //var countYear = validator.Option("",
                //   "count of years must be computed. (1 year by default)"
                //   , Va



                config1.OnExecute((Func <int>)(() =>
                {
                    if (validator.Evaluate() > 0)
                    {
                        return(2);
                    }

                    // Resolve arguments
                    var Namecountries = countryName.Values.Select(c => c.ToLower()).ToList();
                    string file       = outputName.Value;
                    var d             = Path.Combine(Directory.GetCurrentDirectory(), outputName.Value);
                    short cYear       = 1;
                    if (short.TryParse((string)countYear.Value.ToString(), out short cc))
                    {
                        cYear = cc;
                    }

                    // Initialisation parameters
                    var loader = new ConfigurationLoader(Constants.RootPathConfig);
                    List <CountryParameter> countriesToCompute = BuildListOfCountriesMustBeComputed(Namecountries);
                    List <DefaultCountryConfiguration> _list   = LoadConfigurations(countriesToCompute, loader);
                    var _countries = CalendariumConfiguration.GetCalendarium(_list.ToArray());

                    // Init file
                    if (File.Exists(d))
                    {
                        File.Delete(d);
                    }
                    var t = "Name;StartDate;EndDate;ObservedDate;Country;Culture;Free;Calendar";
                    File.AppendAllLines(d, new string[] { t });

                    List <EventDate> dates = new List <EventDate>();
                    foreach (var countryToCompute in countriesToCompute)
                    {
                        if (countryToCompute.OnlineVersion != countryToCompute.LocalVersion)
                        {
                            Output.WriteLine($"{countryToCompute.Name} is not up to date");
                        }

                        var items = _countries.GetNextDates(DateTime.Now, countryToCompute.Name, cYear * 365);

                        foreach (var item in items)
                        {
                            foreach (var item2 in item.Value.Events)
                            {
                                dates.Add(item2);

                                var dateEnd      = item2.DateEnd?.ToString() ?? string.Empty;
                                var dateObserved = item2.Observed?.ToString() ?? string.Empty;

                                var y = $"{item2.Name};{item.Value.Date};{dateEnd};{dateObserved};{item2.Country};{item2.Culture};{item2.Free.ToString().ToLower()};{item2.CalendarUsedToBuild.ToString().Split('.')[2].Replace("Calendar", "")}";
                                File.AppendAllLines(d, new string[] { y });
                            }
                        }
                    }

                    dates.ConvertList(
                        "computed dates"
                        , c => c.Name
                        , c => c.Country
                        , c => c.Culture
                        , c => c.Date
                        , c => c.DateEnd
                        , c => c.Observed
                        , c => c.Free
                        , c => c.Region
                        )
                    .PrintList()
                    ;

                    return(0);
                }));
            }));

            return(app);
        }