Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Buildasaur to Login Items (option) #178

Merged
merged 1 commit into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions BuildaKit/LoginItem.swift
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)
}

}
4 changes: 3 additions & 1 deletion BuildaKit/SyncerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class SyncerManager {

public let storageManager: StorageManager
public let factory: SyncerFactoryType
public let loginItem: LoginItem

public let syncersProducer: SignalProducer<[HDGitHubXCBotSyncer], NoError>
public let projectsProducer: SignalProducer<[Project], NoError>
Expand All @@ -31,9 +32,10 @@ public class SyncerManager {
private var configTriplets: SignalProducer<[ConfigTriplet], NoError>
private var heartbeatManager: HeartbeatManager!

public init(storageManager: StorageManager, factory: SyncerFactoryType) {
public init(storageManager: StorageManager, factory: SyncerFactoryType, loginItem: LoginItem) {

self.storageManager = storageManager
self.loginItem = loginItem

self.factory = factory
self.syncers = []
Expand Down
8 changes: 8 additions & 0 deletions Buildasaur.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
3A32CD141A3D00F800861A34 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A32CD131A3D00F800861A34 /* Comment.swift */; };
3A32CD181A3D01E300861A34 /* Issue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A32CD171A3D01E300861A34 /* Issue.swift */; };
3A32CD1E1A3D2ADD00861A34 /* GitHubServerExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A32CD1D1A3D2ADD00861A34 /* GitHubServerExtensions.swift */; };
3A395B551BCF007000BB6947 /* LoginItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A395B541BCF007000BB6947 /* LoginItem.swift */; settings = {ASSET_TAGS = (); }; };
3A3BDC1F1AF6D34900D2CD99 /* GitHubRateLimit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A3BDC1E1AF6D34900D2CD99 /* GitHubRateLimit.swift */; };
3A4C159F1BBB2DD0007FA970 /* SourceControlFileParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4C159D1BBB2DD0007FA970 /* SourceControlFileParser.swift */; settings = {ASSET_TAGS = (); }; };
3A4C15A01BBB2DD0007FA970 /* WorkspaceMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4C159E1BBB2DD0007FA970 /* WorkspaceMetadata.swift */; settings = {ASSET_TAGS = (); }; };
Expand Down Expand Up @@ -50,6 +51,7 @@
3A9109C31BC2B05C00C2AECA /* MainEditor_EditeeDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9109C21BC2B05C00C2AECA /* MainEditor_EditeeDelegate.swift */; settings = {ASSET_TAGS = (); }; };
3A9109C51BC2B0B200C2AECA /* MainEditor_ViewManipulation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9109C41BC2B0B200C2AECA /* MainEditor_ViewManipulation.swift */; settings = {ASSET_TAGS = (); }; };
3A9109C71BC2B67100C2AECA /* EditableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9109C61BC2B67100C2AECA /* EditableViewController.swift */; settings = {ASSET_TAGS = (); }; };
3A9BB91D1BCEFE8F0049C0C0 /* launch_item.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3A9BB91C1BCEFE8F0049C0C0 /* launch_item.plist */; settings = {ASSET_TAGS = (); }; };
3A9C95A51BBDAE9F00D37135 /* SyncerLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9C95A41BBDAE9F00D37135 /* SyncerLogic.swift */; settings = {ASSET_TAGS = (); }; };
3A9D741D1BCBDDA200DCA23C /* PersistenceMigrator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9D741C1BCBDDA200DCA23C /* PersistenceMigrator.swift */; settings = {ASSET_TAGS = (); }; };
3A9D74201BCBF87900DCA23C /* MigrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9D741F1BCBF87900DCA23C /* MigrationTests.swift */; settings = {ASSET_TAGS = (); }; };
Expand Down Expand Up @@ -227,6 +229,7 @@
3A32CD171A3D01E300861A34 /* Issue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Issue.swift; sourceTree = "<group>"; };
3A32CD1D1A3D2ADD00861A34 /* GitHubServerExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitHubServerExtensions.swift; sourceTree = "<group>"; };
3A38CF951A3CE94C00F41AFA /* Status.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Status.swift; path = BuildaGitServer/Status.swift; sourceTree = SOURCE_ROOT; };
3A395B541BCF007000BB6947 /* LoginItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginItem.swift; sourceTree = "<group>"; };
3A3BDC1E1AF6D34900D2CD99 /* GitHubRateLimit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitHubRateLimit.swift; sourceTree = "<group>"; };
3A4770A51A746BC00016E170 /* Buildasaur.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = Buildasaur.entitlements; sourceTree = "<group>"; };
3A4C159D1BBB2DD0007FA970 /* SourceControlFileParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SourceControlFileParser.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -269,6 +272,7 @@
3A9109C21BC2B05C00C2AECA /* MainEditor_EditeeDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainEditor_EditeeDelegate.swift; sourceTree = "<group>"; };
3A9109C41BC2B0B200C2AECA /* MainEditor_ViewManipulation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainEditor_ViewManipulation.swift; sourceTree = "<group>"; };
3A9109C61BC2B67100C2AECA /* EditableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditableViewController.swift; sourceTree = "<group>"; };
3A9BB91C1BCEFE8F0049C0C0 /* launch_item.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = launch_item.plist; sourceTree = "<group>"; };
3A9C95A41BBDAE9F00D37135 /* SyncerLogic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncerLogic.swift; sourceTree = "<group>"; };
3A9D741C1BCBDDA200DCA23C /* PersistenceMigrator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersistenceMigrator.swift; sourceTree = "<group>"; };
3A9D741F1BCBF87900DCA23C /* MigrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MigrationTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -498,6 +502,7 @@
isa = PBXGroup;
children = (
3A5687741A3B93BD0066DB2B /* Info.plist */,
3A9BB91C1BCEFE8F0049C0C0 /* launch_item.plist */,
);
name = "Supporting Files";
sourceTree = "<group>";
Expand Down Expand Up @@ -694,6 +699,7 @@
3ACBADE41B5ADE2A00204457 /* Persistence.swift */,
3ACBADE71B5ADE2A00204457 /* StorageManager.swift */,
3A9D741C1BCBDDA200DCA23C /* PersistenceMigrator.swift */,
3A395B541BCF007000BB6947 /* LoginItem.swift */,
);
name = Persistence;
sourceTree = "<group>";
Expand Down Expand Up @@ -1069,6 +1075,7 @@
files = (
3A56877A1A3B93BD0066DB2B /* Images.xcassets in Resources */,
3A56877D1A3B93BD0066DB2B /* Main.storyboard in Resources */,
3A9BB91D1BCEFE8F0049C0C0 /* launch_item.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1442,6 +1449,7 @@
3ACBAE091B5ADE2A00204457 /* SyncerGitHubUtils.swift in Sources */,
3ACBAE001B5ADE2A00204457 /* Persistence.swift in Sources */,
3ACBAE0B1B5ADE2A00204457 /* SyncPair_Branch_NoBot.swift in Sources */,
3A395B551BCF007000BB6947 /* LoginItem.swift in Sources */,
3A9D741D1BCBDDA200DCA23C /* PersistenceMigrator.swift in Sources */,
3ACBAE011B5ADE2A00204457 /* Project.swift in Sources */,
3ACBAE0D1B5ADE2A00204457 /* SyncPair_PR_Bot.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion Buildasaur/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
//create storage manager
let storageManager = StorageManager(persistence: persistence)
let factory = SyncerFactory()
let syncerManager = SyncerManager(storageManager: storageManager, factory: factory)
let loginItem = LoginItem()
let syncerManager = SyncerManager(storageManager: storageManager, factory: factory, loginItem: loginItem)
self.syncerManager = syncerManager
}

Expand Down
Loading