Skip to content

Commit 31b3062

Browse files
committed
Refactor microbit example
1 parent 5a65b25 commit 31b3062

File tree

14 files changed

+806
-3
lines changed

14 files changed

+806
-3
lines changed

examples/microbit/.paket/Paket.Restore.targets

Lines changed: 557 additions & 0 deletions
Large diffs are not rendered by default.

examples/microbit/Build.fs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
open Fake.Core
2+
open Fake.IO
3+
4+
open Helpers
5+
6+
initializeContext()
7+
8+
let srcPath = Path.getFullName "src"
9+
10+
Target.create "Clean" (fun _ ->
11+
run dotnet "fable-py clean --yes" srcPath // Delete *.py files created by Fable
12+
)
13+
14+
Target.create "Build" (fun _ ->
15+
run dotnet $"fable-py -c Release" srcPath
16+
)
17+
18+
Target.create "Flash" (fun _ ->
19+
run flash "" $"{srcPath}/app.py"
20+
)
21+
22+
open Fake.Core.TargetOperators
23+
24+
let dependencies = [
25+
"Clean"
26+
==> "Build"
27+
28+
"Clean"
29+
==> "Flash"
30+
]
31+
32+
[<EntryPoint>]
33+
let main args = runOrDefault args

examples/microbit/Build.fsproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Helpers.fs" />
8+
<Compile Include="Build.fs" />
9+
</ItemGroup>
10+
<Import Project=".paket\Paket.Restore.targets" />
11+
</Project>

examples/microbit/Helpers.fs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
module Helpers
2+
3+
open Fake.Core
4+
5+
let initializeContext () =
6+
let execContext = Context.FakeExecutionContext.Create false "build.fsx" [ ]
7+
Context.setExecutionContext (Context.RuntimeContext.Fake execContext)
8+
9+
module Proc =
10+
module Parallel =
11+
open System
12+
13+
let locker = obj()
14+
15+
let colors =
16+
[| ConsoleColor.Blue
17+
ConsoleColor.Yellow
18+
ConsoleColor.Magenta
19+
ConsoleColor.Cyan
20+
ConsoleColor.DarkBlue
21+
ConsoleColor.DarkYellow
22+
ConsoleColor.DarkMagenta
23+
ConsoleColor.DarkCyan |]
24+
25+
let print color (colored: string) (line: string) =
26+
lock locker
27+
(fun () ->
28+
let currentColor = Console.ForegroundColor
29+
Console.ForegroundColor <- color
30+
Console.Write colored
31+
Console.ForegroundColor <- currentColor
32+
Console.WriteLine line)
33+
34+
let onStdout index name (line: string) =
35+
let color = colors.[index % colors.Length]
36+
if isNull line then
37+
print color $"{name}: --- END ---" ""
38+
else if String.isNotNullOrEmpty line then
39+
print color $"{name}: " line
40+
41+
let onStderr name (line: string) =
42+
let color = ConsoleColor.Red
43+
if isNull line |> not then
44+
print color $"{name}: " line
45+
46+
let redirect (index, (name, createProcess)) =
47+
createProcess
48+
|> CreateProcess.redirectOutputIfNotRedirected
49+
|> CreateProcess.withOutputEvents (onStdout index name) (onStderr name)
50+
51+
let printStarting indexed =
52+
for (index, (name, c: CreateProcess<_>)) in indexed do
53+
let color = colors.[index % colors.Length]
54+
let wd =
55+
c.WorkingDirectory
56+
|> Option.defaultValue ""
57+
let exe = c.Command.Executable
58+
let args = c.Command.Arguments.ToStartInfo
59+
print color $"{name}: {wd}> {exe} {args}" ""
60+
61+
let run cs =
62+
cs
63+
|> Seq.toArray
64+
|> Array.indexed
65+
|> fun x -> printStarting x; x
66+
|> Array.map redirect
67+
|> Array.Parallel.map Proc.run
68+
69+
let createProcess exe arg dir =
70+
CreateProcess.fromRawCommandLine exe arg
71+
|> CreateProcess.withWorkingDirectory dir
72+
|> CreateProcess.ensureExitCode
73+
74+
let dotnet = createProcess "dotnet"
75+
let flash = createProcess "uflash"
76+
77+
let npm =
78+
let npmPath =
79+
match ProcessUtils.tryFindFileOnPath "npm" with
80+
| Some path -> path
81+
| None ->
82+
"npm was not found in path. Please install it and make sure it's available from your path. " +
83+
"See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
84+
|> failwith
85+
86+
createProcess npmPath
87+
88+
let run proc arg dir =
89+
proc arg dir
90+
|> Proc.run
91+
|> ignore
92+
93+
let runParallel processes =
94+
processes
95+
|> Proc.Parallel.run
96+
|> ignore
97+
98+
let runOrDefault args =
99+
try
100+
match args with
101+
| [| target |] -> Target.runOrDefault target
102+
| _ -> Target.runOrDefault "Run"
103+
0
104+
with e ->
105+
printfn "%A" e
106+
1

