Skip to content

Commit 2589034

Browse files
committed
C#: Execute commands with the tempDir as the working directory
1 parent 8a6a8fc commit 2589034

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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', "-f", "net5.0", "--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,31 +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)
7073

7174
sdk_version = '5.0.402'
7275
print("\n* Creating new global.json file and setting SDK to " + sdk_version)
73-
helpers.run_cmd(['dotnet', 'new', 'globaljson', '--force', '--sdk-version', sdk_version])
76+
run_cmd(['dotnet', 'new', 'globaljson', '--force', '--sdk-version', sdk_version, '--output', workDir])
7477

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

7982
if not os.path.isdir(dbDir):
8083
print("Expected database directory " + dbDir + " not found.")
8184
exit(1)
8285

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

87-
helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
90+
run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
8891
jsonFile, '--format=json'])
8992

9093
print("\n* Creating new raw output project")
9194
rawSrcOutputDirName = 'src'
9295
rawSrcOutputDir = os.path.join(rawOutputDir, rawSrcOutputDirName)
93-
helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#",
96+
run_cmd(['dotnet', 'new', 'classlib', "--language", "C#",
9497
'--name', rawSrcOutputDirName, '--output', rawSrcOutputDir])
9598
helpers.remove_files(rawSrcOutputDir, '.cs')
9699

@@ -106,7 +109,7 @@ def write_csproj_prefix(ioWrapper):
106109
print("\n --> Generated stub files: " + rawSrcOutputDir)
107110

108111
print("\n* Formatting files")
109-
helpers.run_cmd(['dotnet', 'format', rawSrcOutputDir])
112+
run_cmd(['dotnet', 'format', rawSrcOutputDir], workDir)
110113

111114
print("\n --> Generated (formatted) stub files: " + rawSrcOutputDir)
112115

0 commit comments

Comments
 (0)