Repository navigation
Expand file tree
/
Copy pathUsePublishedSourceGenerators.sh
More file actions
executable file
·71 lines (58 loc) · 2.81 KB
/
Copy pathUsePublishedSourceGenerators.sh
File metadata and controls
executable file
·71 lines (58 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Reverts Sakura.Framework back to using the published NuGet package for
# Sakura.Framework.SourceGenerators. Run this before committing.
# Run UseLocalSourceGenerators.sh to switch back to local.
set -e
CSPROJ="Sakura.Framework/Sakura.Framework.csproj"
python3 - "$CSPROJ" <<'EOF'
import sys, re, subprocess
path = sys.argv[1]
with open(path, 'r') as f:
content = f.read()
package_reference = '<PackageReference Include="Sakura.Framework.SourceGenerators"'
# The package goes back where it was, after the native libraries, in name order.
anchor = re.search(r' <PackageReference Include="Sakura\.Framework\.NativeLibraries"[^\n]*\n', content)
if package_reference not in content and not anchor:
sys.exit(f"Couldn't find where to add the generator package in {path}; nothing changed.")
# Only the reference marked as an analyzer: the plain one beside the code fixes builds the generator before packing,
# and stays.
content = re.sub(
r'\s*<ProjectReference Include="[^"]*Sakura\.Framework\.SourceGenerators\.csproj"[^>]*OutputItemType="Analyzer"[^>]*/>[ \t]*\n',
'\n',
content
)
# Remove empty ItemGroups left behind
content = re.sub(r'\s*<ItemGroup>\s*</ItemGroup>[ \t]*\n', '\n', content)
if package_reference not in content:
def committed_version():
# The version the committed csproj has, which is the one this was switched away from.
try:
committed = subprocess.check_output(['git', 'show', f'HEAD:{path}'], text=True, stderr=subprocess.DEVNULL)
match = re.search(r'<PackageReference Include="Sakura\.Framework\.SourceGenerators" Version="([^"]+)"', committed)
if match:
return match.group(1)
except Exception:
pass
try:
tag = subprocess.check_output(
['git', 'tag', '--list', 'source-generators-*', '--sort=-version:refname'],
text=True
).strip().splitlines()[0]
return tag.replace('source-generators-', '')
except Exception:
print("No committed or tagged version found; using the latest published one.")
return '*'
anchor = re.search(r' <PackageReference Include="Sakura\.Framework\.NativeLibraries"[^\n]*\n', content)
content = (
content[:anchor.end()]
+ ' <PackageReference Include="Sakura.Framework.SourceGenerators" Version="{}">\n'
' <PrivateAssets>all</PrivateAssets>\n'
' <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>\n'
' </PackageReference>\n'.format(committed_version())
+ content[anchor.end():]
)
with open(path, 'w') as f:
f.write(content)
print(f"Updated {path}")
EOF
echo "Done. Sakura.Framework now uses the published Sakura.Framework.SourceGenerators NuGet package."