Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion csharp/scripts/stubs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ python3 make_stubs_all.py /Users/tmp/working-dir
```
The script contains a hardcoded list of `packages`. If a new package is needed for test purposes, it should be added to the `packages` list in the script.

The generated stubs require some manual changes before they are compilable.
The generated `.csproj` files reference the dependency versions that were actually restored. They also reference any framework whose stubs contain types from a package. This happens when a framework supersedes the package's assemblies, as `Microsoft.AspNetCore.App` does for `Microsoft.Extensions.*`. The generated stubs might still require some manual changes before they are compilable.

The output stubs are found in the `[DIR]/output/stubs` folder and can be copied over to `csharp/ql/test/resources/stubs`.
65 changes: 54 additions & 11 deletions csharp/scripts/stubs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import subprocess
import json
import shutil
import re

def run_cmd(cmd, msg="Failed to run command"):
print('Running ' + ' '.join(cmd))
Expand Down Expand Up @@ -162,6 +161,17 @@ def make_stubs(self):
frameworks = set()
copiedFiles = set()

# Map from (lower case) DLL file name to the framework whose stubs contain it.
frameworkDlls = {}
for pathInfo in pathInfos:
segments = pathInfo.split('/')
for segment in segments[:-1]:
if segment.lower().endswith('.ref'):
framework = segment[:-len('.ref')]
if framework.lower() != 'microsoft.netcore.app':
frameworkDlls[segments[-1].lower()] = framework
break

assetsJsonFile = os.path.join(self.projectDirIn, 'obj', 'project.assets.json')
with open(assetsJsonFile) as json_data:
data = json.load(json_data)
Expand All @@ -170,10 +180,27 @@ def make_stubs(self):
exit(1)
target = list(data['targets'].keys())[0]
print("Found target: " + target)

# Map from lower case framework name to its correctly cased name.
frameworkNames = {}
for framework in data['project']['frameworks'].values():
for name in framework.get('frameworkReferences', {}).keys():
frameworkNames[name.lower()] = name
for packageInfo in data['targets'][target].values():
for name in packageInfo.get('frameworkReferences', []):
frameworkNames[name.lower()] = name

# Map from package name to the version that was actually restored.
resolvedVersions = {}
for package in data['targets'][target].keys():
name, version = package.split('/')
resolvedVersions[name.lower()] = version

for package in data['targets'][target].keys():
parts = package.split('/')
name = parts[0]
version = parts[1]
packageInfo = data['targets'][target][package]
packageDir = os.path.join(stubsDir, name, version)
if not os.path.exists(packageDir):
os.makedirs(packageDir)
Expand All @@ -184,31 +211,47 @@ def make_stubs(self):
pf.write(' <ItemGroup>\n')

dlls = set()
if 'compile' in data['targets'][target][package]:
for dll in data['targets'][target][package]['compile']:
if 'compile' in packageInfo:
for dll in packageInfo['compile']:
dlls.add(
(name + '/' + version + '/' + dll).lower())
if 'runtime' in data['targets'][target][package]:
for dll in data['targets'][target][package]['runtime']:
if 'runtime' in packageInfo:
for dll in packageInfo['runtime']:
dlls.add((name + '/' + version + '/' + dll).lower())

stubbedDlls = set()
for pathInfo in pathInfos:
for dll in dlls:
if pathInfo.lower().endswith(dll):
stubbedDlls.add(dll)
copiedFiles.add(pathInfo)
shutil.copy2(pathInfos[pathInfo], packageDir)

if 'dependencies' in data['targets'][target][package]:
for dependency in data['targets'][target][package]['dependencies'].keys():
depString = data['targets'][target][package]['dependencies'][dependency]
depVersion = re.search("(\d+\.\d+\.\d+(-[a-z]+)?)", depString).group(0)
if 'dependencies' in packageInfo:
for dependency in packageInfo['dependencies'].keys():
depVersion = resolvedVersions[dependency.lower()]
pf.write(' <ProjectReference Include="../../' +
dependency + '/' + depVersion + '/' + dependency + '.csproj" />\n')

if 'frameworkReferences' in data['targets'][target][package]:
packageFrameworks = set()
if 'frameworkReferences' in packageInfo:
packageFrameworks.update(packageInfo['frameworkReferences'])

# The DLLs of a package may be superseded by a newer version of the same
# assembly in a framework (e.g. Microsoft.Extensions.* in Microsoft.AspNetCore.App).
# The dependency manager used by the stub generator resolves such conflicts by
# keeping only the highest version of each assembly, so stubs are only
# generated for the framework's copy. The package then needs to reference
# the framework to get those stubs.
for dll in dlls - stubbedDlls:
framework = frameworkDlls.get(dll.split('/')[-1])
if framework is not None:
packageFrameworks.add(frameworkNames.get(framework.lower(), framework))

if packageFrameworks:
if not os.path.exists(frameworksDir):
os.makedirs(frameworksDir)
for framework in data['targets'][target][package]['frameworkReferences']:
for framework in sorted(packageFrameworks):
frameworks.add(framework)
frameworkDir = os.path.join(
frameworksDir, framework)
Expand Down
Loading