Is there an easy way to find custom code?

Posts   
 
    
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 03-Jan-2007 12:05:40   

I've doen a search and looked through the help but I can't finda anything.

I'm porting an old VB.NET 2003, LLBLGen 1.0 project to 2005 and 2.0 - I've found and adapted most of the custom code covering the major functionality in the from the old project but I'm worried there's custom code in rarely used functions that I'm missing. Nothing's documented and I have a lot of entities so I'm struggling at the moment.

Is there an easy way to find custom code or just files that contain custom code?

Surely someone must, at some point, hjave wanted to do the same thing and come up with something that searches for gaps between ' __LLBLGENPRO_USER_CODE_REGION_START and ' __LLBLGENPRO_USER_CODE_REGION_END sections?

Any help would be greatly appreciated.

Cheers,

James

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Jan-2007 13:30:10   

Could be done by a small piece of code (utility), that scans the folder you specify and read the files inside one by one, and check to see if there is a line between any 2 lines starting with " ' __LLBLGENPRO_USER_CODE_REGION "

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 03-Jan-2007 14:58:17   

That's what I was thinking Walaa but since nobody's volunteered anything like that I've written one myself. If anyone else wants such a tool, there's some quick and nasty code below.

In VB 2005 I created a form with two text boxes, the second with multiline true, a button, a FolderBrowserDialog and a atatus strip with a label.

All names were left as default.

My code is as follows:

