Skip to content

Commit

Permalink
Co-locate bazel generated files
Browse files Browse the repository at this point in the history
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
brentleyjones committed Jul 9, 2024
1 parent 55fba0b commit 1af26b5
Show file tree
Hide file tree
Showing 65 changed files with 2,085 additions and 940 deletions.
51 changes: 49 additions & 2 deletions test/internal/pbxproj_partials/write_files_and_groups_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ _FILE_PATHS_FILE = mock_actions.mock_file(
_FOLDER_PATHS_FILE = mock_actions.mock_file(
"a_generator_name_pbxproj_partials/folder_paths_file",
)
_GENERATED_FILE_PATHS_FILE = mock_actions.mock_file(
"a_generator_name_pbxproj_partials/generated_file_paths_file",
)
_GENERATED_FOLDER_PATHS_FILE = mock_actions.mock_file(
"a_generator_name_pbxproj_partials/generated_folder_paths_file",
)

def _write_files_and_groups_test_impl(ctx):
env = unittest.begin(ctx)
Expand Down Expand Up @@ -48,6 +54,20 @@ def _write_files_and_groups_test_impl(ctx):
_RESOLVED_REPOSITORIES_FILE_DECLARED_FILE,
]

files = [
struct(
path = path,
is_source = True,
)
for path in ctx.attr.files
] + [
struct(
path = path,
is_source = False,
)
for path in ctx.attr.generated_files
]

# Act

