Skip to content

Commit

Permalink
chore: simplify code of Publish target
Browse files Browse the repository at this point in the history
  • Loading branch information
MangelMaxime committed Nov 27, 2024
1 parent 6498a3d commit 7ff3cab
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 642 deletions.
9 changes: 7 additions & 2 deletions src/Fable.AST/Fable.AST.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<Description>Fable AST</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>4.5.0</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile
</PropertyGroup>
<ItemGroup>
<Compile Include="Common.fs" />
<Compile Include="Fable.fs" />
<Compile Include="Plugins.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EasyBuild.PackageReleaseNotes.Tasks" Version="2.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
6 changes: 1 addition & 5 deletions src/Fable.Build/Fable.Build.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Utils.fs" />
<Compile Include="Utils/ChangelogParser.fs" />
<Compile Include="Utils/Changelog.fs" />
<Compile Include="Utils/Fsproj.fs" />
<Compile Include="Utils/Npm.fs" />
<Compile Include="Utils/Nuget.fs" />
<Compile Include="Workspace.fs" />
<Compile Include="SimpleExec.Extensions.fs" />
<Compile Include="FableLibrary/Core.fs" />
Expand Down Expand Up @@ -44,6 +39,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BlackFox.CommandLine" Version="1.0.0" />
<PackageReference Include="EasyBuild.Tools" Version="3.1.0" />
<PackageReference Include="Fake.IO.FileSystem" Version="6.1.3" />
<PackageReference Include="SimpleExec" Version="12.0.0" />
<PackageReference Include="Thoth.Json.Net" Version="12.0.0" />
Expand Down
72 changes: 22 additions & 50 deletions src/Fable.Build/Publish.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ open System.Text.RegularExpressions
open Build.FableLibrary
open System
open Build.Workspace
open EasyBuild.Tools.DotNet
open EasyBuild.Tools.Changelog
open EasyBuild.Tools.PackageJson
open EasyBuild.Tools.Npm

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

let fsprojPath = fsprojFiles[0]
let fsprojContent = File.ReadAllText fsprojPath
let changelogPath = Path.Combine(fsprojDir, "CHANGELOG.md")
let lastChangelogVersion = Changelog.getLastVersion changelogPath
let lastVersion = lastChangelogVersion |> fun v -> v.Version.ToString()

let lastVersionBody = ChangelogParser.Version.bodyAsMarkdown lastChangelogVersion

printfn $"Publishing: %s{fsprojDir}"

let nugetKey = Environment.GetEnvironmentVariable("FABLE_NUGET_KEY")

if isNull nugetKey then
failwithf $"Missing FABLE_NUGET_KEY environment variable"

if Fsproj.needPublishing fsprojContent lastVersion then
let updatedFsprojContent =
fsprojContent
|> Fsproj.replaceVersion lastVersion
|> Fsproj.replacePackageReleaseNotes lastVersionBody
printfn $"Publishing: %s{fsprojDir}"

File.WriteAllText(fsprojPath, updatedFsprojContent)
let nupkgPath = Dotnet.pack fsprojDir
Dotnet.Nuget.push (nupkgPath, nugetKey, noSymbols = noSymbols)
printfn $"Published!"
else
printfn $"Already up-to-date, skipping..."
let nupkgPath = DotNet.pack fsprojDir

// We skip duplicates because we might have already published the same version
// This is because we make an optimistic release and delegate the version set to EasyBuild.
DotNet.nugetPush (nupkgPath, apiKey = nugetKey, noSymbols = noSymbols, skipDuplicate = true)

let private publishNpm (projectDir: string) =
let packageJsonPath = Path.Combine(projectDir, "package.json")
let packageJsonContent = File.ReadAllText(packageJsonPath)
let changelogPath = Path.Combine(projectDir, "CHANGELOG.md")
let packageJsonFile = Path.Combine(projectDir, "package.json") |> FileInfo

let lastChangelogVersion =
Changelog.getLastVersion changelogPath |> fun v -> v.Version.ToString()

printfn $"Publishing: %s{projectDir}"
Path.Combine(projectDir, "CHANGELOG.md")
|> FileInfo
|> Changelog.findLastVersion

if Npm.needPublishing packageJsonContent lastChangelogVersion then
let updatedPackageJsonContent =
Npm.replaceVersion packageJsonContent lastChangelogVersion
PackageJson.replaceVersion (packageJsonFile, lastChangelogVersion)

File.WriteAllText(packageJsonPath, updatedPackageJsonContent)
if PackageJson.needPublishing packageJsonFile then
Npm.publish projectDir
printfn $"Published!"
else
printfn $"Already up-to-date, skipping..."

let private updateFableLibraryTsPackageJsonVersion () =
let packageJsonPath = Path.Combine(ProjectDir.fable_library_ts, "package.json")
let packageJsonContent = File.ReadAllText(packageJsonPath)
let changelogPath = Path.Combine(ProjectDir.fable_library_ts, "CHANGELOG.md")

let lastChangelogVersion =
Changelog.getLastVersion changelogPath |> fun v -> v.Version.ToString()

let updatedPackageJsonContent =
Npm.replaceVersion packageJsonContent lastChangelogVersion

File.WriteAllText(packageJsonPath, updatedPackageJsonContent)

let handle (args: string list) =
// Build all the fable-libraries
BuildFableLibraryDart().Run()
Expand All @@ -130,21 +102,21 @@ let handle (args: string list) =

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

publishNpm ProjectDir.fable_metadata

// Update embedded version (both compiler and libraries)
let changelogPath = Path.Combine(ProjectDir.fableCli, "CHANGELOG.md")

let compilerVersion =
Changelog.getLastVersion changelogPath |> fun v -> v.Version.ToString()
Path.Combine(ProjectDir.fableCli, "CHANGELOG.md")
|> FileInfo
|> Changelog.findLastVersion

updateLibraryVersionInFableTransforms
compilerVersion
{|
JavaScript = Npm.getVersionFromProjectDir ProjectDir.temp_fable_library_js
TypeScript = Npm.getVersionFromProjectDir ProjectDir.temp_fable_library_ts
JavaScript = PackageJson.tempFableLibraryJs |> PackageJson.getVersion
TypeScript = PackageJson.tempFableLibraryTs |> PackageJson.getVersion
|}

publishNuget ProjectDir.fableAst false
Expand Down
23 changes: 0 additions & 23 deletions src/Fable.Build/Utils/Changelog.fs

This file was deleted.

Loading

0 comments on commit 7ff3cab

Please sign in to comment.