<commit_before>// Copyright 2017 The Tulsi Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

// Provides methods to patch up Bazel specific PBX objects and references before project generation.
// This patches @external container Bazel files to be relative to the exec root.
final class BazelPBXReferencePatcher {

  private let fileManager: FileManager

  init(fileManager: FileManager) {
    self.fileManager = fileManager
  }

  // Resolves the given Bazel exec-root relative path to a filesystem path.
  // This is intended to be used to resolve "@external_repo" style labels to paths usable by Xcode
  // and any other paths that must be relative to the Bazel exec root.
  private func resolvePathFromBazelExecRoot(_ path: String) -> String {
    return "\(PBXTargetGenerator.TulsiWorkspacePath)/\(path)"
  }

  // Returns true if the file reference patching was handled.
  func patchNonPresentFileReference(file: PBXFileReference,
                                    url: URL,
                                    workspaceRootURL: URL) -> Bool {
    return false
  }

  // Examines the given xcodeProject, patching any groups that were generated under Bazel's magical
  // "external" container to absolute filesystem references.
  func patchExternalRepositoryReferences(_ xcodeProject: PBXProject) {
    let mainGroup = xcodeProject.mainGroup
    guard let externalGroup = mainGroup.childGroupsByName["external"] else { return }

    // The external directory may contain files such as a WORKSPACE file, but we only patch folders
    let childGroups = externalGroup.children.filter { $0 is PBXGroup } as! [PBXGroup]

    for child in childGroups {
      let resolvedPath = resolvePathFromBazelExecRoot("external/\(child.name)")
      let newChild = mainGroup.getOrCreateChildGroupByName("@\(child.name)",
                                                           path: resolvedPath,
                                                           sourceTree: .Group)
      newChild.migrateChildrenOfGroup(child)
    }
    mainGroup.removeChild(externalGroup)<commit_msg>Resolve external source files inside the tulsi workspace<commit_after>// Copyright 2017 The Tulsi Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

// Provides methods to patch up Bazel specific PBX objects and references before project generation.
// This patches @external container Bazel files to be relative to the exec root.
final class BazelPBXReferencePatcher {

  private let fileManager: FileManager

  init(fileManager: FileManager) {
    self.fileManager = fileManager
  }

  // Resolves the given Bazel exec-root relative path to a filesystem path.
  // This is intended to be used to resolve "@external_repo" style labels to paths usable by Xcode
  // and any other paths that must be relative to the Bazel exec root.
  private func resolvePathFromBazelExecRoot(_ xcodeProject: PBXProject, _ path: String) -> String {
    return "\(xcodeProject.name).xcodeproj/.tulsi/\(PBXTargetGenerator.TulsiWorkspacePath)/\(path)"
  }

  // Returns true if the file reference patching was handled.
  func patchNonPresentFileReference(file: PBXFileReference,
                                    url: URL,
                                    workspaceRootURL: URL) -> Bool {
    return false
  }

  // Examines the given xcodeProject, patching any groups that were generated under Bazel's magical
  // "external" container to absolute filesystem references.
  func patchExternalRepositoryReferences(_ xcodeProject: PBXProject) {
    let mainGroup = xcodeProject.mainGroup
    guard let externalGroup = mainGroup.childGroupsByName["external"] else { return }

    // The external directory may contain files such as a WORKSPACE file, but we only patch folders
    let childGroups = externalGroup.children.filter { $0 is PBXGroup } as! [PBXGroup]

    for child in childGroups {
      let resolvedPath = resolvePathFromBazelExecRoot(xcodeProject, "external/\(child.name)")
      let newChild = mainGroup.getOrCreateChildGroupByName("@\(child.name)",
                                                           path: resolvedPath,
                                                           sourceTree: .SourceRoot)
      newChild.migrateChildrenOfGroup(child)
    }
    mainGroup.removeChild(externalGroup)
{'fraction_non_alphanumeric': 0.0699443413729128, 'fraction_numerical': 0.003339517625231911, 'mean_word_length': 2.895231213872832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4620401', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1300, 'n_words': 541}