Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit 26eaf5a

Browse files
committed
Dirty Detect
1 parent 8cd64d8 commit 26eaf5a

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

Bake.Actions/Download.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
open Bake
44
open System.Net
55

6+
let downloadFile (url: string) targetFile =
7+
use webClient = new WebClient ()
8+
webClient.DownloadFile (url, targetFile)
9+
610
let downloadTask echo (targetDir: string) script (url: string) =
711
let fileName = url.[1 + url.LastIndexOf '/'..]
812
let targetDir = targetDir.Trim().Trim('\\', '/') + "/"
913
let targetFile = targetDir + fileName
1014
{
1115
run = fun () ->
12-
1316
if echo then lock stdout (fun () -> printfn "Downloading %s..." fileName)
14-
use webClient = new WebClient ()
15-
webClient.DownloadFile (url, targetFile)
17+
downloadFile url targetFile
1618
inputFiles = Seq.empty
1719
outputFiles = seq { targetFile }
1820
dirty = true

Bake.Actions/Run.fs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ open Bake
44

55
exception ExitCodeIsNotZero of int
66

7+
let run waitForExit workingDir (cmd: string) =
8+
let cmd, args = let cells = cmd.Split ' ' in cells.[0], cells.[1..]
9+
let args = Array.reduce (fun a b -> a + " " + b) args
10+
11+
let startInfo = new System.Diagnostics.ProcessStartInfo ()
12+
startInfo.FileName <- cmd
13+
startInfo.Arguments <- args
14+
startInfo.WorkingDirectory <- workingDir
15+
16+
let prc = System.Diagnostics.Process.Start startInfo
17+
if waitForExit then prc.WaitForExit ()
18+
if prc.ExitCode <> 0 then raise <| ExitCodeIsNotZero prc.ExitCode
19+
720
let runTask echo waitForExit script (command: string seq) = {
821
inputFiles = Seq.empty
922
outputFiles = Seq.empty
@@ -13,17 +26,7 @@ let runTask echo waitForExit script (command: string seq) = {
1326
command
1427
|> Seq.iter (fun cmd ->
1528
if echo then lock stdout (fun () -> printfn "%s" cmd)
16-
let cmd, args = let cells = cmd.Split ' ' in cells.[0], cells.[1..]
17-
let args = Array.reduce (fun a b -> a + " " + b) args
18-
19-
let startInfo = new System.Diagnostics.ProcessStartInfo ()
20-
startInfo.FileName <- cmd
21-
startInfo.Arguments <- args
22-
startInfo.WorkingDirectory <- script.scriptFile.Directory.FullName
23-
24-
let prc = System.Diagnostics.Process.Start startInfo
25-
if waitForExit then prc.WaitForExit ()
26-
if prc.ExitCode <> 0 then raise <| ExitCodeIsNotZero prc.ExitCode)
29+
run waitForExit cmd script.scriptFile.Directory.FullName)
2730
}
2831

2932
let runAction echo waitForExit = fun ctx script ->

Bake.Actions/Unzip.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ let Unzip = {
3333
action = fun ctx script ->
3434
if script.arguments.Length <> 2 then raise <| Action.ActionUsageError "Unzip必须有两个参数。"
3535

36+
let srcDir = script.scriptFile.Directory.FullName.Trim().Trim('\\', '/') + "/"
3637
let targetDir = script.arguments.[0].Trim() |> Action.applyContextToArgument ctx
3738
let targetDir = targetDir.TrimEnd('\\', '/') + "/"
3839

3940
Action.blockArgumentTaskPerLine (fun _ script zip ->
40-
seq { unzipTask true script targetDir zip }) ctx script script.arguments.[1],
41+
seq { unzipTask true script targetDir <| srcDir + zip }) ctx script script.arguments.[1],
4142
ctx
4243
}
4344

Bake/Program.fs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open Bake
44
[<EntryPoint>]
55
let main args =
66
try
7-
let buildScript =
7+
let buildScriptFile =
88
match args with
99
| [||] ->
1010
[
@@ -17,6 +17,9 @@ let main args =
1717
| [|a|] -> [ a; a + ".bake" ] |> List.find System.IO.File.Exists
1818
| _ -> failwithf "Not support"
1919
|> System.IO.FileInfo
20+
21+
let buildScript =
22+
buildScriptFile
2023
|> Parser.parseFromFile
2124
|> function
2225
| Ok x -> x
@@ -32,7 +35,10 @@ let main args =
3235
//buildScript |> Seq.iter (printfn "%A")
3336

3437
let context = {
35-
variables = Map.empty
38+
variables =
39+
Map.empty
40+
|> Map.add "StartDir" buildScriptFile.Directory.FullName
41+
|> Map.add "CurrentDir" System.Environment.CurrentDirectory
3642
actions = defaultActions
3743

3844
runChildBlock = Bake.Actions.Sync.syncBlockRunner

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
* Copy - 复制
3535
* Zip - 打包
3636

37+
## 规则
38+
1. 输入文件全部使用相对路径(相对于脚本文件)
39+
2. 输出文件全部使用绝对路径(可使用$StartDir获取启动脚本的路径以设置$Output变量)
40+
3741
## 脚本语言示例
3842

3943
##### Build.bake

0 commit comments

Comments
 (0)