Ejemplo n.º 1
1
		// args[0] = Dump path
		// args[1] = Number of packages to fetch
		// args[2] = Dump partition size

		static void Main(string[] args)
		{
			var client = new ODataClient("https://www.nuget.org/api/v2/");

			var totalPackageCount = client.For<FeedPackage>("packages").Count().FindScalarAsync<int>().Result;

			var dumpPath = args[0];
			var numberOfPackages = (args[1] == "0") ? totalPackageCount : Convert.ToInt32(args[1]);
			var partitionSize = (args[2] == "0") ? totalPackageCount : Convert.ToInt32(args[2]);

			var startTime = DateTime.Now;
			var take = Math.Min(100, numberOfPackages);
			var numberOfPages = (int)Math.Ceiling((double)numberOfPackages / (double)take);

			var sync = new object();
			var packages = new List<FeedPackage>();
			int page = 0, partition = 0;
			var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 4 };
			using (var pbar = new ProgressBar(numberOfPages, "Downloading nuget data.."))
			{
				Parallel.For(0, numberOfPages, parallelOptions, (i, state) =>
				{
					var foundPackages = client.For<FeedPackage>("packages")
					.Skip(i * take)
					.Top(take)
					.FindEntriesAsync().Result;

					lock (sync)
					{
						packages.AddRange(foundPackages);
						if (packages.Count() >= partitionSize)
						{
							WritePackages(packages, dumpPath, partition);
							partition++;
							packages = new List<FeedPackage>();
						}
					}
					Interlocked.Increment(ref page);
					pbar.Tick(string.Format("Downloaded {0}/{1} pages, written {2} files", page, numberOfPages, partition));
				});
			}

			var span = DateTime.Now - startTime;
			Console.WriteLine("Harvesting completed in: {0}.", span);
			Console.WriteLine("Press any key to quit");
			Console.Read();
		}
Ejemplo n.º 2
1
        //this method is declared as static so it can be run in
        //another static method
        //I've changed this method a bit to read directly into a List<>
        //instead of just a string
        private static List<RosterItem> ReadFileToRosterList(string FileName)
        {
            //I always like to start my method by declaring the return
            //value when there is a value to be returned
            List<RosterItem> returnValue = new List<RosterItem>();

            //set up a counter and line variable
            int counter = 0;
            string line;

            //always be aware of what resources your program is using
            //The C# garbage collector does a pretty good job of maintaining program
            //resources, but you shouldn't count on it whe using librarys that
            //acces external resources sunch as files or databases

            //create a progress bar using a nuget package called ShellProgressBar
            //https://github.com/Mpdreamz/shellprogressbar
            //found this by googling "c# console app progress bar"
            //the max number of items to process
            var maxTicks = File.ReadLines(FileName).Count();

            //create a progress bar
            using (var pbar = new ProgressBar(maxTicks, "Starting", ConsoleColor.DarkGreen))
            {
                //we use a "using" statement to make sure the file resource is released from
                //memory. A try..catch..finally can be used but a "using" statement is easier.
                using (System.IO.StreamReader file = new System.IO.StreamReader(FileName))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        //update the progress bar
                        pbar.Tick("Currently processing " + counter);

                        //skip the first line
                        if (counter > 0)
                        {
                            //returnValue += line;
                            RosterItem myRosterItem = new RosterItem();

                            string[] myLineArray = line.Split(',');

                            myRosterItem.ID = Convert.ToInt32(myLineArray[0]);
                            myRosterItem.FirstName = myLineArray[1];
                            myRosterItem.LastName = myLineArray[2];
                            myRosterItem.Email = myLineArray[3];
                            myRosterItem.TeamColor = myLineArray[4];
                            myRosterItem.Gender = myLineArray[5];
                            myRosterItem.Position = myLineArray[6];

                            //now that we have a completed object
                            //add it to the returnvalue (Remember the special List<> gives us the ad method automatically)
                            returnValue.Add(myRosterItem);

                            //artifically add a little time to the process
                            System.Threading.Thread.Sleep(250);
                        }

                        counter++;
                    }
                }
            }
            return returnValue;
        }
