Skip to content

Commit

Permalink
simple paket support
Browse files Browse the repository at this point in the history
  • Loading branch information
jindraivanek committed Apr 23, 2017
1 parent 3583556 commit ac3f8b9
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 23 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.swp
*~
/bazel-bin
/bazel-genfiles
/bazel-out
/bazel-rules_dotnet
/bazel-testlogs
**/bazel-bin
**/bazel-genfiles
**/bazel-out
**/bazel-rules_dotnet
**/bazel-testlogs
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

This is forked from https://github.com/bazelbuild/rules_dotnet and quick-patched to work with fsharp.

Tested on linux, maybe it works on OSX, Windows not supported yet.

## Usage

Add the following to your `WORKSPACE` file to add the external repositories:
`WORKSPACE` file:

```python
git_repository(
Expand All @@ -16,30 +18,58 @@ git_repository(
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_repositories",
"nuget_package",
)

fsharp_repositories(use_local_mono = True)
```

## Examples
## Examples of BUILD files

### fsharp_library

```python
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_binary",
)

fsharp_library(
name = "lib",
srcs = ["lib.fs"],
deps = ["//my/dependency:SomeLib"],
deps = ["//someLib"],
)
```

### fsharp_binary

```python
csharp_binary(
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_binary",
)

fsharp_binary(
name = "MyApp",
srcs = ["MyApp.fs"],
deps = ["//my/dependency:MyLib"],
deps = ["//lib"],
)
```

## Paket support
Add `"paket_dependencies"` to `load` in WORKSPACE and following:

```python
paket_dependencies(
name = "paket_deps",
deps = """
source https://nuget.org/api/v2
framework: net46
nuget ExtCore
nuget Argu
""",
)
```

where `deps` is contents of generated paket.dependencies file.

Then you can reference all packages with `"@paket_deps//:dylibs"` in `deps` attribute.
2 changes: 1 addition & 1 deletion dotnet/NUGET_BUILD.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_dotnet//dotnet:fsharp.bzl", "dll_import")
load("@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl", "dll_import")

