Create Sketch

This commit is contained in:
Matthias Neeracher 2015-01-12 04:24:29 +01:00 committed by Matthias Neeracher
parent 1e94a7d351
commit 43a9dbfb02
3 changed files with 82 additions and 9 deletions

View File

@ -152,6 +152,23 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
let url = NSURL(fileURLWithPath: examples[item.tag])!
openTemplate(url.URLByDeletingLastPathComponent!)
}
@IBAction func createSketch(AnyObject) {
ASApplication.newProjectLocation(nil,
message: "Create Project")
{ (saveTo) -> Void in
let fileManager = NSFileManager.defaultManager()
fileManager.createDirectoryAtURL(saveTo, withIntermediateDirectories:false, attributes:nil, error:nil)
let proj = saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent+".avrsackproj")
let docController = NSDocumentController.sharedDocumentController() as NSDocumentController
if let doc = docController.openUntitledDocumentAndDisplay(true, error:nil) as? ASProjDoc {
doc.fileURL = proj
doc.updateProjectURL()
doc.createFileAtURL(saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent+".ino"))
doc.writeToURL(proj, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil, error: nil)
}
}
}
class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (NSURL) -> ()) {
let savePanel = NSSavePanel()

View File

@ -151,6 +151,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
return NSPropertyListSerialization.dataFromPropertyList(data, format: .XMLFormat_v1_0, errorDescription: nil)
}
func updateProjectURL() {
files.setProjectURL(fileURL!)
builder.setProjectURL(fileURL!)
}
func importProject(url: NSURL, error outError: NSErrorPointer) -> Bool {
let existingProject = url.URLByAppendingPathComponent(url.lastPathComponent+".avrsackproj")
if existingProject.checkResourceIsReachableAndReturnError(nil) {
@ -160,7 +165,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
let filesInProject =
NSFileManager.defaultManager().contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil,
options: .SkipsHiddenFiles, error: nil)!
files.setProjectURL(fileURL!)
updateProjectURL()
for file in filesInProject {
files.addFileURL(file as NSURL)
}
@ -173,8 +178,6 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
let projectURL = url.URLByDeletingPathExtension!.URLByAppendingPathExtension("avrsackproj")
success = importProject(url.URLByDeletingLastPathComponent!, error: outError)
if success {
files.setProjectURL(projectURL)
builder.setProjectURL(projectURL)
fileURL = projectURL
success = writeToURL(projectURL, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil, error: outError)
}
@ -187,8 +190,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
if typeName != "Project" {
return false
}
files.setProjectURL(fileURL!)
builder.setProjectURL(fileURL!)
updateProjectURL()
let projectData : NSDictionary = NSPropertyListSerialization.propertyListFromData(data, mutabilityOption: .Immutable, format: nil, errorDescription: nil) as NSDictionary
let projectVersion = projectData[kVersionKey] as Double
assert(projectVersion <= floor(kCurVersion+1.0), "Project version too new for this app")
@ -390,6 +392,48 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa
return selectedFiles().count > 0
}
func createFileAtURL(url:NSURL) {
let type = ASFileType.guessForURL(url)
var firstPfx = ""
var prefix = ""
var lastPfx = ""
switch type {
case .Header, .CFile:
firstPfx = "/*"
prefix = " *"
lastPfx = " */"
case .CppFile, .Arduino:
prefix = "//"
case .AsmFile:
prefix = ";"
case .Markdown:
firstPfx = "<!---"
prefix = " "
lastPfx = " -->"
default:
break
}
var header = ""
if prefix != "" {
if firstPfx == "" {
firstPfx = prefix
}
if lastPfx == "" {
lastPfx = prefix
}
let dateFmt = NSDateFormatter()
dateFmt.dateFormat = "yyyy-MM-dd"
header = firstPfx + "\n" +
prefix + " Project: " + fileURL!.URLByDeletingLastPathComponent!.lastPathComponent + "\n" +
prefix + " File: " + url.lastPathComponent + "\n" +
prefix + " Created: " + dateFmt.stringFromDate(NSDate()) + "\n" +
lastPfx + "\n\n"
}
header.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
files.addFileURL(url)
outline.reloadData()
}
// MARK: Editor configuration
@IBAction func changeTheme(item: NSMenuItem) {

View File

@ -74,10 +74,22 @@
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="File" id="bib-Uj-vzu">
<items>
<menuItem title="New" keyEquivalent="n" id="Was-JA-tGl">
<connections>
<action selector="newDocument:" target="-1" id="4Si-XN-c54"/>
</connections>
<menuItem title="New" id="PfG-L0-KCU">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="New" id="sLm-rV-8LL">
<items>
<menuItem title="File" keyEquivalent="n" id="Was-JA-tGl">
<connections>
<action selector="newDocument:" target="-1" id="4Si-XN-c54"/>
</connections>
</menuItem>
<menuItem title="Sketch" keyEquivalent="N" id="2hE-2e-TSR">
<connections>
<action selector="createSketch:" target="Voe-Tx-rLC" id="L9a-gX-ioC"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="Open…" keyEquivalent="o" id="IAo-SY-fd9">
<connections>