Skip to content

Commit 7778841

Browse files
committed
refactor(ci): move NuGet.config package source mapping creation from YAML to Fake
1 parent 450c033 commit 7778841

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

.github/workflows/publish_ci.yml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,6 @@ jobs:
2525
with:
2626
global-json-file: global.json
2727

28-
- name: Add the GitHub source
29-
run: dotnet nuget add source --name "github.com" "https://nuget.pkg.github.com/fsprojects/index.json"
30-
31-
- name: Ensure NuGet package source mapping
32-
shell: pwsh
33-
run: |
34-
$nugetConfigPath = "$HOME/.nuget/NuGet/NuGet.Config"
35-
[xml]$nugetConfig = Get-Content $nugetConfigPath
36-
37-
$packageSourceMapping = $nugetConfig.configuration.packageSourceMapping
38-
if ($packageSourceMapping -ne $null) {
39-
$packageSourceMapping.RemoveAll()
40-
} else {
41-
$packageSourceMapping = $nugetConfig.CreateElement("packageSourceMapping")
42-
$nugetConfig.configuration.AppendChild($packageSourceMapping)
43-
}
44-
45-
$nugetSource = $nugetConfig.CreateElement("packageSource")
46-
$nugetSource.SetAttribute("key", "nuget.org")
47-
$nugetPattern = $nugetConfig.CreateElement("package")
48-
$nugetPattern.SetAttribute("pattern", "*")
49-
$nugetSource.AppendChild($nugetPattern)
50-
$packageSourceMapping.AppendChild($nugetSource)
51-
52-
$nugetConfig.Save($nugetConfigPath)
53-
5428
- name: Publish to GitHub
5529
env:
5630
NUGET_TOKEN: ${{ secrets.NUGET_TOKEN }}

build/build.fs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open System
2+
open System.Xml
23
open Fake.Core
34
open Fake.DotNet
45
open Fake.Tools
@@ -190,6 +191,60 @@ module DocsTool =
190191
let result = Fornax.watch (fun p -> { p with WorkingDirectory = Some docsDir })
191192
result |> ignore
192193

194+
module NuGetConfig =
195+
/// <summary>
196+
/// Add GitHub package source to NuGet configuration
197+
/// </summary>
198+
let addGitHubSource () =
199+
let result =
200+
DotNet.exec id "nuget" "add source --name \"github.com\" \"https://nuget.pkg.github.com/fsprojects/index.json\""
201+
202+
if not result.OK then
203+
Trace.logf "Warning: Failed to add GitHub source: %A" result.Errors
204+
205+
/// <summary>
206+
/// Ensure NuGet package source mapping configuration
207+
/// </summary>
208+
let ensurePackageSourceMapping () =
209+
let homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
210+
let nugetConfigPath = homeDir </> ".nuget" </> "NuGet" </> "NuGet.Config"
211+
212+
try
213+
if IO.File.Exists nugetConfigPath then
214+
let doc = XmlDocument()
215+
doc.Load(nugetConfigPath)
216+
217+
let configNode = doc.SelectSingleNode("//configuration")
218+
if configNode <> null then
219+
// Remove existing packageSourceMapping if it exists
220+
let existingMapping = configNode.SelectSingleNode("packageSourceMapping")
221+
if existingMapping <> null then
222+
configNode.RemoveChild(existingMapping) |> ignore
223+
224+
// Create new packageSourceMapping element
225+
let packageSourceMapping = doc.CreateElement("packageSourceMapping")
226+
227+
// Create nuget.org source mapping
228+
let nugetSource = doc.CreateElement("packageSource")
229+
nugetSource.SetAttribute("key", "nuget.org")
230+
231+
let nugetPattern = doc.CreateElement("package")
232+
nugetPattern.SetAttribute("pattern", "*")
233+
234+
nugetSource.AppendChild(nugetPattern) |> ignore
235+
packageSourceMapping.AppendChild(nugetSource) |> ignore
236+
configNode.AppendChild(packageSourceMapping) |> ignore
237+
238+
doc.Save(nugetConfigPath)
239+
Trace.log "Successfully updated NuGet package source mapping"
240+
else
241+
Trace.logf "Warning: Could not find configuration node in %s" nugetConfigPath
242+
else
243+
Trace.logf "Warning: NuGet config file not found at %s" nugetConfigPath
244+
with
245+
| ex ->
246+
Trace.logf "Warning: Failed to update NuGet package source mapping: %s" ex.Message
247+
193248
let allReleaseChecks () = failOnWrongBranch ()
194249
//Changelog.failOnEmptyChangelog latestEntry
195250

@@ -567,6 +622,11 @@ let watchDocs ctx =
567622
let configuration = configuration (ctx.Context.AllExecutingTargets)
568623
DocsTool.watch (string configuration)
569624

625+
let configureNuGetForGitHub _ =
626+
Trace.log "Configuring NuGet for GitHub package publishing..."
627+
NuGetConfig.addGitHubSource ()
628+
NuGetConfig.ensurePackageSourceMapping ()
629+
570630
let selfCheck _ =
571631
let srcDir = rootDirectory </> "src"
572632
let consoleProj = srcDir </> "FSharpLint.Console"
@@ -607,6 +667,7 @@ let initTargets (ctx : Context.FakeExecutionContext) =
607667
Target.create "Clean" clean
608668
Target.create "DotnetRestore" dotnetRestore
609669
Target.create "DotnetToolRestore" dotnetToolRestore
670+
Target.create "ConfigureNuGetForGitHub" configureNuGetForGitHub
610671
Target.create "UpdateChangelog" updateChangelog
611672
Target.createBuildFailure "RevertChangelog" revertChangelog // Do NOT put this in the dependency chain
612673
Target.createFinal "DeleteChangelogBackupFile" deleteChangelogBackupFile // Do NOT put this in the dependency chain
@@ -676,6 +737,7 @@ let initTargets (ctx : Context.FakeExecutionContext) =
676737
"DotnetRestore"
677738
=?> ("CheckFormatCode", isCI.Value)
678739
=?> ("GenerateAssemblyInfo", isPublishToGitHub)
740+
=?> ("ConfigureNuGetForGitHub", isPublishToGitHub && isCI.Value && githubToken.IsSome)
679741
==> "DotnetBuild"
680742
==> "DotnetTest"
681743
==> "DotnetPack"
File renamed without changes.

src/FSharpLint.Core/FSharpLint.Core.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<IsPackable>true</IsPackable>
88
<RootNamespace>FSharpLint.Core</RootNamespace>
99
<EnableDefaultItems>false</EnableDefaultItems>
10+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1011

1112
<Title>FSharpLint.Core</Title>
1213
<Description>API to programmatically run FSharpLint.</Description>
@@ -15,7 +16,7 @@
1516

1617
<ItemGroup>
1718
<!-- Framework -->
18-
<Compile Include="AssemblyInfo.fs" />
19+
<Compile Include="Attributes.fs" />
1920
<Compile Include="Prelude.fs" />
2021
<Compile Include="Framework/Utilities.fs" />
2122
<Compile Include="Framework/HintParser.fs" />

0 commit comments

Comments
 (0)