John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.packages; |
| 15 | |
| 16 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 17 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 18 | |
| 19 | /** The file (BUILD, WORKSPACE, etc.) that defines this package, referred to as the "build file". */ |
| 20 | public enum BuildFileName { |
| 21 | WORKSPACE("WORKSPACE") { |
| 22 | @Override |
| 23 | public PathFragment getBuildFileFragment(PackageIdentifier packageIdentifier) { |
| 24 | return getFilenameFragment(); |
| 25 | } |
| 26 | }, |
Yun Peng | 0af987b | 2019-11-11 07:19:54 -0800 | [diff] [blame] | 27 | WORKSPACE_DOT_BAZEL("WORKSPACE.bazel") { |
| 28 | @Override |
| 29 | public PathFragment getBuildFileFragment(PackageIdentifier packageIdentifier) { |
| 30 | return getFilenameFragment(); |
| 31 | } |
| 32 | }, |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 33 | BUILD("BUILD") { |
| 34 | @Override |
| 35 | public PathFragment getBuildFileFragment(PackageIdentifier packageIdentifier) { |
| 36 | return packageIdentifier.getPackageFragment().getRelative(getFilenameFragment()); |
| 37 | } |
| 38 | }, |
| 39 | BUILD_DOT_BAZEL("BUILD.bazel") { |
| 40 | @Override |
| 41 | public PathFragment getBuildFileFragment(PackageIdentifier packageIdentifier) { |
| 42 | return packageIdentifier.getPackageFragment().getRelative(getFilenameFragment()); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | private static final BuildFileName[] VALUES = BuildFileName.values(); |
| 47 | |
| 48 | private final PathFragment filenameFragment; |
| 49 | |
| 50 | private BuildFileName(String filename) { |
| 51 | this.filenameFragment = PathFragment.create(filename); |
| 52 | } |
| 53 | |
| 54 | public PathFragment getFilenameFragment() { |
| 55 | return filenameFragment; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns a {@link PathFragment} to the build file that defines the package. |
| 60 | * |
| 61 | * @param packageIdentifier the identifier for this package |
| 62 | */ |
| 63 | public abstract PathFragment getBuildFileFragment(PackageIdentifier packageIdentifier); |
| 64 | |
| 65 | public static BuildFileName lookupByOrdinal(int ordinal) { |
| 66 | return VALUES[ordinal]; |
| 67 | } |
| 68 | } |