• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.File;
26 import java.io.IOException;
27 import java.io.UncheckedIOException;
28 import java.nio.file.Files;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.stream.Stream;
33 
34 /** A module info provider that accepts files that contains package names. */
35 public final class PackagesFileModuleInfoProvider implements ModuleInfoProvider {
36     @VisibleForTesting static final String PACKAGES_FILE_OPTION = "packages-file";
37     @VisibleForTesting static final String COMMENT_LINE_PREFIX = "#";
38     @VisibleForTesting static final String PACKAGE_PLACEHOLDER = "{package}";
39 
40     @Option(
41             name = PACKAGES_FILE_OPTION,
42             description =
43                     "File paths that contain package names separated by newline characters."
44                         + " Comment lines are supported only if the lines start with double slash."
45                         + " Trailing comments are not supported. Empty lines are ignored.",
46             importance = Importance.NEVER)
47     private final Set<File> mPackagesFiles = new HashSet<>();
48 
49     @Override
get(IConfiguration configuration)50     public Stream<ModuleInfoProvider.ModuleInfo> get(IConfiguration configuration)
51             throws IOException {
52         ModuleTemplate moduleTemplate = ModuleTemplate.loadFrom(configuration);
53 
54         return mPackagesFiles.stream()
55                 .flatMap(
56                         file -> {
57                             try {
58                                 return Files.readAllLines(file.toPath()).stream();
59                             } catch (IOException e) {
60                                 throw new UncheckedIOException(e);
61                             }
62                         })
63                 .map(String::trim)
64                 .filter(PackagesFileModuleInfoProvider::isNotCommentLine)
65                 .distinct()
66                 .map(
67                         packageName ->
68                                 new ModuleInfoProvider.ModuleInfo(
69                                         packageName,
70                                         moduleTemplate.substitute(
71                                                 packageName,
72                                                 Map.of(PACKAGE_PLACEHOLDER, packageName))));
73     }
74 
75     private static boolean isNotCommentLine(String text) {
76         // Check the text is not an empty string and not a comment line.
77         return !text.isEmpty() && !text.startsWith(COMMENT_LINE_PREFIX);
78     }
79 }
80