Reputation: 642
Is there any way to just install a package in my system and have any .fs
file be able to reference it?
Is it possible to use F# Packages without following the Project Structure?
All FSharp uses of external packages I have seen either make use of NuGet (for proper big projects) or reference a downloaded dll (for F# .fsx
scripts).
I'm looking to make use of the compiler and generate an executable.
My use case is the following:
I have a very simple command line script that does not justify having a complex project structure with a bunch of project, paket and config files. All I really need is to compile a single FSharp file and produce an .exe
.
Now, this script requires doing some HTTP requests, retrieving a JSON response, and transform that data - I believe FSharp.Data is the perfect tool for the job but I have been unable to find out how integrate it with my problem.
Upvotes: 1
Views: 519
Reputation: 5014
You can always invoke the compiler directly to generate your executable:
Fsc.exe
"..\projects\modules\src\Calculate primes.fs"
-o:"..\projects\modules\bin\Calculate primes.exe"
--debug:full
--define:DEBUG
--define:TRACE
--target:winexe
-g
--warn:3
--warnaserror:76
--vserrors
--utf8output
--fullpaths
--flaterrors
--subsystemversion:6.00
--highentropyva+
--optimize-
--tailcalls-
-I:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1"
-I:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades"
Create a batch file and put it all in one line. Add the libraries you need like this:
-r:"..\packages\FSharp.Data\lib\net45\FSharp.Data.dll"
After compilation you may need to copy all dlls used in the same directory as the executable and it helps to have a <program>.exe.config
file.
Upvotes: 2
Reputation: 10957
I don't think there is a supported way to exactly what you need until this language suggestion is implemented https://github.com/fsharp/fslang-design/blob/master/tooling/FST-1027-fsi-references.md
If you need only FSharp.Data and don't care about editor tooling and lib versioning much, you can download the .dll manually, use from your file (let's call it app.fs
) and then with F# tools installed you can compile it with something like fsc.exe -r:<path-to-FSharp.Data.dll> -o app.exe --standalone --target:exe app.fs
(I haven't tried this particular set of options, but you can check fsc docs)
The project system isn't that complex and heavyweight though, especially if you can use .NET Core SDK with its command-line interface.
If you run dotnet new console -lang=F#
, the complete project file looks like this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
and depending on your tooling you can just add <PackageReference Include="FSharp.Data" Version="3.0.0" />
to the <ItemGroup>
and after you dotnet publish
, you have your app ready.
Upvotes: 2