Пример #1
0
 public virtual void TearDown()
 {
     if (systemIO.DirectoryExists(this.DATAFOLDER))
     {
         systemIO.DirectoryDelete(this.DATAFOLDER, true);
     }
     if (systemIO.DirectoryExists(this.TARGETFOLDER))
     {
         systemIO.DirectoryDelete(this.TARGETFOLDER, true);
     }
     if (systemIO.DirectoryExists(this.RESTOREFOLDER))
     {
         systemIO.DirectoryDelete(this.RESTOREFOLDER, true);
     }
     if (systemIO.FileExists(this.LOGFILE))
     {
         systemIO.FileDelete(this.LOGFILE);
     }
     if (systemIO.FileExists(this.DBFILE))
     {
         systemIO.FileDelete(this.DBFILE);
     }
     if (systemIO.FileExists($"{this.DBFILE}-journal"))
     {
         systemIO.FileDelete($"{this.DBFILE}-journal");
     }
 }
Пример #2
0
        public IList <FileInfo> Directory(string path, bool useCache = true)
        {
            if (!_systemio.DirectoryExists(path))
            {
                logger.LogError(path + " not found");
                throw new ArgumentException(path + " not found");
            }

            var result = new List <FileInfo>();

            if (useCache)
            {
                if (!_systemio.DirectoryExists(path + "/.archive"))
                {
                    _systemio.DirectoryCreateDirectory(path + "/.archive");
                }
            }

            var files = _systemio.DirectoryGetFiles(path);

            foreach (var file in files)
            {
                if (useCache)
                {
                    var fileInfo = _systemio.FileInfo(file);
                    if (_systemio.FileExists(path + "/.archive/" + fileInfo.Name))
                    {
                        result.Add(_fileInfoIO.Load(path + "/.archive/" + fileInfo.Name));
                        continue;
                    }
                    else
                    {
                        var infoObject = File(file);
                        result.Add(infoObject);
                        _fileInfoIO.Save(infoObject, path + "/.archive/" + fileInfo.Name);
                    }
                    continue;
                }

                result.Add(File(file));
            }

            return(result);
        }
        public void PerformAnalysis(string rootFolder)
        {
            Console.WriteLine("Analyzing commits...");
            using (var repo = new Repository(rootFolder))
            {
                var analysis     = new Analysis();
                var renamedFiles = new Dictionary <string, string>();
                var cyclomaticComplexityCounter = new CyclomaticComplexityCounter();
                var linesOfCodeCalculator       = new LinesOfCodeCalculator();
                var typeScriptAst = new TypeScriptAST();
                foreach (var tag in repo.Tags)
                {
                    var commit     = repo.Lookup <Commit>(tag.Target.Sha);
                    var commitDate = commit.Author.When.UtcDateTime.Date;
                    analysis.Tags.Add(commitDate, tag.FriendlyName);
                }
                foreach (var branch in repo.Branches.Where(br => br.IsRemote))
                {
                    analysis.Branches.Add(branch.FriendlyName);
                }
                foreach (var commit in repo.Commits)
                {
                    var username   = commit.Author.Name;
                    var commitDate = commit.Author.When.UtcDateTime.Date;
                    UpdateAnalysisCommitDates(analysis, commitDate);
                    IncDictionaryValue(analysis.CommitsEachDay, commitDate);
                    foreach (var parent in commit.Parents)
                    {
                        var patch = repo.Diff.Compare <Patch>(parent.Tree, commit.Tree);
                        IncDictionaryValue(analysis.LinesOfCodeAddedEachDay, commitDate, patch.LinesAdded);
                        IncDictionaryValue(analysis.LinesOfCodeDeletedEachDay, commitDate, patch.LinesDeleted);
                        foreach (TreeEntryChanges change in repo.Diff.Compare <TreeChanges>(parent.Tree, commit.Tree))
                        {
                            int cyclomaticComplexity = 0;
                            int methodCount          = 0;
                            var fullPath             = Path.Combine(rootFolder, change.Path);
                            if (change.Path != change.OldPath)
                            {
                                if (analysis.FileCommits.ContainsKey(change.OldPath))
                                {
                                    analysis.FileCommits[change.Path] = analysis.FileCommits[change.OldPath];
                                    analysis.FileCommits.Remove(change.OldPath);
                                }
                                if (!renamedFiles.ContainsKey(change.OldPath))
                                {
                                    renamedFiles.Add(change.OldPath, change.Path);
                                }
                            }
                            string filename = renamedFiles.ContainsKey(change.OldPath) ? renamedFiles[change.OldPath] : change.Path;
                            var    fileType = Path.GetExtension(filename);
                            if (IgnoreFiletype(fileType))
                            {
                                break;
                            }

                            if (analysis.FileCommits.ContainsKey(filename))
                            {
                                analysis.FileCommits[filename].CommitCount++;
                            }
                            else
                            {
                                int linesOfCode = 0;
                                var fileExists  = fileHandling.FileExists(fullPath);
                                if (fileExists)
                                {
                                    var fileContents = fileHandling.ReadFileContent(fullPath);
                                    linesOfCode = linesOfCodeCalculator.Calculate(fileContents);
                                    if (change.Path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
                                    {
                                        var syntaxTree            = CodeAnalyser.GetSyntaxTree(fileContents);
                                        var methodDeclarationNode = CodeAnalyser.GetMethodDeclarationSyntaxe(syntaxTree);
                                        cyclomaticComplexity = cyclomaticComplexityCounter.Calculate(methodDeclarationNode, syntaxTree);
                                        methodCount          = MethodCounter.Calculate(methodDeclarationNode);
                                    }
                                    else if (change.Path.EndsWith(".ts", StringComparison.OrdinalIgnoreCase))
                                    {
                                        methodCount = MethodCounter.Calculate(typeScriptAst, fileContents);
                                    }
                                    analysis.LinesOfCodeanalyzed += linesOfCode;
                                }
                                analysis.FileCommits[filename] = new FileStat {
                                    Filename = filename, CyclomaticComplexity = cyclomaticComplexity, LinesOfCode = linesOfCode, MethodCount = methodCount, FileExists = fileExists
                                };
                                IncDictionaryValue(analysis.FileTypes, fileType);
                            }
                            analysis.FileCommits[filename].CommitDates.Add(commitDate);
                            if (analysis.FileCommits[filename].LatestCommit < commitDate)
                            {
                                analysis.FileCommits[filename].LatestCommit = commitDate;
                            }
                            IncDictionaryValue(analysis.CodeAge, analysis.FileCommits[filename].CodeAge);

                            var usernameFilename = UsernameFilename.GetDictKey(filename, username);
                            if (analysis.UserfileCommits.ContainsKey(usernameFilename))
                            {
                                analysis.UserfileCommits[usernameFilename].CommitCount++;
                            }
                            else
                            {
                                analysis.UserfileCommits[usernameFilename] = new FileStat {
                                    Filename = filename, Username = username
                                };
                            }
                            analysis.UserfileCommits[usernameFilename].CommitDates.Add(commitDate);
                        }
                    }
                }
                var folderStats = new Dictionary <string, int>();
                foreach (var fileChange in analysis.FileCommits)
                {
                    int    commitCount = fileChange.Value.CommitCount;
                    var    folders     = fileChange.Key.Split("/");
                    string root        = folders[0];
                    if (fileChange.Key.IndexOf("/") == -1)
                    {
                        root = ".";
                    }
                    IncFolderCommitValue(analysis.FolderCommits, root, commitCount);
                    analysis.FolderCommits[root].IsRoot = true;
                    string currentFolder = root;
                    var    children      = analysis.FolderCommits[currentFolder].Children;
                    for (int i = 1; i < folders.Length; i++)
                    {
                        currentFolder = folders[i];
                        IncFolderCommitValue(children, currentFolder, commitCount);
                        children = children[currentFolder].Children;
                    }
                    var codeAge = fileChange.Value.CommitDates.OrderByDescending(cd => cd).First();
                }
                var o = analysis.FolderCommits.OrderBy(p => p.Key);
                analysis.AnalysisTime = (DateTime.UtcNow.Ticks - analysis.CreatedDate.Ticks) / 10000; // Analysis time in milliseconds
                foreach (var report in reports)
                {
                    report.Generate(analysis);
                }
            }
        }
Пример #4
0
        public File(string url, Dictionary <string, string> options)
        {
            var uri = new Utility.Uri(url);

            m_path = uri.HostAndPath;

            if (options.ContainsKey("auth-username"))
            {
                m_username = options["auth-username"];
            }
            if (options.ContainsKey("auth-password"))
            {
                m_password = options["auth-password"];
            }
            if (!string.IsNullOrEmpty(uri.Username))
            {
                m_username = uri.Username;
            }
            if (!string.IsNullOrEmpty(uri.Password))
            {
                m_password = uri.Password;
            }

            if (!System.IO.Path.IsPathRooted(m_path))
            {
                m_path = systemIO.PathGetFullPath(m_path);
            }

            if (options.ContainsKey(OPTION_ALTERNATE_PATHS))
            {
                List <string> paths = new List <string>
                {
                    m_path
                };
                paths.AddRange(options[OPTION_ALTERNATE_PATHS].Split(new string[] { System.IO.Path.PathSeparator.ToString() }, StringSplitOptions.RemoveEmptyEntries));

                //On windows we expand the drive letter * to all drives
                if (!Platform.IsClientPosix)
                {
                    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

                    for (int i = 0; i < paths.Count; i++)
                    {
                        if (paths[i].StartsWith("*:", StringComparison.Ordinal))
                        {
                            string rpl_path = paths[i].Substring(1);
                            paths.RemoveAt(i);
                            i--;
                            foreach (System.IO.DriveInfo di in drives)
                            {
                                paths.Insert(++i, di.Name[0] + rpl_path);
                            }
                        }
                    }
                }

                string markerfile = null;

                //If there is a marker file, we do not allow the primary target path
                // to be accepted, unless it contains the marker file
                if (options.ContainsKey(OPTION_DESTINATION_MARKER))
                {
                    markerfile = options[OPTION_DESTINATION_MARKER];
                    m_path     = null;
                }

                foreach (string p in paths)
                {
                    try
                    {
                        if (systemIO.DirectoryExists(p) && (markerfile == null || systemIO.FileExists(systemIO.PathCombine(p, markerfile))))
                        {
                            m_path = p;
                            break;
                        }
                    }
                    catch
                    {
                    }
                }

                if (m_path == null)
                {
                    throw new UserInformationException(Strings.FileBackend.NoDestinationWithMarkerFileError(markerfile, paths.ToArray()), "NoDestinationWithMarker");
                }
            }

            m_moveFile                = Utility.Utility.ParseBoolOption(options, OPTION_MOVE_FILE);
            m_forceReauth             = Utility.Utility.ParseBoolOption(options, OPTION_FORCE_REAUTH);
            m_verifyDestinationLength = Utility.Utility.ParseBoolOption(options, OPTION_DISABLE_LENGTH_VERIFICATION);
            m_hasAutenticated         = false;
        }