Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Oct 9, 2023
2 parents 2ea7401 + c69f787 commit a4cd326
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 110 deletions.
97 changes: 97 additions & 0 deletions build/BasicTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,103 @@ open Fake.IO.Globbing.Operators

open ProjectInfo

[<AutoOpen>]
module Helper =

open Fake
open Fake.Core

let createProcess exe arg dir =
CreateProcess.fromRawCommandLine exe arg
|> CreateProcess.withWorkingDirectory dir
|> CreateProcess.ensureExitCode

module Proc =

module Parallel =

open System

let locker = obj()

let colors = [|
ConsoleColor.DarkYellow
ConsoleColor.DarkCyan
ConsoleColor.Magenta
ConsoleColor.Blue
ConsoleColor.Cyan
ConsoleColor.DarkMagenta
ConsoleColor.DarkBlue
ConsoleColor.Yellow
|]

let print color (colored: string) (line: string) =
lock locker
(fun () ->
let currentColor = Console.ForegroundColor
Console.ForegroundColor <- color
Console.Write colored
Console.ForegroundColor <- currentColor
Console.WriteLine line)

let onStdout index name (line: string) =
let color = colors.[index % colors.Length]
if isNull line then
print color $"{name}: --- END ---" ""
else if String.isNotNullOrEmpty line then
print color $"{name}: " line

let onStderr name (line: string) =
let color = ConsoleColor.Red
if isNull line |> not then
print color $"{name}: " line

let redirect (index, (name, createProcess)) =
createProcess
|> CreateProcess.redirectOutputIfNotRedirected
|> CreateProcess.withOutputEvents (onStdout index name) (onStderr name)

let printStarting indexed =
for (index, (name, c: CreateProcess<_>)) in indexed do
let color = colors.[index % colors.Length]
let wd =
c.WorkingDirectory
|> Option.defaultValue ""
let exe = c.Command.Executable
let args = c.Command.Arguments.ToStartInfo
print color $"{name}: {wd}> {exe} {args}" ""

let run cs =
cs
|> Seq.toArray
|> Array.indexed
|> fun x -> printStarting x; x
|> Array.map redirect
|> Array.Parallel.map Proc.run

let dotnet = createProcess "dotnet"

let npm =
let npmPath =
match ProcessUtils.tryFindFileOnPath "npm" with
| Some path -> path
| None ->
"npm was not found in path. Please install it and make sure it's available from your path. " +
"See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
|> failwith

createProcess npmPath

let run proc arg dir =
proc arg dir
|> Proc.run
|> ignore

let runParallel processes =
processes
|> Proc.Parallel.run
|> ignore

