Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6049d5

Browse files
nosamiKevinRansom
authored andcommitted
Make F# resources work more like C# (#3352)
* Make F# resources work more like C#Given a resource in a folder named `AFolder/AResourceFile.txt` in a project witha root namespace of `fsharpresources`, then the following command line argshould be passed to the compiler```--resource:AFolder/AResourceFile.txt,fsharpresources.AFolder.AResourceFile.txt```whereas currently it just passes the name of the file. This also fixes passinga `LogicalName` to the resource.Fixesdotnet/fsharp#1050 &&dotnet/fsharp#922I looked at how Roslyn was doing this [here](https://github.com/dotnet/roslyn/blob/6847f1e5a909395aae9456e8f366cbf4deb86b69/src/Compilers/Core/MSBuildTask/ManagedCompiler.cs#L739)es. Lines starting* Add a UseStandardResourceNames flag (disabled by default)With``` <UseStandardResourceNames>true</UseStandardResourceNames>```we get the following output```--resource:AFolder/AResourceFile.txt,fsharpresource.AFolder.AResourceFile.txt```With the value omitted or set to false, the behaviour is as it was previously.```--resource:AFolder/AResourceFile.txt```
1 parent7af8b4a commitf6049d5

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

‎src/fsharp/FSharp.Build/CreateFSharpManifestResourceName.fs‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ open Microsoft.Build.Utilities
99

1010
typeCreateFSharpManifestResourceNamepublic()=
1111
inherit CreateCSharpManifestResourceName()
12-
12+
13+
// When set to true, generate resource names in the same way as C# with root namespace and folder names
14+
member valUseStandardResourceNames=falsewith get, set
15+
1316
overridethis.CreateManifestName
1417
((fileName:string),
1518
(linkFileName:string),
@@ -23,34 +26,29 @@ type CreateFSharpManifestResourceName public () =
2326
//
2427
// For resx resources, both the Visual FSharp and XBuild FSHarp toolchains do the right thing, i.e.
2528
// SubDir\abc.resx --> SubDir.abc.resources
26-
//
27-
// However for non-resx resources, for some reason Visual FSharp does _not_ add the directory name to the resource name.
28-
// It is very unclear where the directory name gets dropped in the Visual FSharp implementation
29-
// - is it in Microsoft.Common.targets, Microsoft.FSharp.Targets or how the base type CreateCSharpManifestResourceName
30-
// is created and used - who knows, the code is not easy to understand despite it doing something very simple. That's
31-
// the nature of MSBuild/XBuild....
32-
//
33-
// Anyway, dropping the directory name seems like a mistake. But we attempt to replicate the behaviour here
34-
// for consistency with Visual FSharp. This may not be the right place to do this and this many not be consistent
35-
// when cultures are used - that case has not been tested.
3629

37-
letrunningOnMono=
38-
try
39-
System.Type.GetType("Mono.Runtime")<>null
40-
with e->
41-
false
42-
letfileName=ifnot runningOnMono|| fileName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)then fileNameelse Path.GetFileName(fileName)
43-
letlinkFileName=ifnot runningOnMono|| linkFileName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)then linkFileNameelse Path.GetFileName(linkFileName)
30+
letfileName,linkFileName,rootNamespace=
31+
match this.UseStandardResourceNameswith
32+
|true->
33+
fileName, linkFileName, rootNamespace
34+
|false->
35+
letrunningOnMono=
36+
try
37+
System.Type.GetType("Mono.Runtime")<>null
38+
with e->
39+
false
40+
letfileName=ifnot runningOnMono|| fileName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)then fileNameelse Path.GetFileName(fileName)
41+
letlinkFileName=ifnot runningOnMono|| linkFileName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)then linkFileNameelse Path.GetFileName(linkFileName)
42+
fileName, linkFileName,""
4443

4544
letembeddedFileName=
4645
match linkFileNamewith
4746
|null-> fileName
4847
|_-> linkFileName
4948

