|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * These are shared settings common to all Google Ads Java library subprojects. |
| 17 | + */ |
| 18 | + |
| 19 | +plugins { |
| 20 | + id 'java-library' |
| 21 | + id 'maven-publish' |
| 22 | +} |
| 23 | + |
| 24 | +repositories { |
| 25 | + mavenLocal() |
| 26 | + mavenCentral() |
| 27 | +} |
| 28 | + |
| 29 | +group = 'com.google.api-ads' |
| 30 | +version = '11.0.1-SNAPSHOT' |
| 31 | +java.sourceCompatibility = JavaVersion.VERSION_1_8 |
| 32 | +java.targetCompatibility = JavaVersion.VERSION_1_8 |
| 33 | + |
| 34 | +java { |
| 35 | + withSourcesJar() |
| 36 | +} |
| 37 | + |
| 38 | +publishing { |
| 39 | + publications { |
| 40 | + maven(MavenPublication) { |
| 41 | + from(components.java) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +tasks.withType(JavaCompile) { |
| 47 | + options.encoding = 'UTF-8' |
| 48 | +} |
| 49 | + |
| 50 | +dependencies { |
| 51 | + implementation 'com.google.guava:guava:26.0-android' |
| 52 | + implementation 'com.google.auto.service:auto-service:1.0-rc2' |
| 53 | + implementation 'com.google.api:gax:1.60.1' |
| 54 | + implementation 'com.google.api:gax-grpc:1.60.1' |
| 55 | + implementation 'com.google.protobuf:protobuf-java:3.14.0' |
| 56 | + annotationProcessor 'com.google.auto.service:auto-service:1.0-rc2' |
| 57 | + testImplementation 'junit:junit:4.13.1' |
| 58 | +} |
| 59 | + |
| 60 | +public class ExampleRunnerTask extends JavaExec { |
| 61 | + |
| 62 | + @Input |
| 63 | + public String basePackage = 'com.google.ads.googleads.examples.' |
| 64 | + |
| 65 | + @Optional |
| 66 | + @Input |
| 67 | + private String exampleArguments |
| 68 | + |
| 69 | + public ExampleRunnerTask() { |
| 70 | + group = 'Execution' |
| 71 | + description = 'Run a Google Ads API example.' |
| 72 | + errorOutput = System.err |
| 73 | + } |
| 74 | + |
| 75 | + @Option(option = 'example', description = 'Sets the example to launch and' + |
| 76 | + ' any arguments. Required for execution. E.g. ' + |
| 77 | + '"basicoperations.GetCampaigns --customerId 1234567890"') |
| 78 | + public void setExampleArguments(String exampleArguments) { |
| 79 | + this.exampleArguments = exampleArguments.trim() |
| 80 | + int firstSpaceIndex = this.exampleArguments.indexOf(' ') |
| 81 | + |
| 82 | + // No additional arguments were passed, just the example name. |
| 83 | + if (firstSpaceIndex == -1) { |
| 84 | + main = basePackage + this.exampleArguments |
| 85 | + } |
| 86 | + // Otherwise, separate the input and set the arguments to pass to the |
| 87 | + // main class. |
| 88 | + else { |
| 89 | + main = basePackage + this.exampleArguments[0..firstSpaceIndex - 1] |
| 90 | + argsString(exampleArguments[(firstSpaceIndex + 1)..-1]) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @TaskAction |
| 95 | + @Override |
| 96 | + public void exec() { |
| 97 | + if (!(exampleArguments?.trim())) { |
| 98 | + throw new GradleException('\033[0;31mMissing example!\033[0m ' + |
| 99 | + 'Please rerun with one provided, e.g. ' + |
| 100 | + '\033[0;35m--example="basicoperations.GetCampaigns"\033[0m') |
| 101 | + } |
| 102 | + logQuietMessage('Running example: ' + main + ', args: ' + |
| 103 | + args.toString()) |
| 104 | + try { |
| 105 | + super.exec() |
| 106 | + } |
| 107 | + catch (Exception e) { |
| 108 | + logQuietMessage('\n\033[0;31mrunExample exception!\033[0m Did ' + |
| 109 | + 'you provide a valid example identifier? E.g. ' + |
| 110 | + '\033[0;35m--example="basicoperations.GetCampaigns"\033[0m\n\n' + |
| 111 | + e.message) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public String getExampleArguments() { |
| 116 | + return exampleArguments |
| 117 | + } |
| 118 | + |
| 119 | + private void logQuietMessage(String message) { |
| 120 | + logger.quiet(message) |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments