1 /* 2 * Copyright (C) 2020 The Android Open Source Project 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 * http://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 17 package com.android.csuite.core; 18 19 import com.android.tradefed.config.IConfiguration; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.config.Option.Importance; 22 23 import com.google.common.annotations.VisibleForTesting; 24 25 import java.io.IOException; 26 import java.util.HashSet; 27 import java.util.Map; 28 import java.util.Set; 29 import java.util.stream.Stream; 30 31 /** A module info provider that accepts package names and files that contains package names. */ 32 public final class PackageModuleInfoProvider implements ModuleInfoProvider { 33 34 @VisibleForTesting static final String ALT_PACKAGE_OPTION = "alt-package"; 35 @VisibleForTesting static final String PACKAGE_OPTION = "package"; 36 @VisibleForTesting static final String PACKAGE_PLACEHOLDER = "{package}"; 37 @VisibleForTesting static final String USE_ALT_PACKAGE_OPTION = "use-alt-package"; 38 39 @Option( 40 name = USE_ALT_PACKAGE_OPTION, 41 description = "Use --alt-package to specify app package names.", 42 importance = Importance.NEVER) 43 private boolean mUseAltPackage = false; 44 45 @Option( 46 name = ALT_PACKAGE_OPTION, 47 description = 48 "App package names. This is an alternative of '--package' in case of a name" 49 + " conflict of options.", 50 importance = Importance.NEVER) 51 private final Set<String> mAltPackages = new HashSet<>(); 52 53 @Option( 54 name = PACKAGE_OPTION, 55 description = "App package names.", 56 importance = Importance.NEVER) 57 private final Set<String> mPackages = new HashSet<>(); 58 59 @Override get(IConfiguration configuration)60 public Stream<ModuleInfoProvider.ModuleInfo> get(IConfiguration configuration) 61 throws IOException { 62 ModuleTemplate moduleTemplate = ModuleTemplate.loadFrom(configuration); 63 Set<String> packages = mUseAltPackage ? mAltPackages : mPackages; 64 65 return packages.stream() 66 .distinct() 67 .map( 68 packageName -> 69 new ModuleInfoProvider.ModuleInfo( 70 packageName, 71 moduleTemplate.substitute( 72 packageName, 73 Map.of(PACKAGE_PLACEHOLDER, packageName)))); 74 } 75 } 76