1+ Imports System.Drawing
12Imports System.IO
3+ Imports System.ServiceModel.Channels
24Imports System.Text.RegularExpressions
35Imports System.Windows.Forms
46Imports System.Xml
@@ -15,9 +17,9 @@ Imports Org.Hsf.Tooling.CodeGen.Views.GenTree
1517Imports Org.Hsf.Utility.Core.Configuration
1618Imports Org.Hsf.Utility.Core.Logging
1719Imports Org.Hsf.Utility.Core.Memory
20+ Imports Org.Hsf.Utility.Visuals.Core
1821
1922Namespace Controllers
20-
2123 Public Class TestGenGeneratorController
2224 Implements IDisposable
2325
@@ -90,7 +92,7 @@ Namespace Controllers
9092 ''' </summary>
9193 ''' <param name="view"></param>
9294 ''' <param name="project">The project file that is driving this </param>
93- Protected Friend Sub New (view As TreeView, project As FileInfo)
95+ Sub New (view As TreeView, project As FileInfo)
9496 _config = ConfigSelector.GetLoadedConfig( Of UnitTestGen)(
9597 "UnitTestGen"
9698 )
@@ -99,22 +101,54 @@ Namespace Controllers
99101 _projectFile = project
100102 End Sub
101103
104+ Private Sub CreateProgressDialog()
105+ Dim pLoc As Point = _trvNodesContainer.Parent.Location
106+ Dim pSize As Size = _trvNodesContainer.Parent.Size
107+ _progressDialog = New GenerationProgressDialogView() With {
108+ .StartPosition = FormStartPosition.Manual,
109+ .Location = New Point(
110+ CType (Math.Floor((pLoc.X + (pSize.Width / 2 )) - (.Size.Width / 2 )), Integer ),
111+ CType (Math.Floor((pLoc.Y + (pSize.Height / 2 )) - (.Size.Height / 2 )), Integer )
112+ )
113+ }
114+ End Sub
115+
116+ Friend Shared Sub ProgressWriteGenAction(pd As GenerationProgressDialogView, message As String )
117+ pd.Invoke( Sub ()
118+ pd.lblGenAction.Text = message
119+ pd.Refresh()
120+ End Sub )
121+ End Sub
122+
123+ Friend Shared Sub ProgressWriteCurGenItm(pd As GenerationProgressDialogView, message As String )
124+ pd.Invoke( Sub ()
125+ pd.lblCurrentlyGeneratingItem.Text = message
126+ pd.Refresh()
127+ End Sub )
128+ End Sub
129+
102130 ''' <summary>
103131 '''
104132 ''' </summary>
105133 ''' <param name="outputDir"></param>
106134 Protected Friend Sub GenerateForAllItems(outputDir As String , lang As String )
107135 _language = lang
108- _progressDialog = New GenerationProgressDialogView()
109- 'FIXME TreeViewUtils.CheckAllCheckboxesInSubTree(_trvNodesContainer.TopNode)
136+
137+ CreateProgressDialog()
138+
139+ _trvNodesContainer.Invoke( Sub () TreeViewUtils.CheckAllCheckboxesInSubTree(_trvNodesContainer.TopNode))
110140
111141 Task.Run(
112142 Sub ()
113- Dim items As List( Of TypeTreeNode) = _trvNodesContainer.Nodes.OfType( Of AssemblyTreeNode)().
114- SelectMany(FlattenSelector).
115- Where (_typeNodeTypeFilter).
116- OfType( Of TypeTreeNode)().
117- ToList()
143+ Dim items As List( Of TypeTreeNode) = _trvNodesContainer.Invoke(
144+ Function ()
145+ Return _trvNodesContainer.Nodes.OfType( Of AssemblyTreeNode)().
146+ SelectMany(FlattenSelector).
147+ Where (_typeNodeTypeFilter).
148+ OfType( Of TypeTreeNode)().
149+ ToList()
150+ End Function
151+ )
118152
119153 GenerateForItems(outputDir, items)
120154
@@ -133,16 +167,21 @@ Namespace Controllers
133167
134168 Friend Sub GenerateForCheckedItems(outputDir As String , lang As String )
135169 _language = lang
136- _progressDialog = New GenerationProgressDialogView()
170+
171+ CreateProgressDialog()
137172
138173 Task.Run(
139174 Sub ()
140- Dim items As List( Of TypeTreeNode) = _trvNodesContainer.Nodes.OfType( Of AssemblyTreeNode)().
141- SelectMany(FlattenSelector).
142- Where (_typeNodeTypeFilter).
143- Where (_selectedFilter).
144- OfType( Of TypeTreeNode)().
145- ToList()
175+ Dim items As List( Of TypeTreeNode) = _trvNodesContainer.Invoke(
176+ Function ()
177+ Return _trvNodesContainer.Nodes.OfType( Of AssemblyTreeNode)().
178+ SelectMany(FlattenSelector).
179+ Where (_typeNodeTypeFilter).
180+ Where (_selectedFilter).
181+ OfType( Of TypeTreeNode)().
182+ ToList()
183+ End Function
184+ )
146185
147186 GenerateForItems(outputDir, items)
148187
@@ -167,22 +206,15 @@ Namespace Controllers
167206 Sub GenerateForItems(outputDir As String , items As List( Of TypeTreeNode))
168207 generator = SyntaxGenerator.GetGenerator(genWorkspace, _language)
169208
170- _progressDialog.Invoke( Sub () _progressDialog.lblGenAction.Text =
171- "Generating namespace and assembly structure in output directory " &
172- outputDir)
173-
174- GenAssemblyDirectories(outputDir)
175-
176209 Dim compilationUnits As New Dictionary( Of Type, SyntaxNode)()
177210
178- Dim TEST_GEN_PREFIX As String = "Generating tests"
179- _progressDialog.Invoke( Sub () _progressDialog.lblGenAction.Text = TEST_GEN_PREFIX )
180- _progressDialog.Invoke( Sub () _progressDialog.lblCurrentlyGeneratingItem.Text = "Loading..." )
211+ ProgressWriteGenAction(_progressDialog, "Generating tests" )
212+ ProgressWriteCurGenItm( _progressDialog, "Loading..." )
213+
181214 _trvNodesContainer.Invoke(
182215 Sub ()
183216 For Each typeNode As TypeTreeNode In items
184- _progressDialog.Invoke( Sub () _progressDialog.lblGenAction.Text = TEST_GEN_PREFIX &
185- $" for class under test: {typeNode.codeGenType.FullName}" )
217+ ProgressWriteGenAction(_progressDialog, $"Generating tests for type under test: {typeNode.codeGenType.FullName}" )
186218
187219 logger.Info( $"Generating tests for type {typeNode.codeGenType.FullName}" )
188220 compilationUnits.Add(typeNode.codeGenType, GenerateCodeForType(typeNode))
@@ -194,8 +226,17 @@ Namespace Controllers
194226 End Sub
195227
196228 Sub WriteCompilationUnits(outputDir As String , compilationUnits As Dictionary( Of Type, SyntaxNode))
197- Dim asmOut As String = $"{outputDir}{Path.DirectorySeparatorChar}{_projectFile.Name.Replace(_projectFile.Extension, "")}"
198- Dim asmOutDir As DirectoryInfo = Directory.CreateDirectory(asmOut)
229+ Dim asmOut As String
230+ Dim asmOutDir As DirectoryInfo
231+
232+ If Not _config.OmitContainerDirectory Then
233+ asmOut = $"{outputDir}{Path.DirectorySeparatorChar}{_projectFile.Name.Replace(_projectFile.Extension, "")}"
234+ asmOutDir = Directory.CreateDirectory(asmOut)
235+
236+ Else
237+ asmOut = outputDir
238+ asmOutDir = New DirectoryInfo(asmOut)
239+ End If
199240
200241 If Not asmOutDir.Exists Then
201242 Throw New DirectoryNotFoundException( $"Could not create directory {asmOut}!" )
@@ -208,9 +249,7 @@ Namespace Controllers
208249 Continue For
209250 End If
210251
211- _progressDialog.Invoke( Sub () _progressDialog.lblGenAction.Text =
212- "Generating namespace and assembly structure in output directory " &
213- outputDir)
252+ ProgressWriteGenAction(_progressDialog, $"Generating namespace and assembly structure in output directory {outputDir}" )
214253
215254 Dim cuRawTypeName As String = compilationUnit.Key.Name
216255 Dim cuTypeName As String = Regex.Replace(
@@ -239,21 +278,19 @@ Namespace Controllers
239278 Dim cuNamespace As String = compilationUnit.Key.Namespace.Replace( $"{rootNs}." , "" )
240279 Dim cuSubdir As String = cuNamespace.Replace( "."c , Path.DirectorySeparatorChar)
241280
242- _progressDialog.Invoke( Sub () _progressDialog.lblCurrentlyGeneratingItem.Text =
243- $"Creating directory {asmOut}{Path.DirectorySeparatorChar}{cuSubdir}" &
244- $" for generated test unit {fileName}" )
281+ ProgressWriteCurGenItm(_progressDialog, $"Creating directory {asmOut}{Path.DirectorySeparatorChar}{cuSubdir}" &
282+ $" for generated test unit {fileName}" )
245283
246284 Dim cuOutDir As DirectoryInfo = asmOutDir.CreateSubdirectory(cuSubdir)
247285 Dim cuFullPath As String = Path.Combine(cuOutDir.FullName, fileName)
248286
249287 Using cuWriter As New StreamWriter(cuFullPath)
250-
251- _progressDialog.Invoke( Sub () _progressDialog.lblGenAction.Text =
252- $"Writing generated test compilation unit" )
253- _progressDialog.Invoke( Sub () _progressDialog.lblCurrentlyGeneratingItem.Text =
254- cuFullPath)
288+ ProgressWriteGenAction(_progressDialog, "Writing generated test compilation unit" )
289+ ProgressWriteCurGenItm(_progressDialog, cuFullPath)
255290
256291 compilationUnit.Value.WriteTo(cuWriter)
292+
293+ ProgressWriteGenAction(_progressDialog, "Wrote generated test compilation unit" )
257294 End Using
258295 Next
259296 End Sub
@@ -271,41 +308,27 @@ Namespace Controllers
271308
272309 Dim cuNodes As List( Of SyntaxNode)
273310 If genTypeUnderTest.IsClass Then
311+ ProgressWriteCurGenItm(_progressDialog, $"Generating tests for class {genTypeUnderTest.FullName}" )
274312 cuNodes = codeGenerator.GenerateCodeForClass(selectedTestGenItem, genTypeUnderTest)
313+ ProgressWriteCurGenItm(_progressDialog, $"Done generating tests for class {genTypeUnderTest.FullName}" )
275314
276315 ElseIf genTypeUnderTest.IsEnum Then
316+ ProgressWriteCurGenItm(_progressDialog, $"Generating tests for enum {genTypeUnderTest.FullName}" )
277317 cuNodes = codeGenerator.GenerateCodeForEnum(selectedTestGenItem, genTypeUnderTest)
318+ ProgressWriteCurGenItm(_progressDialog, $"Done generating tests for enum {genTypeUnderTest.FullName}" )
278319
279320 ElseIf genTypeUnderTest.IsValueType Then
280321 logger.Warn( $"Skipping value type {genTypeUnderTest.FullName};" &
281322 " please manually implement one-or-more tests for this type!" )
282323 Return Nothing
324+
283325 Else
284326 Throw New NotSupportedException( $"The type of {genTypeUnderTest.FullName} is not supported!" )
285327 End If
286328
287329 Return generator.CompilationUnit(cuNodes).NormalizeWhitespace()
288330 End Function
289331
290- Sub GenAssemblyDirectories(outputDir As String )
291- Dim asmNodes As IEnumerable( Of CodeGenTreeNode) = _trvNodesContainer.Nodes.OfType( Of CodeGenTreeNode)().
292- SelectMany(FlattenSelector).
293- Where (_asmNodeTypeFilter)
294-
295- For Each asmNode As CodeGenTreeNode In asmNodes
296- Dim asmSimpleName As String = asmNode.Text.Split(separator, StringSplitOptions.RemoveEmptyEntries)( 0 )
297- Dim asmOutDirStr As String = $"{outputDir}{Path.DirectorySeparatorChar}{asmSimpleName}"
298-
299- _progressDialog.Invoke( Sub () _progressDialog.lblCurrentlyGeneratingItem.Text =
300- $"Creating directory {asmOutDirStr} for assembly {asmSimpleName}" )
301-
302- Dim asmOutDir As DirectoryInfo = Directory.CreateDirectory(asmOutDirStr)
303- If Not asmOutDir.Exists Then
304- Throw New InvalidOperationException( $"Failed to create Assembly-based test output directory {asmOutDirStr}" )
305- End If
306- Next
307- End Sub
308-
309332 Private Property disposed As Boolean
310333
311334 Public Sub Dispose() Implements IDisposable.Dispose
0 commit comments