@@ -18,19 +18,25 @@ def write_csproj_prefix(ioWrapper):
18
18
19
19
print ('Script to generate stub file from a nuget package' )
20
20
print (' Usage: python3 ' + sys .argv [0 ] +
21
- ' NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir]' )
21
+ ' TEMPLATE NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir]' )
22
22
print (' 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`.' )
23
24
24
25
if len (sys .argv ) < 2 :
26
+ print ("\n Please supply a template name." )
27
+ exit (1 )
28
+
29
+ if len (sys .argv ) < 3 :
25
30
print ("\n Please supply a nuget package name." )
26
31
exit (1 )
27
32
28
33
thisScript = sys .argv [0 ]
29
34
thisDir = os .path .abspath (os .path .dirname (thisScript ))
30
- nuget = sys .argv [1 ]
35
+ template = sys .argv [1 ]
36
+ nuget = sys .argv [2 ]
31
37
32
38
# /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" ))
34
40
projectNameIn = "input"
35
41
projectDirIn = os .path .join (workDir , projectNameIn )
36
42
@@ -57,10 +63,10 @@ def run_cmd(cmd, msg="Failed to run command"):
57
63
outputFile = os .path .join (projectDirOut , outputName + '.cs' )
58
64
bqrsFile = os .path .join (rawOutputDir , outputName + '.bqrs' )
59
65
jsonFile = os .path .join (rawOutputDir , outputName + '.json' )
60
- version = helpers .get_argv (2 , "latest" )
66
+ version = helpers .get_argv (3 , "latest" )
61
67
62
68
print ("\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' ,
64
70
projectNameIn , '--output' , projectDirIn ])
65
71
helpers .remove_files (projectDirIn , '.cs' )
66
72
@@ -75,36 +81,27 @@ def run_cmd(cmd, msg="Failed to run command"):
75
81
print ("\n * Creating new global.json file and setting SDK to " + sdk_version )
76
82
run_cmd (['dotnet' , 'new' , 'globaljson' , '--force' , '--sdk-version' , sdk_version , '--output' , workDir ])
77
83
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 )
92
86
93
87
print ("\n * Creating new raw output project" )
94
88
rawSrcOutputDirName = 'src'
95
89
rawSrcOutputDir = os .path .join (rawOutputDir , rawSrcOutputDirName )
96
- run_cmd (['dotnet' , 'new' , 'classlib' , "--language" , "C#" ,
90
+ run_cmd (['dotnet' , 'new' , template , "--language" , "C#" ,
97
91
'--name' , rawSrcOutputDirName , '--output' , rawSrcOutputDir ])
98
92
helpers .remove_files (rawSrcOutputDir , '.cs' )
99
93
100
- # load json from query result file and split it into separate .cs files
94
+ # copy each file from projectDirIn to rawSrcOutputDir
101
95
pathInfos = {}
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 )
108
105
109
106
print ("\n --> Generated stub files: " + rawSrcOutputDir )
110
107
@@ -214,14 +211,16 @@ def run_cmd(cmd, msg="Failed to run command"):
214
211
copiedFiles .add (pathInfo )
215
212
shutil .copy2 (pathInfos [pathInfo ], frameworkDir )
216
213
214
+ exitCode = 0
217
215
for pathInfo in pathInfos :
218
216
if pathInfo not in copiedFiles :
219
217
print ('Not copied to nuget or framework folder: ' + pathInfo )
220
218
othersDir = os .path .join (stubsDir , 'others' )
221
219
if not os .path .exists (othersDir ):
222
220
os .makedirs (othersDir )
223
221
shutil .copy2 (pathInfos [pathInfo ], othersDir )
222
+ exitCode = 1
224
223
225
224
print ("\n --> Generated structured stub files: " + stubsDir )
226
225
227
- exit (0 )
226
+ exit (exitCode )
0 commit comments