<commit_before>/*
  swiftup
  The Swift toolchain installer

  Copyright (C) 2016-present swiftup Authors

  Authors:
    <NAME>
*/

import libNix
import Foundation
import Environment

struct Toolchains {
  var versioningFolder: String {
    if let path = Env["SWIFTUP_ROOT"] {
      return path.addingPath("versions")
    }

    return ""
  }

  var globalVersion: String {
    get {
      //return (try? String(contentsOfFile: Env["SWIFTUP_ROOT"]!.addingPath("version"), encoding: .utf8)) ?? ""
      return (try? getContentsOf(file: Env["SWIFTUP_ROOT"]!.addingPath("version"))) ?? ""
    }

    set {
      if isInstalled(version: newValue) {
        //try? newValue.write(toFile: Env["SWIFTUP_ROOT"]!.addingPath("version"), atomically: true, encoding: .utf8)
        try? writeTo(file: Env["SWIFTUP_ROOT"]!.addingPath("version"), with: newValue)
      } else {
        print("Version \(newValue) is not installed!")
      }
    }
  }

  func installedVersions() -> [String]? {
    //let paths = try? FileManager.default.contentsOfDirectory(atPath: versioningFolder)
    let paths = try? contentsOfDirectory(atPath: versioningFolder)
    return paths
  }

  func isInstalled(version: String) -> Bool {
    if let versions = installedVersions() {
      return versions.contains {
        return ($0 == version) ? true : false
      }
    }

    return false
  }

  func getBinaryPath() -> String {
    return versioningFolder.addingPath(globalVersion).addingPath("usr/bin/")
  }

  mutating func installToolchain(version: String) {
    let distribution = Distribution(target: version)

    guard !isInstalled(version: distribution.versionName) else {
      print("Version \(distribution.versionName) is already installed!")
      return
    }

    print("Will install version \(distribution.versionName)")
    installTarToolchain(distribution: distribution)
    print("Version \(distribution.versionName) has been installed!")

    globalVersion = distribution.versionName
    rehashToolchain()
  }

  func installTarToolchain(distribution: Distribution) {
    let tempDir = getTempDir().addingPath("swiftup-\(distribution.versionName)")
    let tempFile = tempDir.addingPath("toolchain.tar.gz")
    let tempEFile = tempDir.addingPath("\(distribution.fileName)")
    let installDir = Toolchains().versioningFolder.addingPath("\(distribution.versionName)")

    // Create temp direcyory
    _ = try! createDirectory(atPath: tempDir)

    print("Downloading toolchain \(distribution.downloadUrl)")

    run(program: "/usr/bin/curl", arguments: ["-C", "-", "\(distribution.downloadUrl)", "-o", "\(tempFile)"])

    guard fileExists(atPath: tempFile) else {
      print("Error occurred when downloading the toolchain")
      exit(1)
    }

    run(program: "/bin/tar", arguments: ["xzf", "\(tempFile)", "-C", "\(tempDir)"])

    guard fileExists(atPath: tempEFile) else {
      print("Error occurred when extracting the toolchain")
      exit(1)
    }

    moveItem(src: tempEFile, dest: installDir)

    guard fileExists(atPath: installDir) else {
      print("Error occurred when installing the toolchain")
      exit(1)
    }
  }

  func rehashToolchain() {
    let binaryDir = versioningFolder.addingPath(globalVersion).addingPath("usr/bin")

    let binaries = try! contentsOfDirectory(atPath: binaryDir)

    func createShims(name: String) {
      let script = "#!/usr/bin/env bash\nset -e\nexec `swiftup which $0` $@\n"
      let shims = Env["SWIFTUP_ROOT"]!.addingPath("shims/\(name)")
      try! writeTo(file: shims, with: script)
      run(program: "/bin/chmod", arguments: ["+x", shims])
    }

    for binary in binaries {
      createShims(name: binary)
    }
  }
}<commit_msg>Fix a shim scripting error<commit_after>/*
  swiftup
  The Swift toolchain installer

  Copyright (C) 2016-present swiftup Authors

  Authors:
    <NAME>
*/

