Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 2773b16

Browse files
authored
feature: Enable Sdk usage as a normal dependency (#204)
* feature: Enable Sdk usage as a normal dependency * move main props/targets to build folder, import them in Sdk folder * stop circular dependency for Packing on Build by setting NoBuild=true, closes #203 * use GeneratePackageOnBuild in PackagedGenerator sample, to demonstrate the change * refactor a little sample build scripts
1 parent f9e687e commit 2773b16

11 files changed

Lines changed: 81 additions & 35 deletions

File tree

samples/MetapackageSample/build.ps1

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@
33
Write-Host "Running in $PSScriptRoot" -ForegroundColor Cyan
44
Push-Location $PSScriptRoot
55
try {
6-
$env:Configuration ??= 'Debug'
7-
8-
# clean up all restore and build artifacts:
9-
Remove-Item .nuget, bin, obj -Recurse -Force -ErrorAction Ignore
10-
11-
# set env variable to use local CG.R packages
12-
$env:LocalNuGetVersion = dotnet nbgv get-version --variable NuGetPackageVersion --project ../../src
13-
14-
Write-Host "Using CG.R package version: $env:LocalNuGetVersion" -ForegroundColor Cyan
15-
166
# pack directory (solution), then pack Metapackage
17-
'.','MetapackageSample' | ForEach-Object {
7+
'.', 'MetapackageSample' | ForEach-Object {
188
Write-Host "dotnet pack $_" -ForegroundColor Green
199
dotnet pack $_
2010
}
21-
Write-Host "dotnet run MetapackageConsumer" -ForegroundColor Green
11+
Write-Host "dotnet run -p MetapackageConsumer" -ForegroundColor Green
2212
dotnet run -p MetapackageConsumer
2313
}
2414
finally {

samples/PackagedGenerator/PackagedGenerator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
<PropertyGroup>
66
<TargetFramework>netcoreapp2.1</TargetFramework>
7+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
78
</PropertyGroup>
89

910
<Import Project="$(CodeGenerationRoslynPluginSdkPath)Sdk.targets" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env pwsh
2+
3+
Write-Host "Running in $PSScriptRoot" -ForegroundColor Cyan
4+
Push-Location $PSScriptRoot
5+
try {
6+
Write-Host "dotnet build" -ForegroundColor Green
7+
dotnet build
8+
}
9+
finally {
10+
Pop-Location
11+
}

samples/build.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ param (
55
$Projects
66
)
77

8+
function PrintAndInvoke {
9+
param ($expression)
10+
Write-Host "$expression" -ForegroundColor Green
11+
Invoke-Expression $expression
12+
}
13+
814
Write-Host "Running in $PSScriptRoot" -ForegroundColor Cyan
915
Push-Location $PSScriptRoot
1016
try {
@@ -22,20 +28,22 @@ try {
2228
$generators = Get-ChildItem -Directory -Name | Where-Object { $_ -match 'Generator$' }
2329

2430
# pack generators to make them available in folder feed
25-
$generators | Where-Object { $Projects -eq $null -or $Projects -contains $_} | ForEach-Object {
26-
Write-Host "dotnet pack $_" -ForegroundColor Green
27-
dotnet pack $_
31+
$generators | Where-Object { $Projects -eq $null -or $Projects -contains $_ } | ForEach-Object {
32+
if (Get-ChildItem $_/* -File -Include 'build.ps1') {
33+
PrintAndInvoke "$_/build.ps1"
34+
}
35+
elseif (Get-ChildItem $_/* -File -Include *.csproj, *.sln) {
36+
PrintAndInvoke "dotnet pack $_"
37+
}
2838
}
2939

3040
# build all other projects/solutions
31-
Get-ChildItem -Directory -Name -Exclude $generators | Where-Object { $Projects -eq $null -or $Projects -contains $_} | ForEach-Object {
41+
Get-ChildItem -Directory -Name -Exclude $generators | Where-Object { $Projects -eq $null -or $Projects -contains $_ } | ForEach-Object {
3242
if (Get-ChildItem $_/* -File -Include 'build.ps1') {
33-
Write-Host "$_/build.ps1" -ForegroundColor Green
34-
& "$_/build.ps1"
43+
PrintAndInvoke "$_/build.ps1"
3544
}
3645
elseif (Get-ChildItem $_/* -File -Include *.csproj, *.sln) {
37-
Write-Host "dotnet build $_" -ForegroundColor Green
38-
dotnet build $_
46+
PrintAndInvoke "dotnet build $_"
3947
}
4048
}
4149
}

src/CodeGeneration.Roslyn.Plugin.Sdk/CodeGeneration.Roslyn.Plugin.Sdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
</PropertyGroup>
5151
<WriteLinesToFile File="$(VersionPropsPath)" Lines="$(VersionPropsContent)" Overwrite="true" />
5252
<ItemGroup>
53-
<None Include="$(VersionPropsPath)" Pack="true" PackagePath="Sdk" />
53+
<None Include="$(VersionPropsPath)" Pack="true" PackagePath="build/" />
5454
<FileWrites Include="$(VersionPropsPath)" />
5555
</ItemGroup>
5656
</Target>

src/CodeGeneration.Roslyn.Plugin.Sdk/Sdk/Sdk.props

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33

44
<PropertyGroup>
5-
<!-- Announce this SDK -->
6-
<UsingCodeGenerationRoslynPluginSdk>true</UsingCodeGenerationRoslynPluginSdk>
5+
<!-- Announce this SDK (add *AsSdk to differentiate between using this as Sdk
6+
and normal PackageReference/dependency usage)-->
7+
<UsingCodeGenerationRoslynPluginSdkAsSdk>true</UsingCodeGenerationRoslynPluginSdkAsSdk>
78
</PropertyGroup>
89

9-
<!--
10-
Version.props defines CodeGenerationRoslynPluginSdkVersion Property in NuGet package.
11-
This is used to add an implicit reference to the CodeGeneration.Roslyn package,
12-
so that the versions of Plugin.Sdk and CG.R dependency stay in sync.
13-
-->
14-
<Import Project="Version.props" Condition=" Exists('Version.props') " />
15-
16-
<Import Project="BuildPluginPackage.props" />
10+
<Import Project="../build/CodeGeneration.Roslyn.Plugin.Sdk.props" />
1711

1812
</Project>

src/CodeGeneration.Roslyn.Plugin.Sdk/Sdk/Sdk.targets

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
<!--
55
Add an implicit reference to CodeGeneration.Roslyn package if not disabled.
66
Disable by setting AddImplicitCodeGenerationRoslynReference to false.
7+
8+
This is Sdk-only because adding PackageReference in build/.targets doesn't actually
9+
impact restore (or *shouldn't*).
710
-->
811
<ItemGroup Condition=" '$(AddImplicitCodeGenerationRoslynReference)' != 'false' ">
912
<!--
10-
CodeGenerationRoslynPluginSdkVersion Property is defined in Version.props (imported in Sdk.props).
13+
CodeGenerationRoslynPluginSdkVersion Property is defined in Version.props (imported in .props).
1114
This is used to add an implicit reference to the CodeGeneration.Roslyn package,
1215
so that the versions of Plugin.Sdk and CG.R dependency stay in sync.
1316
-->
@@ -17,6 +20,6 @@
1720
ExcludeAssets="runtime" />
1821
</ItemGroup>
1922

20-
<Import Project="BuildPluginPackage.targets" />
23+
<Import Project="../build/CodeGeneration.Roslyn.Plugin.Sdk.targets" />
2124

2225
</Project>

src/CodeGeneration.Roslyn.Plugin.Sdk/Sdk/BuildPluginPackage.props renamed to src/CodeGeneration.Roslyn.Plugin.Sdk/build/BuildPluginPackage.props

File renamed without changes.

src/CodeGeneration.Roslyn.Plugin.Sdk/Sdk/BuildPluginPackage.targets renamed to src/CodeGeneration.Roslyn.Plugin.Sdk/build/BuildPluginPackage.targets

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#targetsfortfmspecificcontentinpackage
2020
-->
2121
<TargetsForTfmSpecificContentInPackage>
22-
$(TargetsForTfmSpecificContentInPackage);
23-
PackCodeGenerationRoslynPlugin
22+
$(TargetsForTfmSpecificContentInPackage);
23+
_SetNoBuildTrueWhenGeneratePackageOnBuild;
24+
PackCodeGenerationRoslynPlugin
2425
</TargetsForTfmSpecificContentInPackage>
2526
<!-- Declare dependencies of PackCodeGenerationRoslynPlugin target -->
2627
<PackCodeGenerationRoslynPluginDependsOn>
@@ -108,5 +109,19 @@
108109
</PropertyGroup>
109110
<Warning Condition=" '$(PackAsCodeGenerationRoslynPlugin)' == '' AND '$(TargetFrameworks)' != '' " Text="$(_PackAsCgrPluginRequiresSingleTargetFrameworkMessage.Trim())" />
110111
</Target>
112+
113+
<!--
114+
This is required to prevent circular dependency:
115+
- Build -> GeneratePackageOnBuild -> Pack
116+
- Pack -> PackCodeGenerationRoslynPlugin depends on Publish
117+
- Publish depends on Build (**if NoBuild != true**)
118+
119+
If it's causing trouble, disable this target by setting SkipSetNoBuildTrueWhenGeneratePackageOnBuild=true
120+
-->
121+
<Target Name="_SetNoBuildTrueWhenGeneratePackageOnBuild" Condition=" '$(GeneratePackageOnBuild)' == 'true' and '$(SkipSetNoBuildTrueWhenGeneratePackageOnBuild)' != 'true' ">
122+
<PropertyGroup>
123+
<NoBuild>true</NoBuild>
124+
</PropertyGroup>
125+
</Target>
111126

112127
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup>
5+
<!-- Announce this SDK -->
6+
<UsingCodeGenerationRoslynPluginSdk>true</UsingCodeGenerationRoslynPluginSdk>
7+
</PropertyGroup>
8+
9+
<!--
10+
Version.props defines CodeGenerationRoslynPluginSdkVersion Property in NuGet package.
11+
This is used to add an implicit reference to the CodeGeneration.Roslyn package,
12+
so that the versions of Plugin.Sdk and CG.R dependency stay in sync.
13+
-->
14+
<Import Project="Version.props" Condition=" Exists('Version.props') " />
15+
16+
<Import Project="BuildPluginPackage.props" />
17+
18+
</Project>

0 commit comments

Comments
 (0)