dll_import(
name = "dylibs",
Expand Down
7 changes: 7 additions & 0 deletions dotnet/PAKET_BUILD.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl", "dll_import")

dll_import(
name = "dylibs",
srcs = glob(["**/*.dll"]),
visibility = ["//visibility:public"],
)
92 changes: 85 additions & 7 deletions dotnet/fsharp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ def _make_fsc_arglist(ctx, output, depinfo, extra_refs=[]):

# /lib:dir1,[dir1]
if libdirs:
args += [_make_fsc_flag(flag_start, "lib", ",".join(list(libdirs)))]
args += [_make_fsc_flag(flag_start, "lib", x) for x in list(libdirs)]

# /reference:filename[,filename2]
if depinfo.refs or extra_refs:
args += [_make_fsc_flag(flag_start, "reference",
",".join(list(depinfo.refs + extra_refs)))]
args += [_make_fsc_flag(flag_start, "reference", x) for x in list(depinfo.refs + extra_refs)]
else:
args += extra_refs

Expand Down Expand Up @@ -366,7 +365,7 @@ fsharp_library = rule(
Args:
name: A unique name for this rule.
srcs: F# `.cs` or `.resx` files.
srcs: F# `.fs` or `.resx` files.
deps: Dependencies for this rule
warn: Compiler warning level for this library. (Defaults to 4).
fsc: Override the default F# compiler.
Expand All @@ -384,7 +383,7 @@ fsharp_binary = rule(
Args:
name: A unique name for this rule.
srcs: F# `.cs` or `.resx` files.
srcs: F# `.fs` or `.resx` files.
deps: Dependencies for this rule
main_class: Name of class with `main()` method to use as entry point.
warn: Compiler warning level for this library. (Defaults to 4).
Expand All @@ -406,7 +405,7 @@ testing framework.
Args:
name: A unique name for this rule.
srcs: F# `.cs` or `.resx` files.
srcs: F# `.fs` or `.resx` files.
deps: Dependencies for this rule
warn: Compiler warning level for this library. (Defaults to 4).
fsc: Override the default F# compiler.
Expand Down Expand Up @@ -532,6 +531,68 @@ Args:
build_file_content: content for the BUILD file.
"""

def _paket_run(repository_ctx, paket_cmd):
print("Paket command:", paket_cmd)
result = repository_ctx.execute(paket_cmd)
if result.return_code:
fail("Paket command failed: %s (%s)" % (result.stderr, " ".join(paket_cmd)))

def _paket_package_impl(repository_ctx,
build_file = None,
build_file_content = None):
# figure out the output_path
output_dir = repository_ctx.path("")

mono = repository_ctx.path(repository_ctx.attr.mono_exe)
paket = repository_ctx.path(repository_ctx.attr.paket_exe)

# assemble our nuget command
paket_cmd = [
mono,
"--config", "%s/../etc/mono/config" % mono.dirname,
paket,
]

repository_ctx.file(repository_ctx.path("paket.dependencies"), repository_ctx.attr.deps)
_paket_run(repository_ctx, paket_cmd + ["install"])

tpl_file = Label("//dotnet:PAKET_BUILD.tpl")
# add the BUILD file
repository_ctx.template(
"BUILD",
tpl_file,
{"%{package}": repository_ctx.name,
"%{output_dir}": "%s" % output_dir})

_paket_package_attrs = {
# The name of the paket package
"deps":attr.string(mandatory=True),
# Reference to the mono binary
"mono_exe":attr.label(
executable=True,
default=Label("@mono//bin:mono"),
cfg="host",
),
# Reference to the paket.exe file
"paket_exe":attr.label(
default=Label("@paket//:paket.exe"),
),
}

paket_dependencies = repository_rule(
implementation=_paket_package_impl,
attrs=_paket_package_attrs,
)
"""Fetches a paket package as an external dependency.
Args:
package_sources: list of sources to use for paket package feeds.
package: name of the paket package.
version: version of the paket package (e.g. 0.1.2)
mono_exe: optional label to the mono executable.
paket_exe: optional label to the paket.exe file.
"""

fsharp_autoconf = repository_rule(
implementation = _fsharp_autoconf,
local = True,
Expand Down Expand Up @@ -571,6 +632,23 @@ mono_package = repository_rule(
local = True,
)

def _paket_repository_impl(repository_ctx):
download_output = repository_ctx.path("paket.exe")
# download the package
repository_ctx.download(
"https://github.com/fsprojects/Paket/releases/download/4.5.1/paket.bootstrapper.exe",
download_output)

# now we create the build file.
toolchain_build = """
package(default_visibility = ["//visibility:public"])
exports_files(["paket.exe"])
"""
repository_ctx.file("BUILD", toolchain_build)

paket_binary = repository_rule(
implementation = _paket_repository_impl)

def fsharp_repositories(use_local_mono=False):
"""Adds the repository rules needed for using the F# rules."""

Expand All @@ -597,4 +675,4 @@ def fsharp_repositories(use_local_mono=False):
)

mono_package(name="mono", use_local=use_local_mono)

paket_binary(name="paket")
12 changes: 11 additions & 1 deletion examples/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ local_repository(
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_repositories",
"nuget_package",
"paket_dependencies",
)

fsharp_repositories(use_local_mono = True)

paket_dependencies(
name = "paket_deps",
deps = """
source https://nuget.org/api/v2
framework: net46
nuget ExtCore
nuget Argu
""",
)
2 changes: 0 additions & 2 deletions examples/hello/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_repositories",
"fsharp_binary",
"nuget_package",
)

fsharp_binary(
Expand Down
10 changes: 10 additions & 0 deletions examples/hello_paket/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_binary",
)

fsharp_binary(
name = "hello_paket",
srcs = ["hello.fs"],
deps = ["//lib", "@paket_deps//:dylibs"]
)
5 changes: 5 additions & 0 deletions examples/hello_paket/hello.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
open ExtCore

printfn "Hello world"
printfn "%i" (Lib.f())
printfn "%A" (None |> Option.fill "Hello from ExtCore!")
1 change: 0 additions & 1 deletion examples/lib/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load(
"@io_bazel_rules_dotnet_fsharp//dotnet:fsharp.bzl",
"fsharp_library",
"nuget_package",
)

fsharp_library(
Expand Down

0 comments on commit ac3f8b9

Please sign in to comment.