Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active August 10, 2018 15:47
Show Gist options
  • Save amirrajan/79fb627e79f6a50e09e6 to your computer and use it in GitHub Desktop.
Save amirrajan/79fb627e79f6a50e09e6 to your computer and use it in GitHub Desktop.
FSharpData Quick Start for OSX

Steps:

  1. Install this version of Mono (You can use the current version. I've just tested it with the version linked).
  2. Clone this gist.
  3. cd into the cloned directory.
  4. Download nuget.exe and put it in the same directory.
  5. Run sh ./build.sh.
  6. HelloWorld/HelloWorld.exe should be generated.
  7. Now you should be able to run mono HelloWorld/HelloWorld.exe --batchsize 10
#r @"FSharpModules/FAKE/tools/FakeLib.dll"
open Fake
open Fake.FscHelper
let commonDlls = [
//if you installed a different version of mono, update these path entires
"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/mscorlib.dll";
"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/System.Numerics.dll"
"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/System.Data.dll"
"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/System.Xml.Linq.dll"
"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/FSharp.Core.dll";
"./FSharpModules/UnionArgParser/lib/net40/UnionArgParser.dll";
"./FSharpModules/Microsoft.SqlServer.Types/lib/net20/Microsoft.SqlServer.Types.dll";
"./FSharpModules/FSharp.Data/lib/net40/FSharp.Data.dll";
"./FSharpModules/FSharp.Data.SqlClient/lib/net40/FSharp.Data.SqlClient.dll";
"./FsharpModules/Http.fs/lib/net40/HttpClient.dll";
"./FSharpModules/Newtonsoft.Json/lib/net40/Newtonsoft.Json.dll";
]
Target "HelloWor.exe" (fun _ ->
["HelloWorld.fsx"] |> Fsc (fun p -> { p with References = commonDlls })
CreateDir "HelloWorld"
MoveFile "HelloWorld" "HelloWorld.exe"
commonDlls |> List.iter (fun f -> CopyFile "HelloWorld" f)
)
RunTargetOrDefault "HelloWorld.exe"
mono nuget.exe install FSharp.Core -ExcludeVersion -OutputDirectory FSharpModules -Version 3.1.2.1
mono nuget.exe install FSharp.Data -ExcludeVersion -OutputDirectory FSharpModules -Version 2.2.3
mono nuget.exe install FSharp.Data.SqlClient -ExcludeVersion -OutputDirectory FSharpModules -Version 1.7.2
mono nuget.exe install Http.fs -ExcludeVersion -OutputDirectory FSharpModules -Version 1.5.1
mono nuget.exe install Newtonsoft.Json -ExcludeVersion -OutputDirectory FSharpModules -Version 6.0.1
mono nuget.exe install UnionArgParser -ExcludeVersion -OutputDirectory FSharpModules -Version 0.8.7
mono nuget.exe install Microsoft.SqlServer.Types -ExcludeVersion -OutputDirectory FSharpModules -Version 11.0.2
mono nuget.exe install Newtonsoft.Json -ExcludeVersion -OutputDirectory FSharpModules -Version 6.0.1
mono nuget.exe install Http.fs -ExcludeVersion -OutputDirectory FSharpModules -Version 1.5.1
mono nuget.exe install FAKE -ExcludeVersion -OutputDirectory FSharpModules -Version 4.3.1
mono FSharpModules/FAKE/tools/FAKE.exe build.fsx
#load "References.fsx"
open System
open Microsoft.FSharp.Reflection
open Nessos.UnionArgParser
let fromString<'a> s =
match FSharpType.GetUnionCases typeof<'a> |> Array.filter (fun case -> case.Name = s) with
| [|case|] -> FSharpValue.MakeUnion(case,[||]) :?> 'a
| _ -> failwith <| sprintf "Can't convert %s to DU" s
let notFn p = fun a -> p a |> not
let reject predicate = Seq.filter(predicate |> notFn)
let processArgs<'a when 'a :> IArgParserTemplate> _ =
let cliArgs = System.Environment.GetCommandLineArgs()
let parser = UnionArgParser.Create<'a>()
parser.Parse(cliArgs |>
reject(fun (s : String) -> s.Contains("exe") || s.Contains("fsx")) |>
Seq.toArray)
let printRow row = printfn "%A" row
#load "References.fsx"
#load "Sql.fsx"
#load "Cli.fsx"
open System
open FSharp.Data
open Sql
open Cli
open Nessos.UnionArgParser
open Newtonsoft.Json
open Newtonsoft.Json.Serialization
[<Literal>]
let getValidUsersQuery = """
select top (@batchSize) u.Email
from Users as u (nolock)
where u.IsValid = 1
"""
type GetValidUsersQuery = SqlCommandProvider<getValidUsersQuery, designTimeConnectionString>
type CLIArguments =
| BatchSize of Int64
with
interface IArgParserTemplate with
member s.Usage =
match s with
| BatchSize _ -> "specify number of users to return."
let getValidUsers batchSize =
let cmd = new GetValidUsersQuery(runTimeConnectionString)
cmd.Execute(batchSize = batchSize) |> List.ofSeq
let args = processArgs<CLIArguments>()
let batchSize = args.GetResult (<@ BatchSize @>)
let users = getValidUsers Man Woman batchSize environment
type Results = {
Users : GetValidUsersQuery.Record list;
}
JsonConvert.DefaultSettings <- fun _ ->
new JsonSerializerSettings(
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver())
let result = { Users = users }
let resultString = JsonConvert.SerializeObject(result)
printfn "%s" resultString
#I "./FSharpModules/UnionArgParser/lib/net40"
#I "./FSharpModules/Microsoft.SqlServer.Types/lib/net20"
#I "./FSharpModules/FSharp.Data/lib/net40"
#I "./FSharpModules/FSharp.Data.SqlClient/lib/net40"
#I "./FsharpModules/Http.fs/lib/net40"
#I "./FSharpModules/Newtonsoft.Json/lib/net40"
#r "System.Core.dll"
#r "System.Xml.Linq.dll"
#r "UnionArgParser.dll"
#r "Microsoft.SqlServer.Types.dll"
#r "FSharp.Data.dll"
#r "FSharp.Data.SqlClient.dll"
#r "HttpClient.dll"
#r "Newtonsoft.Json.dll"
open System
open System.Numerics
open System.Data
open System.Xml
open System.Xml.Linq
open FSharp.Data
open Newtonsoft.Json
open Newtonsoft.Json.Linq
module Sql
[<Literal>]
let designTimeConnectionString = """Server=SOMESERVER;Database=SOMEDATABASE;User Id=SOMEUSERID;Password=SOMEPASSWORD;"""
let runTimeConnectionString = """Server=SOMESERVER;Database=SOMEDATABASE;User Id=SOMEUSERID;Password=SOMEPASSWORD;"""
@ilyakharlamov
Copy link

[Loading /private/tmp/2018-08-10/79fb627e79f6a50e09e6-7bc722f085dc2ef81fd42d19efc92d160f7832c2/build.fsx]

System.Exception: Target "HelloWorld.exe" is not defined.
at Fake.TargetHelper+getTarget@77-2.Invoke (System.String message) [0x00001] in <55deb2c7ccf1c534a7450383c7b2de55>:0
at Microsoft.FSharp.Core.PrintfImpl+StringPrintfEnv1[TResult].Finalize () [0x00012] in <5584aad2904cf4daa7450383d2aa8455>:0 at Microsoft.FSharp.Core.PrintfImpl+Final1@224[TState,TResidue,TResult,A].Invoke (Microsoft.FSharp.Core.FSharpFunc2[T,TResult] env, A a) [0x00038] in <5584aad2904cf4daa7450383d2aa8455>:0
at Microsoft.FSharp.Core.OptimizedClosures+Invoke@3253[T2,TResult,T1].Invoke (T2 u) [0x00001] in <5584aad2904cf4daa7450383d2aa8455>:0
at Fake.TargetHelper.getTarget (System.String name) [0x000c3] in <55deb2c7ccf1c534a7450383c7b2de55>:0
at Fake.TargetHelper.determineBuildOrder (System.String target) [0x00001] in <55deb2c7ccf1c534a7450383c7b2de55>:0
at Fake.TargetHelper.run$cont@499 (System.String targetName, Microsoft.FSharp.Core.Unit unitVar) [0x00072] in <55deb2c7ccf1c534a7450383c7b2de55>:0
at Fake.TargetHelper.run (System.String targetName) [0x00011] in <55deb2c7ccf1c534a7450383c7b2de55>:0
at Fake.AdditionalSyntax.RunTargetOrDefault (System.String defaultTarget) [0x00021] in <55deb2c7ccf1c534a7450383c7b2de55>:0
at <StartupCode$FSI_0001>.$FSI_0001_Build$fsx.main@ () [0x0008f] in <4933c6e754d9463e9b1371580ca7f81c>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in :0
Stopped due to error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment