Skip to content

Commit f42258d

Browse files
committed
Set up NuGet packages and related CI infrastructure.
Contributes to MochiLibraries#214
1 parent ad822e7 commit f42258d

File tree

14 files changed

+323
-8
lines changed

14 files changed

+323
-8
lines changed

.github/workflows/Biohazrd.yml

+149-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
name: Biohazrd
22
on:
33
push:
4+
# This prevents tag pushes from triggering this workflow
5+
branches: ['*']
46
pull_request:
7+
release:
8+
types: [published]
59
workflow_dispatch:
10+
inputs:
11+
version:
12+
description: "Version"
13+
default: ""
14+
will_publish_packages:
15+
description: "Publish packages?"
16+
default: "false"
617
env:
718
DOTNET_NOLOGO: true
819
DOTNET_CLI_TELEMETRY_OPTOUT: true
@@ -13,37 +24,171 @@ jobs:
1324
strategy:
1425
fail-fast: false
1526
matrix:
16-
os: ['windows-latest', 'ubuntu-latest']
27+
platform:
28+
- name: Windows
29+
architecture: x64
30+
runs-on: windows-latest
31+
- name: Linux
32+
architecture: x64
33+
runs-on: ubuntu-latest
1734
configuration: ['Debug', 'Release']
18-
name: Build and Test ${{matrix.configuration}} ${{matrix.os}}
19-
runs-on: ${{matrix.os}}
35+
include:
36+
# Linux Release x64 builds create packages (if applicable)
37+
- platform:
38+
runs-on: ubuntu-latest
39+
configuration: Release
40+
create-packages: true
41+
name: Build and Test ${{matrix.platform.name}} ${{matrix.platform.architecture}} ${{matrix.configuration}}
42+
runs-on: ${{matrix.platform.runs-on}}
2043
steps:
2144
# ----------------------------------------------------------------------- Checkout
2245
- name: Checkout
2346
uses: actions/checkout@v2
2447
with:
2548
submodules: recursive
2649

50+
# ----------------------------------------------------------------------- Setup Python
51+
- name: Setup Python 3.8
52+
uses: actions/setup-python@v2
53+
with:
54+
python-version: '3.8'
55+
2756
# ----------------------------------------------------------------------- Setup .NET
2857
- name: Setup .NET
2958
uses: actions/setup-dotnet@v1
3059
with:
3160
dotnet-version: 5.0.x
3261

62+
# ----------------------------------------------------------------------- Configure versioning
63+
- name: Configure build
64+
id: configuration
65+
run: python .github/workflows/configure-build.py
66+
env:
67+
github_event_name: ${{github.event_name}}
68+
github_run_number: ${{github.run_number}}
69+
release_version: ${{github.event.release.tag_name}}
70+
workflow_dispatch_version: ${{github.event.inputs.version}}
71+
3372
# ----------------------------------------------------------------------- Build
3473
- name: Restore
3574
run: dotnet restore
3675

3776
- name: Build
3877
run: dotnet build --no-restore --configuration ${{matrix.configuration}}
78+
79+
- name: Pack
80+
id: pack
81+
if: matrix.create-packages
82+
run: dotnet pack --no-build --configuration ${{matrix.configuration}}
3983