5049
// since we do not support resources dependent on a form, we always pass null for a binary stream
51-
// rootNamespace is always empty - we do not support it
5250
letcSharpResult=
53-
base.CreateManifestName(fileName, linkFileName,"", dependentUponFileName,null)
51+
base.CreateManifestName(fileName, linkFileName,rootNamespace, dependentUponFileName,null)
5452
// Workaround that makes us keep .resources extension on both 3.5 and 3.5SP1
5553
// 3.5 stripped ".resources", 3.5 SP1 does not. We should do 3.5SP1 thing
5654
letextensionToWorkaround=".resources"

‎src/fsharp/FSharp.Build/CreateFSharpManifestResourceName.fsi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ namespace Microsoft.FSharp.Build
66
typeCreateFSharpManifestResourceName=
77
inherit Microsoft.Build.Tasks.CreateCSharpManifestResourceName
88
publicnew: unit-> CreateFSharpManifestResourceName
9+
memberUseStandardResourceNames:bool with get,set

‎src/fsharp/FSharp.Build/Fsc.fs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ type FscCommandLineBuilder () =
6060
if s<> String.Emptythen
6161
args<- s:: args
6262

63-
memberx.AppendSwitchIfNotNull(switch:string,value:string)=
63+
memberx.AppendSwitchIfNotNull(switch:string,value:string,?metadataNames:string array)=
64+
letmetadataNames= defaultArg metadataNames[||]
6465
builder.AppendSwitchIfNotNull(switch, value)
6566
lettmp=new CommandLineBuilder()
6667
tmp.AppendSwitchUnquotedIfNotNull(switch, value)
68+
letprovidedMetaData=
69+
metadataNames
70+
|> Array.filter(String.IsNullOrWhiteSpace>>not)
71+
if providedMetaData.Length>0then
72+
tmp.AppendTextUnquoted","
73+
tmp.AppendTextUnquoted(providedMetaData|> String.concat",")
6774
lets= tmp.ToString()
6875
if s<> String.Emptythen
6976
args<- s:: args
@@ -161,6 +168,7 @@ type [<Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:Iden
161168
| Some s-> s
162169
| None->""
163170
let mutabletreatWarningsAsErrors:bool=false
171+
let mutableuseStandardResourceNames:bool=false
164172
let mutablewarningsAsErrors:string=null
165173
let mutableversionFile:string=null
166174
let mutablewarningLevel:string=null
@@ -243,7 +251,10 @@ type [<Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:Iden
243251
// Resources
244252
if resources<>nullthen
245253
for itemin resourcesdo
246-
builder.AppendSwitchIfNotNull("--resource:", item.ItemSpec)
254+
match useStandardResourceNameswith
255+
|true-> builder.AppendSwitchIfNotNull("--resource:", item.ItemSpec,[|item.GetMetadata("LogicalName"); item.GetMetadata("Access")|])
256+
|false-> builder.AppendSwitchIfNotNull("--resource:", item.ItemSpec)
257+
247258
// VersionFile
248259
builder.AppendSwitchIfNotNull("--versionfile:", versionFile)
249260
// References
@@ -514,6 +525,10 @@ type [<Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:Iden
514525
with get()= toolPath
515526
andset(s)= toolPath<- s
516527

528+
// When set to true, generate resource names in the same way as C# with root namespace and folder names
529+
memberfsc.UseStandardResourceNames
530+
with get()= useStandardResourceNames
531+
andset(s)= useStandardResourceNames<- s
517532
// --version-file <string>:
518533
memberfsc.VersionFile
519534
with get()= versionFile

‎src/fsharp/FSharp.Build/Fsc.fsi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Fsc = class
5555
member ToolPath: string with get,set
5656
member TargetProfile: string with get,set
5757
member TreatWarningsAsErrors: bool with get,set
58+
member UseStandardResourceNames: bool with get,set
5859
member Utf8Output: bool with get,set
5960
member VisualStudioStyleErrors: bool with get,set
6061
member WarningLevel: string with get,set

‎src/fsharp/FSharp.Build/Microsoft.FSharp.Targets‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,28 @@ this file.
8989

9090
<CreateFSharpManifestResourceName
9191
Condition="'@(ResxWithNoCulture)' != '' AND '$(UsingXBuild)' == 'true'"
92+
UseStandardResourceNames="$(UseStandardResourceNames)"
9293
ResourceFiles="@(ResxWithNoCulture)"RootNamespace="$(RootNamespace)">
9394
<Output TaskParameter ="ManifestResourceNames" ItemName ="ManifestResourceWithNoCultureName" />
9495
</CreateFSharpManifestResourceName>
9596

9697
<CreateFSharpManifestResourceName
9798
Condition="'@(NonResxWithNoCulture)' != '' AND '$(UsingXBuild)' == 'true'"
99+
UseStandardResourceNames="$(UseStandardResourceNames)"
98100
ResourceFiles="@(NonResxWithNoCulture)"RootNamespace="$(RootNamespace)">
99101
<Output TaskParameter ="ManifestResourceNames" ItemName ="ManifestNonResxWithNoCulture" />
100102
</CreateFSharpManifestResourceName>
101103

102104
<CreateFSharpManifestResourceName
103105
Condition="'@(ResxWithCulture)' != '' AND '$(UsingXBuild)' == 'true'"
106+
UseStandardResourceNames="$(UseStandardResourceNames)"
104107
ResourceFiles="@(ResxWithCulture)"RootNamespace="$(RootNamespace)">
105108
<Output TaskParameter ="ManifestResourceNames" ItemName ="ManifestResourceWithCultureName" />
106109
</CreateFSharpManifestResourceName>
107110

108111
<CreateFSharpManifestResourceName
109112
Condition="'@(NonResxWithCulture)' != '' AND '$(UsingXBuild)' == 'true'"
113+
UseStandardResourceNames="$(UseStandardResourceNames)"
110114
ResourceFiles="@(NonResxWithCulture)"RootNamespace="$(RootNamespace)">
111115
<Output TaskParameter ="ManifestResourceNames" ItemName ="ManifestNonResxWithCulture" />
112116
</CreateFSharpManifestResourceName>
@@ -123,6 +127,7 @@ this file.
123127
<CreateFSharpManifestResourceName
124128
ResourceFiles="@(EmbeddedResource)"
125129
RootNamespace="$(RootNamespace)"
130+
UseStandardResourceNames="$(UseStandardResourceNames)"
126131
Condition="'%(EmbeddedResource.ManifestResourceName)' == '' and ('%(EmbeddedResource.WithCulture)' == 'false' or '%(EmbeddedResource.Type)' == 'Resx') AND '$(UsingXBuild)' == 'false'">
127132

128133
<OutputTaskParameter="ResourceFilesWithManifestResourceNames"ItemName="_Temporary" />
@@ -133,6 +138,7 @@ this file.
133138
<CreateFSharpManifestResourceName
134139
ResourceFiles="@(EmbeddedResource)"
135140
RootNamespace="$(RootNamespace)"
141+
UseStandardResourceNames="$(UseStandardResourceNames)"
136142
PrependCultureAsDirectory="false"
137143
Condition="'%(EmbeddedResource.ManifestResourceName)' == '' and '%(EmbeddedResource.WithCulture)' == 'true' and '%(EmbeddedResource.Type)' == 'Non-Resx' AND '$(UsingXBuild)' == 'false'">
138144

@@ -259,6 +265,7 @@ this file.
259265
ToolExe="$(FscToolExe)"
260266
ToolPath="$(FscToolPath)"
261267
TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
268+
UseStandardResourceNames="$(UseStandardResourceNames)"
262269
Utf8Output="$(Utf8Output)"
263270
VersionFile="$(VersionFile)"
264271
VisualStudioStyleErrors="$(VisualStudioStyleErrors)"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp