Skip to content

Commit 3ce59b4

Browse files
committed
Add GetProperties method to provide all properties as seq
1 parent f26aa43 commit 3ce59b4

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/CssClassesTypeProvider.fs

+30-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module TypeProvider =
2020
type CssClassesTypeProvider ( config : TypeProviderConfig ) as this =
2121
inherit DisposableTypeProviderForNamespaces( config )
2222

23+
#if LOGGING_ENABLED
2324
do
2425
IO.log ( sprintf "TypeProviderConfig.IsHostedExecution = %b" config.IsHostedExecution )
2526
IO.log ( sprintf "TypeProviderConfig.IsInvalidationSupported = %b" config.IsInvalidationSupported )
@@ -40,6 +41,7 @@ module TypeProvider =
4041

4142
IO.log ( sprintf "Environment.CommandLine = %s" Environment.CommandLine )
4243
IO.log ( sprintf "Environment.CurrentDirectory = %s" Environment.CurrentDirectory )
44+
#endif
4345

4446
let ns = "Zanaptak.TypedCssClasses"
4547
let asm = Assembly.GetExecutingAssembly()
@@ -51,6 +53,7 @@ module TypeProvider =
5153
let source = args.[ 0 ] :?> string
5254
let naming = args.[ 1 ] :?> Naming
5355
let resolutionFolder = args.[ 2 ] :?> string
56+
let getProperties = args.[ 3 ] :?> bool
5457

5558
let getSpec _ value =
5659

@@ -75,6 +78,28 @@ module TypeProvider =
7578
cssType.AddMember prop
7679
)
7780

81+
if getProperties then
82+
let rowType = ProvidedTypeDefinition("Property", Some(typeof<string[]>), hideObjectMethods = true)
83+
let rowNameProp = ProvidedProperty("Name", typeof<string>, getterCode = fun (Singleton row) -> <@@ (%%row:string[]).[0] @@>)
84+
rowNameProp.AddXmlDoc "Generated property name using specified naming strategy."
85+
let rowValueProp = ProvidedProperty("Value", typeof<string>, getterCode = fun (Singleton row) -> <@@ (%%row:string[]).[1] @@>)
86+
rowValueProp.AddXmlDoc "The underlying CSS class value."
87+
88+
rowType.AddMember rowNameProp
89+
rowType.AddMember rowValueProp
90+
cssType.AddMember rowType
91+
92+
let propsArray = cssClasses |> Seq.map ( fun p -> [| p.Name ; p.Value |] ) |> Seq.toArray
93+
let usedNames = cssClasses |> Seq.map ( fun p -> p.Name ) |> Set.ofSeq
94+
let methodName =
95+
Seq.init 99 ( fun i -> "GetProperties" + if i = 0 then "" else "_" + string ( i + 1 ) )
96+
|> Seq.find ( fun s -> usedNames |> Set.contains s |> not )
97+
let staticMethod =
98+
ProvidedMethod(methodName, [], typedefof<seq<_>>.MakeGenericType(rowType), isStatic = true,
99+
invokeCode = fun _-> <@@ propsArray @@>)
100+
101+
cssType.AddMember staticMethod
102+
78103
{
79104
GeneratedType = cssType
80105
RepresentationType = cssType
@@ -88,18 +113,16 @@ module TypeProvider =
88113
ProvidedStaticParameter( "source" , typeof< string >, parameterDefaultValue = "" )
89114
ProvidedStaticParameter( "naming" , typeof< Naming >, parameterDefaultValue = Naming.Verbatim )
90115
ProvidedStaticParameter( "resolutionFolder" , typeof< string >, parameterDefaultValue = "" )
116+
ProvidedStaticParameter( "getProperties" , typeof< bool >, parameterDefaultValue = false )
91117
]
92118

93119
let helpText = """
94-
<summary>Typed CSS classes.</summary>
120+
<summary>Typed CSS classes. Provides generated properties representing CSS classes from a stylesheet.</summary>
95121
<param name='source'>Location of a CSS stylesheet (file path or web URL), or a string containing CSS text.</param>
96-
<param name='naming'>Naming strategy for class name properties, specified by the Naming enum.
97-
Verbatim: (default) use class names verbatim from source CSS, requiring backtick-quotes for names with special characters.
98-
Underscores: replace all non-alphanumeric characters with underscores.
99-
CamelCase: convert to camel case names with all non-alphanumeric characters removed.
100-
PascalCase: convert to Pascal case names with all non-alphanumeric characters removed.
101-
</param>
122+
<param name='naming'>Naming strategy for class name properties.
123+
One of: Naming.Verbatim (default), Naming.Underscores, Naming.CamelCase, Naming.PascalCase.</param>
102124
<param name='resolutionFolder'>A directory that is used when resolving relative file references.</param>
125+
<param name='getProperties'>Adds a GetProperties() method that returns a seq of all generated property name/value pairs.</param>
103126
"""
104127

105128
do parentType.AddXmlDoc helpText

src/TypedCssClasses.fsproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
5-
<Version>0.0.2</Version>
5+
<Version>0.0.3</Version>
66
<PackageId>Zanaptak.TypedCssClasses</PackageId>
77
<Authors>zanaptak</Authors>
88
<Product>Zanaptak.TypedCssClasses</Product>
99
<PackageTags>f#;fsharp;css</PackageTags>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11-
<Description>A CSS class type provider for F# web development.</Description>
11+
<Description>A CSS class type provider for F# web development. Bring external stylesheet classes into your F# code as design-time discoverable compiler-verified properties.</Description>
1212
<AssemblyName>Zanaptak.TypedCssClasses</AssemblyName>
1313
<PackageProjectUrl>https://github.com/zanaptak/TypedCssClasses</PackageProjectUrl>
1414
<Configurations>Debug;Release;ReleaseTest;DebugLog</Configurations>

0 commit comments

Comments
 (0)