Ejemplo n.º 3
0
		private static RestApiSpec CreateRestApiSpecModel(string downloadBranch, string[] folders)
		{
			var directories = Directory.GetDirectories(CodeConfiguration.RestSpecificationFolder, "*", SearchOption.AllDirectories)
				.Where(f=>folders == null || folders.Length == 0 || folders.Contains(new DirectoryInfo(f).Name))
				.ToList();

			var endpoints = new Dictionary<string, ApiEndpoint>();
			using (var pbar = new ProgressBar(directories.Count, $"Listing {directories.Count} directories", new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
			{
				foreach (var jsonFiles in directories.Select(dir => Directory.GetFiles(dir).Where(f => f.EndsWith(".json")).ToList()))
				{
					using (var fileProgress = pbar.Spawn(jsonFiles.Count, $"Listing {jsonFiles.Count} files", new ProgressBarOptions { ProgressCharacter = '─', BackgroundColor = ConsoleColor.DarkGray }))
					{
						foreach (var endpoint in jsonFiles.Select(CreateApiEndpoint))
						{
							endpoints.Add(endpoint.Key, endpoint.Value);
							fileProgress.Tick();
						}
					}
					pbar.Tick();
				}
			}

			return new RestApiSpec { Endpoints = endpoints, Commit = downloadBranch };
		}
Ejemplo n.º 4
0
        public static void GenerateProject(YamlSpecification specification)
        {
            var project = CsProjFile.LoadFrom(_testProjectFolder + @"Elasticsearch.Net.Integration.Yaml.csproj");
            var existingYamlTests = project.All<CodeFile>().Where(c=>c.Link != null && c.Link.EndsWith(".yaml.cs"));
            foreach (var c in existingYamlTests)
                project.Remove(c);

            var definitions = specification.Definitions;

            using (var pbar = new ProgressBar(definitions.Count, "Generating Code and project for yaml tests", ConsoleColor.Blue))
            foreach (var kv in specification.Definitions)
            {
                var folder = kv.Key;
                foreach (var d in kv.Value)
                {
                    var path = folder + @"\" + d.FileName + ".cs";
                    if (!Directory.Exists(_testProjectFolder + folder))
                        Directory.CreateDirectory(_testProjectFolder + folder);

                    GenerateTestFileFromView(_testProjectFolder + path, d);
                    project.Add<CodeFile>(path);
                }
                pbar.Tick();
            }
            project.Save();
        }
Ejemplo n.º 5
0
		public static void Generate(string downloadBranch, params string[] folders)
		{
			var spec = CreateRestApiSpecModel(downloadBranch, folders);
			var actions = new Dictionary<Action<RestApiSpec>, string>
			{
				{  GenerateClientInterface, "Client interface" },
				{  GenerateRequestParameters, "Request parameters" },
				{  GenerateRequestParametersExtensions, "Request parameters override" },
				{  GenerateDescriptors, "Descriptors" },
				{  GenerateRequests, "Requests" },
				{  GenerateEnums, "Enums" },
				{  GenerateRawClient, "Lowlevel client" },
				{  GenerateRawDispatch, "Dispatch" },
			};

			using (var pbar = new ProgressBar(actions.Count, "Generating code", new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }))
			{
				foreach(var kv in actions)
				{
					pbar.UpdateMessage("Generating " + kv.Value);
					kv.Key(spec);
					pbar.Tick("Generated " + kv.Value);
				}
			}
		}
Ejemplo n.º 6
0
        public ProgressBar(int numTtf, int numTtc)
        {
            NumTtf = numTtf;
            NumTtc = numTtc;

            OverallProgressBar = new ShellProgressBar.ProgressBar(numTtf + numTtc,
                                                                  $"{numTtf + numTtc} fonts to be processed.", OverallProgressBarOptions);
        }
Ejemplo n.º 7
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposedValue)
            {
                return;
            }
            if (disposing)
            {
                _progressBar?.Dispose();
                _progressBar = null;
            }

            _disposedValue = true;
        }
