|
2 | 2 | import os |
3 | 3 | import helpers |
4 | 4 |
|
5 | | - |
6 | | -print('Script to generate stub.cs file from a nuget package') |
7 | | -print('Usage: python ' + sys.argv[0] + |
| 5 | +print('Script to generate stub file from a nuget package') |
| 6 | +print(' Usage: python ' + sys.argv[0] + |
8 | 7 | ' NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir] [OUTPUT_NAME=stub]') |
| 8 | +print(' The script uses the dotnet cli, codeql cli, and dotnet format global tool') |
9 | 9 |
|
10 | 10 | if len(sys.argv) < 2: |
11 | 11 | print("\nPlease supply a nuget package name.") |
|
16 | 16 | nuget = sys.argv[1] |
17 | 17 |
|
18 | 18 | workDir = os.path.abspath(helpers.get_argv(3, "tempDir")) |
19 | | -projectName = "proj" |
20 | | -projectDir = os.path.join(workDir, projectName) |
| 19 | +projectNameIn = "input" |
| 20 | +projectDirIn = os.path.join(workDir, projectNameIn) |
| 21 | + |
| 22 | +projectNameOut = "output" |
| 23 | +projectDirOut = os.path.join(workDir, projectNameOut) |
21 | 24 | dbName = 'db' |
22 | 25 | dbDir = os.path.join(workDir, dbName) |
23 | 26 | outputName = helpers.get_argv(4, "stub") |
24 | | -outputFile = os.path.join(workDir, outputName + '.cs') |
25 | | -bqrsFile = os.path.join(workDir, outputName + '.bqrs') |
| 27 | +outputFile = os.path.join(projectDirOut, outputName + '.cs') |
| 28 | +bqrsFile = os.path.join(projectDirOut, outputName + '.bqrs') |
26 | 29 | version = helpers.get_argv(2, "latest") |
27 | 30 |
|
28 | | -print("\nCreating new project") |
| 31 | +print("\n* Creating new input project") |
29 | 32 | helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#", '--name', |
30 | | - projectName, '--output', projectDir]) |
| 33 | + projectNameIn, '--output', projectDirIn]) |
31 | 34 |
|
32 | | -print("\nAdding reference") |
33 | | -cmd = ['dotnet', 'add', projectDir, 'package', nuget] |
| 35 | +print("\n* Adding reference to package: " + nuget) |
| 36 | +cmd = ['dotnet', 'add', projectDirIn, 'package', nuget] |
34 | 37 | if (version != "latest"): |
35 | 38 | cmd.append('--version') |
36 | 39 | cmd.append(version) |
37 | 40 | helpers.run_cmd(cmd) |
38 | 41 |
|
39 | | -print("\nCreating DB") |
| 42 | +print("\n* Creating DB") |
40 | 43 | helpers.run_cmd(['codeql', 'database', 'create', dbDir, '--language=csharp', |
41 | | - '--command', 'dotnet build /t:rebuild ' + projectDir]) |
| 44 | + '--command', 'dotnet build /t:rebuild ' + projectDirIn]) |
42 | 45 |
|
43 | 46 | if not os.path.isdir(dbDir): |
44 | 47 | print("Expected database directory " + dbDir + " not found.") |
45 | 48 | exit(1) |
46 | 49 |
|
47 | | -print("\nRunning query") |
| 50 | +print("\n* Creating new output project") |
| 51 | +helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#", '--name', |
| 52 | + projectNameOut, '--output', projectDirOut]) |
| 53 | + |
| 54 | +print("\n* Running stubbing CodeQL query") |
48 | 55 | helpers.run_cmd(['codeql', 'query', 'run', os.path.join( |
49 | 56 | thisDir, 'AllStubsFromReference.ql'), '--database', dbDir, '--output', bqrsFile]) |
50 | 57 |
|
|
53 | 60 |
|
54 | 61 | helpers.trim_output_file(outputFile) |
55 | 62 |
|
56 | | -print("\nOutput: " + outputFile) |
| 63 | +print("\n --> Generated output file: " + outputFile) |
| 64 | + |
| 65 | +print("\n* Formatting file") |
| 66 | +helpers.run_cmd(['dotnet', 'format', projectDirOut, |
| 67 | + '--include', outputName + '.cs']) |
| 68 | + |
| 69 | +print("\n* Building output project") |
| 70 | +helpers.run_cmd(['dotnet', 'build', '/t:rebuild', projectDirOut], |
| 71 | + 'ERR: Build failed. Script failed to generate a stub that builds') |
| 72 | + |
| 73 | +print("\n --> Generated output file: " + outputFile) |
57 | 74 | exit(0) |
0 commit comments