Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --@io_bazel_rules_docker//transitions:enable=no
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0.0
16 changes: 16 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: bazelbuild/setup-bazelisk@v1
- run: bazel build //...
- run: bazel test //...
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bazel-bin
bazel-multi-language-bazel-monorepo
bazel-out
bazel-testlogs

__pycache__

.idea

node_modules
Empty file added BUILD.bazel
Empty file.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

128 changes: 128 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Declares that this directory is the root of a Bazel workspace.
# See https://docs.bazel.build/versions/main/build-ref.html#workspace
workspace(
# How this workspace would be referenced with absolute labels from another workspace
name = "multi_language_monorepo",
# Map the @npm bazel workspace to the node_modules directory.
# This lets Bazel use the same node_modules as other local tooling.
managed_directories = {"@npm": ["node_modules"]},
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

### PYTHON

http_archive(
name = "rules_python",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.5.0/rules_python-0.5.0.tar.gz",
sha256 = "cd6730ed53a002c56ce4e2f396ba3b3be262fd7cb68339f0377a45e8227fe332",
)

load("@rules_python//python:pip.bzl", "pip_install")

pip_install(
name = "python_deps",
requirements = "//third_party:requirements.txt",
)

### GO

http_archive(
name = "io_bazel_rules_go",
sha256 = "ab21448cef298740765f33a7f5acee0607203e4ea321219f2a4c85a6e0fb0a27",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.32.0/rules_go-v0.32.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.32.0/rules_go-v0.32.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

############################################################
# Define your own dependencies here using go_repository.
# Else, dependencies declared by rules_go/gazelle will be used.
# The first declaration of an external repository "wins".
############################################################
go_repository(
name = "com_github_gorilla_mux",
importpath = "github.com/gorilla/mux",
tag = "v1.8.0",
)

go_rules_dependencies()

go_register_toolchains(version = "1.18")

gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel")

### NODEJS

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "0fad45a9bda7dc1990c47b002fd64f55041ea751fafc00cd34efb96107675778",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.0/rules_nodejs-5.5.0.tar.gz"],
)

load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies")

build_bazel_rules_nodejs_dependencies()

# fetches nodejs, npm, and yarn
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories")
node_repositories()

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

# bazel run @yarn//:yarn
yarn_install(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)

### DOCKER

http_archive(
name = "io_bazel_rules_docker",
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
)

load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
container_repositories()

load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")

container_deps()

### Loading base docker images for different languages

load("@io_bazel_rules_docker//nodejs:image.bzl", _nodejs_image_repos = "repositories")
_nodejs_image_repos()

load("@io_bazel_rules_docker//go:image.bzl", _go_image_repos = "repositories")
_go_image_repos()

load("@io_bazel_rules_docker//python3:image.bzl", _py_image_repos = "repositories")
_py_image_repos()

### BAZOKU

http_archive(
name = "bazoku",
sha256 = "65c8d074f4aa0b0604142ce540d0d8383c3781f7609ac490bd7b4023e75b48f8",
urls = ["https://github.com/salesforce/bazoku/releases/download/v0.1.0/bazoku-v0.1.0.tar.gz"],
)

load("@bazoku//tools:deps.bzl", "bazoku_deps")
bazoku_deps()
1 change: 1 addition & 0 deletions bazel-monorepo-examples
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "multi-language-bazel-monorepo",
"version": "0.1.0",
"private": true,
"devDependencies": {
"@bazel/jasmine": "5.3.0",
"jasmine": "4.0.2",
"jasmine-core": "4.0.1"
},
"scripts": {
"build": "bazel build //...",
"test": "bazel test //..."
},
"dependencies": {
"@bazel/typescript": "^5.5.0",
"express": "4.17.3",
"typescript": "^4.7.2"
}
}
14 changes: 14 additions & 0 deletions projects/go_hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_hello_world",
importpath = "github.com/kriscfoster/multi-language-bazel-monorepo/projects/go_hello_world",
srcs = ["hello_world.go"],
visibility = ["//projects/go_web:__pkg__"],
)

go_test(
name = "go_hello_world_test",
srcs = ["hello_world_test.go"],
embed = [":go_hello_world"]
)
5 changes: 5 additions & 0 deletions projects/go_hello_world/hello_world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package go_hello_world