import libNix
import Foundation
import Environment

struct Toolchains {
  var versioningFolder: String {
    if let path = Env["SWIFTUP_ROOT"] {
      return path.addingPath("versions")
    }

    return ""
  }

  var globalVersion: String {
    get {
      //return (try? String(contentsOfFile: Env["SWIFTUP_ROOT"]!.addingPath("version"), encoding: .utf8)) ?? ""
      return (try? getContentsOf(file: Env["SWIFTUP_ROOT"]!.addingPath("version"))) ?? ""
    }

    set {
      if isInstalled(version: newValue) {
        //try? newValue.write(toFile: Env["SWIFTUP_ROOT"]!.addingPath("version"), atomically: true, encoding: .utf8)
        try? writeTo(file: Env["SWIFTUP_ROOT"]!.addingPath("version"), with: newValue)
      } else {
        print("Version \(newValue) is not installed!")
      }
    }
  }

  func installedVersions() -> [String]? {
    //let paths = try? FileManager.default.contentsOfDirectory(atPath: versioningFolder)
    let paths = try? contentsOfDirectory(atPath: versioningFolder)
    return paths
  }

  func isInstalled(version: String) -> Bool {
    if let versions = installedVersions() {
      return versions.contains {
        return ($0 == version) ? true : false
      }
    }

    return false
  }

  func getBinaryPath() -> String {
    return versioningFolder.addingPath(globalVersion).addingPath("usr/bin/")
  }

  mutating func installToolchain(version: String) {
    let distribution = Distribution(target: version)

    guard !isInstalled(version: distribution.versionName) else {
      print("Version \(distribution.versionName) is already installed!")
      return
    }

    print("Will install version \(distribution.versionName)")
    installTarToolchain(distribution: distribution)
    print("Version \(distribution.versionName) has been installed!")

    globalVersion = distribution.versionName
    rehashToolchain()
  }

  func installTarToolchain(distribution: Distribution) {
    let tempDir = getTempDir().addingPath("swiftup-\(distribution.versionName)")
    let tempFile = tempDir.addingPath("toolchain.tar.gz")
    let tempEFile = tempDir.addingPath("\(distribution.fileName)")
    let installDir = Toolchains().versioningFolder.addingPath("\(distribution.versionName)")

    // Create temp direcyory
    _ = try! createDirectory(atPath: tempDir)

    print("Downloading toolchain \(distribution.downloadUrl)")

    run(program: "/usr/bin/curl", arguments: ["-C", "-", "\(distribution.downloadUrl)", "-o", "\(tempFile)"])

    guard fileExists(atPath: tempFile) else {
      print("Error occurred when downloading the toolchain")
      exit(1)
    }

    run(program: "/bin/tar", arguments: ["xzf", "\(tempFile)", "-C", "\(tempDir)"])

    guard fileExists(atPath: tempEFile) else {
      print("Error occurred when extracting the toolchain")
      exit(1)
    }

    moveItem(src: tempEFile, dest: installDir)

    guard fileExists(atPath: installDir) else {
      print("Error occurred when installing the toolchain")
      exit(1)
    }
  }

  func rehashToolchain() {
    let binaryDir = versioningFolder.addingPath(globalVersion).addingPath("usr/bin")

    let binaries = try! contentsOfDirectory(atPath: binaryDir)

    func createShims(name: String) {
      let script = "#!/usr/bin/env bash\nset -e\nexec `swiftup which \(name)` $@\n"
      let shims = Env["SWIFTUP_ROOT"]!.addingPath("shims/\(name)")
      try! writeTo(file: shims, with: script)
      run(program: "/bin/chmod", arguments: ["+x", shims])
    }

    for binary in binaries {
      createShims(name: binary)
    }
  }
}
{'fraction_non_alphanumeric': 0.1344333243170138, 'fraction_numerical': 0.0028401406545847984, 'mean_word_length': 3.525703794369645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '103557', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2178, 'n_words': 508}