@@ -18,19 +18,25 @@ def write_csproj_prefix(ioWrapper):
1818
1919print ('Script to generate stub file from a nuget package' )
2020print (' Usage: python3 ' + sys .argv [0 ] +
21- ' NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir]' )
21+ ' TEMPLATE NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir]' )
2222print (' The script uses the dotnet cli, codeql cli, and dotnet format global tool' )
23+ print (' TEMPLATE should be either classlib or webapp, depending on the nuget package. For example, `Swashbuckle.AspNetCore.Swagger` should use `webapp` while `newtonsoft.json` should use `classlib`.' )
2324
2425if len (sys .argv ) < 2 :
26+ print ("\n Please supply a template name." )
27+ exit (1 )
28+
29+ if len (sys .argv ) < 3 :
2530 print ("\n Please supply a nuget package name." )
2631 exit (1 )
2732
2833thisScript = sys .argv [0 ]
2934thisDir = os .path .abspath (os .path .dirname (thisScript ))
30- nuget = sys .argv [1 ]
35+ template = sys .argv [1 ]
36+ nuget = sys .argv [2 ]
3137
3238# /input contains a dotnet project that's being extracted
33- workDir = os .path .abspath (helpers .get_argv (3 , "tempDir" ))
39+ workDir = os .path .abspath (helpers .get_argv (4 , "tempDir" ))
3440projectNameIn = "input"
3541projectDirIn = os .path .join (workDir , projectNameIn )
3642
@@ -57,10 +63,10 @@ def run_cmd(cmd, msg="Failed to run command"):
5763outputFile = os .path .join (projectDirOut , outputName + '.cs' )
5864bqrsFile = os .path .join (rawOutputDir , outputName + '.bqrs' )
5965jsonFile = os .path .join (rawOutputDir , outputName + '.json' )
60- version = helpers .get_argv (2 , "latest" )
66+ version = helpers .get_argv (3 , "latest" )
6167
6268print ("\n * Creating new input project" )
63- run_cmd (['dotnet' , 'new' , 'classlib' , "-f" , "net7.0" , "--language" , "C#" , '--name' ,
69+ run_cmd (['dotnet' , 'new' , template , "-f" , "net7.0" , "--language" , "C#" , '--name' ,
6470 projectNameIn , '--output' , projectDirIn ])
6571helpers .remove_files (projectDirIn , '.cs' )
6672
@@ -75,36 +81,27 @@ def run_cmd(cmd, msg="Failed to run command"):
7581print ("\n * Creating new global.json file and setting SDK to " + sdk_version )
7682run_cmd (['dotnet' , 'new' , 'globaljson' , '--force' , '--sdk-version' , sdk_version , '--output' , workDir ])
7783
78- print ("\n * Creating DB" )
79- run_cmd (['codeql' , 'database' , 'create' , dbDir , '--language=csharp' ,
80- '--command' , 'dotnet build /t:rebuild ' + projectDirIn ])
81-
82- if not os .path .isdir (dbDir ):
83- print ("Expected database directory " + dbDir + " not found." )
84- exit (1 )
85-
86- print ("\n * Running stubbing CodeQL query" )
87- run_cmd (['codeql' , 'query' , 'run' , os .path .join (
88- thisDir , 'AllStubsFromReference.ql' ), '--database' , dbDir , '--output' , bqrsFile ])
89-
90- run_cmd (['codeql' , 'bqrs' , 'decode' , bqrsFile , '--output' ,
91- jsonFile , '--format=json' ])
84+ print ("\n * Running stub generator" )
85+ helpers .run_cmd_cwd (['dotnet' , 'run' , '--project' , thisDir + '/../../../extractor/Semmle.Extraction.CSharp.DependencyStubGenerator/Semmle.Extraction.CSharp.DependencyStubGenerator.csproj' ], projectDirIn )
9286
9387print ("\n * Creating new raw output project" )
9488rawSrcOutputDirName = 'src'
9589rawSrcOutputDir = os .path .join (rawOutputDir , rawSrcOutputDirName )
96- run_cmd (['dotnet' , 'new' , 'classlib' , "--language" , "C#" ,
90+ run_cmd (['dotnet' , 'new' , template , "--language" , "C#" ,
9791 '--name' , rawSrcOutputDirName , '--output' , rawSrcOutputDir ])
9892helpers .remove_files (rawSrcOutputDir , '.cs' )
9993
100- # load json from query result file and split it into separate .cs files
94+ # copy each file from projectDirIn to rawSrcOutputDir
10195pathInfos = {}
102- with open (jsonFile ) as json_data :
103- data = json .load (json_data )
104- for row in data ['#select' ]['tuples' ]:
105- pathInfos [row [3 ]] = os .path .join (rawSrcOutputDir , row [1 ] + '.cs' )
106- with open (pathInfos [row [3 ]], 'a' ) as f :
107- f .write (row [4 ])
96+ codeqlStubsDir = os .path .join (projectDirIn , 'codeql_csharp_stubs' )
97+ for root , dirs , files in os .walk (codeqlStubsDir ):
98+ for file in files :
99+ if file .endswith ('.cs' ):
100+ path = os .path .join (root , file )
101+ relPath , _ = os .path .splitext (os .path .relpath (path , codeqlStubsDir ))
102+ origDllPath = "/" + relPath + ".dll"
103+ pathInfos [origDllPath ] = os .path .join (rawSrcOutputDir , file )
104+ shutil .copy2 (path , rawSrcOutputDir )
108105
109106print ("\n --> Generated stub files: " + rawSrcOutputDir )
110107
@@ -214,14 +211,16 @@ def run_cmd(cmd, msg="Failed to run command"):
214211 copiedFiles .add (pathInfo )
215212 shutil .copy2 (pathInfos [pathInfo ], frameworkDir )
216213
214+ exitCode = 0
217215for pathInfo in pathInfos :
218216 if pathInfo not in copiedFiles :
219217 print ('Not copied to nuget or framework folder: ' + pathInfo )
220218 othersDir = os .path .join (stubsDir , 'others' )
221219 if not os .path .exists (othersDir ):
222220 os .makedirs (othersDir )
223221 shutil .copy2 (pathInfos [pathInfo ], othersDir )
222+ exitCode = 1
224223
225224print ("\n --> Generated structured stub files: " + stubsDir )
226225
227- exit (0 )
226+ exit (exitCode )
0 commit comments