Imports System.IO
Public Class Form1

    Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
        FolderBrowserDialog1.ShowDialog()
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = ""
        SearchFiles(TextBox1.Text)
        Dim FSO
        Dim SourceFolder
        Dim SubFolder
        FSO = CreateObject("Scripting.FileSystemObject")
        SourceFolder = FSO.GetFolder(TextBox1.Text)
        ToolStripStatusLabel1.Text = "Starting Test"
        For Each SubFolder In SourceFolder.SubFolders
            SearchFiles(SubFolder.Path)
        Next SubFolder
        ToolStripStatusLabel1.Text = "Test Completed"
    End Sub

    Private Sub SearchFiles(ByVal pathName As String)
        Dim FSO
        Dim FileItem
        Dim SourceFolder
        FSO = CreateObject("Scripting.FileSystemObject")
        SourceFolder = FSO.GetFolder(pathName)
        For Each FileItem In SourceFolder.Files
            Dim ContainsCustomCode As Boolean = False
            Dim fs As New FileStream(pathName & "\" & FileItem.Name, FileMode.Open, FileAccess.Read)
            Dim d As New StreamReader(fs)
            d.BaseStream.Seek(0, SeekOrigin.Begin)
            ToolStripStatusLabel1.Text = "Testing " & pathName & "\" & FileItem.Name
            While Not d.EndOfStream
                Dim tempstring As String = d.ReadLine()
                If tempstring.IndexOf("__LLBLGENPRO_USER_CODE_REGION_START") >= 0 Then
                    Dim TestString As String = d.ReadLine()
                    If Not TestString.IndexOf("__LLBLGENPRO_USER_CODE_REGION_END") >= 0 Then
                        ContainsCustomCode = True
                    End If
                End If
            End While
            If ContainsCustomCode Then
                TextBox2.Text = TextBox2.Text & pathName & "\" & FileItem.Name & vbNewLine
            End If
            d.Close()
        Next
    End Sub
End Class
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Jan-2007 15:04:42   

Thanks for the contribution.

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 03-Jan-2007 15:12:33   

No problem.

A little info if anyone wants to use this.

When you load the form, click on the first text box and the find folder dialog will appear. Find your DAL folder and click OK, then click on your button.

It will check each file in the DAL folder and all folders one level down from there and populate the second textbox with a list of full path / filenames of files which appear to contain custom code.

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 03-Jan-2007 17:01:33   

My code's got worse but it works better!

The code above didn't take into account empty lines or comment lines...

The checking for comment lines isn't ideal as trim isn't removing tabs.

I added a status strip just so I could see that it was still doing something as it takes a while...


Imports System.IO
Public Class Form1
    Dim Ticker As Integer = 0
    Dim LineNumber As Integer

    Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
        FolderBrowserDialog1.ShowNewFolderButton = False
        If TextBox1.Text.Length > 0 Then
            FolderBrowserDialog1.SelectedPath = TextBox1.Text
        End If
        FolderBrowserDialog1.ShowDialog()
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = ""
        SearchFiles(TextBox1.Text)
        Dim FSO
        Dim SourceFolder
        Dim SubFolder
        FSO = CreateObject("Scripting.FileSystemObject")
        SourceFolder = FSO.GetFolder(TextBox1.Text)
        ToolStripStatusLabel1.Text = "Starting Test"
        ToolStripStatusLabel1.Visible = True
        For Each SubFolder In SourceFolder.SubFolders
            SearchFiles(SubFolder.Path)
        Next SubFolder
        ToolStripStatusLabel1.Text = "Test Completed"
        ToolStripStatusLabel1.Visible = False
    End Sub

    Private Sub SearchFiles(ByVal pathName As String)
        Dim FSO
        Dim FileItem
        Dim SourceFolder
        FSO = CreateObject("Scripting.FileSystemObject")
        SourceFolder = FSO.GetFolder(pathName)
        For Each FileItem In SourceFolder.Files
            CheckFile(pathName, FileItem.Name)
        Next
    End Sub

    Private Sub CheckFile(ByVal pathName As String, ByVal FileName As String)
        LineNumber = 0
        Dim ContainsCustomCode As Boolean = False
        Dim fs As New FileStream(pathName & "\" & FileName, FileMode.Open, FileAccess.Read)
        Dim d As New StreamReader(fs)
        d.BaseStream.Seek(0, SeekOrigin.Begin)
        ToolStripStatusLabel1.Text = "Testing " & FileName
        While Not d.EndOfStream
            Dim tempstring As String = d.ReadLine()
            LineNumber = LineNumber + 1
            updateStatus(FileName)
            If tempstring.IndexOf("__LLBLGENPRO_USER_CODE_REGION_START") >= 0 Then
                Dim TestString As String = d.ReadLine()
                LineNumber = LineNumber + 1
                If Not TestString.IndexOf("__LLBLGENPRO_USER_CODE_REGION_END") >= 0 Then
                    'MsgBox("Detected something between start and end on line " & LineNumber)
                    Dim PreviousString As String = TestString
                    'Check if it's just a comment
                    While Trim(TestString).IndexOf("'") < 7 And Not Trim(TestString).IndexOf("'") = -1 'It's probably a comment
                        'MsgBox("""" & Trim(TestString) & """ - " & Trim(TestString).IndexOf("'"))
                        PreviousString = TestString
                        TestString = d.ReadLine()
                        LineNumber = LineNumber + 1
                        'MsgBox(TestString & " is a comment! - " & TestString.IndexOf("__LLBLGENPRO_USER_CODE_REGION_END") & " " & Trim(TestString).IndexOf("'"))
                    End While
                    'MsgBox(TestString & " is not a comment!")
                    If Not PreviousString.IndexOf("__LLBLGENPRO_USER_CODE_REGION_END") >= 0 Then
                        If False Then
                            ContainsCustomCode = True
                        Else
                            'Check if it's empty
                            If Len(Trim(TestString)) < 1 Then
                                Dim newTempString As String = d.ReadLine
                                LineNumber = LineNumber + 1
                                If Not newTempString.IndexOf("__LLBLGENPRO_USER_CODE_REGION_END") >= 0 Then
                                    ContainsCustomCode = True
                                    'MsgBox("True because empty then " & newTempString)
                                End If
                            Else
                                ContainsCustomCode = True
                                'MsgBox("True because """ & Trim(TestString) & """ - " & Trim(TestString).IndexOf("'"))
                            End If
                        End If
                    End If
                End If
            End If
        End While
        If ContainsCustomCode Then
            TextBox2.Text = TextBox2.Text & pathName & "\" & FileName & vbNewLine
        End If
        d.Close()
    End Sub

    Private Sub updateStatus(Optional ByVal FileName As String = "")
        Ticker = (Ticker Mod 100) + 1
        'MsgBox(Ticker & " " & FileName & " " & LineNumber)
        ToolStripProgressBar1.Value = Ticker
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ToolStripStatusLabel1.Visible = False
    End Sub
End Class