Пример #1
0
        /// <summary>
        /// Calculates the loc metrics for the whole project.
        /// </summary>
        /// <param name="projectWithFileInfo">The project info.</param>
        /// <param name="map">The map of <see cref="ILocStats"/> objects which can calculate LoC metrics for different source file types.</param>
        /// <returns>
        /// Returns the VSProjectLocMetrics instance.
        /// </returns>
        public static VSProjectLocMetrics CalculateLocForProject(VSProjectWithFileInfo projectWithFileInfo, LocStatsMap map)
        {
            VSProjectLocMetrics projectMetrics = new VSProjectLocMetrics(projectWithFileInfo);

            //For each Item file in project
            foreach (VSProjectItem item in projectWithFileInfo.Project.Items)
            {
                if (item.ItemType == VSProjectItem.CompileItem ||
                    item.ItemType == VSProjectItem.Content)
                {
                    string filePath = Path.Combine(
                        projectMetrics.ProjectWithFileInfo.ProjectDirectoryPath,
                        item.Item);
                    SourceFileLocMetrics sourceFile = SourceFileLocMetrics.CalcLocStatData(filePath, map);

                    // make sure the file was not ignored (it wasn't a source file)
                    if (sourceFile != null)
                    {
                        projectMetrics.AddLocMetrics(sourceFile);
                    }
                }
            }

            return(projectMetrics);
        }
        /// <summary>
        /// Calculates the loc stat data of the file.
        /// </summary>
        /// <param name="sourceFileName">The source file to analyze.</param>
        /// <param name="map">The map of <see cref="ILocStats"/> objects which can calculate LoC metrics for different source file types.</param>
        /// <returns>A newly created <see cref="SourceFileLocMetrics"/> object containing LoC data for the source file;
        /// <c>null</c> if the file was ignored by the <see cref="map"/></returns>
        public static SourceFileLocMetrics CalcLocStatData(string sourceFileName, LocStatsMap map)
        {
            SourceFileLocMetrics metrics = new SourceFileLocMetrics(sourceFileName);

            using (Stream streamOfFile = File.OpenRead(sourceFileName))
            {
                string fileExtension = Path.GetExtension(sourceFileName);

                ILocStats locStats = map.GetLocStatsForExtension(fileExtension);
                if (locStats != null)
                {
                    metrics.locStatsData = locStats.CountLocStream(streamOfFile);
                    return(metrics);
                }
            }

            return(null);
        }