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

resolve watch issues with empty siteContents; remove unneeded Option #103

Merged
merged 1 commit into from
May 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
8 changes: 6 additions & 2 deletions src/Fornax/Fornax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ let main argv =
0
| Some Build ->
try
do generateFolder cwd false
let sc = SiteContents ()
do generateFolder sc cwd false
0
with
| FornaxGeneratorException message ->
Expand All @@ -223,9 +224,12 @@ let main argv =
let mutable lastAccessed = Map.empty<string, DateTime>
let waitingForChangesMessage = "Generated site with errors. Waiting for changes..."

let sc = SiteContents ()


let guardedGenerate () =
try
do generateFolder cwd true
do generateFolder sc cwd true
with
| FornaxGeneratorException message ->
printfn "%s%s%s" message Environment.NewLine waitingForChangesMessage
Expand Down
86 changes: 40 additions & 46 deletions src/Fornax/Generator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ module Logger =
let errorfn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.Red in printfn "%s" s) str

///`projectRoot` - path to the root of website
let generateFolder (projectRoot : string) (isWatch: bool) =
let generateFolder (sc : SiteContents) (projectRoot : string) (isWatch: bool) =
let sw = Stopwatch.StartNew()

let relative toPath fromPath =
Expand All @@ -452,50 +452,44 @@ let generateFolder (projectRoot : string) (isWatch: bool) =
else projectRoot + (string Path.DirectorySeparatorChar)

use fsi = EvaluatorHelpers.fsi (isWatch)
let sc = SiteContents ()
let config =
let configPath = Path.Combine(projectRoot, "config.fsx")
if File.Exists configPath then
match ConfigEvaluator.evaluate fsi sc configPath with
| Ok cfg -> Some cfg
| Error error -> raise (FornaxGeneratorException error)
else
None

match config with
| None ->
raise (FornaxGeneratorException "Couldn't find config.fsx")
| Some config ->
let loaders = Directory.GetFiles(Path.Combine(projectRoot, "loaders"), "*.fsx")
let sc =
(sc, loaders)
||> Array.fold (fun state e ->
match LoaderEvaluator.evaluate fsi state e projectRoot with
| Ok sc ->
sc
| Error er ->
printfn "LOADER ERROR: %s" er
state)
sc.Errors() |> List.iter (fun er -> printfn "BAD FILE: %s" er.Path)

let logResult (result : GeneratorResult) =
match result with
| GeneratorIgnored -> ()
| GeneratorSuccess None -> ()
| GeneratorSuccess (Some message) ->
Logger.informationfn "%s" message
| GeneratorFailure message ->
// if one generator fails we want to exit early and report the problem to the operator
raise (FornaxGeneratorException message)

runOnceGenerators fsi config sc projectRoot
|> List.iter logResult

Directory.GetFiles(projectRoot, "*", SearchOption.AllDirectories)
|> Array.iter (fun filePath ->
filePath
|> relative projectRoot
|> generate fsi config sc projectRoot
|> logResult)

Logger.informationfn "Generation time: %A" sw.Elapsed
if not (File.Exists configPath) then
raise (FornaxGeneratorException "Couldn't find config.fsx")
match ConfigEvaluator.evaluate fsi sc configPath with
| Ok cfg -> cfg
| Error error -> raise (FornaxGeneratorException error)

let loaders = Directory.GetFiles(Path.Combine(projectRoot, "loaders"), "*.fsx")
let sc =
(sc, loaders)
||> Array.fold (fun state e ->
match LoaderEvaluator.evaluate fsi state e projectRoot with
| Ok sc ->
sc
| Error er ->
printfn "LOADER ERROR: %s" er
state)
sc.Errors() |> List.iter (fun er -> printfn "BAD FILE: %s" er.Path)

let logResult (result : GeneratorResult) =
match result with
| GeneratorIgnored -> ()
| GeneratorSuccess None -> ()
| GeneratorSuccess (Some message) ->
Logger.informationfn "%s" message
| GeneratorFailure message ->
// if one generator fails we want to exit early and report the problem to the operator
raise (FornaxGeneratorException message)

runOnceGenerators fsi config sc projectRoot
|> List.iter logResult

Directory.GetFiles(projectRoot, "*", SearchOption.AllDirectories)
|> Array.iter (fun filePath ->
filePath
|> relative projectRoot
|> generate fsi config sc projectRoot
|> logResult)

Logger.informationfn "Generation time: %A" sw.Elapsed