(
Expand All @@ -62,10 +82,11 @@ def _write_files_and_groups_test_impl(ctx):
colorize = ctx.attr.colorize,
compile_stub_needed = ctx.attr.compile_stub_needed,
execution_root_file = ctx.attr.execution_root_file,
generator_name = "a_generator_name",
files = depset(ctx.attr.files),
files = depset(files),
file_paths = depset(ctx.attr.file_paths),
folders = depset(ctx.attr.folders),
generated_folders = depset(ctx.attr.genreated_folders),
generator_name = "a_generator_name",
install_path = ctx.attr.install_path,
project_options = ctx.attr.project_options,
selected_model_versions_file = ctx.attr.selected_model_versions_file,
Expand Down Expand Up @@ -164,6 +185,8 @@ write_files_and_groups_test = unittest.make(
"file_paths": attr.string_list(mandatory = True),
"files": attr.string_list(mandatory = True),
"folders": attr.string_list(mandatory = True),
"generated_files": attr.string_list(mandatory = True),
"generated_folders": attr.string_list(mandatory = True),
"install_path": attr.string(mandatory = True),
"project_options": attr.string_dict(mandatory = True),
"selected_model_versions_file": attr.string(mandatory = True),
Expand Down Expand Up @@ -196,6 +219,8 @@ def write_files_and_groups_test_suite(name):
files = [],
file_paths = [],
folders = [],
generated_files = [],
generated_folders = [],
install_path,
project_options,
selected_model_versions_file,
Expand All @@ -216,6 +241,8 @@ def write_files_and_groups_test_suite(name):
files = files,
file_paths = file_paths,
folders = folders,
generated_files = generated_files,
generated_folders = generated_folders,
install_path = install_path,
project_options = project_options,
selected_model_versions_file = selected_model_versions_file,
Expand Down Expand Up @@ -273,6 +300,10 @@ def write_files_and_groups_test_suite(name):
_FILE_PATHS_FILE.path,
# folderPathsFile
_FOLDER_PATHS_FILE.path,
# generatedFilePathsFile
_GENERATED_FILE_PATHS_FILE.path,
# generatedFolderPathsFile
_GENERATED_FOLDER_PATHS_FILE.path,
# developmentRegion
"en",
# useBaseInternationalization
Expand Down Expand Up @@ -313,6 +344,14 @@ def write_files_and_groups_test_suite(name):
"a/path/to/a/folder",
"another/path/to/another/folder",
],
generated_files = [
"a/path/to/a/genreated/file",
"another/path/to/another/generated/file",
],
generated_folders = [
"a/path/to/a/generated/folder",
"another/path/to/another/generated/folder",
],
install_path = "best/vision.xcodeproj",
project_options = {
"development_region": "enGB",
Expand Down Expand Up @@ -349,6 +388,10 @@ def write_files_and_groups_test_suite(name):
_FILE_PATHS_FILE.path,
# folderPathsFile
_FOLDER_PATHS_FILE.path,
# generatedFilePathsFile
_GENERATED_FILE_PATHS_FILE.path,
# generatedFolderPathsFile
_GENERATED_FOLDER_PATHS_FILE.path,
# developmentRegion
"enGB",
# useBaseInternationalization
Expand All @@ -372,6 +415,10 @@ another/path/to/another/file_path.framework
_FOLDER_PATHS_FILE: """\
a/path/to/a/folder
another/path/to/another/folder
""",
_GENERATED_FOLDER_PATHS_FILE: """\
a/path/to/a/generated/folder
another/path/to/another/generated/folder
""",
},
)
Expand Down
4 changes: 4 additions & 0 deletions tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ _XCSCHEMES = [
"/tmp/pbxproj_partials/file_paths_file",
# folderPathsFile
"/tmp/pbxproj_partials/folder_paths_file",
# generatedFilePathsFile
"/tmp/pbxproj_partials/generated_file_paths_file",
# generatedFolderPathsFile
"/tmp/pbxproj_partials/generated_folder_paths_file",
# developmentRegion
"en",
# useBaseInternationalization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,48 @@ extension ElementCreator.CollectBazelPaths {
bazelPath: BazelPath,
includeSelf: Bool
) -> [BazelPath] {
switch node.kind {
switch node {
case .file:
return includeSelf ? [bazelPath] : []
case .group(let group):
var bazelPaths = group.children.flatMap { node in
case .group(_, let children):
var bazelPaths = children.flatMap { node in
return handleChildNode(node, parentBazelPath: bazelPath)
}
if includeSelf {
bazelPaths.append(bazelPath)
}
return bazelPaths
case .generatedFiles:
// Impossible to have generated files under localized or model files
fatalError()
}
}

static func handleChildNode(
_ node: PathTreeNode,
parentBazelPath: BazelPath
) -> [BazelPath] {
switch node.kind {
case .file(let isFolder):
switch node {
case .file(let name, let isFolder):
let bazelPath = BazelPath(
parent: parentBazelPath,
path: node.name,
path: name,
isFolder: isFolder
)
return [bazelPath]
case .group(let group):
case .group(let name, let children):
let bazelPath = BazelPath(
parent: parentBazelPath,
path: node.name
path: name
)
var bazelPaths = group.children.flatMap { node in
var bazelPaths = children.flatMap { node in
return handleChildNode(node, parentBazelPath: bazelPath)
}
bazelPaths.append(bazelPath)
return bazelPaths
case .generatedFiles:
// Impossible to have generated files under localized or model files
fatalError()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ extension ElementCreator {
/// - bazelPath: The `BazelPath` for the node.
/// - isGroup: `true` if this a group element (e.g. `PBXGroup`,
/// `XCVersionGroup`, etc.).
/// - specialRootGroupType: The `SpecialRootGroupType` this element is
/// under. For example, if this element is for a Bazel generated
/// file, `specialRootGroupType` will be `.bazelGenerated`. If this
/// element is for a file in the workspace, `specialRootGroupType`
/// will be `nil`.
/// - bazelPathType: The type of path that `BazelPath` represents.
/// For example, if this element is for a Bazel generated file,
/// `bazelPathType` will be `.bazelGenerated`. If this element is for
/// a file in the workspace, `bazelPathType` will be `.workspace`.
func callAsFunction(
name: String,
bazelPath: BazelPath,
isGroup: Bool,
specialRootGroupType: SpecialRootGroupType?
bazelPathType: BazelPathType,
isGroup: Bool
) -> (
elementAttributes: ElementAttributes,
resolvedRepository: ResolvedRepository?
) {
return callable(
/*name:*/ name,
/*bazelPath:*/ bazelPath,
/*bazelPathType:*/ bazelPathType,
/*isGroup:*/ isGroup,
/*specialRootGroupType:*/ specialRootGroupType,
/*executionRoot:*/ executionRoot,
/*externalDir:*/ externalDir,
/*workspace:*/ workspace,
Expand All @@ -89,8 +88,8 @@ extension ElementCreator.CreateAttributes {
typealias Callable = (
_ name: String,
_ bazelPath: BazelPath,
_ bazelPathType: BazelPathType,
_ isGroup: Bool,
_ specialRootGroupType: SpecialRootGroupType?,
_ executionRoot: String,
_ externalDir: String,
_ workspace: String,
Expand All @@ -103,8 +102,8 @@ extension ElementCreator.CreateAttributes {
static func defaultCallable(
name: String,
bazelPath: BazelPath,
bazelPathType: BazelPathType,
isGroup: Bool,
specialRootGroupType: SpecialRootGroupType?,
executionRoot: String,
externalDir: String,
workspace: String,
Expand All @@ -116,28 +115,28 @@ extension ElementCreator.CreateAttributes {
let relativePath: String.SubSequence
var absolutePath: String
let resolvedRepositoryPrefix: String?
switch specialRootGroupType {
case .legacyBazelExternal?:
switch bazelPathType {
case .workspace:
relativePath = String.SubSequence(bazelPath.path)
absolutePath = "\(workspace)/\(relativePath)"
resolvedRepositoryPrefix = nil

case .legacyBazelExternal:
// Drop "external/"
relativePath = bazelPath.path.dropFirst(9)
absolutePath = "\(externalDir)/\(relativePath)"
resolvedRepositoryPrefix = isGroup ? "./external/" : nil

case .siblingBazelExternal?:
case .siblingBazelExternal:
// Drop "../"
relativePath = bazelPath.path.dropFirst(3)
absolutePath = "\(externalDir)/\(relativePath)"
resolvedRepositoryPrefix = isGroup ? "../" : nil

case .bazelGenerated?:
case .bazelGenerated:
relativePath = String.SubSequence(bazelPath.path)
absolutePath = "\(executionRoot)/\(relativePath)"
resolvedRepositoryPrefix = nil

case nil:
relativePath = String.SubSequence(bazelPath.path)
absolutePath = "\(workspace)/\(relativePath)"
resolvedRepositoryPrefix = nil
}

guard let symlinkDest = resolveSymlink(absolutePath) else {
Expand Down Expand Up @@ -171,3 +170,10 @@ extension ElementCreator.CreateAttributes {
)
}
}

enum BazelPathType {
case bazelGenerated
case legacyBazelExternal
case siblingBazelExternal
case workspace
}
Loading

0 comments on commit 1af26b5

Please sign in to comment.