This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
Added option to remove Command-Line Tool again (#338) #344
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,7 @@ - (instancetype)initWithSuggestedDestinationURL:(NSURL *)suggestedDestinationURL | |
|
||
- (BOOL)shouldInstallBinstubIfNecessary; | ||
{ | ||
[self verifyExistingInstallDestinations]; | ||
|
||
if (self.previouslyInstalledToDestinations.count > 0) { | ||
if ([self hasInstalledBinstubBefore]) { | ||
NSLog(@"Already installed binstub."); | ||
return NO; | ||
} | ||
|
@@ -75,6 +73,41 @@ - (BOOL)installBinstub; | |
return installed; | ||
} | ||
|
||
#pragma mark - Uninstallation | ||
|
||
- (BOOL)hasInstalledBinstubBefore | ||
{ | ||
[self verifyExistingInstallDestinations]; | ||
|
||
return self.previouslyInstalledToDestinations.count > 0; | ||
} | ||
|
||
- (BOOL)removeBinstub | ||
{ | ||
[self verifyExistingInstallDestinations]; | ||
|
||
if (![self hasInstalledBinstubBefore]) { | ||
NSLog(@"Tried to remove binstub, but it was never installed using the app before."); | ||
return NO; | ||
} | ||
|
||
if (![self promptIfUserReallyWantsToUninstall]) { | ||
NSLog(@"User canceled removing binstub."); | ||
return NO; | ||
} | ||
|
||
// go ahead and delete it | ||
NSLog(@"Now removing binstub ..."); | ||
|
||
NSDictionary *remainingURLs = [self removeBinstubAccordingToPrivileges]; | ||
|
||
[self saveBookmarksWithURLs:remainingURLs]; | ||
|
||
// success only when we have successfully removed all urls from file system | ||
NSLog(@"Finished removing binstub: %@", self.previouslyInstalledToDestinations.count == 0 ? @"success" : @"failed"); | ||
return self.previouslyInstalledToDestinations.count == 0; | ||
} | ||
|
||
#pragma mark - Installation destination bookmarks | ||
|
||
static NSData * | ||
|
@@ -111,7 +144,6 @@ - (void)verifyExistingInstallDestinations; | |
} else { | ||
NSLog(@"Verifying existing destinations."); | ||
NSUInteger bookmarkCount = bookmarks.count; | ||
NSMutableArray *verifiedBookmarks = [NSMutableArray arrayWithCapacity:bookmarkCount]; | ||
NSMutableDictionary *URLs = [NSMutableDictionary dictionaryWithCapacity:bookmarkCount]; | ||
for (NSUInteger i = 0; i < bookmarkCount; i++) { | ||
NSData *bookmark = [bookmarks objectAtIndex:i]; | ||
|
@@ -141,12 +173,9 @@ - (void)verifyExistingInstallDestinations; | |
} | ||
#endif | ||
URLs[URL] = bookmark; | ||
[verifiedBookmarks addObject:bookmark]; | ||
} | ||
} | ||
self.previouslyInstalledToDestinations = [URLs copy]; | ||
[defaults setObject:[verifiedBookmarks copy] | ||
forKey:kCPCLIToolInstalledToDestinationsKey]; | ||
[self saveBookmarksWithURLs:URLs]; | ||
} | ||
} | ||
|
||
|
@@ -160,9 +189,7 @@ - (void)saveInstallationDestination; | |
NSMutableDictionary *URLs = [self.previouslyInstalledToDestinations mutableCopy]; | ||
// Update any previous bookmark data pointing to the same destination. | ||
URLs[self.destinationURL] = bookmark; | ||
NSArray *bookmarks = [URLs allValues]; | ||
[[NSUserDefaults standardUserDefaults] setObject:bookmarks | ||
forKey:kCPCLIToolInstalledToDestinationsKey]; | ||
[self saveBookmarksWithURLs:URLs]; | ||
} | ||
} | ||
|
||
|
@@ -196,8 +223,33 @@ - (BOOL)promptIfOverwriting | |
return [alert runModal] == NSAlertFirstButtonReturn; | ||
} | ||
|
||
- (BOOL)promptIfUserReallyWantsToUninstall | ||
{ | ||
NSAlert *alert = [NSAlert new]; | ||
alert.alertStyle = NSCriticalAlertStyle; | ||
alert.messageText = NSLocalizedString(@"UNINSTALL_CLI_WARNING_MESSAGE_TEXT", nil); | ||
|
||
[alert addButtonWithTitle:NSLocalizedString(@"UNINSTALL_CLI_REMOVE", nil)]; | ||
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", nil)]; | ||
|
||
return [alert runModal] == NSAlertFirstButtonReturn; | ||
} | ||
|
||
#pragma mark - Utility | ||
|
||
- (void)saveBookmarksWithURLs:(NSDictionary *)URLs | ||
{ | ||
self.previouslyInstalledToDestinations = [URLs copy]; | ||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | ||
NSArray *bookmarks = [URLs allValues]; | ||
if (bookmarks.count == 0) { | ||
[defaults removeObjectForKey:kCPCLIToolInstalledToDestinationsKey]; | ||
} else { | ||
[defaults setObject:bookmarks | ||
forKey:kCPCLIToolInstalledToDestinationsKey]; | ||
} | ||
} | ||
|
||
- (NSURL *)binstubSourceURL; | ||
{ | ||
NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; | ||
|
@@ -229,7 +281,12 @@ - (BOOL)binstubAlreadyIsTheLatestVersion; | |
|
||
- (BOOL)hasWriteAccessToBinstub; | ||
{ | ||
NSURL *destinationDirURL = [self.destinationURL URLByDeletingLastPathComponent]; | ||
return [self hasWriteAccessToBinstubWithURL:self.destinationURL]; | ||
} | ||
|
||
- (BOOL)hasWriteAccessToBinstubWithURL:(NSURL *)url; | ||
{ | ||
NSURL *destinationDirURL = [url URLByDeletingLastPathComponent]; | ||
return access([destinationDirURL.path UTF8String], W_OK) == 0; | ||
} | ||
|
||
|
@@ -358,4 +415,146 @@ - (BOOL)installBinstubToPrivilegedDestination; | |
return succeeded; | ||
} | ||
|
||
#pragma mark - Binstub uninstallation | ||
|
||
/// Loops through all installed destinations and tries to remove them. | ||
/// | ||
/// @return Dictionary of remaining bookmarks which couldn't be removed. Empty dict means everything was succuessfully removed. | ||
/// | ||
- (NSDictionary *)removeBinstubAccordingToPrivileges | ||
{ | ||
self.errorMessage = nil; | ||
|
||
NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
|
||
NSMutableArray *privilegedURLs = [NSMutableArray array]; | ||
NSMutableDictionary *URLs = [self.previouslyInstalledToDestinations mutableCopy]; | ||
for (NSURL *url in self.previouslyInstalledToDestinations) { | ||
|
||
if (![fileManager fileExistsAtPath:url.path]) { | ||
// remove url from our list | ||
[URLs removeObjectForKey:url]; | ||
continue; | ||
} | ||
|
||
NSLog(@"Removing binstub: %@", url.path); | ||
|
||
BOOL removed = NO; | ||
if ([self hasWriteAccessToBinstubWithURL:url]) { | ||
removed = [self removeBinstubFromAccessibleDestinationWithURL:url]; | ||
} else { | ||
removed = NO; // will be done few lines below | ||
[privilegedURLs addObject:url]; | ||
} | ||
|
||
if (removed) { | ||
[URLs removeObjectForKey:url]; | ||
} | ||
} | ||
|
||
// now remove privileged urls all at once | ||
if (privilegedURLs.count > 0) { | ||
BOOL removed = [self removeBinstubFromPrivilegedDestinationWithURLs:privilegedURLs]; | ||
if (removed) { | ||
[URLs removeObjectsForKeys:privilegedURLs]; | ||
} | ||
} | ||
|
||
return [URLs copy]; | ||
} | ||
|
||
- (BOOL)removeBinstubFromAccessibleDestinationWithURL:(NSURL *)url | ||
{ | ||
NSError *error = nil; | ||
BOOL success = [[NSFileManager defaultManager] removeItemAtURL:url error:&error]; | ||
|
||
if (error) { | ||
NSLog(@"Failed to remove binstub: %@", error); | ||
self.errorMessage = @"Failed to remove pod command"; | ||
success = NO; | ||
} | ||
|
||
return success; | ||
} | ||
|
||
// Possible Solutions how to gain privileged access to remove files: | ||
// 1. `AuthorizationExecuteWithPrivileges` but its deprecated since OS X 10.7: [1] and [2] | ||
// 2. `ServiceManagement.framework`'s `SMJobBless()`: [3] and [4] | ||
// 3. AppleScript: [5] and [6] | ||
// | ||
// Disadvantage of solution #3 is that authorization dialog will pop up for each | ||
// | ||
// References: | ||
// [1] http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/ | ||
// [2] https://developer.apple.com/library/mac/documentation/Security/Conceptual/authorization_concepts/03authtasks/authtasks.html | ||
// [3] http://stackoverflow.com/a/6842129 | ||
// [4] https://developer.apple.com/library/mac/samplecode/EvenBetterAuthorizationSample/Listings/Read_Me_About_EvenBetterAuthorizationSample_txt.html#//apple_ref/doc/uid/DTS40013768-Read_Me_About_EvenBetterAuthorizationSample_txt-DontLinkElementID_17 | ||
// [5] http://stackoverflow.com/a/8865284 | ||
// [6] http://stackoverflow.com/a/15248621 | ||
- (BOOL)removeBinstubFromPrivilegedDestinationWithURLs:(NSArray<NSURL *> *)urls { | ||
if (urls.count == 0) { | ||
return NO; | ||
} | ||
|
||
NSArray<NSString *> *paths = [urls valueForKey:@"path"]; // NSURL.path | ||
NSString *pathsArgumentString = [NSString stringWithFormat:@"'%@'", [paths componentsJoinedByString:@"' '"]]; // [asdf, wasd] --> 'asdf' 'wasd' | ||
|
||
NSString *output = nil; | ||
NSString *processErrorDescription = nil; | ||
|
||
// Command: `'/bin/rm' -f '/usr/local/bin/pod' '/usr/local/bin/path with space/someOtherBinary'` | ||
BOOL success = [self runProcessAsAdministrator:@"/bin/rm" | ||
withArguments:@[@"-f", pathsArgumentString] | ||
output:&output | ||
errorDescription:&processErrorDescription]; | ||
|
||
// Process failed to run | ||
if (!success) { | ||
NSLog(@"Failed to remove Binstub from privileged destination: %@", processErrorDescription); | ||
} | ||
return success; | ||
} | ||
|
||
// Using AppleScript | ||
// Source: [6] (StackOverflow) | ||
- (BOOL)runProcessAsAdministrator:(NSString *)scriptPath | ||
withArguments:(NSArray *)arguments | ||
output:(NSString **)output | ||
errorDescription:(NSString **)errorDescription { | ||
|
||
NSString *allArgs = [arguments componentsJoinedByString:@" "]; | ||
NSString *fullScript = [NSString stringWithFormat:@"'%@' %@", scriptPath, allArgs]; | ||
|
||
NSDictionary *errorInfo = [NSDictionary new]; | ||
NSString *script = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript]; | ||
|
||
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script]; | ||
NSAppleEventDescriptor *eventResult = [appleScript executeAndReturnError:&errorInfo]; | ||
|
||
if (eventResult) { | ||
// Set output to the AppleScript's output | ||
*output = [eventResult stringValue]; | ||
|
||
return YES; | ||
} | ||
|
||
// Check errorInfo & describe common errors | ||
*errorDescription = nil; | ||
if ([errorInfo valueForKey:NSAppleScriptErrorNumber]) { | ||
NSNumber *errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber]; | ||
if ([errorNumber intValue] == -128) { | ||
*errorDescription = @"The administrator password is required to do this."; | ||
} | ||
} | ||
|
||
// Set error message from provided message | ||
if (*errorDescription == nil) { | ||
if ([errorInfo valueForKey:NSAppleScriptErrorMessage]) { | ||
*errorDescription = (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage]; | ||
} | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is all really well documented, thanks! |
||
@end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for all the doc fixes