Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Giraffe tests (wip) #68

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"fable": {
"version": "4.0.0-snake-island-alpha-024",
"version": "4.0.0-snake-island-alpha-025",
"commands": [
"fable"
]
Expand Down
2 changes: 0 additions & 2 deletions Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ let cliPath = Path.getFullName "../Fable/src/Fable.Cli"

Target.create "Clean" (fun _ ->
Shell.cleanDir buildPath
// run dotnet "fable clean --yes" buildPath // Delete *.py files created by Fable
// run dotnet $"run -c Release -p {cliPath} -- clean --yes --lang Python " buildPath
)

Target.create "Build" (fun _ ->
Expand Down
64 changes: 64 additions & 0 deletions examples/giraffe/Build.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
open Fake.Core
open Fake.IO

open Helpers

initializeContext()

let buildPath = Path.getFullName "build"
let srcPath = Path.getFullName "src"
let deployPath = Path.getFullName "deploy"
let testsPath = Path.getFullName "test"

// Until Fable (beyond) is released, we need to compile with locally installed Fable branch.
let cliPath = Path.getFullName "../Fable/src/Fable.Cli"

Target.create "Clean" (fun _ ->
Shell.cleanDir buildPath
)

Target.create "Build" (fun _ ->
Shell.mkdir buildPath
run dotnet $"fable --exclude Fable.Core --lang Python --outDir {buildPath}" srcPath
)

Target.create "Run" (fun _ ->
run dotnet "build" srcPath
)

Target.create "Test" (fun _ ->
run dotnet "build" testsPath
[ "native", dotnet "run" testsPath
"python", dotnet $"fable --lang Python --outDir {buildPath}/tests" testsPath
]
|> runParallel
run poetry $"run python -m pytest {buildPath}/tests" ""
)

Target.create "Pack" (fun _ ->
run dotnet "pack -c Release" srcPath
)

Target.create "Format" (fun _ ->
run dotnet "fantomas . -r" srcPath
run dotnet "fantomas . -r" testsPath
)

open Fake.Core.TargetOperators

let dependencies = [
"Clean"
==> "Build"

"Clean"
==> "Run"

"Build"
==> "Test"

"Build"
==> "Pack"
]

[<EntryPoint>]
let main args = runOrDefault args
15 changes: 15 additions & 0 deletions examples/giraffe/Build.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Helpers.fs" />
<Compile Include="Build.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fake.Core.Target" Version="5.23.0" />
<PackageReference Include="Fake.IO.Filesystem" Version="5.23.0" />
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
</ItemGroup>
</Project>
141 changes: 109 additions & 32 deletions examples/giraffe/Helpers.fs
Original file line number Diff line number Diff line change
@@ -1,32 +1,109 @@
namespace Giraffe

[<AutoOpen>]
module Helpers =
open System
open System.IO

/// <summary>
/// Checks if an object is not null.
/// </summary>
/// <param name="x">The object to validate against `null`.</param>
/// <returns>Returns true if the object is not null otherwise false.</returns>
let inline isNotNull x = not (isNull x)

/// <summary>
/// Converts a string into a string option where null or an empty string will be converted to None and everything else to Some string.
/// </summary>
/// <param name="str">The string value to be converted into an option of string.</param>
/// <returns>Returns None if the string was null or empty otherwise Some string.</returns>
let inline strOption (str : string) =
if String.IsNullOrEmpty str then None else Some str

/// <summary>
/// Reads a file asynchronously from the file system.
/// </summary>
/// <param name="filePath">The absolute path of the file.</param>
/// <returns>Returns the string contents of the file wrapped in a Task.</returns>
// let readFileAsStringAsync (filePath : string) =
// task {
// use reader = new StreamReader(filePath)
// return! reader.ReadToEndAsync()
// }
module Helpers

open Fake.Core

let initializeContext () =
let execContext = Context.FakeExecutionContext.Create false "build.fsx" [ ]
Context.setExecutionContext (Context.RuntimeContext.Fake execContext)

module Proc =
module Parallel =
open System

let locker = obj()

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

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 createProcess exe arg dir =
CreateProcess.fromRawCommandLine exe arg
|> CreateProcess.withWorkingDirectory dir
|> CreateProcess.ensureExitCode

let dotnet = createProcess "dotnet"


let pytest = createProcess "pytest"
let poetry = createProcess "poetry"

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 runOrDefault args =
try
match args with
| [| target |] -> Target.runOrDefault target
| _ -> Target.runOrDefault "Run"
0
with e ->
printfn "%A" e
1
8 changes: 0 additions & 8 deletions examples/giraffe/Program.fs

This file was deleted.

28 changes: 28 additions & 0 deletions examples/giraffe/giraffe.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Python", "src\Giraffe.Python.fsproj", "{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Tests", "test\Giraffe.Tests.fsproj", "{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Release|Any CPU.Build.0 = Release|Any CPU
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading