-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from czechboy0/hd/login_item
Add Buildasaur to Login Items (option)
- Loading branch information
Showing
7 changed files
with
164 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// LoginItem.swift | ||
// Buildasaur | ||
// | ||
// Created by Honza Dvorsky on 10/14/15. | ||
// Copyright © 2015 Honza Dvorsky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import BuildaUtils | ||
|
||
//manages adding/removing Buildasaur as a login item | ||
|
||
public class LoginItem { | ||
|
||
public init() { } | ||
|
||
public var isLaunchItem: Bool { | ||
get { | ||
return self.hasPlistInstalled() | ||
} | ||
set { | ||
if newValue { | ||
do { | ||
try self.addLaunchItemPlist() | ||
} catch { | ||
Log.error("Error while adding login item: \(error)") | ||
} | ||
} else { | ||
self.removeLaunchItemPlist() | ||
} | ||
} | ||
} | ||
|
||
private func hasPlistInstalled() -> Bool { | ||
return NSFileManager.defaultManager().fileExistsAtPath(self.launchItemPlistURL().path!) | ||
} | ||
|
||
private func launchItemPlistURL() -> NSURL { | ||
let path = ("~/Library/LaunchAgents/com.honzadvorsky.Buildasaur.plist" as NSString).stringByExpandingTildeInPath | ||
let url = NSURL(fileURLWithPath: path, isDirectory: false) | ||
return url | ||
} | ||
|
||
private func currentBinaryPath() -> String { | ||
|
||
let processInfo = NSProcessInfo.processInfo() | ||
let launchPath = processInfo.arguments.first! | ||
return launchPath | ||
} | ||
|
||
private func launchItemPlistWithLaunchPath(launchPath: String) throws -> String { | ||
|
||
let plistStringUrl = NSBundle.mainBundle().URLForResource("launch_item", withExtension: "plist")! | ||
let plistString = try String(contentsOfURL: plistStringUrl) | ||
|
||
//replace placeholder with launch path | ||
let patchedPlistString = plistString.stringByReplacingOccurrencesOfString("LAUNCH_PATH_PLACEHOLDER", withString: launchPath) | ||
return patchedPlistString | ||
} | ||
|
||
public func removeLaunchItemPlist() { | ||
_ = try? NSFileManager.defaultManager().removeItemAtURL(self.launchItemPlistURL()) | ||
} | ||
|
||
public func addLaunchItemPlist() throws { | ||
let launchPath = self.currentBinaryPath() | ||
let contents = try self.launchItemPlistWithLaunchPath(launchPath) | ||
let url = self.launchItemPlistURL() | ||
try contents.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.