Skip to content

Commit 43a32e6

Browse files
committed
Get Go SDK version from pulumi-language-dotnet/go.mod
1 parent 6b9d9d4 commit 43a32e6

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

build/Program.fs

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
open System
22
open System.IO
33
open System.Linq
4+
open System.Text.RegularExpressions
45
open Fake.IO
56
open Fake.Core
67
open Publish
@@ -27,6 +28,24 @@ let pulumiFSharp = Path.Combine(sdk, "Pulumi.FSharp")
2728
let integrationTests = Path.Combine(repositoryRoot, "integration_tests")
2829
let pulumiLanguageDotnet = Path.Combine(repositoryRoot, "pulumi-language-dotnet")
2930

31+
// Find the version of the Pulumi Go SDK that we are using for the language plugin.
32+
let findGoSDKVersion =
33+
let goMod = Path.Combine(pulumiLanguageDotnet, "go.mod")
34+
try
35+
let lines = File.ReadAllLines(goMod)
36+
let patternRegex = new Regex("^\\s*github.com/pulumi/pulumi/sdk", RegexOptions.IgnoreCase)
37+
match Array.tryFind (patternRegex.IsMatch) lines with
38+
| Some(matchingLine) ->
39+
let version = matchingLine.Split(' ')[1]
40+
let version = version.TrimStart('v')
41+
Some(version)
42+
| None ->
43+
None
44+
with
45+
| ex ->
46+
printfn "Error while trying to find the GO SDK version: %s" ex.Message
47+
None
48+
3049
/// Runs `dotnet clean` command against the solution file,
3150
/// then proceeds to delete the `bin` and `obj` directory of each project in the solution
3251
let cleanSdk() =
@@ -79,9 +98,12 @@ let listIntegrationTests() =
7998
let buildSdk() =
8099
cleanSdk()
81100
restoreSdk()
82-
printfn "Building Pulumi SDK"
83-
if Shell.Exec("dotnet", "build --configuration Release", sdk) <> 0
84-
then failwith "build failed"
101+
match findGoSDKVersion with
102+
| None -> failwith "Could not find the Pulumi SDK version in go.mod"
103+
| Some(version) ->
104+
printfn "Building Pulumi SDK"
105+
if Shell.Exec("dotnet", "build --configuration Release -p:PulumiSDKVersion=" + version, sdk) <> 0
106+
then failwith "build failed"
85107

86108
/// Publishes packages for Pulumi, Pulumi.Automation and Pulumi.FSharp to nuget.
87109
/// Requires NUGET_PUBLISH_KEY and PULUMI_VERSION environment variables.
@@ -148,10 +170,13 @@ let testPulumiSdk coverage =
148170
let testPulumiAutomationSdk coverage =
149171
cleanSdk()
150172
restoreSdk()
151-
printfn "Testing Pulumi Automation SDK"
152-
let coverageArgs = if coverage then $" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput={coverageDir}/coverage.pulumi.automation.xml" else ""
153-
if Shell.Exec("dotnet", "test --configuration Release" + coverageArgs, pulumiAutomationSdkTests) <> 0
154-
then failwith "automation tests failed"
173+
match findGoSDKVersion with
174+
| None -> failwith "Could not find the Pulumi SDK version in go.mod"
175+
| Some(version) ->
176+
printfn "Testing Pulumi Automation SDK"
177+
let coverageArgs = if coverage then $" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput={coverageDir}/coverage.pulumi.automation.xml" else ""
178+
if Shell.Exec("dotnet", "test --configuration Release -p:PulumiSDKVersion=" + version + " --filter InstallDefaultRoot" + coverageArgs, pulumiAutomationSdkTests) <> 0
179+
then failwith "automation tests failed"
155180

156181
let syncProtoFiles() = GitSync.repository {
157182
remoteRepository = "https://github.com/pulumi/pulumi.git"

sdk/Pulumi.Automation/Commands/LocalPulumiCommand.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ private LocalPulumiCommand(string command, SemVersion? version)
110110
/// <param name="cancellationToken">A cancellation token.</param>
111111
public static async Task<LocalPulumiCommand> Install(LocalPulumiCommandOptions? options = null, CancellationToken cancellationToken = default)
112112
{
113-
var _assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
114-
if (_assemblyVersion == null)
115-
{
116-
throw new Exception("Failed to get assembly version.");
117-
}
118-
var assemblyVersion = new SemVersion(_assemblyVersion.Major, _assemblyVersion.Minor, _assemblyVersion.Build);
119-
var version = options?.Version ?? assemblyVersion;
113+
var sdkVersion = Assembly.GetExecutingAssembly()
114+
.GetCustomAttributes(typeof(PulumiSDKVersion), false)
115+
.Cast<PulumiSDKVersion>().First().Version;
116+
117+
var version = options?.Version ?? sdkVersion;
120118
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
121119
var optionsWithDefaults = new LocalPulumiCommandOptions
122120
{

sdk/Pulumi.Automation/Pulumi.Automation.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
</None>
5959
</ItemGroup>
6060

61+
<ItemGroup>
62+
<AssemblyAttribute Include="Pulumi.Automation.PulumiSDKVersion">
63+
<_Parameter1>$(PulumiSDKVersion)</_Parameter1>
64+
</AssemblyAttribute>
65+
</ItemGroup>
66+
6167
<ItemGroup>
6268
<ProjectReference Include="..\Pulumi\Pulumi.csproj" />
6369
</ItemGroup>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Semver;
3+
4+
namespace Pulumi.Automation
5+
{
6+
[AttributeUsage(AttributeTargets.Assembly)]
7+
public class PulumiSDKVersion : Attribute
8+
{
9+
public SemVersion Version { get; private set; }
10+
11+
public PulumiSDKVersion() : this("") { }
12+
13+
public PulumiSDKVersion(string value)
14+
{
15+
value = value.Trim();
16+
if (value.StartsWith("v"))
17+
{
18+
value = value.Substring(1);
19+
}
20+
Version = SemVersion.Parse(value, SemVersionStyles.Strict);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)