Skip to content

Commit 0bc751e

Browse files
authored
Simplify Fable.Build code (#3964)
* chore: simplify code of Publish target * fix: GithubRelease.fs
1 parent 3a6142f commit 0bc751e

16 files changed

Lines changed: 220 additions & 706 deletions

File tree

src/Fable.AST/Fable.AST.fsproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
<Description>Fable AST</Description>
66
<TargetFramework>netstandard2.0</TargetFramework>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8-
<Version>5.0.0-beta.1</Version>
98
</PropertyGroup>
109
<ItemGroup>
1110
<Compile Include="Common.fs" />
1211
<Compile Include="Fable.fs" />
1312
<Compile Include="Plugins.fs" />
1413
</ItemGroup>
14+
<ItemGroup>
15+
<PackageReference Include="EasyBuild.PackageReleaseNotes.Tasks" Version="2.0.0">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
</ItemGroup>
1520
</Project>

src/Fable.Build/Fable.Build.fsproj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<Compile Include="Utils.fs" />
9-
<Compile Include="Utils/ChangelogParser.fs" />
10-
<Compile Include="Utils/Changelog.fs" />
11-
<Compile Include="Utils/Fsproj.fs" />
12-
<Compile Include="Utils/Npm.fs" />
13-
<Compile Include="Utils/Nuget.fs" />
9+
<Compile Include="Utils/LastVersionFinder.fs" />
1410
<Compile Include="Workspace.fs" />
1511
<Compile Include="SimpleExec.Extensions.fs" />
1612
<Compile Include="FableLibrary/Core.fs" />
@@ -44,10 +40,11 @@
4440
</ItemGroup>
4541
<ItemGroup>
4642
<PackageReference Include="BlackFox.CommandLine" Version="1.0.0" />
43+
<PackageReference Include="EasyBuild.Tools" Version="4.1.0" />
4744
<PackageReference Include="Fake.IO.FileSystem" Version="6.1.3" />
45+
<PackageReference Include="FsToolkit.ErrorHandling" Version="4.18.0" />
4846
<PackageReference Include="SimpleExec" Version="12.0.0" />
4947
<PackageReference Include="Thoth.Json.Net" Version="12.0.0" />
50-
<PackageReference Include="Octokit" Version="8.0.0" />
5148
<PackageReference Include="Semver" Version="3.0.0" />
5249
</ItemGroup>
5350
</Project>

src/Fable.Build/GithubRelease.fs

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,42 @@
11
module Build.GithubRelease
22

3-
open Build.Utils
4-
open Octokit
5-
open System
3+
open System.IO
64
open Build.Workspace
75
open SimpleExec
86
open BlackFox.CommandLine
7+
open EasyBuild.Tools.Git
98

10-
let private createGithubRelease (githubToken: string) (version: ChangelogParser.Types.Version) =
9+
let private createGithubRelease (version: LastVersionFinder.Version) =
1110

1211
let struct (lastestTag, _) =
1312
Command.ReadAsync("git", "describe --abbrev=0 --tags")
1413
|> Async.AwaitTask
1514
|> Async.RunSynchronously
1615

16+
let versionText = version.Version.ToString()
17+
1718
// Only create a Github release if the tag doesn't exist
18-
// It can happens that we trigger a release whre Fable.Cli
19+
// It can happens that we trigger a release where Fable.Cli
1920
// is already up to date.
20-
if lastestTag.Trim() <> version.Version.ToString() then
21-
let githubClient = GitHubClient(ProductHeaderValue("fable-release-tool"))
22-
23-
githubClient.Credentials <- Credentials(githubToken)
24-
25-
let newRelease = NewRelease(version.Version.ToString())
26-
newRelease.Name <- version.Version.ToString()
27-
newRelease.Body <- ChangelogParser.Version.bodyAsMarkdown version
28-
newRelease.Draft <- false
29-
newRelease.Prerelease <- false // TODO: Detect if this is a prerelease
30-
31-
githubClient.Repository.Release.Create("fable-compiler", "Fable", newRelease)
32-
|> Async.AwaitTask
33-
|> Async.RunSynchronously
34-
|> ignore
35-
36-
let private createReleaseCommitAndPush (version: ChangelogParser.Types.Version) =
21+
if lastestTag.Trim() <> versionText then
22+
Command.Run(
23+
"gh",
24+
CmdLine.empty
25+
|> CmdLine.appendRaw "release"
26+
|> CmdLine.appendRaw "create"
27+
|> CmdLine.appendRaw versionText
28+
|> CmdLine.appendPrefix "--title" versionText
29+
|> CmdLine.appendPrefix "--notes" version.Body
30+
|> CmdLine.appendIf version.Version.IsPrerelease "--prerelease"
31+
|> CmdLine.toString
32+
)
33+
34+
let private createReleaseCommitAndPush (version: LastVersionFinder.Version) =
3735
let versionText = version.Version.ToString()
3836

39-
Command.Run(
40-
"git",
41-
CmdLine.empty
42-
|> CmdLine.appendRaw "commit"
43-
|> CmdLine.appendPrefix "-am" $"Release {versionText}"
44-
|> CmdLine.toString
45-
)
46-
47-
Command.Run("git", "push")
48-
37+
Git.addAll ()
38+
Git.commit ($"Release {versionText}")
39+
Git.push ()
4940

5041
let handle (args: string list) =
5142
let struct (currentBranch, _) =
@@ -56,16 +47,15 @@ let handle (args: string list) =
5647
if currentBranch.Trim() <> "main" then
5748
failwith "You must be on the main branch to release"
5849

59-
Publish.handle args
50+
// Check if the user is authenticated
51+
Command.Run("gh", "auth status")
6052

61-
let githubToken = Environment.GetEnvironmentVariable("GITHUB_TOKEN_FABLE_ORG")
62-
63-
if isNull githubToken then
64-
failwith "Missing GITHUB_TOKEN_FABLE_ORG environment variable"
65-
66-
let versionInfo = Changelog.getLastVersion Changelog.fableCLi
53+
Publish.handle args
6754

68-
createReleaseCommitAndPush versionInfo
55+
let changelogContent = File.ReadAllText(Changelog.fableCLi)
6956

70-
// Disable Github release for now, because it's not working
71-
// createGithubRelease githubToken versionInfo
57+
match LastVersionFinder.tryFindLastVersion changelogContent with
58+
| Ok version ->
59+
createReleaseCommitAndPush version
60+
createGithubRelease version
61+
| Error err -> err.ToText() |> failwith

src/Fable.Build/Package.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ module Build.Package
22

33
open Build.Utils
44
open Build.FableLibrary
5-
open Octokit
65
open System
76
open Build.Workspace
87
open SimpleExec
98
open BlackFox.CommandLine
109
open System.IO
10+
open EasyBuild.Tools.PackageJson
1111

1212
let private packageDestination = Path.Resolve("temp", "packages")
1313

@@ -32,8 +32,8 @@ let handle (args: string list) =
3232
Publish.updateLibraryVersionInFableTransforms
3333
tempVersion
3434
{|
35-
JavaScript = Npm.getVersionFromProjectDir ProjectDir.temp_fable_library_js
36-
TypeScript = Npm.getVersionFromProjectDir ProjectDir.temp_fable_library_ts
35+
JavaScript = PackageJson.tempFableLibraryJs |> PackageJson.getVersion
36+
TypeScript = PackageJson.tempFableLibraryTs |> PackageJson.getVersion
3737
|}
3838

3939
Command.Run(

src/Fable.Build/Publish.fs

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ open System.Text.RegularExpressions
66
open Build.FableLibrary
77
open System
88
open Build.Workspace
9+
open EasyBuild.Tools.DotNet
10+
open EasyBuild.Tools.Changelog
11+
open EasyBuild.Tools.PackageJson
12+
open EasyBuild.Tools.Npm
913

1014
let updateLibraryVersionInFableTransforms
1115
(compilerVersion: string)
@@ -52,67 +56,35 @@ let private publishNuget (fsprojDir: string) (noSymbols: bool) =
5256
if Array.length fsprojFiles <> 1 then
5357
failwithf $"Expected to find exactly one fsproj file in %s{fsprojDir}"
5458

55-
let fsprojPath = fsprojFiles[0]
56-
let fsprojContent = File.ReadAllText fsprojPath
57-
let changelogPath = Path.Combine(fsprojDir, "CHANGELOG.md")
58-
let lastChangelogVersion = Changelog.getLastVersion changelogPath
59-
let lastVersion = lastChangelogVersion |> fun v -> v.Version.ToString()
60-
61-
let lastVersionBody = ChangelogParser.Version.bodyAsMarkdown lastChangelogVersion
62-
63-
printfn $"Publishing: %s{fsprojDir}"
64-
6559
let nugetKey = Environment.GetEnvironmentVariable("FABLE_NUGET_KEY")
6660

6761
if isNull nugetKey then
6862
failwithf $"Missing FABLE_NUGET_KEY environment variable"
6963

70-
if Fsproj.needPublishing fsprojContent lastVersion then
71-
let updatedFsprojContent =
72-
fsprojContent
73-
|> Fsproj.replaceVersion lastVersion
74-
|> Fsproj.replacePackageReleaseNotes lastVersionBody
64+
printfn $"Publishing: %s{fsprojDir}"
7565

76-
File.WriteAllText(fsprojPath, updatedFsprojContent)
77-
let nupkgPath = Dotnet.pack fsprojDir
78-
Dotnet.Nuget.push (nupkgPath, nugetKey, noSymbols = noSymbols)
79-
printfn $"Published!"
80-
else
81-
printfn $"Already up-to-date, skipping..."
66+
let nupkgPath = DotNet.pack fsprojDir
67+
68+
// We skip duplicates because we might have already published the same version
69+
// This is because we make an optimistic release and delegate the version set to EasyBuild.PackageReleaseNotes.Tasks
70+
DotNet.nugetPush (nupkgPath, apiKey = nugetKey, noSymbols = noSymbols, skipDuplicate = true)
8271

8372
let private publishNpm (projectDir: string) =
84-
let packageJsonPath = Path.Combine(projectDir, "package.json")
85-
let packageJsonContent = File.ReadAllText(packageJsonPath)
86-
let changelogPath = Path.Combine(projectDir, "CHANGELOG.md")
73+
let packageJsonFile = Path.Combine(projectDir, "package.json") |> FileInfo
8774

8875
let lastChangelogVersion =
89-
Changelog.getLastVersion changelogPath |> fun v -> v.Version.ToString()
90-
91-
printfn $"Publishing: %s{projectDir}"
76+
Path.Combine(projectDir, "CHANGELOG.md")
77+
|> FileInfo
78+
|> Changelog.findLastVersion
9279

93-
if Npm.needPublishing packageJsonContent lastChangelogVersion then
94-
let updatedPackageJsonContent =
95-
Npm.replaceVersion packageJsonContent lastChangelogVersion
80+
PackageJson.replaceVersion (packageJsonFile, lastChangelogVersion)
9681

97-
File.WriteAllText(packageJsonPath, updatedPackageJsonContent)
82+
if PackageJson.needPublishing packageJsonFile then
9883
Npm.publish projectDir
9984
printfn $"Published!"
10085
else
10186
printfn $"Already up-to-date, skipping..."
10287

103-
let private updateFableLibraryTsPackageJsonVersion () =
104-
let packageJsonPath = Path.Combine(ProjectDir.fable_library_ts, "package.json")
105-
let packageJsonContent = File.ReadAllText(packageJsonPath)
106-
let changelogPath = Path.Combine(ProjectDir.fable_library_ts, "CHANGELOG.md")
107-
108-
let lastChangelogVersion =
109-
Changelog.getLastVersion changelogPath |> fun v -> v.Version.ToString()
110-
111-
let updatedPackageJsonContent =
112-
Npm.replaceVersion packageJsonContent lastChangelogVersion
113-
114-
File.WriteAllText(packageJsonPath, updatedPackageJsonContent)
115-
11688
let handle (args: string list) =
11789
// Build all the fable-libraries
11890
BuildFableLibraryDart().Run()
@@ -130,21 +102,21 @@ let handle (args: string list) =
130102

131103
// We also want to update the original package.json if needed
132104
// This is to keep the versions consistent across the project
133-
updateFableLibraryTsPackageJsonVersion ()
105+
PackageJson.replaceVersion (PackageJson.fableLibraryTs, Changelog.fableLibraryTs |> Changelog.findLastVersion)
134106

135107
publishNpm ProjectDir.fable_metadata
136108

137109
// Update embedded version (both compiler and libraries)
138-
let changelogPath = Path.Combine(ProjectDir.fableCli, "CHANGELOG.md")
139-
140110
let compilerVersion =
141-
Changelog.getLastVersion changelogPath |> fun v -> v.Version.ToString()
111+
Path.Combine(ProjectDir.fableCli, "CHANGELOG.md")
112+
|> FileInfo
113+
|> Changelog.findLastVersion
142114

143115
updateLibraryVersionInFableTransforms
144116
compilerVersion
145117
{|
146-
JavaScript = Npm.getVersionFromProjectDir ProjectDir.temp_fable_library_js
147-
TypeScript = Npm.getVersionFromProjectDir ProjectDir.temp_fable_library_ts
118+
JavaScript = PackageJson.tempFableLibraryJs |> PackageJson.getVersion
119+
TypeScript = PackageJson.tempFableLibraryTs |> PackageJson.getVersion
148120
|}
149121

150122
publishNuget ProjectDir.fableAst false

src/Fable.Build/Utils/Changelog.fs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)