Esempio n. 1
0
        /// <summary>
        ///     Downloads project code for all registered projects.
        /// </summary>
        public void DownloadProjects()
        {
            DateTime currentDate = DateTime.Now.Date;

            FileInfo[] users = FileStore.GetFiles(Resources.ProjectDirectory);

            int userTotal   = users.Length;
            int userCurrent = 0;

            Logger.Log(string.Format("Downloading code for {0} users.", userTotal));

            // Iterate over users
            foreach (FileInfo user in users)
            {
                string username = user.Name.Remove(user.Name.Length - 5);

                userCurrent++;
                Logger.Log(LoggerHelper.FormatProgress(
                               "Downloading code for user " + LoggerHelper.ForceLength(username, 10), userCurrent, userTotal));

                JArray projects;
                try
                {
                    projects = JArray.Parse(FileStore.ReadFile(Resources.ProjectDirectory, username + ".json"));
                }
                catch (JsonReaderException e)
                {
                    Logger.Log("Could not parse list of projects of user `" + username + "`", e);
                    return;
                }

                // Iterate over user projects
                foreach (JToken project in projects)
                {
                    DateTime modifyDate = DateTime.Parse(project["history"]["modified"].ToString()).Date;

                    int    projectId         = Convert.ToInt32(project["id"].ToString());
                    string codeDir           = Resources.CodeDirectory + "/" + projectId;
                    string yesterdayFileName = currentDate.AddDays(-1).ToString("yyyy-MM-dd") + ".json";
                    string todayFileName     = currentDate.ToString("yyyy-MM-dd") + ".json";

                    if (FileStore.FileExists(codeDir, todayFileName))
                    {
                        // Code already downloaded today
                        continue;
                    }

                    if (currentDate.Subtract(modifyDate).Days > 0 && FileStore.FileExists(codeDir, yesterdayFileName))
                    {
                        // No code modifications in last day, copy old file
                        FileStore.CopyFile(codeDir, yesterdayFileName, codeDir, todayFileName);
                        continue;
                    }

                    string projectCode = GetProjectCode(projectId);
                    if (projectCode == null)
                    {
                        // Code not downloaded for whatever reason
                        continue;
                    }
                    if (!Downloader.IsValidJson(projectCode))
                    {
                        // Invalid JSON, no need to save it
                        continue;
                    }

                    FileStore.WriteFile(codeDir, todayFileName, projectCode);
                }
            }

            Logger.Log(string.Format("Successfully downloaded code for {0} users.\n", userCurrent));
        }