Skip to content

Commit 1d819b9

Browse files
authored
Generate signature files for resource files (#15176)
* First attempt to generate signature file * Verify there are holes for return type arrow. * Correct signature member. * Include signature member name when there are no holes. * Add static to SwallowResourceText signature member. * Correct return type of members. * Format code
1 parent 1727f90 commit 1d819b9

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/FSharp.Build/FSharpEmbedResourceText.fs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ open Printf
367367
// END BOILERPLATE
368368
"
369369

370+
let StringBoilerPlateSignature =
371+
" // BEGIN BOILERPLATE
372+
/// If set to true, then all error messages will just return the filled 'holes' delimited by ',,,'s - this is for language-neutral testing (e.g. localization-invariant baselines).
373+
static member SwallowResourceText: bool with get, set
374+
// END BOILERPLATE"
375+
370376
let generateResxAndSource (fileName: string) =
371377
try
372378
let printMessage fmt = Printf.ksprintf this.Log.LogMessage fmt
@@ -383,6 +389,7 @@ open Printf
383389
)
384390

385391
let outFileName = Path.Combine(_outputPath, justFileName + ".fs")
392+
let outFileSignatureName = Path.Combine(_outputPath, justFileName + ".fsi")
386393
let outXmlFileName = Path.Combine(_outputPath, justFileName + ".resx")
387394

388395
let condition1 = File.Exists(outFileName)
@@ -400,7 +407,7 @@ open Printf
400407
if condition5 then
401408
printMessage "Skipping generation of %s and %s from %s since up-to-date" outFileName outXmlFileName fileName
402409

403-
Some(fileName, outFileName, outXmlFileName)
410+
Some(fileName, outFileSignatureName, outFileName, outXmlFileName)
404411
else
405412
printMessage
406413
"Generating %s and %s from %s, because condition %d is false, see FSharpEmbedResourceText.fs in the F# source"
@@ -464,12 +471,20 @@ open Printf
464471
printMessage "Generating %s" outFileName
465472
use outStream = File.Create outFileName
466473
use out = new StreamWriter(outStream)
474+
use outSignatureStream = File.Create outFileSignatureName
475+
use outSignature = new StreamWriter(outSignatureStream)
467476
fprintfn out "// This is a generated file; the original input is '%s'" fileName
477+
fprintfn outSignature "// This is a generated file; the original input is '%s'" fileName
468478
fprintfn out "namespace %s" justFileName
479+
fprintfn outSignature "namespace %s" justFileName
469480
fprintfn out "%s" stringBoilerPlatePrefix
481+
fprintfn outSignature "%s" stringBoilerPlatePrefix
470482
fprintfn out "type internal SR private() ="
483+
fprintfn outSignature "type internal SR ="
484+
fprintfn outSignature " private new: unit -> SR"
471485
let theResourceName = justFileName
472486
fprintfn out "%s" (StringBoilerPlate theResourceName)
487+
fprintfn outSignature "%s" StringBoilerPlateSignature
473488

474489
printMessage "Generating resource methods for %s" outFileName
475490
// gen each resource method
@@ -494,7 +509,9 @@ open Printf
494509

495510
formalArgs.Append ")" |> ignore
496511
fprintfn out " /// %s" str
512+
fprintfn outSignature " /// %s" str
497513
fprintfn out " /// (Originally from %s:%d)" fileName (lineNum + 1)
514+
fprintfn outSignature " /// (Originally from %s:%d)" fileName (lineNum + 1)
498515

499516
let justPercentsFromFormatString =
500517
(holes
@@ -523,14 +540,31 @@ open Printf
523540
errPrefix
524541
ident
525542
justPercentsFromFormatString
526-
(actualArgs.ToString()))
543+
(actualArgs.ToString())
544+
545+
let signatureMember =
546+
let returnType =
547+
match optErrNum with
548+
| None -> "string"
549+
| Some _ -> "int * string"
550+
551+
if Array.isEmpty holes then
552+
sprintf " static member %s: unit -> %s" ident returnType
553+
else
554+
holes
555+
|> Array.mapi (fun idx holeType -> sprintf "a%i: %s" idx holeType)
556+
|> String.concat " * "
557+
|> fun parameters -> sprintf " static member %s: %s -> %s" ident parameters returnType
558+
559+
fprintfn outSignature "%s" signatureMember)
527560

528561
printMessage "Generating .resx for %s" outFileName
529562
fprintfn out ""
530563
// gen validation method
531564
fprintfn out " /// Call this method once to validate that all known resources are valid; throws if not"
532565

533566
fprintfn out " static member RunStartupValidation() ="
567+
fprintfn outSignature " static member RunStartupValidation: unit -> unit"
534568

535569
stringInfos
536570
|> Seq.iter (fun (lineNum, (optErrNum, ident), str, holes, netFormatString) ->
@@ -554,7 +588,7 @@ open Printf
554588
use outXmlStream = File.Create outXmlFileName
555589
xd.Save outXmlStream
556590
printMessage "Done %s" outFileName
557-
Some(fileName, outFileName, outXmlFileName)
591+
Some(fileName, outFileSignatureName, outFileName, outXmlFileName)
558592
with e ->
559593
PrintErr(fileName, 0, sprintf "An exception occurred when processing '%s'\n%s" fileName (e.ToString()))
560594
None
@@ -584,7 +618,14 @@ open Printf
584618

585619
let generatedSource, generatedResx =
586620
[|
587-
for (textFile, source, resx) in generatedFiles do
621+
for (textFile, signature, source, resx) in generatedFiles do
622+
let signatureItem =
623+
let item = TaskItem(signature)
624+
item.SetMetadata("AutoGen", "true")
625+
item.SetMetadata("DesignTime", "true")
626+
item.SetMetadata("DependentUpon", resx)
627+
item :> ITaskItem
628+
588629
let sourceItem =
589630
let item = TaskItem(source)
590631
item.SetMetadata("AutoGen", "true")
@@ -598,13 +639,13 @@ open Printf
598639
item.SetMetadata("SourceDocumentPath", textFile)
599640
item :> ITaskItem
600641

601-
yield (sourceItem, resxItem)
642+
yield ([| signatureItem; sourceItem |], resxItem)
602643
|]
603644
|> Array.unzip
604645

605646
let generatedResult = (generatedFiles.Length = this.EmbeddedText.Length)
606647

607-
_generatedSource <- generatedSource
648+
_generatedSource <- Array.collect id generatedSource
608649
_generatedResx <- generatedResx
609650
generatedResult && not this.Log.HasLoggedErrors
610651
with TaskFailed ->

0 commit comments

Comments
 (0)