diff --git a/BUILD.bazel b/BUILD.bazel index f7bfb69..3426ba3 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -14,3 +14,12 @@ gazelle( ], command = "update-repos", ) + +filegroup( + name = "go_mod", + srcs = [ + "go.mod", + "go.sum", + ], + visibility = ["//:__subpackages__"], +) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index edcf860..a78a183 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -12,6 +12,40 @@ workspace(name = "com_github_cloneable_rules_ent") # load("@com_github_cloneable_rules_ent//:deps.bzl", "ent_go_dependencies") -# ent_go_dependencies() +# gazelle:repository_macro deps.bzl%ent_go_dependencies -#gazelle:repository_macro deps.bzl%ent_go_dependencies +load("//:workspace.bzl", "install_workspace_dependencies") + +install_workspace_dependencies() + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") + +# Gazelle -- (for the example) + +# gazelle:repo bazel_gazelle +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") + + +# Protobuf -- (for the example) +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") + +protobuf_deps() + +load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") + +rules_proto_dependencies() + +rules_proto_toolchains() + +# Go -- (for the example) + +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") + +go_rules_dependencies() + +go_register_toolchains(version = "1.19.5") + +# Ent -- (for the example) +load("@com_github_cloneable_rules_ent//:deps.bzl", "ent_go_dependencies") + +ent_go_dependencies() diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..392bd23 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,3 @@ +module github.com/cloneable/rules_ent + +go 1.19 diff --git a/example/protos/BUILD.bazel b/example/protos/BUILD.bazel new file mode 100644 index 0000000..40f53a7 --- /dev/null +++ b/example/protos/BUILD.bazel @@ -0,0 +1,23 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") + +proto_library( + name = "user_settings_proto", + srcs = ["user_settings.proto"], + visibility = ["//visibility:public"], +) + +go_proto_library( + name = "user_settings_go_proto", + importpath = "github.com/cloneable/rules_ent/example/protos", + proto = ":user_settings_proto", + visibility = ["//visibility:public"], +) + +go_library( + name = "proto", + embed = [":user_settings_go_proto"], + importpath = "github.com/cloneable/rules_ent/example/protos", + visibility = ["//visibility:public"], +) diff --git a/example/protos/user_settings.proto b/example/protos/user_settings.proto new file mode 100644 index 0000000..79faae6 --- /dev/null +++ b/example/protos/user_settings.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package user_settings; + +message UserSettings { + bool color_blind = 1; + AppLanguage lang = 2; +} + +enum AppLanguage { + ENGLISH = 0; + FRENCH = 1; + PORTUGUESE = 2; +} diff --git a/example/schema/BUILD.bazel b/example/schema/BUILD.bazel new file mode 100644 index 0000000..c983c4d --- /dev/null +++ b/example/schema/BUILD.bazel @@ -0,0 +1,27 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("//:defs.bzl", "go_ent_library") + +go_library( + name = "schema", + srcs = ["user.go"], + importpath = "github.com/cloneable/rules_ent/example/schema", + visibility = ["//visibility:public"], + deps = [ + "//example/protos:proto", + "@com_github_google_uuid//:uuid", + "@io_entgo_ent//:ent", + "@io_entgo_ent//schema/field", + "@io_entgo_ent//schema/mixin", + ], +) + +go_ent_library( + name = "db", + entities = [ + "user", + ], + gomod = "//:go_mod", + importpath = "github.com/cloneable/rules_ent/example/db", + schema = ":schema", + visibility = ["//:__subpackages__"], +) diff --git a/example/schema/user.go b/example/schema/user.go new file mode 100644 index 0000000..e21c78f --- /dev/null +++ b/example/schema/user.go @@ -0,0 +1,31 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/mixin" + "github.com/cloneable/rules_ent/example/protos" + "github.com/google/uuid" +) + +type User struct { + ent.Schema +} + +func (User) Fields() []ent.Field { + return []ent.Field{ + field.UUID("id", uuid.UUID{}).Default(uuid.New), + field.String("email").Unique(), + field.Bytes("preferences").GoType(&protos.UserSettings{}), + } +} + +func (User) Mixin() []ent.Mixin { + return []ent.Mixin{ + mixin.Time{}, + } +} + +func (User) Edges() []ent.Edge { + return []ent.Edge{} +} diff --git a/workspace.bzl b/workspace.bzl new file mode 100644 index 0000000..2a90b92 --- /dev/null +++ b/workspace.bzl @@ -0,0 +1,77 @@ +""" Morf workspace dependencies """ + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") + +def install_workspace_dependencies(): + """ Defines repositories for core rules sets + """ + + # Open source license validations + http_archive( + name = "rules_license", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.3/rules_license-0.0.3.tar.gz", + "https://github.com/bazelbuild/rules_license/releases/download/0.0.3/rules_license-0.0.3.tar.gz", + ], + sha256 = "00ccc0df21312c127ac4b12880ab0f9a26c1cff99442dc6c5a331750360de3c3", + ) + + # Protobuf-related rules + http_archive( + name = "com_google_protobuf", + sha256 = "9c0fd39c7a08dff543c643f0f4baf081988129a411b977a07c46221793605638", + strip_prefix = "protobuf-3.20.3", + urls = [ + "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.20.3.tar.gz", + "https://github.com/protocolbuffers/protobuf/archive/v3.20.3.tar.gz", + ], + ) + + http_archive( + name = "rules_proto", + sha256 = "e017528fd1c91c5a33f15493e3a398181a9e821a804eb7ff5acdd1d2d6c2b18d", + strip_prefix = "rules_proto-4.0.0-3.20.0", + urls = [ + "https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0-3.20.0.tar.gz", + ], + ) + + # go-related rules + http_archive( + name = "io_bazel_rules_go", + sha256 = "16e9fca53ed6bd4ff4ad76facc9b7b651a89db1689a2877d6fd7b82aa824e366", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + ], + ) + + http_archive( + name = "bazel_skylib", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", + ], + sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", + ) + + # gazelle + http_archive( + name = "bazel_gazelle", + sha256 = "ecba0f04f96b4960a5b250c8e8eeec42281035970aa8852dda73098274d14a1d", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz", + ], + ) + + # buildifier + http_archive( + name = "com_github_bazelbuild_buildtools", + sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3", + strip_prefix = "buildtools-4.2.2", + urls = [ + "https://github.com/bazelbuild/buildtools/archive/refs/tags/4.2.2.tar.gz", + ], + )