AVRSack/AVRsack/ASFileTree.swift

329 lines
10 KiB
Swift
Raw Normal View History

2014-11-16 18:39:32 +00:00
//
// ASFileTree.swift
// AVRsack
//
// Created by Matthias Neeracher on 11/16/14.
// Copyright (c) 2014 Aere Perennius. All rights reserved.
//
import Foundation
enum ASFileType : String {
case Unknown = ""
case Header = "source.h"
case CFile = "source.c"
case Arduino = "source.ino"
case CppFile = "source.c++"
case AsmFile = "source.asm"
case Markdown = "doc.md"
static func guessForURL(url: NSURL) -> ASFileType {
switch url.pathExtension!.lowercaseString {
2014-11-16 18:39:32 +00:00
case "hpp", "hh", "h":
return .Header
case "c":
return .CFile
case "ino":
return .Arduino
case "cpp", "c++", "cxx", "cc":
return .CppFile
case "s":
return .AsmFile
case "md":
return .Markdown
default:
return .Unknown
}
}
var aceMode : ACEMode {
switch self {
case .Header,.CFile,.CppFile,.Arduino:
2015-03-14 20:15:27 +00:00
return .CPP
2014-11-16 18:39:32 +00:00
case .Markdown:
2015-03-14 20:15:27 +00:00
return .Markdown
2014-11-16 18:39:32 +00:00
default:
2015-03-14 20:15:27 +00:00
return .Text
2014-11-16 18:39:32 +00:00
}
}
}
2014-11-17 01:29:55 +00:00
private let kTypeKey = "Type"
private let kNodeTypeProject = "Project"
private let kNodeTypeGroup = "Group"
private let kNodeTypeFile = "File"
private let kNameKey = "Name"
2015-11-16 01:56:33 +00:00
class ASFileNode {
2015-03-16 04:39:09 +00:00
var name : String
init(name: String) {
self.name = name
}
2014-11-16 18:39:32 +00:00
func nodeName() -> String {
return ""
}
func apply(closure:(ASFileNode)->()) {
closure(self)
}
2015-02-14 16:43:20 +00:00
func propertyList(rootPath: String) -> AnyObject {
2014-11-17 01:29:55 +00:00
return ""
}
class func readPropertyList(prop: NSDictionary, rootURL: NSURL) -> ASFileNode {
2015-02-14 16:43:20 +00:00
switch prop[kTypeKey] as! String {
2014-11-17 01:29:55 +00:00
case kNodeTypeProject:
return ASProject(prop, withRootURL:rootURL)
case kNodeTypeGroup:
return ASFileGroup(prop, withRootURL:rootURL)
case kNodeTypeFile:
return ASFileItem(prop, withRootURL:rootURL)
default:
assertionFailure("Undefined item type in file hierarchy")
2015-04-04 21:43:47 +00:00
abort()
2014-11-17 01:29:55 +00:00
}
}
2015-02-14 16:43:20 +00:00
func paths(rootPath: String) -> [String] {
return [String]()
2014-11-28 13:18:53 +00:00
}
2015-01-11 01:53:50 +00:00
func exists() -> Bool {
return true
}
2015-03-16 04:39:09 +00:00
func modDate() -> NSDate? {
return nil;
}
func revision() -> String? {
return nil;
}
2014-11-16 18:39:32 +00:00
}
2014-12-01 02:34:53 +00:00
class ASLogNode : ASFileNode {
var path : String
init(name: String, path: String) {
self.path = path
2015-03-16 04:39:09 +00:00
super.init(name: name)
2014-12-01 02:34:53 +00:00
}
override func nodeName() -> String {
return "📜 "+name
}
}
2014-11-16 18:39:32 +00:00
class ASFileGroup : ASFileNode {
var children : [ASFileNode]
var expanded : Bool
2014-11-17 01:29:55 +00:00
private let kChildrenKey = "Children"
private let kExpandedKey = "Expanded"
private var kNodeType : String { return kNodeTypeGroup }
2015-03-16 04:39:09 +00:00
override init(name: String = "") {
2014-11-16 18:39:32 +00:00
self.children = []
self.expanded = true
2015-03-16 04:39:09 +00:00
super.init(name: name)
2014-11-16 18:39:32 +00:00
}
2014-11-17 01:29:55 +00:00
init(_ prop: NSDictionary, withRootURL rootURL: NSURL) {
2015-02-14 16:43:20 +00:00
expanded = prop[kExpandedKey] as! Bool
2014-11-17 01:29:55 +00:00
children = []
2015-02-14 16:43:20 +00:00
for child in (prop[kChildrenKey] as! [NSDictionary]) {
children.append(ASFileNode.readPropertyList(child, rootURL: rootURL))
2014-11-17 01:29:55 +00:00
}
2015-03-16 04:39:09 +00:00
super.init(name: prop[kNameKey] as! String)
2014-11-16 18:39:32 +00:00
}
override func nodeName() -> String {
return (expanded ? "📂" : "📁")+" "+name
}
override func apply(closure: (ASFileNode) -> ()) {
super.apply(closure)
for child in children {
child.apply(closure)
}
}
2015-02-14 16:43:20 +00:00
func childrenPropertyList(rootPath: String) -> [AnyObject] {
2014-11-17 01:29:55 +00:00
return children.map() { (node) in node.propertyList(rootPath) }
}
2015-02-14 16:43:20 +00:00
override func propertyList(rootPath: String) -> AnyObject {
2014-11-17 01:29:55 +00:00
return [kTypeKey: kNodeType, kNameKey: name, kExpandedKey: expanded,
kChildrenKey: childrenPropertyList(rootPath)]
}
2015-02-14 16:43:20 +00:00
override func paths(rootPath: String) -> [String] {
var allPaths = [String]()
2014-11-28 13:18:53 +00:00
for child in children {
allPaths += child.paths(rootPath)
}
return allPaths
}
}
class ASProject : ASFileGroup {
2014-11-17 01:29:55 +00:00
override private var kNodeType : String { return kNodeTypeProject }
2015-04-29 03:21:23 +00:00
override init(name: String = "") {
super.init(name: name)
}
override init(_ prop: NSDictionary, withRootURL rootURL: NSURL) {
super.init(prop, withRootURL:rootURL)
name = rootURL.lastPathComponent!
}
override func nodeName() -> String {
return "📘 "+name
}
2014-11-16 18:39:32 +00:00
}
class ASFileItem : ASFileNode {
var url : NSURL
var type : ASFileType
2014-11-17 01:29:55 +00:00
private let kPathKey = "Path"
private let kKindKey = "Kind"
2014-11-16 18:39:32 +00:00
init(url: NSURL, type: ASFileType) {
self.url = url
self.type = type
2015-03-16 04:39:09 +00:00
super.init(name:url.lastPathComponent!)
2014-11-16 18:39:32 +00:00
}
2014-11-17 01:29:55 +00:00
init(_ prop: NSDictionary, withRootURL rootURL: NSURL) {
2015-02-14 16:43:20 +00:00
type = ASFileType(rawValue: prop[kKindKey] as! String)!
if let relativeURL = NSURL(string: prop[kPathKey] as! String, relativeToURL: rootURL) {
url = relativeURL.URLByStandardizingPath!
2014-12-22 06:00:05 +00:00
} else {
2015-11-16 01:56:33 +00:00
url = NSURL(fileURLWithPath:(prop[kPathKey] as! String)).URLByStandardizingPath!
2014-12-22 06:00:05 +00:00
}
2015-04-29 03:21:23 +00:00
if !url.checkResourceIsReachableAndReturnError(nil) {
//
// When projects get moved, .ino files get renamed but that fact is not
// yet reflected in the project file.
//
let urlDir = url.URLByDeletingLastPathComponent
2015-11-16 01:56:33 +00:00
let newName = rootURL.URLByAppendingPathExtension(url.pathExtension!).lastPathComponent!
if let altURL = urlDir?.URLByAppendingPathComponent(newName) {
if altURL.checkResourceIsReachableAndReturnError(nil) {
url = altURL
}
2015-04-29 03:21:23 +00:00
}
}
2015-03-16 04:39:09 +00:00
super.init(name:url.lastPathComponent!)
2014-11-17 01:29:55 +00:00
}
2014-11-16 18:39:32 +00:00
override func nodeName() -> String {
2015-03-16 04:39:09 +00:00
return "📄 "+name
2014-11-16 18:39:32 +00:00
}
2014-11-17 01:29:55 +00:00
func relativePath(relativeTo: String) -> String {
2015-11-16 01:56:33 +00:00
let path = (url.path! as NSString).stringByResolvingSymlinksInPath
2014-11-17 01:29:55 +00:00
let relComp = relativeTo.componentsSeparatedByString("/") as [String]
let pathComp = path.componentsSeparatedByString("/") as [String]
let relCount = relComp.count
let pathCount = pathComp.count
var matchComp = 0
while (matchComp < relCount && matchComp < pathCount) {
if pathComp[matchComp] == relComp[matchComp] {
++matchComp
} else {
break
}
}
if matchComp==1 {
return path
}
let resComp = Array(count: relCount-matchComp, repeatedValue: "..")+pathComp[matchComp..<pathCount]
2015-11-16 01:56:33 +00:00
return resComp.joinWithSeparator("/")
2014-11-17 01:29:55 +00:00
}
2015-02-14 16:43:20 +00:00
override func propertyList(rootPath: String) -> AnyObject {
2014-11-17 01:29:55 +00:00
return [kTypeKey: kNodeTypeFile, kKindKey: type.rawValue, kPathKey: relativePath(rootPath)]
}
2015-02-14 16:43:20 +00:00
override func paths(rootPath: String) -> [String] {
2014-11-28 13:18:53 +00:00
return [relativePath(rootPath)]
}
2015-01-11 01:53:50 +00:00
override func exists() -> Bool {
return url.checkResourceIsReachableAndReturnError(nil)
}
2015-03-16 04:39:09 +00:00
override func modDate() -> NSDate? {
var date: AnyObject?
2015-11-16 01:56:33 +00:00
do {
try url.getResourceValue(&date, forKey: NSURLContentModificationDateKey)
return date as? NSDate
} catch _ {
return nil
}
2015-03-16 04:39:09 +00:00
}
override func revision() -> String? {
let task = NSTask()
task.launchPath = NSBundle.mainBundle().pathForResource("FileRevision", ofType: "")!
let outputPipe = NSPipe()
task.standardOutput = outputPipe
task.standardError = NSFileHandle.fileHandleWithNullDevice()
task.arguments = [url.path!]
task.launch()
return NSString(data: outputPipe.fileHandleForReading.readDataToEndOfFile(),
encoding: NSUTF8StringEncoding) as? String
}
2014-11-16 18:39:32 +00:00
}
class ASFileTree : NSObject, NSOutlineViewDataSource {
2014-12-01 02:34:53 +00:00
var root = ASProject()
var dir = NSURL()
var buildLog = ASLogNode(name: "Build Log", path: "build/build.log")
var uploadLog = ASLogNode(name: "Upload Log", path: "build/upload.log")
2014-12-10 14:30:51 +00:00
var disassembly = ASLogNode(name: "Disassembly", path: "build/disasm.log")
2014-11-16 18:39:32 +00:00
func addFileURL(url: NSURL, omitUnknown: Bool = true) {
let type = ASFileType.guessForURL(url)
if !omitUnknown || type != .Unknown {
2015-03-17 23:17:55 +00:00
root.children.append(ASFileItem(url: url.URLByStandardizingPath!, type: type))
2014-11-16 18:39:32 +00:00
}
}
func setProjectURL(url: NSURL) {
2015-11-16 01:56:33 +00:00
root.name = url.URLByDeletingPathExtension!.lastPathComponent!
dir = url.URLByDeletingLastPathComponent!.URLByStandardizingPath!
}
func projectPath() -> String {
2015-11-16 01:56:33 +00:00
return (dir.path! as NSString).stringByResolvingSymlinksInPath
}
func apply(closure: (ASFileNode) -> ()) {
root.apply(closure)
}
2014-11-17 01:29:55 +00:00
func propertyList() -> AnyObject {
return root.propertyList(projectPath())
2014-11-17 01:29:55 +00:00
}
func readPropertyList(prop: NSDictionary) {
2015-02-14 16:43:20 +00:00
root = ASFileNode.readPropertyList(prop, rootURL:dir) as! ASProject
2014-11-17 01:29:55 +00:00
}
2015-02-14 16:43:20 +00:00
var paths : [String] {
return root.paths(projectPath())
2014-11-28 13:18:53 +00:00
}
2014-11-16 18:39:32 +00:00
2014-11-17 01:29:55 +00:00
// MARK: Outline Data Source
2014-11-16 18:39:32 +00:00
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
if item == nil {
2014-12-10 14:30:51 +00:00
return 4
2014-11-16 18:39:32 +00:00
} else {
2015-02-14 16:43:20 +00:00
return (item as! ASFileGroup).children.count
2014-11-16 18:39:32 +00:00
}
}
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
if item == nil {
2014-12-01 02:34:53 +00:00
switch index {
case 1:
return buildLog
case 2:
return uploadLog
2014-12-10 14:30:51 +00:00
case 3:
return disassembly
2014-12-01 02:34:53 +00:00
default:
return root
}
} else {
2015-02-14 16:43:20 +00:00
let group = item as! ASFileGroup
return group.children[index]
}
2014-11-16 18:39:32 +00:00
}
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
return item is ASFileGroup
}
func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {
2015-02-14 16:43:20 +00:00
return (item as! ASFileNode).nodeName()
2014-11-16 18:39:32 +00:00
}
}