CountLines

From Unify Community Wiki
Revision as of 20:45, 10 January 2012 by NCarter (Talk | contribs)

Jump to: navigation, search

Author: Scrat

Description

Count all the files and lines in your project.

Usage

Place it inside the Editor folder. Then, you can click "Custom/Stats/Count Lines"

Note: this was developed to be used on Unity iPhone. So the GUI is very basic (just a DisplayDialog).

With Unity 2.5 out, it would be easy to add more advanced looking windows...

C# - CountLines.cs

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class CountLines: Editor{
 
	struct File
	{
		public string 	name;
		public int 		nbLines;
 
		public File(string name, int nbLines)
		{
			this.name 		= name;
			this.nbLines 	= nbLines;
		}
	}	
 
	[MenuItem("Custom/Stats/Count Lines")]
	static void CountLines()
	{		
		string strDir = System.IO.Directory.GetCurrentDirectory();
		strDir += @"/Assets";
		int iLengthOfRootPath = strDir.Length;
		ArrayList stats = new ArrayList();	
		ProcessDirectory(stats, strDir);	
 
		int iTotalNbLines = 0;
		foreach(File f in stats)
		{
			iTotalNbLines += f.nbLines;
		}
 
		System.Text.StringBuilder strStats = new System.Text.StringBuilder();
		strStats.Append("Number of Files: " + stats.Count + "\n");		
		strStats.Append("Number of Lines: " + iTotalNbLines + "\n");	
		strStats.Append("================\n");	
 
		foreach(File f in stats)
		{
			strStats.Append(f.name.Substring(iLengthOfRootPath+1, f.name.Length-iLengthOfRootPath-1) + " --> " + f.nbLines + "\n");
		}		
 
		EditorUtility.DisplayDialog("Statistics", strStats.ToString(), "Ok");
	}
 
	static void ProcessDirectory(ArrayList stats, string dir)
	{	
        string[] strArrFiles = System.IO.Directory.GetFiles(dir, "*.cs");
        foreach (string strFileName in strArrFiles)
            ProcessFile(stats, strFileName);
 
        strArrFiles = System.IO.Directory.GetFiles(dir, "*.js");
        foreach (string strFileName in strArrFiles)
            ProcessFile(stats, strFileName);
 
        string[] strArrSubDir = System.IO.Directory.GetDirectories(dir);
        foreach (string strSubDir in strArrSubDir)
            ProcessDirectory(stats, strSubDir);
	}
 
	static void ProcessFile(ArrayList stats, string filename)
	{
        System.IO.StreamReader reader = System.IO.File.OpenText(filename);
        int iLineCount = 0;
        while (reader.Peek() >= 0)
        {
            reader.ReadLine();
            ++iLineCount;
        }
		stats.Add(new File(filename, iLineCount));
        reader.Close();			
	}	
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox