@@ -19,144 +19,88 @@ public static partial class TypeToMarkdown
1919
2020 private static string Links ( this Type type )
2121 {
22- string link = type . GithubLink ( ) ;
23- if ( string . IsNullOrEmpty ( link ) )
24- return "" ;
25-
26- string classWord = type . IsEnum ? "enum" : type . IsInterface ? "interface" : "class" ;
22+ string classWord = type . TypeWord ( ) ;
2723
2824 string markdown = "## Code and Schema\n \n " ;
2925 markdown += "### C# implementation\n \n " ;
30- markdown += $ "The { classWord } is defined in C#. The class definition is available on github:\n \n ";
31- markdown += $ "- { link } \n ";
32- markdown += "\n All history and changes of the class can be found by inspection the history.\n " ;
3326
34- return markdown ;
35- }
27+ markdown += "``` C# title=\" C#\" \n " ;
3628
37- /***************************************************/
38-
39- private static string GithubLink ( this Type type )
40- {
29+ markdown += $ "public { classWord } { type . Name } ";
30+ List < Type > baseTypes = type . GetBaseTypes ( ) ;
31+ if ( baseTypes . Count > 0 )
32+ {
33+ List < string > names = baseTypes . Select ( x => x . TypeNameBaseTypes ( ) ) . ToList ( ) ;
34+ int charCount = names . Sum ( x => x . Length ) ;
35+ string split = charCount > 120 ? ",\n " : ", " ;
36+ markdown += $ " : { string . Join ( split , names ) } ";
37+ }
38+ markdown += "\n " ;
39+ markdown += "```\n \n " ;
40+ markdown += $ "Assembly: { type . Assembly . GetName ( ) . Name } .dll\n \n ";
4141
42- if ( Settings . AssemblyLink . TryGetValue ( type . Assembly . GetName ( ) . Name , out string repoLink ) && TryCheckObjectLink ( type , out string objectLink ) )
42+ string link = type . GithubLink ( ) ;
43+ if ( ! string . IsNullOrEmpty ( link ) )
4344 {
44- string link = $ "{ repoLink } /blob/develop/{ type . Assembly . GetName ( ) . Name } /{ objectLink } "; //Skip the first three parts of the namespace
45- return $ "[{ type . Name . Split ( '`' ) [ 0 ] } .cs]({ link } )";
45+ markdown += $ "The { classWord } is defined in C#. The class definition is available on github:\n \n ";
46+ markdown += $ "- { link } \n ";
47+ markdown += "\n All history and changes of the class can be found by inspection the history.\n " ;
4648 }
47- return "" ;
49+ return markdown ;
50+
51+ markdown += "### JSON Schema implementation\n \n " ;
52+ markdown += "The object is defined as a JSON schema, available on github:" ;
4853 }
4954
5055 /***************************************************/
5156
52- private static bool TryCheckObjectLink ( this Type type , out string objectLink )
57+ private static string TypeWord ( this Type type )
5358 {
54- //Method to find/validate the relative link to the file on github
59+ if ( type . IsEnum )
60+ return "enum" ;
61+ if ( type . IsInterface )
62+ return "interface" ;
63+ if ( type . IsAbstract )
64+ return "abstract class" ;
65+
66+ return "class" ;
67+ }
5568
56- //Get the assumed relative link based on type name and namespace
57- string relativeLink = type . GetBaseLink ( ) ;
58- if ( string . IsNullOrEmpty ( relativeLink ) )
59- {
60- Console . WriteLine ( $ "Unable to generate relative link for type { type . Name } ") ;
61- objectLink = "" ;
62- return false ;
63- }
69+ /***************************************************/
6470
65- string folder ;
66- //If cant find folder to check against, for now, assume ok and return relative link
67- if ( ! m_AssemblyFolder . TryGetValue ( type . Assembly , out folder ) )
71+ private static string TypeNameBaseTypes ( this Type type )
72+ {
73+ if ( type . IsGenericType )
6874 {
69- objectLink = relativeLink ;
70- return true ;
71- }
75+ string genType = type . GetGenericTypeDefinition ( ) . FullName . Split ( '`' ) [ 0 ] ;
7276
73- //Check if the assumed place that it for msot cases should be (namespace matching folder path)
74- if ( File . Exists ( Path . Combine ( folder , relativeLink ) ) )
75- {
76- objectLink = relativeLink ;
77- return true ;
78- }
77+ List < string > args = new List < string > ( ) ;
7978
80- //If not, try scan folders matching the namespace, starting from inner subfolder and going up a level each time
81- string [ ] relPathSplit = relativeLink . Split ( Path . DirectorySeparatorChar ) ;
82- string fileName = relPathSplit . Last ( ) ;
83- for ( int i = relPathSplit . Length - 1 ; i >= 0 ; i -- )
84- {
85- string dir = Path . Combine ( folder , Path . Combine ( relPathSplit . Take ( i ) . ToArray ( ) ) ) ;
86- if ( Directory . Exists ( dir ) )
87- {
88- string [ ] files = Directory . GetFiles ( dir , fileName , SearchOption . AllDirectories ) ; //All directories to check if contained in subfolder. Checking for file amtching type name exactly. Should be the case and required compliance check
89- if ( files . Length == 1 ) //Single file found -> return
90- {
91- string [ ] fileSplit = files [ 0 ] . Split ( Path . DirectorySeparatorChar ) ;
92- objectLink = string . Join ( "/" , fileSplit . Skip ( folder . Split ( Path . DirectorySeparatorChar ) . Length ) ) ; //Get rid of part leading up to the oM folder, and return the rest after. Link generation above handles all before.
93- return true ;
94- }
79+ foreach ( Type t in type . GetGenericArguments ( ) )
80+ {
81+ args . Add ( t . TypeNameBaseTypes ( ) ) ;
9582 }
96- }
9783
98- string [ ] allFiles = Directory . GetFiles ( folder , fileName , SearchOption . AllDirectories ) ; //Most likely not required and should be handled by final iteration above, but final scanning of all files in the folder
99- if ( allFiles . Length == 1 )
100- {
101- string [ ] fileSplit = allFiles [ 0 ] . Split ( Path . DirectorySeparatorChar ) ;
102- objectLink = string . Join ( "/" , fileSplit . Skip ( folder . Split ( Path . DirectorySeparatorChar ) . Length ) ) ;
103- return true ;
84+ return $ "{ genType } <{ string . Join ( ", " , args ) } >";
10485 }
105-
106-
107- Console . WriteLine ( $ "Unable to find valid github link for { type . Name } .") ;
108-
109- objectLink = "" ;
110- return false ;
111- }
112-
113- /***************************************************/
114-
115- private static string GetBaseLink ( this Type type )
116- {
117- string fullName = type . FullName . Split ( '`' ) [ 0 ] ;
118- string [ ] split = fullName . Split ( '.' ) ;
119- if ( split . Length < 4 )
120- return "" ;
121-
122- IEnumerable < string > parts ;
123- if ( split [ 3 ] == "Adapters" )
124- parts = split . Skip ( 4 ) ;
12586 else
126- parts = split . Skip ( 3 ) ;
127-
128- return string . Join ( Path . DirectorySeparatorChar , parts ) + ".cs" ;
87+ return type . FullName ;
12988 }
13089
13190 /***************************************************/
13291
133- public static void SetupAssemblyFolders ( string gitFolderPath , List < Assembly > assemblies )
134- {
135- List < Assembly > assembliesCopy = assemblies . ToList ( ) ;
136-
137- foreach ( string repo in Directory . GetDirectories ( gitFolderPath ) )
138- {
139- foreach ( string projFolder in Directory . GetDirectories ( repo ) )
140- {
141- string dirName = Path . GetFileName ( projFolder ) ;
142- Assembly assembly = assemblies . FirstOrDefault ( a => a . GetName ( ) . Name == dirName ) ;
143-
144- if ( assembly != null )
145- m_AssemblyFolder [ assembly ] = projFolder ;
146- }
147- }
92+ private static string GithubLink ( this Type type )
93+ {
14894
149- if ( m_AssemblyFolder . Count != assemblies . Count )
95+ if ( Settings . AssemblyLink . TryGetValue ( type . Assembly . GetName ( ) . Name , out string repoLink ) && TryGetFileLink ( type , out string objectLink ) )
15096 {
151- Console . WriteLine ( $ "Unable to find a filepath for git link checking for { string . Join ( ", " , assemblies . Except ( m_AssemblyFolder . Keys ) ) } .") ;
97+ string link = $ "{ repoLink } /blob/develop/{ type . Assembly . GetName ( ) . Name } /{ objectLink } "; //Skip the first three parts of the namespace
98+ return $ "[{ type . Name . Split ( '`' ) [ 0 ] } .cs]({ link } )";
15299 }
153-
100+ return "" ;
154101 }
155102
156103 /***************************************************/
157104
158- private static Dictionary < Assembly , string > m_AssemblyFolder = new Dictionary < Assembly , string > ( ) ;
159-
160- /***************************************************/
161105 }
162106}
0 commit comments