4084
# ----------------------------------------------------------------------- Test
4185
- name: Test
4286
run: dotnet test --no-restore --no-build --configuration ${{matrix.configuration}} --verbosity normal
87+
88+
# ----------------------------------------------------------------------- Collect Artifacts
89+
- name: Collect NuGet Packages
90+
uses: actions/upload-artifact@v2
91+
# We always want to collect packages when they were produced
92+
if: steps.pack.outcome == 'success' && always()
93+
with:
94+
name: Packages
95+
if-no-files-found: error
96+
path: packages/**
97+
98+
publish-packages-github:
99+
name: Publish to GitHub
100+
runs-on: ubuntu-latest
101+
needs: build-and-test
102+
# Pushes to main always publish CI packages
103+
# Published releases always publish packages
104+
# A manual workflow only publishes packages if explicitly enabled
105+
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.will_publish_packages == 'true')
106+
steps:
107+
# ----------------------------------------------------------------------- Setup .NET
108+
- name: Setup .NET
109+
uses: actions/setup-dotnet@v1
110+
with:
111+
dotnet-version: 5.0.x
112+
113+
# ----------------------------------------------------------------------- Download built packages
114+
- name: Download built packages
115+
uses: actions/download-artifact@v2
116+
with:
117+
name: Packages
118+
119+
# ----------------------------------------------------------------------- Upload release assets
120+
- name: Upload release assets
121+
if: github.event_name == 'release'
122+
uses: actions/github-script@v4
123+
with:
124+
user-agent: actions/github-script for ${{github.repository}}
125+
script: |
126+
const fs = require('fs').promises;
127+
const path = require('path');
128+
const upload_url = context.payload.release.upload_url;
129+
130+
if (!upload_url) {
131+
throw "Missing release asset upload URL!";
132+
}
133+
134+
for (let filePath of await fs.readdir('.')) {
135+
const fileExtension = path.extname(filePath);
136+
if (fileExtension != '.nupkg' && fileExtension != '.snupkg') {
137+
continue;
138+
}
139+
140+
console.log(`Uploading '${filePath}'`);
141+
const contentLength = (await fs.stat(filePath)).size;
142+
const fileContents = await fs.readFile(filePath);
143+
await github.repos.uploadReleaseAsset({
144+
url: upload_url,
145+
headers: {
146+
'content-type': 'application/octet-stream',
147+
'content-length': contentLength
148+
},
149+
name: path.basename(filePath),
150+
data: fileContents
151+
});
152+
}
153+
154+
# ----------------------------------------------------------------------- Push to GitHub Packages
155+
- name: Push to GitHub Packages
156+
run: dotnet nuget push "*.nupkg" --skip-duplicate --no-symbols true --api-key ${{secrets.GITHUB_TOKEN}} --source https://nuget.pkg.github.com/${{github.repository_owner}}
157+
env:
158+
# This is a workaround for https://github.com/NuGet/Home/issues/9775
159+
DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER: 0
160+
161+
publish-packages-nuget-org:
162+
name: Publish to NuGet.org
163+
runs-on: ubuntu-latest
164+
needs: build-and-test
165+
environment: NuGet.org
166+
# Release builds always publish packages to NuGet.org
167+
# Workflow dispatch builds will only publish packages if enabled and an explicit version number is given
168+
if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.will_publish_packages == 'true' && github.event.inputs.version != '')
169+
steps:
170+
# ----------------------------------------------------------------------- Setup .NET
171+
- name: Setup .NET
172+
uses: actions/setup-dotnet@v1
173+
with:
174+
dotnet-version: 5.0.x
175+
176+
# ----------------------------------------------------------------------- Download built packages
177+
- name: Download built packages
178+
uses: actions/download-artifact@v2
179+
with:
180+
name: Packages
181+
182+
# ----------------------------------------------------------------------- Push to NuGet.org
183+
- name: Push to NuGet.org
184+
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.NUGET_API_KEY}} --source ${{secrets.NUGET_API_URL}}
185+
env:
186+
# This is a workaround for https://github.com/NuGet/Home/issues/9775
187+
DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER: 0
43188

44189
send-ci-failure-notification:
45190
name: Send CI Failure Notification
46-
needs: build-and-test
191+
needs: [build-and-test, publish-packages-github, publish-packages-nuget-org]
47192
if: failure() && github.event_name != 'pull_request'
48193
continue-on-error: true
49194
runs-on: ubuntu-latest

.github/workflows/configure-build.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import re
4+
5+
import gha
6+
7+
#==================================================================================================
8+
# Get inputs
9+
#==================================================================================================
10+
def get_environment_variable(name):
11+
ret = os.getenv(name)
12+
13+
if ret is None:
14+
gha.print_error(f"Missing required parameter '{name}'")
15+
16+
if (ret == ''):
17+
return None
18+
19+
return ret
20+
21+
github_event_name = get_environment_variable('github_event_name')
22+
github_run_number = get_environment_variable('github_run_number')
23+
24+
#==================================================================================================
25+
# Determine build settings
26+
#==================================================================================================
27+
28+
version = f'0.0.0-ci{github_run_number}'
29+
30+
if github_event_name == 'release':
31+
version = get_environment_variable('release_version')
32+
elif github_event_name == 'workflow_dispatch':
33+
workflow_dispatch_version = get_environment_variable('workflow_dispatch_version')
34+
if workflow_dispatch_version is not None:
35+
version = workflow_dispatch_version
36+
37+
# Trim leading v off of version if present
38+
if version.startswith('v'):
39+
version = version[1:]
40+
41+
# Validate the version number
42+
if not re.match('^\d+\.\d+\.\d+(-[0-9a-zA-Z.-]+)?$', version):
43+
gha.print_error(f"'{version}' is not a valid semver version!")
44+
45+
# If there are any errors at this point, make sure we exit with an error code
46+
gha.fail_if_errors()
47+
48+
#==================================================================================================
49+
# Emit MSBuild properties
50+
#==================================================================================================
51+
print(f"Configuring build environment to build version {version}")
52+
gha.set_environment_variable('CiBuildVersion', version)
53+
54+
#==================================================================================================
55+
# Final check to exit with an error code if any errors were printed
56+
#==================================================================================================
57+
gha.fail_if_errors()

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vs/
22
bin/
33
obj/
4+
/packages/
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<!--
7+
This package is just a convenience package which brings in all the other components of Biohazrd.
8+
It does not contain code.
9+
-->
10+
<PackageId>Biohazrd</PackageId>
11+
<IncludeBuildOutput>false</IncludeBuildOutput>
12+
<IncludeSymbols>false</IncludeSymbols>
13+
14+
<!--
15+
This warning basically happens because this package has no assemblies but does have dependencies
16+
https://github.com/NuGet/Home/issues/8583
17+
-->
18+
<NoWarn>$(NoWarn);NU5128</NoWarn>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\Biohazrd.CSharp\Biohazrd.CSharp.csproj" />
23+
<ProjectReference Include="..\Biohazrd.OutputGeneration\Biohazrd.OutputGeneration.csproj" />
24+
<ProjectReference Include="..\Biohazrd.Transformation\Biohazrd.Transformation.csproj" />
25+
<ProjectReference Include="..\Biohazrd.Utilities\Biohazrd.Utilities.csproj" />
26+
<ProjectReference Include="..\Biohazrd\Biohazrd.csproj" />
27+
</ItemGroup>
28+
29+
</Project>

Biohazrd.CSharp/Biohazrd.CSharp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<PackageDescription>$(PackageDescription) — Transformations and binding generation for C#</PackageDescription>
56
</PropertyGroup>
67

78
<ItemGroup>

Biohazrd.OutputGeneration/Biohazrd.OutputGeneration.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<PackageDescription>$(PackageDescription) — Language-agnostic functionality for writing out code and other files</PackageDescription>
56
</PropertyGroup>
67

78
<ItemGroup>

Biohazrd.Transformation/Biohazrd.Transformation.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<PackageDescription>$(PackageDescription) — Language-agnostic functionality for manipulating declaration trees</PackageDescription>
56
</PropertyGroup>
67

78
<ItemGroup>

Biohazrd.Utilities/Biohazrd.Utilities.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<PackageDescription>$(PackageDescription) — Language-agnostic helpers</PackageDescription>
56
</PropertyGroup>
67

78
<ItemGroup>

Biohazrd.sln

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30011.22
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31612.314
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A8649210-1355-403A-8B2D-45BD759B1B32}"
77
ProjectSection(SolutionItems) = preProject
@@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tooling", "tooling", "{09A8
2121
ProjectSection(SolutionItems) = preProject
2222
tooling\Common.csproj.props = tooling\Common.csproj.props
2323
tooling\Common.csproj.targets = tooling\Common.csproj.targets
24+
tooling\Common.props = tooling\Common.props
25+
tooling\Common.targets = tooling\Common.targets
2426
EndProjectSection
2527
EndProject
2628
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Biohazrd.Utilities", "Biohazrd.Utilities\Biohazrd.Utilities.csproj", "{12BCC25F-9712-4223-8E34-50B7C060D8E2}"
@@ -41,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Biohazrd.CSharp.Tests", "Te
4143
EndProject
4244
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Biohazrd.OutputGeneration.Tests", "Tests\Biohazrd.OutputGeneration.Tests\Biohazrd.OutputGeneration.Tests.csproj", "{00E42A88-A883-4C43-BE2E-8A241A32EDA9}"
4345
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Biohazrd.AllInOne", "Biohazrd.AllInOne\Biohazrd.AllInOne.csproj", "{093ABF1F-E8E2-4E24-BCEA-845255E5BC5F}"
47+
EndProject
4448
Global
4549
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4650
Debug|Any CPU = Debug|Any CPU
@@ -87,6 +91,10 @@ Global
8791
{00E42A88-A883-4C43-BE2E-8A241A32EDA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
8892
{00E42A88-A883-4C43-BE2E-8A241A32EDA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
8993
{00E42A88-A883-4C43-BE2E-8A241A32EDA9}.Release|Any CPU.Build.0 = Release|Any CPU
94+
{093ABF1F-E8E2-4E24-BCEA-845255E5BC5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
95+
{093ABF1F-E8E2-4E24-BCEA-845255E5BC5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
96+
{093ABF1F-E8E2-4E24-BCEA-845255E5BC5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
97+
{093ABF1F-E8E2-4E24-BCEA-845255E5BC5F}.Release|Any CPU.Build.0 = Release|Any CPU
9098
EndGlobalSection
9199
GlobalSection(SolutionProperties) = preSolution
92100
HideSolutionNode = FALSE

Biohazrd/Biohazrd.csproj

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<!-- The Biohazrd package is an all-in-one package which brings in everything so we need to disambiguate this one. -->
6+
<PackageId>Biohazrd.Core</PackageId>
7+
<PackageDescription>$(PackageDescriptionPrefix)This package provides core functionality for parsing and representing C/C++.</PackageDescription>
58
</PropertyGroup>
69

710
<ItemGroup>
811
<PackageReference Include="ClangSharp" Version="[12.0.0-beta2]" />
912
<PackageReference Include="ClangSharp.Pathogen" Version="[0.0.0-ci45]" />
10-
13+
1114
<!-- Exclude ClangSharp's runtime packages (we provide libclang and libClangSharp via ClangSharp.Pathogen -->
1215
<PackageReference Include="libClang" Version="12.0.0" PrivateAssets="all" />
1316
<PackageReference Include="libClangSharp" Version="12.0.0-beta1" PrivateAssets="all" />
14-
17+
1518
<!-- This package is needed to support __HACK__Stl1300Workaround -->
1619
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="2.3.2262-g94fae01e" />
1720
</ItemGroup>

Tests/Directory.Build.props

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<Project>
22
<Import Project="../Directory.Build.props" />
3+
<PropertyGroup>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
36
<ItemGroup>
47
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
58
<!--

0 commit comments

Comments
 (0)