Ejemplo n.º 8
0
        private void WriteToFile(TextWriter outWriter, IEnumerable <GroupDescriptor> consecutiveGroups)
        {
            using (var pbar = new ShellProgressBar.ProgressBar(3000, "Writing groups", ConsoleColor.Cyan))
            {
                int i = 0;
                foreach (GroupDescriptor groupDescriptor in consecutiveGroups)
                {
                    if (i++ % 500000 == 0)
                    {
                        pbar.Tick("Writing groups " + groupDescriptor.ConsecutiveStart);
                    }

                    string formatedSequence = _groupConverter.Convert(groupDescriptor.GetSequence());
                    outWriter.WriteLine(formatedSequence);
                }
            }
        }
        private static async Task <string> IteratePlaylists(List <string> playlists, string network, string region)
        {
            string hits = string.Empty;

            using (ShellProgressBar.ProgressBar progressBar = new ShellProgressBar.ProgressBar(playlists.Count, string.Empty, ConsoleColor.Green))
            {
                using HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(3) };
                await playlists.AsParallel().AsOrdered().ParallelForEachAsync(async playlist =>
                {
                    string[] lines = File.ReadAllLines(playlist);
                    using (ChildProgressBar child = progressBar.Spawn(lines.Length,
                                                                      "processing " + Path.GetFileName(playlist),
                                                                      new ProgressBarOptions {
                        ProgressCharacter = '-', CollapseWhenFinished = true
                    }))
                    {
                        for (int i = 0; i < lines.Length; i++)
                        {
                            if (ContainsStream(lines[i], network, region))
                            {
                                try
                                {
                                    if (!string.IsNullOrEmpty(await client.GetStringAsync(lines[i + 1])))
                                    {
                                        hits += lines[i] + "\n" + lines[i + 1] + "\n";
                                    } // take the link that should be after the line, test if working
                                }
                                catch (Exception e) when(e is HttpRequestException || e is InvalidOperationException)
                                {
                                }                                                                                          // some links are dumb, this fixes program from breaking from dumb links
                            }
                            child.Tick();
                        }
                    }
                    progressBar.Tick();
                }, maxDegreeOfParallelism: Environment.ProcessorCount * 5);

                while (progressBar.CurrentTick < progressBar.MaxTicks)
                {
                    progressBar.Tick();
                } // progress bar percentage correction in case spaghetti
            }
            return(hits);
        }
Ejemplo n.º 10
0
		public static YamlSpecification GetYamlTestSpecification(bool useCache = false)
		{
			var folders = GetTestFolders(useCache);

			var yamlFiles = new ConcurrentDictionary<string, IList<YamlDefinition>>();
			using (var pbar = new ProgressBar(folders.Count, "Finding all the yaml files"))
			//Parallel.ForEach(folders, (folder) =>
				foreach (var folder in folders)
				{
					var definitions = GetFolderFiles(folder, useCache);
					yamlFiles.TryAdd(folder, definitions);
					pbar.Tick(string.Format("Found {0} yaml test files in {1}", definitions.Count(), folder));
					//});
				}
			return new YamlSpecification
			{
				Definitions = yamlFiles.ToDictionary(k => k.Key, v => v.Value)
			};
		}
		private RestSpecDownloader(string branch)
		{
			var specifications =
				(from kv in OnlineSpecifications
					let url = kv.Value.Replace("{version}", branch)
					select new Specification { FolderOnDisk = kv.Key, Branch = branch, GithubListingUrl = url }).ToList();


			using (var pbar = new ProgressBar(specifications.Count, "Downloading specifications", MainProgressBarOptions))
			{
				foreach (var spec in specifications)
				{
					pbar.UpdateMessage($"Downloading rest-api-spec to {spec.FolderOnDisk} for branch {branch}");
					DownloadJsonDefinitions(spec, pbar);
					pbar.Tick($"Downloaded rest-api-spec to {spec.FolderOnDisk} for branch {branch}");
				}
			}

			File.WriteAllText(CodeConfiguration.LastDownloadedVersionFile, branch);
		}
