Skip to content

Commit 8b2c233

Browse files
committed
C#: Use new stub generator in make_stubs_nuget.py
1 parent e021fb4 commit 8b2c233

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

csharp/ql/src/Stubs/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def run_cmd(cmd, msg="Failed to run command"):
99
exit(1)
1010

1111

12-
def run_cmd_cwd(cmd, cwd, msg):
12+
def run_cmd_cwd(cmd, cwd, msg="Failed to run command"):
1313
print('Change working directory to: ' + cwd)
1414
print('Running ' + ' '.join(cmd))
1515
if subprocess.check_call(cmd, cwd=cwd):

csharp/ql/src/Stubs/make_stubs_nuget.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ def write_csproj_prefix(ioWrapper):
1818

1919
print('Script to generate stub file from a nuget package')
2020
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]')
2222
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`.')
2324

2425
if len(sys.argv) < 2:
26+
print("\nPlease supply a template name.")
27+
exit(1)
28+
29+
if len(sys.argv) < 3:
2530
print("\nPlease supply a nuget package name.")
2631
exit(1)
2732

2833
thisScript = sys.argv[0]
2934
thisDir = 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"))
3440
projectNameIn = "input"
3541
projectDirIn = os.path.join(workDir, projectNameIn)
3642

@@ -57,10 +63,10 @@ def run_cmd(cmd, msg="Failed to run command"):
5763
outputFile = os.path.join(projectDirOut, outputName + '.cs')
5864
bqrsFile = os.path.join(rawOutputDir, outputName + '.bqrs')
5965
jsonFile = os.path.join(rawOutputDir, outputName + '.json')
60-
version = helpers.get_argv(2, "latest")
66+
version = helpers.get_argv(3, "latest")
6167

6268
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',
6470
projectNameIn, '--output', projectDirIn])
6571
helpers.remove_files(projectDirIn, '.cs')
6672

@@ -75,36 +81,27 @@ def run_cmd(cmd, msg="Failed to run command"):
7581
print("\n* Creating new global.json file and setting SDK to " + sdk_version)
7682
run_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

9387
print("\n* Creating new raw output project")
9488
rawSrcOutputDirName = 'src'
9589
rawSrcOutputDir = 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])
9892
helpers.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
10195
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)
108105

109106
print("\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
217215
for 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

225224
print("\n --> Generated structured stub files: " + stubsDir)
226225

227-
exit(0)
226+
exit(exitCode)

0 commit comments

Comments
 (0)