Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Finished implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AptiviCEO committed Feb 7, 2022
1 parent 3113400 commit c4cdc45
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 32 deletions.
119 changes: 119 additions & 0 deletions LineNumbers.Core/LinesInfo.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'
' MIT License
'
' Copyright (c) 2021 EoflaOE and its companies
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE Or THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'

Imports System.IO
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.MSBuild

Public Class LinesInfo

''' <summary>
''' The line numbers by project
''' </summary>
Public ReadOnly Property LineNumbersByProject() As Dictionary(Of String, Long)

''' <summary>
''' The line number of the whole solution
''' </summary>
Public ReadOnly Property SolutionLineNumber As Long

''' <summary>
''' Paths to the individual code files
''' </summary>
Public ReadOnly Property CodeFiles() As List(Of String)

''' <summary>
''' Paths to the individual code files by project
''' </summary>
Public ReadOnly Property CodeFilesByProject() As Dictionary(Of String, List(Of String))

''' <summary>
''' Line numbers of individual code files
''' </summary>
Public ReadOnly Property LineNumbersByCodeFiles() As Dictionary(Of String, Long)

''' <summary>
''' Line numbers of individual code files by project
''' </summary>
Public ReadOnly Property LineNumbersByCodeFilesByProject() As Dictionary(Of String, Dictionary(Of String, Long))

''' <summary>
''' Creates a new lines information instance from the Visual Studio solution
''' </summary>
''' <param name="SolutionPath">Path to the Visual Studio solution</param>
Public Sub New(SolutionPath As String)
Me.New(MSBuildWorkspace.Create().OpenSolutionAsync(SolutionPath).Result)
End Sub

''' <summary>
''' Creates a new lines information instance from the Visual Studio solution
''' </summary>
''' <param name="Solution">The Visual Studio solution</param>
Public Sub New(Solution As Solution)
Dim Projects As List(Of Project) = ReturnProjects(Solution)
Dim LineNumbersByProject As New Dictionary(Of String, Long)
Dim CodeFiles As New List(Of String)
Dim CodeFilesByProject As New Dictionary(Of String, List(Of String))
Dim LineNumbersByCodeFiles As New Dictionary(Of String, Long)
Dim LineNumbersByCodeFilesByProject As New Dictionary(Of String, Dictionary(Of String, Long))
Dim Total As Long

'Enumerate through each project
For Each Project As Project In Projects
Dim ProjectTotal As Long
Dim ProjectCodeFiles As List(Of String) = ReturnCodeFiles(Project)
Dim LineNumbersByCodeFilesOfProject As New Dictionary(Of String, Long)

'Enumerate through each code file
For Each CodeFile As String In ProjectCodeFiles
Debug.WriteLine(CodeFile)
Dim FileLines() As String = File.ReadAllLines(CodeFile)
Dim FileLinesLength As Long = FileLines.Length
Total += FileLinesLength
ProjectTotal += FileLinesLength
Debug.WriteLine(FileLinesLength.ToString + ", " + Total.ToString)
LineNumbersByCodeFiles.Add(CodeFile, FileLinesLength)
LineNumbersByCodeFilesOfProject.Add(CodeFile, FileLinesLength)
Next

'Install the values
CodeFiles.AddRange(ProjectCodeFiles)
CodeFilesByProject.Add(Project.Name, ProjectCodeFiles)
LineNumbersByProject.Add(Project.Name, ProjectTotal)
LineNumbersByCodeFilesByProject.Add(Project.Name, LineNumbersByCodeFilesOfProject)

'Reset some values
ProjectTotal = 0
Next

'Install all the values to the new instance
Me.LineNumbersByProject = LineNumbersByProject
Me.CodeFilesByProject = CodeFilesByProject
Me.CodeFiles = CodeFiles
Me.LineNumbersByCodeFiles = LineNumbersByCodeFiles
Me.LineNumbersByCodeFilesByProject = LineNumbersByCodeFilesByProject
SolutionLineNumber = Total
End Sub

End Class
8 changes: 8 additions & 0 deletions LineNumbers.Core/Tools.vb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ Public Module Tools
End If
End Function

''' <summary>
''' Get the projects in the solution
''' </summary>
''' <param name="Solution">The solution</param>
Function ReturnProjects(Solution As Solution) As List(Of Project)
Return Solution.Projects.ToList
End Function

''' <summary>
''' Returns the code files that are in the project
''' </summary>
Expand Down
52 changes: 20 additions & 32 deletions LineNumbers/LinesMain.vb
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,33 @@

Imports System.IO
Imports LineNumbers.Core
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.MSBuild

Module LinesMain

Sub Main(args As String())
Dim Total As Long

'Check to see if solution exists
Dim Workspace As MSBuildWorkspace = MSBuildWorkspace.Create()
If File.Exists(args(0)) Then
Dim Projects As List(Of Project) = ReturnProjects(args(0), Workspace)

'Enumerate through each project
For Each Project As Project In Projects
Dim ProjectTotal As Long
Dim CodeFiles As List(Of String) = ReturnCodeFiles(Project)

'Enumerate through each code file
For Each CodeFile As String In CodeFiles
Debug.WriteLine(CodeFile)
Dim FileLines() As String = File.ReadAllLines(CodeFile)
Dim FileLinesLength As Long = FileLines.Length
Total += FileLinesLength
ProjectTotal += FileLinesLength
Debug.WriteLine(FileLinesLength.ToString + ", " + Total.ToString)
Console.WriteLine("File {0}: {1} lines", Path.GetFileName(CodeFile), FileLinesLength)
If args.Length > 0 Then
'Check to see if solution exists
If File.Exists(args(0)) Then
Dim LinesInfo As New LinesInfo(args(0))
Dim LineNumbers As Dictionary(Of String, Long) = LinesInfo.LineNumbersByProject

'Enumerate through each project
For Each ProjectName As String In LineNumbers.Keys
Dim CodeFileLines As Dictionary(Of String, Long) = LinesInfo.LineNumbersByCodeFilesByProject(ProjectName)
For Each FileName As String In CodeFileLines.Keys
Console.WriteLine("File {0}: {1} lines", Path.GetFileName(FileName), CodeFileLines(FileName))
Next
Console.WriteLine(Environment.NewLine + "Total for project {0}: {1} lines" + Environment.NewLine, ProjectName, LineNumbers(ProjectName))
Next

'Total for solution
Console.WriteLine(Environment.NewLine + "Total for project {0}: {1} lines" + Environment.NewLine, Project.Name, ProjectTotal)
ProjectTotal = 0
Next

'Return the total
Console.WriteLine(Environment.NewLine + "Total: {0} lines", Total)
Console.ReadKey()
'Return the total
Console.WriteLine("Total: {0} lines", LinesInfo.SolutionLineNumber)
Else
Console.WriteLine("Solution not found.")
Environment.Exit(1)
End If
Else
Console.WriteLine("Solution not found.")
Console.WriteLine("Specify path to solution.")
Environment.Exit(1)
End If
End Sub
Expand Down

0 comments on commit c4cdc45

Please sign in to comment.