Skip to content

Commit

Permalink
Add Bazel example using new macros.
Browse files Browse the repository at this point in the history
RELNOTES=Update Bazel example.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=315488934
  • Loading branch information
bcorso authored and cpovirk committed Jun 10, 2020
1 parent 22b1ec6 commit 9494f40
Show file tree
Hide file tree
Showing 32 changed files with 851 additions and 237 deletions.
18 changes: 18 additions & 0 deletions examples/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2020 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Description:
# Dagger examples

package(default_visibility = ["//:src"])
20 changes: 20 additions & 0 deletions examples/bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) 2020 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Description:
# Dagger Bazel examples

load("@dagger//:workspace_defs.bzl", "dagger_rules")

dagger_rules()
83 changes: 83 additions & 0 deletions examples/bazel/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (C) 2020 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Description:
# Defines the Bazel workspace rules for the Dagger examples.

########################
# Load Dagger repository
########################

# TODO(user): Replace with `http_archive` pointing to tagged released.
local_repository(
name = "dagger",
path = "../../",
)

load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES")

#########################
# Load Android repository
#########################

android_sdk_repository(
name = "androidsdk",
api_level = 29,
build_tools_version = "29.0.2",
)

#############################
# Load Robolectric repository
#############################

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

http_archive(
name = "robolectric",
strip_prefix = "robolectric-bazel-4.1",
urls = ["https://github.com/robolectric/robolectric-bazel/archive/4.1.tar.gz"],
)

load("@robolectric//bazel:robolectric.bzl", "robolectric_repositories")

robolectric_repositories()

#########################
# Load Maven repositories
#########################

RULES_JVM_EXTERNAL_TAG = "2.7"

RULES_JVM_EXTERNAL_SHA = "f04b1466a00a2845106801e0c5cec96841f49ea4e7d1df88dc8e4bf31523df74"

http_archive(
name = "rules_jvm_external",
sha256 = RULES_JVM_EXTERNAL_SHA,
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = DAGGER_ARTIFACTS + [
"androidx.test.ext:junit:1.1.1",
"androidx.test:runner:1.1.1",
"com.google.truth:truth:1.0.1",
"junit:junit:4.13",
"org.robolectric:robolectric:4.1",
"org.robolectric:annotations:4.1",
],
repositories = DAGGER_REPOSITORIES,
)
25 changes: 25 additions & 0 deletions examples/bazel/java/example/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (C) 2020 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

java_library(
name = "common",
srcs = glob(["*.java"]),
visibility = [
"//java/example:__subpackages__",
"//javatests/example:__subpackages__",
],
deps = [
"//:dagger",
],
)
39 changes: 39 additions & 0 deletions examples/bazel/java/example/common/CoffeeLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2020 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.common;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;

/** A logger to logs steps while brewing coffee. */
@Singleton
public final class CoffeeLogger {
private final List<String> logs = new ArrayList<>();

@Inject
CoffeeLogger() {}

public void log(String msg) {
logs.add(msg);
}

public List<String> logs() {
return new ArrayList<>(logs);
}
}
41 changes: 41 additions & 0 deletions examples/bazel/java/example/common/CoffeeMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2020 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.common;

import dagger.Lazy;
import javax.inject.Inject;

/** A coffee maker to brew the coffee. */
public class CoffeeMaker {
private final CoffeeLogger logger;
private final Lazy<Heater> heater; // Create a possibly costly heater only when we use it.
private final Pump pump;

@Inject
CoffeeMaker(CoffeeLogger logger, Lazy<Heater> heater, Pump pump) {
this.logger = logger;
this.heater = heater;
this.pump = pump;
}

public void brew() {
heater.get().on();
pump.pump();
logger.log(" [_]P coffee! [_]P ");
heater.get().off();
}
}
47 changes: 47 additions & 0 deletions examples/bazel/java/example/common/ElectricHeater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2020 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.common;

import javax.inject.Inject;

/** An electric heater to heat the coffee. */
public class ElectricHeater implements Heater {

private final CoffeeLogger logger;
private boolean heating;

@Inject
ElectricHeater(CoffeeLogger logger) {
this.logger = logger;
}

@Override
public void on() {
this.heating = true;
logger.log("~ ~ ~ heating ~ ~ ~");
}

@Override
public void off() {
this.heating = false;
}

@Override
public boolean isHot() {
return heating;
}
}
24 changes: 24 additions & 0 deletions examples/bazel/java/example/common/Heater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2020 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.common;

/** A heater to heat the coffee. */
public interface Heater {
void on();
void off();
boolean isHot();
}
22 changes: 22 additions & 0 deletions examples/bazel/java/example/common/Pump.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2020 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.common;

/** A pump to pump the coffee. */
public interface Pump {
void pump();
}
38 changes: 38 additions & 0 deletions examples/bazel/java/example/common/Thermosiphon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2020 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.common;

import javax.inject.Inject;

/** A thermosiphon to pump the coffee. */
public class Thermosiphon implements Pump {
private final CoffeeLogger logger;
private final Heater heater;

@Inject
Thermosiphon(CoffeeLogger logger, Heater heater) {
this.logger = logger;
this.heater = heater;
}

@Override
public void pump() {
if (heater.isHot()) {
logger.log("=> => pumping => =>");
}
}
}
23 changes: 23 additions & 0 deletions examples/bazel/java/example/dagger/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2020 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

java_binary(
name = "dagger",
srcs = glob(["*.java"]),
main_class = "dagger.coffee.CoffeeApp",
deps = [
"//:dagger",
"//java/example/common",
],
)
Loading

0 comments on commit 9494f40

Please sign in to comment.