func HelloWorld() string {
return "hello world!"
}
11 changes: 11 additions & 0 deletions projects/go_hello_world/hello_world_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package go_hello_world

import "testing"

func TestGreeter(t *testing.T) {
expected := "hello world!"
actual := HelloWorld()
if actual != expected {
t.Errorf("expected %q but got %q", expected, actual)
}
}
16 changes: 16 additions & 0 deletions projects/node_calculator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")

js_library(
name = "node_calculator",
srcs = ["calculator.js"],
visibility = ["//projects/node_web:__pkg__"],
)

jasmine_node_test(
name = "calculator_test",
srcs = ["calculator.spec.js"],
data = [
":node_calculator",
]
)
5 changes: 5 additions & 0 deletions projects/node_calculator/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class Calculator {
add(x, y) {
return x + y;
}
}
7 changes: 7 additions & 0 deletions projects/node_calculator/calculator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Calculator = require('./calculator');
const calculator = new Calculator();

it("1 + 2 = 3 >", () => {
const expected = "Hello World!"
expect(calculator.add(1, 2)).toEqual(3);
});
23 changes: 23 additions & 0 deletions projects/node_web/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")

nodejs_image(
name = "node_web_image",
binary = "node_web",
node_repository_name = "nodejs_linux_amd64",
include_node_repo_args = False,
)

nodejs_binary(
name = "node_web",
entry_point = ":index.js",
data = [
"//projects/node_calculator",
"@npm//express"
],
templated_args = [
"--nobazel_run_linker",
"--nobazel_node_patches",
],
)
20 changes: 20 additions & 0 deletions projects/node_web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require('express');
const Calculator = require('../node_calculator/calculator.js');
const app = express();
const calculator = new Calculator();

const port = process.env.PORT | 8080;

app.get('/', (_req, res) => {
const rand1 = getRandomInt(100);
const rand2 = getRandomInt(100);
res.send(`did you know that ${rand1} + ${rand2} = ${calculator.add(rand1, rand2)}`);
});

app.listen(port, () => {
console.log(`listening on port ${port}`);
});

function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
10 changes: 10 additions & 0 deletions projects/python_calculator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
py_library(
name = "calculator",
srcs = ["calculator.py"],
visibility = ["//projects/python_web:__pkg__"],
)

py_test(
name = "calculator_test",
srcs = ["calculator_test.py"]
)
Empty file.
2 changes: 2 additions & 0 deletions projects/python_calculator/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Calculator:
def add(self, x, y): return x + y
14 changes: 14 additions & 0 deletions projects/python_calculator/calculator_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest
from calculator import Calculator

class TestSum(unittest.TestCase):
def test_sum(self):
calculator = Calculator()
self.assertEqual(calculator.add(1, 2), 3)

def test_sum_negative(self):
calculator = Calculator()
self.assertEqual(calculator.add(-4, -11), -15)

if __name__ == '__main__':
unittest.main()
35 changes: 35 additions & 0 deletions projects/python_web/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@python_deps//:requirements.bzl", "requirement")
load("@io_bazel_rules_docker//python3:image.bzl", py_image="py3_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_push")

py_library(
name = "python_web_lib",
srcs = ["python_web.py"],
deps = [
requirement("Flask"),
"//projects/python_calculator:calculator"
],
)

py_binary(
name = "python_web",
srcs = ["python_web.py"],
deps = [":python_web_lib"],
main = "python_web.py",
)

py_image(
name = "python_web_image",
srcs = ["python_web.py"],
deps = [":python_web_lib"],
main = "python_web.py",
)

container_push(
name = "publish",
format = "Docker",
registry = "registry.hub.docker.com",
repository = "krisfoster96/monorepo-python-web",
image = ":python_web_image",
tag = "1",
)
17 changes: 17 additions & 0 deletions projects/python_web/python_web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from flask import Flask
from random import randint

from projects.python_calculator.calculator import Calculator

app = Flask(__name__)
my_calculator = Calculator()

@app.route('/')
def hello():
num1 = randint(0, 100)
num2 = randint(0, 100)
message = "GUESSS WHAT!!!!, it turns out that {} + {} = {}?".format(num1, num2, my_calculator.add(num1, num2))
return message

if __name__ == '__main__':
app.run(host='0.0.0.0')
Loading