P4ClientApiExtensions.swift #4

  • //
  • main/
  • guest/
  • tjuricek/
  • DocHub/
  • DocHub/
  • P4ClientApiExtensions.swift
  • View
  • Commits
  • Open Download .zip Download (3 KB)
//
//  P4ClientApiExtensions.swift
//  DocHub
//
//  Created by Tristan Juricek on 6/12/14.
//  Copyright (c) 2014 Perforce. All rights reserved.
//

import Foundation

extension P4ClientApi {

    func info() -> (NSError?, P4Info?) {
        
        var apiDelegate = P4CachingDelegate()
        
        runCommand("info", withArguments: nil, delegate: apiDelegate)
        
        if apiDelegate.error {
            return (apiDelegate.error, nil)
        }
        
        return (nil, P4Info(map: apiDelegate.tagged[0]))
    }
    
    // Executes login
    func loginToken(password:String, localHost:String) -> (NSError?, String) {
        
        var apiDelegate = P4CachingDelegate()
        
        apiDelegate.inputText = password
        
        println("localHost: \(localHost)")
        
        runCommand("login", withArguments: ["-p", "-h", localHost], delegate: apiDelegate)
        
        if apiDelegate.error {
            return (apiDelegate.error, "")
        }
        
        let token = apiDelegate.messages[0]
        
        return (nil, token)
    }
    
    // Executes the "p4 depots" command
    func depots() -> (NSError?, P4Depot[]) {
        
        var apiDelegate = P4CachingDelegate()
        
        runCommand("depots", withArguments: nil, delegate: apiDelegate)
        
        if apiDelegate.error {
            return (apiDelegate.error, [])
        }
        
        let depots = apiDelegate.tagged.map({
            (dict) -> P4Depot in
            P4Depot(time: dict["time"]!.toInt()!,
                type: dict["type"]!,
                desc: dict["desc"]!,
                name: dict["name"]!)
            })
        
        return (nil, depots)
    }
    
    func dirs(paths:String...) -> (NSError?, P4Dir[]) {
        
        let delegate = P4CachingDelegate()
        
        runCommand("dirs", withArguments: paths, delegate: delegate)
        
        if delegate.error {
            return (delegate.error, [])
        }
        
        let dirs = delegate.tagged.map({ P4Dir(depotPath: $0["dir"]!) })
        
        return (nil, dirs)
    }
    
    func files(paths:String...) -> (NSError?, P4File[]) {
        
        let delegate = P4CachingDelegate()
        
        runCommand("files", withArguments: paths, delegate: delegate)
        
        if let err = delegate.error {
            var isErr = true
            // If there are no files here, it's returned as an "error" from 
            // the p4api, which is really not an error, just no files.
            if let info = err.userInfo {
                if info["P4ErrorGeneric"] as Int == 17 &&
                    info["P4ErrorSeverity"] as Int == 2 &&
                    info["P4ErrorSubCode"] as Int == 375 &&
                    info["P4ErrorSubsystem"] as Int == 6 {
                    isErr = false
                }
            }
            if isErr {
                return (err, [])                
            }
        }

        var files = delegate.tagged.map({
            P4File(depotPath: $0["depotFile"]!,
                revision: $0["change"]!.toInt()!,
                action: $0["action"]!,
                type: $0["type"]!)
        })
        
        return (nil, files)
    }
}
# Change User Description Committed
#4 9583 tjuricek Set up basic directory browsing.
#3 9547 tjuricek Use p4 info to grab the clientAddress which is then used to restrict the hostname token correctly from p4d.

(Idea via dscheirer)
#2 9475 tjuricek Set up the positive workflow for AddPerforceController to get a login token.

This required adding another couple of methods to provide input to the p4api (which were surprisingly absent from the earlier system).
#1 9397 tjuricek Basic p4d interaction with p4 using Swift