examples/microbit/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Fable Python on BBC micro:bit
2+
3+
Write your F# program in `src/App.fs`.
4+
5+
## Install
6+
7+
```sh
8+
pip install uflash
9+
```
10+
11+
## Build
12+
13+
```sh
14+
dotnet run Build
15+
```
16+
17+
## Flash to MicroBit
18+
19+
```sh
20+
dotnet run Flash
21+
```
22+
23+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source https://api.nuget.org/v3/index.json
2+
framework: net5.0
3+
storage: none
4+
5+
nuget Fable.Core.Experimental >= 4.0.0-alpha-002
6+
nuget Fable.Python
7+
8+
nuget Fake.Core.Target
9+
nuget Fake.IO.FileSystem

examples/microbit/paket.lock

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
STORAGE: NONE
2+
RESTRICTION: == net5.0
3+
NUGET
4+
remote: https://api.nuget.org/v3/index.json
5+
Fable.Core.Experimental (4.0.0-alpha-002)
6+
FSharp.Core (>= 4.7.2)
7+
Fable.Python (0.9)
8+
Fable.Core.Experimental (>= 4.0.0-alpha-002)
9+
FSharp.Core (>= 6.0)
10+
Fake.Core.CommandLineParsing (5.20.4)
11+
FParsec (>= 1.1.1)
12+
FSharp.Core (>= 4.7.2)
13+
Fake.Core.Context (5.20.4)
14+
FSharp.Core (>= 4.7.2)
15+
Fake.Core.Environment (5.20.4)
16+
FSharp.Core (>= 4.7.2)
17+
Fake.Core.FakeVar (5.20.4)
18+
Fake.Core.Context (>= 5.20.4)
19+
FSharp.Core (>= 4.7.2)
20+
Fake.Core.Process (5.20.4)
21+
Fake.Core.Environment (>= 5.20.4)
22+
Fake.Core.FakeVar (>= 5.20.4)
23+
Fake.Core.String (>= 5.20.4)
24+
Fake.Core.Trace (>= 5.20.4)
25+
Fake.IO.FileSystem (>= 5.20.4)
26+
FSharp.Core (>= 4.7.2)
27+
System.Collections.Immutable (>= 1.7.1)
28+
Fake.Core.String (5.20.4)
29+
FSharp.Core (>= 4.7.2)
30+
Fake.Core.Target (5.20.4)
31+
Fake.Core.CommandLineParsing (>= 5.20.4)
32+
Fake.Core.Context (>= 5.20.4)
33+
Fake.Core.Environment (>= 5.20.4)
34+
Fake.Core.FakeVar (>= 5.20.4)
35+
Fake.Core.Process (>= 5.20.4)
36+
Fake.Core.String (>= 5.20.4)
37+
Fake.Core.Trace (>= 5.20.4)
38+
FSharp.Control.Reactive (>= 4.4.2)
39+
FSharp.Core (>= 4.7.2)
40+
Fake.Core.Trace (5.20.4)
41+
Fake.Core.Environment (>= 5.20.4)
42+
Fake.Core.FakeVar (>= 5.20.4)
43+
FSharp.Core (>= 4.7.2)
44+
Fake.IO.FileSystem (5.20.4)
45+
Fake.Core.String (>= 5.20.4)
46+
FSharp.Core (>= 4.7.2)
47+
FParsec (1.1.1)
48+
FSharp.Core (>= 4.3.4)
49+
FSharp.Control.Reactive (5.0.2)
50+
FSharp.Core (>= 4.7.2)
51+
System.Reactive (>= 5.0)
52+
FSharp.Core (6.0)
53+
System.Collections.Immutable (5.0)
54+
System.Reactive (5.0)

examples/microbit/paket.references

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fake.Core.Target
2+
Fake.IO.FileSystem
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
<Compile Include="App.fs" />
1212
</ItemGroup>
1313
<ItemGroup>
14-
<ProjectReference Include="../../src/Fable.Python.fsproj" />
14+
<ProjectReference Include="../../../src/Fable.Python.fsproj" />
1515
</ItemGroup>
1616
<ItemGroup>
1717
<PackageReference Include="Fable.Core.Experimental" Version="4.0.0-alpha-002" />
1818
</ItemGroup>
19+
<Import Project="..\.paket\Paket.Restore.targets" />
1920
</Project>

0 commit comments

Comments
 (0)