let setPrereleaseTag = BuildTask.create "SetPrereleaseTag" [] {
printfn "Please enter pre-release package suffix"
let suffix = System.Console.ReadLine()
Expand Down
8 changes: 4 additions & 4 deletions build/Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ open ReleaseTasks
let _release =
BuildTask.createEmpty
"Release"
[clean; build; runTests; pack; buildDocs; createTag; publishNuget; releaseDocs]
[clean; build; runTests; pack; buildDocs; createTag; publishNuget; publishNPM; releaseDocs]

/// Full release of nuget package, git tag, and documentation for the prerelease version.
let _preRelease =
BuildTask.createEmpty
"PreRelease"
[setPrereleaseTag; clean; build; runTests; packPrerelease; buildDocsPrerelease; createPrereleaseTag; publishNugetPrerelease; prereleaseDocs]
[setPrereleaseTag; clean; build; runTests; packPrerelease; buildDocsPrerelease; createPrereleaseTag; publishNugetPrerelease; publishNPMPrerelease; prereleaseDocs]

/// Full release of nuget package for the prerelease version.
let _releaseNoDocs =
BuildTask.createEmpty
"ReleaseNoDocs"
[clean; build; runTests; pack; createTag; publishNuget;]
[clean; build; runTests; pack; createTag; publishNuget; publishNPM]

/// Full release of nuget package for the prerelease version.
let _preReleaseNoDocs =
BuildTask.createEmpty
"PreReleaseNoDocs"
[setPrereleaseTag; clean; build; runTests; packPrerelease; createPrereleaseTag; publishNugetPrerelease]
[setPrereleaseTag; clean; build; runTests; packPrerelease; createPrereleaseTag; publishNugetPrerelease; publishNPMPrerelease;]

ReleaseNotesTasks.updateReleaseNotes |> ignore

Expand Down
36 changes: 31 additions & 5 deletions build/PackageTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let private replaceCommitLink input =
let commitLinkPattern = @"\[\[#[a-z0-9]*\]\(.*\)\] "
Regex.Replace(input,commitLinkPattern,"")

let pack = BuildTask.create "Pack" [clean; build; runTests] {
if promptYesNo (sprintf "creating stable package with version %s OK?" stableVersionTag )
let packDotNet = BuildTask.create "PackDotNet" [clean; build; runTests] {
if promptYesNo (sprintf "[.NET] creating stable package with version %s OK?" stableVersionTag )
then
!! "src/**/*.*proj"
-- "src/bin/*"
Expand All @@ -39,8 +39,8 @@ let pack = BuildTask.create "Pack" [clean; build; runTests] {
else failwith "aborted"
}

let packPrerelease = BuildTask.create "PackPrerelease" [setPrereleaseTag; clean; build; runTests] {
if promptYesNo (sprintf "package tag will be %s OK?" prereleaseTag )
let packDotNetPrerelease = BuildTask.create "PackDotNetPrerelease" [setPrereleaseTag; clean; build; runTests] {
if promptYesNo (sprintf "[.NET] creating prerelease package with version %s OK?" prereleaseTag )
then
!! "src/**/*.*proj"
-- "src/bin/*"
Expand All @@ -61,4 +61,30 @@ let packPrerelease = BuildTask.create "PackPrerelease" [setPrereleaseTag; clean;
))
else
failwith "aborted"
}
}

module BundleJs =
let bundle (versionTag) =
run npm "run bundle" ""
Fake.IO.File.readAsString "package.json"
|> fun t ->
let t = t.Replace(ProjectInfo.stableVersionTag, versionTag)
Fake.IO.File.writeString false $"{ProjectInfo.npmPkgDir}/package.json" t
"" // "fable-library.**/**"
|> Fake.IO.File.writeString false $"{ProjectInfo.npmPkgDir}/fable_modules/.npmignore"

let packJS = BuildTask.create "PackJS" [clean; build; runTests] {
if promptYesNo (sprintf "[NPM] creating stable package with version %s OK?" ProjectInfo.stableVersionTag )
then
BundleJs.bundle ProjectInfo.stableVersionTag
else failwith "aborted"
}

let packJSPrerelease = BuildTask.create "PackJSPrerelease" [setPrereleaseTag; clean; build; runTests] {
if promptYesNo (sprintf "[NPM] creating prerelease package with version %s OK?" ProjectInfo.prereleaseTag ) then
BundleJs.bundle ProjectInfo.prereleaseTag
else failwith "aborted"
}
let pack = BuildTask.createEmpty "Pack" [packDotNet; packJS]

let packPrerelease = BuildTask.createEmpty "PackPrerelease" [packDotNetPrerelease;packJSPrerelease]
1 change: 1 addition & 0 deletions build/ProjectInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let gitHome = $"https://github.com/{gitOwner}"
let projectRepo = $"https://github.com/{gitOwner}/{project}"

let pkgDir = "pkg"
let npmPkgDir = "dist"

// Create RELEASE_NOTES.md if not existing. Or "release" would throw an error.
Fake.Extensions.Release.ReleaseNotes.ensure()
Expand Down
2 changes: 1 addition & 1 deletion build/ReleaseNotesTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let updateReleaseNotes = BuildTask.createFn "ReleaseNotes" [] (fun config ->

let semVer =
Fake.Core.ReleaseNotes.load "RELEASE_NOTES.md"
|> fun x -> x.SemVer.AsString
|> fun x -> sprintf "%i.%i.%i" x.SemVer.Major x.SemVer.Minor x.SemVer.Patch

// Update Version in src/Nfdi4Plants.Fornax.Template/package.json
let p = "package.json"
Expand Down
22 changes: 20 additions & 2 deletions build/ReleaseTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let createPrereleaseTag = BuildTask.create "CreatePrereleaseTag" [setPrereleaseT
let publishNuget = BuildTask.create "PublishNuget" [clean; build; runTests; pack] {
let targets = (!! (sprintf "%s/*.*pkg" pkgDir ))
for target in targets do printfn "%A" target
let msg = sprintf "release package with version %s?" stableVersionTag
let msg = sprintf "[.NET] release package with version %s?" stableVersionTag
if promptYesNo msg then
let source = "https://api.nuget.org/v3/index.json"
let apikey = Environment.environVar "NUGET_KEY"
Expand All @@ -48,7 +48,7 @@ let publishNuget = BuildTask.create "PublishNuget" [clean; build; runTests; pack
let publishNugetPrerelease = BuildTask.create "PublishNugetPrerelease" [clean; build; runTests; packPrerelease] {
let targets = (!! (sprintf "%s/*.*pkg" pkgDir ))
for target in targets do printfn "%A" target
let msg = sprintf "release package with version %s?" prereleaseTag
let msg = sprintf "[.NET] release package with version %s?" prereleaseTag
if promptYesNo msg then
let source = "https://api.nuget.org/v3/index.json"
let apikey = Environment.environVar "NUGET_KEY"
Expand All @@ -58,6 +58,24 @@ let publishNugetPrerelease = BuildTask.create "PublishNugetPrerelease" [clean; b
else failwith "aborted"
}

let publishNPM = BuildTask.create "PublishNPM" [clean; build; runTests; packJS] {
let msg = sprintf "[NPM] release package with version %s?" stableVersionTag
if promptYesNo msg then
let apikey = Environment.environVarOrNone "NPM_KEY"
let otp = if apikey.IsSome then $" --otp + {apikey.Value}" else ""
run npm $"publish --access public{otp}" ProjectInfo.npmPkgDir
else failwith "aborted"
}

let publishNPMPrerelease = BuildTask.create "PublishNPMPrerelease" [clean; build; runTests; packJSPrerelease] {
let msg = sprintf "[NPM] release package with version %s?" prereleaseTag
if promptYesNo msg then
let apikey = Environment.environVarOrNone "NPM_KEY"
let otp = if apikey.IsSome then $" --otp {apikey.Value}" else ""
run npm $"publish --access public --tag next{otp}" ProjectInfo.npmPkgDir
else failwith "aborted"
}

let releaseDocs = BuildTask.create "ReleaseDocs" [buildDocs] {
let msg = sprintf "release docs for version %s?" stableVersionTag
if promptYesNo msg then
Expand Down
98 changes: 0 additions & 98 deletions build/TestTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,104 +9,6 @@ open BasicTasks
[<Literal>]
let FableTestPath_input = "tests/FsSpreadsheet.Tests"

[<AutoOpen>]
module private Helper =

open Fake
open Fake.Core

let createProcess exe arg dir =
CreateProcess.fromRawCommandLine exe arg
|> CreateProcess.withWorkingDirectory dir
|> CreateProcess.ensureExitCode

module Proc =

module Parallel =

open System

let locker = obj()

let colors = [|
ConsoleColor.DarkYellow
ConsoleColor.DarkCyan
ConsoleColor.Magenta
ConsoleColor.Blue
ConsoleColor.Cyan
ConsoleColor.DarkMagenta
ConsoleColor.DarkBlue
ConsoleColor.Yellow
|]

let print color (colored: string) (line: string) =
lock locker
(fun () ->
let currentColor = Console.ForegroundColor
Console.ForegroundColor <- color
Console.Write colored
Console.ForegroundColor <- currentColor
Console.WriteLine line)

let onStdout index name (line: string) =
let color = colors.[index % colors.Length]
if isNull line then
print color $"{name}: --- END ---" ""
else if String.isNotNullOrEmpty line then
print color $"{name}: " line

let onStderr name (line: string) =
let color = ConsoleColor.Red
if isNull line |> not then
print color $"{name}: " line

let redirect (index, (name, createProcess)) =
createProcess
|> CreateProcess.redirectOutputIfNotRedirected
|> CreateProcess.withOutputEvents (onStdout index name) (onStderr name)

let printStarting indexed =
for (index, (name, c: CreateProcess<_>)) in indexed do
let color = colors.[index % colors.Length]
let wd =
c.WorkingDirectory
|> Option.defaultValue ""
let exe = c.Command.Executable
let args = c.Command.Arguments.ToStartInfo
print color $"{name}: {wd}> {exe} {args}" ""

let run cs =
cs
|> Seq.toArray
|> Array.indexed
|> fun x -> printStarting x; x
|> Array.map redirect
|> Array.Parallel.map Proc.run

let dotnet = createProcess "dotnet"

let npm =
let npmPath =
match ProcessUtils.tryFindFileOnPath "npm" with
| Some path -> path
| None ->
"npm was not found in path. Please install it and make sure it's available from your path. " +
"See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
|> failwith

createProcess npmPath

let run proc arg dir =
proc arg dir
|> Proc.run
|> ignore

let runParallel processes =
processes
|> Proc.Parallel.run
|> ignore


module RunTests =

/// runs `npm test` in root.
Expand Down

0 comments on commit a4cd326

Please sign in to comment.