<commit_before>//
//  DeployCluster.swift
//  Flock
//
//  Created by <NAME> on 12/28/15.
//  Copyright © 2015 jakeheis. All rights reserved.
//

import Foundation

extension Flock {
    public static let Deploy: [Task] = [
        DeployTask(),
        GitTask(),
        BuildTask(),
        LinkTask()
    ]
}

extension Config {
    public static var projectName = "" // Must be supplied
    public static var executableName = "" // Must be supplied
    public static var repoURL = "" // Must be supplied
    public static var deployDirectory = "/var/www"
}

private let deploy = "deploy"

class DeployTask: Task {
    let name = deploy
    
    func run(on server: Server) throws {
        try invoke("deploy:git")
        try invoke("deploy:build")
        try invoke("deploy:link")
    }
}

class GitTask: Task {
    let name = "git"
    let namespace = deploy
    
    func run(on server: Server) throws {
        guard !Config.projectName.isEmpty else {
            throw TaskError.error("You must supply a projectName in your configuration")
        }
        guard !Config.repoURL.isEmpty else {
            throw TaskError.error("You must supply a repoURL in your configuration")
        }
        
        let currentTime = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "YYYYMMddHHMMSS"
        let timestamp = formatter.string(from: currentTime)
        
        let cloneDirectory = "\(Paths.releasesDirectory)/\(timestamp)"
        try server.execute("git clone \(Config.repoURL) \(cloneDirectory)")
        try server.execute("ln -sfn \(cloneDirectory) \(Paths.nextDirectory)")
    }
}

class BuildTask: Task {
    let name = "build"
    let namespace = deploy
    
    func run(on server: Server) throws {
        print("Building swift project")
        
        let buildPath: String
        if server.directoryExists(Paths.nextDirectory) {
            buildPath = Paths.nextDirectory
        } else {
            buildPath = Paths.currentDirectory
        }
        try server.within(buildPath) {
            try server.execute("swift build")
        }
    }
}

class LinkTask: Task {
    let name = "link"
    let namespace = deploy
    
    func run(on server: Server) throws {
        guard let nextDestination = try server.capture("readlink \(Paths.nextDirectory)") else {
            throw TaskError.error("Couldn't find location of next directory to link - try running full `flock deploy`")
        }
        try server.execute("ln -sfn \(nextDestination) \(Paths.currentDirectory)")
        try server.execute("rm \(Paths.nextDirectory)")
    }
}<commit_msg>Trim readlink contents<commit_after>//
//  DeployCluster.swift
//  Flock
//
//  Created by <NAME> on 12/28/15.
//  Copyright © 2015 jakeheis. All rights reserved.
//

import Foundation

extension Flock {
    public static let Deploy: [Task] = [
        DeployTask(),
        GitTask(),
        BuildTask(),
        LinkTask()
    ]
}

extension Config {
    public static var projectName = "" // Must be supplied
    public static var executableName = "" // Must be supplied
    public static var repoURL = "" // Must be supplied
    public static var deployDirectory = "/var/www"
}

private let deploy = "deploy"

class DeployTask: Task {
    let name = deploy
    
    func run(on server: Server) throws {
        try invoke("deploy:git")
        try invoke("deploy:build")
        try invoke("deploy:link")
    }
}

class GitTask: Task {
    let name = "git"
    let namespace = deploy
    
    func run(on server: Server) throws {
        guard !Config.projectName.isEmpty else {
            throw TaskError.error("You must supply a projectName in your configuration")
        }
        guard !Config.repoURL.isEmpty else {
            throw TaskError.error("You must supply a repoURL in your configuration")
        }
        
        let currentTime = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "YYYYMMddHHMMSS"
        let timestamp = formatter.string(from: currentTime)
        
        let cloneDirectory = "\(Paths.releasesDirectory)/\(timestamp)"
        try server.execute("git clone \(Config.repoURL) \(cloneDirectory)")
        try server.execute("ln -sfn \(cloneDirectory) \(Paths.nextDirectory)")
    }
}

class BuildTask: Task {
    let name = "build"
    let namespace = deploy
    
    func run(on server: Server) throws {
        print("Building swift project")
        
        let buildPath: String
        if server.directoryExists(Paths.nextDirectory) {
            buildPath = Paths.nextDirectory
        } else {
            buildPath = Paths.currentDirectory
        }
        try server.within(buildPath) {
            try server.execute("swift build")
        }
    }
}

class LinkTask: Task {
    let name = "link"
    let namespace = deploy
    
    func run(on server: Server) throws {
        guard let nextDestination = try server.capture("readlink \(Paths.nextDirectory)")?.trimmingCharacters(in: .whitespacesAndNewlines) else {
            throw TaskError.error("Couldn't find location of next directory to link - try running full `flock deploy`")
        }
        try server.execute("ln -sfn \(nextDestination) \(Paths.currentDirectory)")
        try server.execute("rm \(Paths.nextDirectory)")
    }
}
{'fraction_non_alphanumeric': 0.10355253212396069, 'fraction_numerical': 0.003779289493575208, 'mean_word_length': 2.308125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4088102', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1438, 'n_words': 433}