Skip to content

Commit ed706d9

Browse files
authored
Merge pull request #7269 from michaelnebel/chspar-nuget-stub-script
C#: Update the make_stubs_nuget script
2 parents e41cd81 + 186ba42 commit ed706d9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

csharp/ql/src/Stubs/helpers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
import os
33
import subprocess
44

5-
65
def run_cmd(cmd, msg="Failed to run command"):
76
print('Running ' + ' '.join(cmd))
87
if subprocess.check_call(cmd):
98
print(msg)
109
exit(1)
1110

1211

12+
def run_cmd_cwd(cmd, cwd, msg):
13+
print('Change working directory to: ' + cwd)
14+
print('Running ' + ' '.join(cmd))
15+
if subprocess.check_call(cmd, cwd=cwd):
16+
print(msg)
17+
exit(1)
18+
19+
1320
def get_argv(index, default):
1421
if len(sys.argv) > index:
1522
return sys.argv[index]

csharp/ql/src/Stubs/make_stubs_nuget.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def write_csproj_prefix(ioWrapper):
1717

1818

1919
print('Script to generate stub file from a nuget package')
20-
print(' Usage: python ' + sys.argv[0] +
20+
print(' Usage: python3 ' + sys.argv[0] +
2121
' NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir]')
2222
print(' The script uses the dotnet cli, codeql cli, and dotnet format global tool')
2323

@@ -34,6 +34,9 @@ def write_csproj_prefix(ioWrapper):
3434
projectNameIn = "input"
3535
projectDirIn = os.path.join(workDir, projectNameIn)
3636

37+
def run_cmd(cmd, msg="Failed to run command"):
38+
helpers.run_cmd_cwd(cmd, workDir, msg)
39+
3740
# /output contains the output of the stub generation
3841
outputDirName = "output"
3942
outputDir = os.path.join(workDir, outputDirName)
@@ -57,7 +60,7 @@ def write_csproj_prefix(ioWrapper):
5760
version = helpers.get_argv(2, "latest")
5861

5962
print("\n* Creating new input project")
60-
helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#", '--name',
63+
run_cmd(['dotnet', 'new', 'classlib', "-f", "net5.0", "--language", "C#", '--name',
6164
projectNameIn, '--output', projectDirIn])
6265
helpers.remove_files(projectDirIn, '.cs')
6366

@@ -66,27 +69,31 @@ def write_csproj_prefix(ioWrapper):
6669
if (version != "latest"):
6770
cmd.append('--version')
6871
cmd.append(version)
69-
helpers.run_cmd(cmd)
72+
run_cmd(cmd)
73+
74+
sdk_version = '5.0.402'
75+
print("\n* Creating new global.json file and setting SDK to " + sdk_version)
76+
run_cmd(['dotnet', 'new', 'globaljson', '--force', '--sdk-version', sdk_version, '--output', workDir])
7077

7178
print("\n* Creating DB")
72-
helpers.run_cmd(['codeql', 'database', 'create', dbDir, '--language=csharp',
73-
'--command', 'dotnet build /t:rebuild ' + projectDirIn])
79+
run_cmd(['codeql', 'database', 'create', dbDir, '--language=csharp',
80+
'--command', 'dotnet build /t:rebuild /p:UseSharedCompilation=false ' + projectDirIn])
7481

7582
if not os.path.isdir(dbDir):
7683
print("Expected database directory " + dbDir + " not found.")
7784
exit(1)
7885

7986
print("\n* Running stubbing CodeQL query")
80-
helpers.run_cmd(['codeql', 'query', 'run', os.path.join(
87+
run_cmd(['codeql', 'query', 'run', os.path.join(
8188
thisDir, 'AllStubsFromReference.ql'), '--database', dbDir, '--output', bqrsFile])
8289

83-
helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
90+
run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
8491
jsonFile, '--format=json'])
8592

8693
print("\n* Creating new raw output project")
8794
rawSrcOutputDirName = 'src'
8895
rawSrcOutputDir = os.path.join(rawOutputDir, rawSrcOutputDirName)
89-
helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#",
96+
run_cmd(['dotnet', 'new', 'classlib', "--language", "C#",
9097
'--name', rawSrcOutputDirName, '--output', rawSrcOutputDir])
9198
helpers.remove_files(rawSrcOutputDir, '.cs')
9299

@@ -102,7 +109,7 @@ def write_csproj_prefix(ioWrapper):
102109
print("\n --> Generated stub files: " + rawSrcOutputDir)
103110

104111
print("\n* Formatting files")
105-
helpers.run_cmd(['dotnet', 'format', rawSrcOutputDir])
112+
run_cmd(['dotnet', 'format', rawSrcOutputDir])
106113

107114
print("\n --> Generated (formatted) stub files: " + rawSrcOutputDir)
108115

0 commit comments

Comments
 (0)