Implement library import
This commit is contained in:
parent
5ea9dc38a8
commit
c549c2aae2
|
@ -88,6 +88,12 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|||
let examplePath = arduinoPath.stringByAppendingPathComponent("Contents/Resources/Java/examples")
|
||||
ASSketchBook.addSketches(menu, target: self, action: "openExample:", path: examplePath, sketches: &examples)
|
||||
}
|
||||
case "Import Standard Library":
|
||||
menu.removeAllItems()
|
||||
ASLibraries.instance().addStandardLibrariesToMenu(menu)
|
||||
case "Import Contributed Library":
|
||||
menu.removeAllItems()
|
||||
ASLibraries.instance().addContribLibrariesToMenu(menu)
|
||||
case "Serial Monitor":
|
||||
menu.itemAtIndex(0)?.hidden = !hasDocument()
|
||||
while menu.numberOfItems > 2 {
|
||||
|
|
|
@ -134,7 +134,6 @@ class ASBuilder {
|
|||
return
|
||||
}
|
||||
|
||||
let libPath = (ASLibraries.instance().directories as NSArray).componentsJoinedByString(":")
|
||||
let boardProp = ASHardware.instance().boards[board]!
|
||||
let progProp = ASHardware.instance().programmers[programmer]
|
||||
let hasBootloader = !useProgrammer && boardProp["upload.protocol"] != nil
|
||||
|
|
|
@ -143,31 +143,66 @@ class ASHardware {
|
|||
}
|
||||
|
||||
private let librariesInstance = ASLibraries()
|
||||
class ASLibraries {
|
||||
class ASLibraries : NSObject {
|
||||
class func instance() -> ASLibraries { return librariesInstance }
|
||||
var directories = [String]()
|
||||
var libraries = [String]()
|
||||
init() {
|
||||
var standardLib = [String]()
|
||||
var contribLib = [String]()
|
||||
override init() {
|
||||
//
|
||||
// Gather hardware directories
|
||||
//
|
||||
let userDefaults = NSUserDefaults.standardUserDefaults()
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
if let arduinoPath = userDefaults.stringForKey("Arduino") {
|
||||
let arduinoLibrariesPath = arduinoPath + "/Contents/Resources/Java/libraries"
|
||||
let dirs = subdirectories(arduinoLibrariesPath)
|
||||
if dirs.count > 0 {
|
||||
directories.append(arduinoLibrariesPath)
|
||||
libraries += dirs
|
||||
}
|
||||
}
|
||||
for sketchDir in userDefaults.objectForKey("Sketchbooks") as! [String] {
|
||||
let librariesPath = sketchDir + "/libraries"
|
||||
let dirs = subdirectories(librariesPath)
|
||||
if dirs.count > 0 {
|
||||
directories.append(librariesPath)
|
||||
libraries += dirs
|
||||
contribLib += dirs
|
||||
}
|
||||
}
|
||||
if let arduinoPath = userDefaults.stringForKey("Arduino") {
|
||||
let arduinoLibrariesPath = arduinoPath + "/Contents/Resources/Java/libraries"
|
||||
let dirs = subdirectories(arduinoLibrariesPath)
|
||||
if dirs.count > 0 {
|
||||
directories.append(arduinoLibrariesPath)
|
||||
libraries += dirs
|
||||
standardLib += dirs
|
||||
}
|
||||
}
|
||||
}
|
||||
func addStandardLibrariesToMenu(menu: NSMenu) {
|
||||
for (index,lib) in enumerate(standardLib) {
|
||||
let menuItem = menu.addItemWithTitle(lib.lastPathComponent, action: "importStandardLibrary:", keyEquivalent: "")
|
||||
menuItem?.target = self
|
||||
menuItem?.tag = index
|
||||
}
|
||||
}
|
||||
func addContribLibrariesToMenu(menu: NSMenu) {
|
||||
for (index,lib) in enumerate(contribLib) {
|
||||
let menuItem = menu.addItemWithTitle(lib.lastPathComponent, action: "importContribLibrary:", keyEquivalent: "")
|
||||
menuItem?.target = self
|
||||
menuItem?.tag = index
|
||||
}
|
||||
}
|
||||
@IBAction func importStandardLibrary(menuItem: AnyObject) {
|
||||
if let tag = (menuItem as? NSMenuItem)?.tag {
|
||||
NSApplication.sharedApplication().sendAction("importLibrary:", to: nil, from: standardLib[tag])
|
||||
}
|
||||
}
|
||||
@IBAction func importContribLibrary(menuItem: AnyObject) {
|
||||
if let tag = (menuItem as? NSMenuItem)?.tag {
|
||||
NSApplication.sharedApplication().sendAction("importLibrary:", to: nil, from: contribLib[tag])
|
||||
}
|
||||
}
|
||||
|
||||
func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool {
|
||||
if let validator = NSApplication.sharedApplication().targetForAction("importLibrary:") as? NSUserInterfaceValidations {
|
||||
return validator.validateUserInterfaceItem(anItem)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -605,6 +605,29 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
|
|||
})
|
||||
}
|
||||
|
||||
func importLibrary(lib: String) {
|
||||
var includes = ""
|
||||
let fileManager = NSFileManager.defaultManager()
|
||||
for file in fileManager.contentsOfDirectoryAtPath(lib, error: nil) as! [String] {
|
||||
if file.hasSuffix(".h") {
|
||||
includes += "#include <\(file)>\n"
|
||||
}
|
||||
}
|
||||
var text = editor.string() as NSString
|
||||
var insert = NSMakeRange(text.length, 0)
|
||||
let postHeaderComments = NSRegularExpression(pattern: "((?:\\s+|/\\*.*?\\*/|//.*?\\n)*)(.*?\\n)", options: .DotMatchesLineSeparators, error: nil)!
|
||||
if let match = postHeaderComments.firstMatchInString(text as String, options:.Anchored, range:NSMakeRange(0, text.length)) {
|
||||
let range = match.rangeAtIndex(2)
|
||||
insert.location = range.location
|
||||
let content = text.substringWithRange(range)
|
||||
if !content.hasPrefix("#include") {
|
||||
includes += "\n"
|
||||
}
|
||||
}
|
||||
|
||||
editor.setString(text.stringByReplacingCharactersInRange(insert, withString: includes))
|
||||
}
|
||||
|
||||
// MARK: Editor configuration
|
||||
|
||||
@IBAction func changeTheme(item: NSMenuItem) {
|
||||
|
@ -633,6 +656,10 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
|
|||
menuItem.title = port
|
||||
|
||||
return true
|
||||
} else if menuItem.action == "importStandardLibrary:" ||
|
||||
menuItem.action == "importContribLibrary:"
|
||||
{
|
||||
return mainEditor is ASFileItem
|
||||
}
|
||||
}
|
||||
return super.validateUserInterfaceItem(anItem)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6250" systemVersion="14C79" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="8052.1" systemVersion="14C1510" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6250"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="8052.1"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<customObject id="-2" userLabel="File's Owner" customClass="ASSerialWin" customModule="AVRsack" customModuleProvider="target">
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="7519.1" systemVersion="14C109" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="8052.1" systemVersion="14C1510" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="7519.1"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="8052.1"/>
|
||||
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
|
@ -34,13 +34,13 @@
|
|||
<box autoresizesSubviews="NO" title="Box" boxType="custom" borderType="none" titlePosition="noTitle" id="0X4-Im-JAh">
|
||||
<rect key="frame" x="0.0" y="0.0" width="229" height="500"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
|
||||
<view key="contentView">
|
||||
<view key="contentView" id="lVt-bf-lMt">
|
||||
<rect key="frame" x="0.0" y="0.0" width="229" height="500"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
<scrollView misplaced="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ObX-J0-NIB">
|
||||
<rect key="frame" x="0.0" y="0.0" width="2" height="2"/>
|
||||
<clipView key="contentView" misplaced="YES" id="kme-nx-YDz">
|
||||
<clipView key="contentView" id="kme-nx-YDz">
|
||||
<rect key="frame" x="1" y="0.0" width="238" height="134"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
|
@ -51,7 +51,7 @@
|
|||
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
|
||||
<tableColumns>
|
||||
<tableColumn width="40" minWidth="40" maxWidth="1000" id="89u-w6-f1G">
|
||||
<tableColumn width="224" minWidth="40" maxWidth="1000" id="89u-w6-f1G">
|
||||
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left">
|
||||
<font key="font" metaFont="smallSystem"/>
|
||||
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
|
||||
|
@ -76,12 +76,12 @@
|
|||
<rect key="frame" x="1" y="119" width="223" height="15"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
</scroller>
|
||||
<scroller key="verticalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="NO" id="b7f-0t-Ab5">
|
||||
<scroller key="verticalScroller" verticalHuggingPriority="750" horizontal="NO" id="b7f-0t-Ab5">
|
||||
<rect key="frame" x="224" y="17" width="15" height="102"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
</scroller>
|
||||
</scrollView>
|
||||
<button verticalHuggingPriority="750" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Mar-ts-Ae2">
|
||||
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Mar-ts-Ae2">
|
||||
<rect key="frame" x="0.0" y="-1" width="114" height="23"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="height" constant="21" id="eBY-nc-VZ2"/>
|
||||
|
@ -94,7 +94,7 @@
|
|||
<action selector="add:" target="-1" id="2u6-qG-b4q"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button verticalHuggingPriority="750" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="nX2-CL-r24">
|
||||
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="nX2-CL-r24">
|
||||
<rect key="frame" x="114" y="-1" width="115" height="23"/>
|
||||
<buttonCell key="cell" type="smallSquare" title="➖" bezelStyle="smallSquare" imagePosition="overlaps" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="bih-cm-8LF">
|
||||
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
|
||||
|
@ -108,18 +108,18 @@
|
|||
</subviews>
|
||||
</view>
|
||||
<constraints>
|
||||
<constraint firstItem="nX2-CL-r24" firstAttribute="leading" secondItem="Mar-ts-Ae2" secondAttribute="trailing" id="0rn-RP-nRD"/>
|
||||
<constraint firstAttribute="trailing" secondItem="ObX-J0-NIB" secondAttribute="trailing" id="3Ap-4y-TqA"/>
|
||||
<constraint firstItem="Mar-ts-Ae2" firstAttribute="height" secondItem="nX2-CL-r24" secondAttribute="height" id="D6Z-ZC-w6I"/>
|
||||
<constraint firstItem="nX2-CL-r24" firstAttribute="width" secondItem="Mar-ts-Ae2" secondAttribute="width" id="DQN-Ql-JUB"/>
|
||||
<constraint firstAttribute="bottom" secondItem="Mar-ts-Ae2" secondAttribute="bottom" id="GFr-dH-HnB"/>
|
||||
<constraint firstAttribute="trailing" secondItem="nX2-CL-r24" secondAttribute="trailing" id="Pwh-aV-fuf"/>
|
||||
<constraint firstItem="ObX-J0-NIB" firstAttribute="leading" secondItem="0X4-Im-JAh" secondAttribute="leading" id="Zh7-ms-2Sa"/>
|
||||
<constraint firstItem="ObX-J0-NIB" firstAttribute="top" secondItem="0X4-Im-JAh" secondAttribute="top" id="dTf-YD-Yhg"/>
|
||||
<constraint firstItem="ObX-J0-NIB" firstAttribute="bottom" secondItem="Mar-ts-Ae2" secondAttribute="top" id="lLZ-KN-JtG"/>
|
||||
<constraint firstItem="nX2-CL-r24" firstAttribute="width" secondItem="Mar-ts-Ae2" secondAttribute="width" id="0Z4-sC-lN4"/>
|
||||
<constraint firstItem="nX2-CL-r24" firstAttribute="leading" secondItem="Mar-ts-Ae2" secondAttribute="trailing" id="32r-O9-eJd"/>
|
||||
<constraint firstAttribute="trailing" secondItem="ObX-J0-NIB" secondAttribute="trailing" id="4NL-k3-wDk"/>
|
||||
<constraint firstItem="ObX-J0-NIB" firstAttribute="top" secondItem="0X4-Im-JAh" secondAttribute="top" id="8RS-VN-6yz"/>
|
||||
<constraint firstItem="ObX-J0-NIB" firstAttribute="leading" secondItem="0X4-Im-JAh" secondAttribute="leading" id="Cl7-2D-JIG"/>
|
||||
<constraint firstAttribute="trailing" secondItem="nX2-CL-r24" secondAttribute="trailing" id="DH4-AX-W9x"/>
|
||||
<constraint firstItem="Mar-ts-Ae2" firstAttribute="height" secondItem="nX2-CL-r24" secondAttribute="height" id="Muq-TB-QQx"/>
|
||||
<constraint firstItem="Mar-ts-Ae2" firstAttribute="top" secondItem="nX2-CL-r24" secondAttribute="top" id="OMH-VF-y0p"/>
|
||||
<constraint firstItem="ObX-J0-NIB" firstAttribute="bottom" secondItem="Mar-ts-Ae2" secondAttribute="top" id="YAF-Rd-m4c"/>
|
||||
<constraint firstItem="Mar-ts-Ae2" firstAttribute="leading" secondItem="0X4-Im-JAh" secondAttribute="leading" id="rbc-mv-ICZ"/>
|
||||
<constraint firstAttribute="bottom" secondItem="Mar-ts-Ae2" secondAttribute="bottom" id="rpT-1h-STT"/>
|
||||
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="200" id="vjU-TE-WcO"/>
|
||||
<constraint firstItem="Mar-ts-Ae2" firstAttribute="leading" secondItem="0X4-Im-JAh" secondAttribute="leading" id="vsi-w4-onJ"/>
|
||||
<constraint firstItem="Mar-ts-Ae2" firstAttribute="top" secondItem="nX2-CL-r24" secondAttribute="top" id="zo6-sp-k5r"/>
|
||||
</constraints>
|
||||
<color key="borderColor" white="0.0" alpha="0.41999999999999998" colorSpace="calibratedWhite"/>
|
||||
<color key="fillColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6250" systemVersion="14C79" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="8052.1" systemVersion="14C1510" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6250"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="8052.1"/>
|
||||
</dependencies>
|
||||
<objects>
|
||||
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
|
||||
|
@ -450,6 +450,33 @@ CA
|
|||
<action selector="cleanProject:" target="-1" id="4hi-zQ-Zoi"/>
|
||||
</connections>
|
||||
</menuItem>
|
||||
<menuItem isSeparatorItem="YES" id="wCN-i4-dYR"/>
|
||||
<menuItem title="Import Standard Library" id="Wr9-mg-Y2Y">
|
||||
<modifierMask key="keyEquivalentModifierMask"/>
|
||||
<menu key="submenu" title="Import Standard Library" id="kn7-VM-hJE">
|
||||
<items>
|
||||
<menuItem title="Item" id="ih0-ik-T3d">
|
||||
<modifierMask key="keyEquivalentModifierMask"/>
|
||||
</menuItem>
|
||||
</items>
|
||||
<connections>
|
||||
<outlet property="delegate" destination="Voe-Tx-rLC" id="WVo-hX-ewO"/>
|
||||
</connections>
|
||||
</menu>
|
||||
</menuItem>
|
||||
<menuItem title="Import Contributed Library" id="cdO-LF-UJz">
|
||||
<modifierMask key="keyEquivalentModifierMask"/>
|
||||
<menu key="submenu" title="Import Contributed Library" id="Rgy-03-GTO">
|
||||
<items>
|
||||
<menuItem title="Item" id="sK8-xk-24A">
|
||||
<modifierMask key="keyEquivalentModifierMask"/>
|
||||
</menuItem>
|
||||
</items>
|
||||
<connections>
|
||||
<outlet property="delegate" destination="Voe-Tx-rLC" id="I0y-YE-Ayo"/>
|
||||
</connections>
|
||||
</menu>
|
||||
</menuItem>
|
||||
</items>
|
||||
<connections>
|
||||
<outlet property="delegate" destination="Voe-Tx-rLC" id="hr7-QB-jdo"/>
|
||||
|
|
Loading…
Reference in New Issue
Block a user