Ejemplo n.º 12
0
        static async Task MainAsync(string[] args)
        {
            var fileList = ConfigurationManager.AppSettings["EmailsList"];
            var outputFileList = ConfigurationManager.AppSettings["OutputEmailsList"];

            emailsToValidate = File.ReadAllLines(fileList);

            var maxTicks = emailsToValidate.Length;
            using (var pbar = new ProgressBar(maxTicks, "Starting", ConsoleColor.Green, '\u2593'))
            {
                var allProviders = GetProvidersGrouped(emailsToValidate);

                if (allProviders != null)
                {
                    var emailsChecklist = new Dictionary<string, EmailInfo>();

                    foreach (var provider in allProviders)
                    {
                        try
                        {
                            var mxInfo = await FindServerInfoAsync(provider.Key);
                            var allProvidersInfo = ParseMxServerResults(mxInfo);

                            var mxToUse = allProvidersInfo.OrderBy(
                                p => p.Preference
                            ).FirstOrDefault();

                            var emailsChecked = CheckEmailsOrquestrator(
                                32,
                                provider.Value,
                                mxToUse?.Address,
                                SMTP_PORT,
                                pbar
                            );

                            foreach (var email in emailsChecked)
                            {
                                emailsChecklist.Add(
                                    email.Key,
                                    email.Value
                                );
                            }
                        }
                        catch (Exception ex)
                        {
                            _log.Error(string.Empty, ex);
                        }
                    }

                    if (emailsChecklist != null && emailsChecklist.Count > 0)
                    {
                        var outputFileName = Path.Combine(
                            outputFileList,
                            string.Format(
                                "output-{0}.csv",
                                DateTime.Now.ToString("yyyyMMddHHmmssfff")
                            )
                        );

                        if (!Directory.Exists(outputFileList))
                            Directory.CreateDirectory(outputFileList);

                        using (var writer = new StreamWriter(outputFileName))
                        {
                            writer.WriteLine("email;provider;email;");

                            foreach (var emailChecked in emailsChecklist)
                            {
                                writer.WriteLine(
                                    string.Format(
                                        "{0};{1};{2};",
                                        emailChecked.Key,
                                        emailChecked.Value.ProviderExists,
                                        emailChecked.Value.EmailResolution
                                    )
                                );
                            }

                            writer.Flush();
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
        static async Task<EmailInfo> CheckEmailAddresses(
            string email,
            string mxHostAddress,
            int port,
            ProgressBar pbar
        )
        {
            pbar.Tick($"Currently processing: #{pbar.CurrentTick} emails searched...");

            var mailInfo = new EmailInfo
            {
                Email = email,
                ProviderExists = false,
                EmailResolution = EmailResolutionType.CantDetemine
            };

            try
            {
                if (!string.IsNullOrEmpty(mxHostAddress) && !string.IsNullOrEmpty(email))
                {
                    using (var telnetClient = new Client(
                        mxHostAddress,
                        port,
                        CancellationToken.None
                    ))
                    {
                        if (mailInfo.ProviderExists = telnetClient.IsConnected)
                        {
                            mailInfo.Email = email;
                            await telnetClient.Write("HELO localhost\r\n");
                            await telnetClient.ReadAsync(new TimeSpan(0, 0, 10));

                            await telnetClient.Write("MAIL FROM:<*****@*****.**>\r\n");
                            await telnetClient.ReadAsync(new TimeSpan(0, 0, 10));

                            await telnetClient.Write(
                                string.Format("RCPT TO:<{0}>\r\n", email)
                            );

                            var response = await telnetClient.ReadAsync(new TimeSpan(0, 1, 0));

                            if (response.StartsWith("2"))
                                mailInfo.EmailResolution = EmailResolutionType.Exist;
                            else if (response.StartsWith("5") && response.Contains("exist"))
                                mailInfo.EmailResolution = EmailResolutionType.NotExist;
                            else
                                mailInfo.EmailResolution = EmailResolutionType.CantDetemine;
                        }

                        await telnetClient.Write("QUIT");
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error(string.Empty, ex);
            }

            return mailInfo;
        }
Ejemplo n.º 14
0
        static IReadOnlyDictionary<string, EmailInfo> CheckEmailsOrquestrator(
            int maxExecutions,
            IEnumerable<string> emails,
            string mxHostAddress,
            int port,
            ProgressBar pbar
        )
        {
            var emailsChecklist = new Dictionary<string, EmailInfo>(
                emails.Count()
            );

            var mailsCount = emails.Count();
            for (int i = 0; i < mailsCount; i += maxExecutions)
            {
                var emailsToExecute = emails.Skip(i).Take(maxExecutions);
                var allTasks = emailsToExecute.Select(
                    p => CheckEmailAddresses(p, mxHostAddress, port, pbar)
                ).ToArray();

                Task.WaitAll(allTasks);

                foreach (var task in allTasks)
                {
                    var taskResult = task.Result;
                    if (!emailsChecklist.ContainsKey(taskResult.Email))
                    {
                        emailsChecklist.Add(taskResult.Email, taskResult);
                    }
                }
            }

            return emailsChecklist;
        }
Ejemplo n.º 15
0
 public void Start(int maxTicks, string message) => _progressBar = new ShellProgressBar.ProgressBar(Math.Max(maxTicks, 1), message, _options);
Ejemplo n.º 16
0
        public static void Run(string[] args, ConfiguratorInputOptions InputOptions)
        {
            ShellProgressBarHack.PatchExcerptFunc();

            var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            log = new StreamWriter(File.Open(Path.Combine(rootPath, LogFilePath), FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.UTF8);
            var localPackageDirPath = Path.Combine(rootPath, "Pkg");
            var profileDirPath      = Path.Combine(rootPath, "Prfl");

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Unhandled);
            DateTime now = DateTime.Now;

            log.Log(now.ToString("F", new CultureInfo("en-US")));
            log.Log("Configurator for Dotnet Runtime Patcher by Meigyoku Thmn");
            log.Log($"Version {Application.ProductVersion}");
            Console.OutputEncoding = Encoding.UTF8;
            var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            Package selectedPackage = new Package(null, null, null);
            string  targetPath      = null;

            if (InputOptions.Specify == true)
            {
                if (InputOptions.Id == null || InputOptions.Name == null || InputOptions.ServerUrl == null || InputOptions.TargetPath == null)
                {
                    log.Log("Invalid arg syntax!");
                    Environment.Exit(-1);
                }
                selectedPackage.Id        = InputOptions.Id;
                selectedPackage.Name      = InputOptions.Name;
                selectedPackage.ServerUrl = InputOptions.ServerUrl;
                targetPath = InputOptions.TargetPath;
                goto BeforeStep3;
            }

            // STEP 1: List all servers/packages, then ask user to select a package
            log.Log($"Get server list from {SystemCfgUrl}\n");
            string systemCfgStr;

            using (var wc = new WebClient {
                Encoding = Encoding.UTF8
            }) {
                systemCfgStr = wc.DownloadString(SystemCfgUrl);
            }
            var systemCfg = JObject.Parse(systemCfgStr);
            var servers   = systemCfg["servers"].Children <JProperty>().Aggregate(new List <JObject>(), (acc, server) => {
                var serverUri = new Uri((string)server.Value).Concat("server.jsonc");
                try {
                    string serverCfgStr;
                    using (var wc = new WebClient {
                        Encoding = Encoding.UTF8
                    }) {
                        serverCfgStr = wc.DownloadString(serverUri);
                    }
                    var serverCfg    = JObject.Parse(serverCfgStr);
                    serverCfg["url"] = serverUri.ToString();
                    serverCfg["id"]  = server.Name;
                    acc.Add(serverCfg);
                }
                catch (Exception err) {
                    log.Log($"Url: {serverUri}");
                    log.Log(err.Message);
                }
                return(acc);
            });
            var count    = 0;
            var packages = new List <Package>();

            foreach (var serverCfg in servers)
            {
                log.Log(" " + serverCfg["title"]);
                foreach (JProperty package in serverCfg["packages"])
                {
                    log.Log($"  [{count++}] {package.Name}: {package.Value}");
                    packages.Add(new Package(package.Name, (string)serverCfg["id"], (string)serverCfg["url"]));
                }
            }
            log.Log($"\nPlease select your desired package (from 0 to {count - 1}): ");
            int selectedIndex;

            while (!int.TryParse(Console.ReadLine(), out selectedIndex) || (selectedIndex > count - 1 || selectedIndex < 0))
            {
                ;
            }
            log.WriteLine(selectedIndex);
            selectedPackage = packages[selectedIndex];
            if (selectedPackage.Id.IndexOf('/') != -1 || selectedPackage.Name.IndexOf('/') != -1 || selectedPackage.Id.IndexOf('\\') != -1 || selectedPackage.Name.IndexOf('\\') != -1)
            {
                log.Log($"Package name ({selectedPackage.Name}) or package id ({selectedPackage.Id}) has slash character, our system cannot allow such character.");
                return;
            }

            // STEP 2: ask user to select the exe that they want to patch
            log.Log("\nPlease select your target executable file.");
            OpenFileDialog openFileDg = new OpenFileDialog();

            openFileDg.Title           = "Please select your target executable file";
            openFileDg.Filter          = "Executable file|*.exe|All files|*.*";
            openFileDg.CheckFileExists = true;
            openFileDg.CheckPathExists = true;
            var dialogRs = openFileDg.ShowDialog();

            if (dialogRs == DialogResult.Cancel)
            {
                return;
            }
            targetPath = openFileDg.FileName;

BeforeStep3:
            log.Log(targetPath);
            var targetInfo = GetChecksumAndSize(targetPath);

            // STEP 3: download packages
            log.Log();
            log.Log("Selected Package:");
            log.Log($"  Name = {selectedPackage.Name}");
            log.Log($"  Id = {selectedPackage.Id}");
            log.Log($"  ServerUrl = {selectedPackage.ServerUrl}");
            var    versionUri = new Uri(selectedPackage.ServerUrl).Concat(selectedPackage.Name, "versions.jsonc");
            string versionCfgStr;

            using (var wc = new WebClient {
                Encoding = Encoding.UTF8
            }) {
                versionCfgStr = wc.DownloadString(versionUri);
            }
            var versionCfg    = JObject.Parse(versionCfgStr);
            var targetVersion = versionCfg[targetInfo.Hash];

            if (targetVersion == null || (long)targetVersion["size"] != targetInfo.Size)
            {
                log.Log("We can't find proper patch for your application file, maybe your app is a newer version, or we don't have a patch for it.");
                return;
            }
            var patchName = (string)targetVersion["name"];

            if (patchName == null || patchName.IndexOf("/") != -1 || patchName.IndexOf("\\") != -1)
            {
                log.Log("We found a patch, but its patch name is null or contains slash character, such character is invalid.");
                log.Log($"Hash: {targetInfo.Hash}");
                log.Log($"Name: {patchName}");
                log.Log($"Ver: {targetVersion["ver"]}");
                log.Log($"Size: {targetInfo.Size}");
                return;
            }
            var    patchRoot = patchName + '/';
            var    filesUri  = new Uri(selectedPackage.ServerUrl).Concat(selectedPackage.Name, "files.jsonc");
            string filesCfgStr;

            using (var wc = new WebClient {
                Encoding = Encoding.UTF8
            }) {
                filesCfgStr = wc.DownloadString(filesUri);
            }
            var     filesCfg     = JObject.Parse(filesCfgStr);
            var     files        = filesCfg.Children <JProperty>().Where(file => file.Name.IndexOf(patchRoot, 0, patchRoot.Length) != -1);
            var     oldFilesPath = Path.Combine(localPackageDirPath, selectedPackage.Id, selectedPackage.Name, "files.jsonc");
            JObject oldFiles;

            try {
                log.Log($"Read local file list: {oldFilesPath}");
                oldFiles = JObject.Parse(File.ReadAllText(oldFilesPath));
            }
            catch (Exception ex) when(ex is FileNotFoundException || ex is DirectoryNotFoundException)
            {
                log.Log("Local file list doesn't exist.");
                oldFiles = new JObject();
            }
            Console.Clear();
            Console.WriteLine(); // for some reason, on some computers, the first line of console is updated very slow, so I just leave the first line alone
            var writeCount = 0;
            var skipCount  = 0;
            int totalTicks = files.Count();
            var options    = new ProgressBarOptions {
                BackgroundCharacter   = '\u2593',
                ProgressBarOnBottom   = true,
                EnableTaskBarProgress = true,
                ForegroundColor       = ConsoleColor.Yellow,
                ForegroundColorDone   = ConsoleColor.Green,
                BackgroundColor       = ConsoleColor.DarkGray,
            };
            var childOptions = new ProgressBarOptions {
                BackgroundCharacter = '\u2593',
                ProgressBarOnBottom = true,
                ForegroundColor     = ConsoleColor.Yellow,
                ForegroundColorDone = ConsoleColor.Green,
                BackgroundColor     = ConsoleColor.DarkGray,
            };
            var mainMessage = "Files downloading progress";

            using (var pbar = new SProgressBar(totalTicks, mainMessage, options)) {
                pbar.Tick(0, $"0/{totalTicks}");
                // use 5 connection as the maximium, unless you want your whole country blocked by github
                Parallel.ForEach(Partitioner.Create(files, EnumerablePartitionerOptions.NoBuffering), new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 5
                }, (file) => {
                    var oldChecksum = oldFiles[file.Name];
                    if (oldChecksum != null && (uint)file.Value == (uint)oldChecksum)
                    {
                        pbar.Tick($"{pbar.CurrentTick + 1}/{totalTicks} {mainMessage}");
                        Interlocked.Increment(ref skipCount);
                        return;
                    }
                    var fileUri  = new Uri(selectedPackage.ServerUrl).Concat(selectedPackage.Name, file.Name);
                    var filePath = Path.Combine(localPackageDirPath, selectedPackage.Id, selectedPackage.Name, file.Name);
                    log.Log(fileUri.ToString(), noPrint: true, useLock: true);
                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    new Func <Task>(async() => {
                        using (var child = pbar.Spawn(100, fileUri.ToString(), childOptions))
                            using (var wc = new WebClient {
                                Encoding = Encoding.UTF8
                            }) {
                                child.Tick(0);
                                wc.DownloadProgressChanged += (sender, e) => {
                                    child.Tick(e.ProgressPercentage);
                                };
                                await wc.DownloadFileTaskAsync(fileUri, filePath);
                            }
                    })().Wait();
                    pbar.Tick($"{pbar.CurrentTick + 1}/{totalTicks} {mainMessage}");
                    Interlocked.Increment(ref writeCount);
                });
            }
            log.Log($"Downloaded {writeCount} file(s)");
            log.Log($"Skipped {skipCount} file(s)");
            log.Log($"Write new file list to {oldFilesPath}");
            Directory.CreateDirectory(Path.GetDirectoryName(oldFilesPath));
            File.WriteAllText(oldFilesPath, filesCfgStr);
            var versionsPath = Path.Combine(localPackageDirPath, selectedPackage.Id, selectedPackage.Name, "versions.jsonc");

            log.Log($"Write {versionsPath}");
            Directory.CreateDirectory(Path.GetDirectoryName(versionsPath));
            File.WriteAllText(versionsPath, versionCfgStr);

            if (InputOptions.Specify == true)
            {
                goto AfterAllSteps;
            }

            var urlFilePath = Path.Combine(localPackageDirPath, selectedPackage.Id, "urls.jsonc");
            var urlFileCfg  = new JObject();

            urlFileCfg["serverUrl"] = selectedPackage.ServerUrl;
            log.Log($"Write {urlFilePath}");
            Directory.CreateDirectory(Path.GetDirectoryName(urlFilePath));
            File.WriteAllText(urlFilePath, urlFileCfg.ToString());

            // STEP 4: create config file
            var defaultShortcutName = $"{targetVersion["name"]}_{selectedPackage.Name}";
            var exeCfg = new JObject();

            exeCfg["targetPath"] = targetPath;
            exeCfg["package"]    = $"{selectedPackage.Id}/{selectedPackage.Name}";
            log.Log($"Write profile {defaultShortcutName + ".jsonc"} to root directory");
            var exeProfilePath = Path.Combine(profileDirPath, defaultShortcutName + ".jsonc");

            Directory.CreateDirectory(Path.GetDirectoryName(exeProfilePath));
            File.WriteAllText(exeProfilePath, exeCfg.ToString());

            // STEP 5: create shortcut on desktop
            log.Log("\nPlease set a name of a shortcut file that we will create on your desktop:");
            log.Log($"(Default: {defaultShortcutName})");
            var newShortcutName = Console.ReadLine().Trim();

            log.WriteLine(newShortcutName);
            if (newShortcutName.Length == 0)
            {
                newShortcutName = defaultShortcutName;
            }
            if (!newShortcutName.IsValidPath())
            {
                log.Log("Invalid path, use the default instead.");
                newShortcutName = defaultShortcutName;
            }
            var arguments = $"\"{defaultShortcutName}\"";

            CreateShortcut(
                Path.Combine(desktopPath, newShortcutName + ".lnk"),
                typeof(RuntimePatcher.Helper).Assembly.Location,
                arguments,
                targetPath + ", 0"
                );
            log.Log("Created a shortcut on desktop.");

AfterAllSteps:
            log.Log("\nAll done! Press enter to exit this wizard.");
            log.Close();
            Console.ReadLine();
        }
Ejemplo n.º 17
0
 public void Start(int maxTicks, string message)
 {
     _progressBar = new ShellProgressBar.ProgressBar(maxTicks, message, _options);
 }
Ejemplo n.º 18
0
 public void StartTask(string name, int maxTicks)
 {
     _progressBar = new ProgressBar(maxTicks, name);
 }
Ejemplo n.º 19
0
        static void SaveExerciseResults(List<ExerciseResult> exerciseResults)
        {
            if (exerciseResults.Any())
            {
                var ticks = exerciseResults.Count/1000;
                var rest = exerciseResults.Count%1000;
                var kernel = InitializeKernel();
                var exerciseResultService = kernel.Get<IExerciseResultService>();
                Console.WriteLine("Starting full data removal...");
                exerciseResultService.DeleteAllData();
                Console.WriteLine("All data removed...");

                Console.WriteLine("Starting adding related data...");
                exerciseResultService.SaveRelatedData(exerciseResults);

                var subjects = exerciseResultService.GetSubjects();
                var domains = exerciseResultService.GetDomains();
                var learningObjectives = exerciseResultService.GetLearningObjectives();

                foreach (var exerciseResult in exerciseResults)
                {
                    exerciseResult.SubjectId = subjects.FirstOrDefault(x => x.Name == exerciseResult.Subject).Id;
                    exerciseResult.DomainId = domains.FirstOrDefault(x => x.Name == exerciseResult.Domain).Id;
                    exerciseResult.LearningObjectiveId = learningObjectives.FirstOrDefault(x => x.Name == exerciseResult.LearningObjective).Id;

                }

                using (var pbar = new ProgressBar(ticks, "Starting", ConsoleColor.Cyan, '\u2593'))
                {
                    for (var i = 0; i <= ticks; i++)
                    {
                        var index = i == 0 ? 0 : i*1000;
                        var exercises = i==ticks? exerciseResults.GetRange(index, rest) : exerciseResults.GetRange(index, 1000);
                        exerciseResultService.SaveExerciseResults(exercises);
                        var message = "Curently saving " + index + " to " + (i == ticks ? index + rest : index + 1000 )+ " records";
                        pbar.Tick(message);

                    }
                }
            }
        }