/core/externals/update-engine/Samples/EngineRunner/ERDryRunCommand.m

http://macfuse.googlecode.com/ · Objective C · 136 lines · 72 code · 35 blank · 29 comment · 8 complexity · 5745eb276917e641f3311db394468a48 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "ERDryRunCommand.h"
  15. #import "KSUpdateEngine.h"
  16. @implementation ERDryRunCommand
  17. - (void)dealloc {
  18. [products_ release];
  19. [super dealloc];
  20. } // dealloc
  21. - (NSString *)name {
  22. return @"dryrun";
  23. } // name
  24. - (NSString *)blurb {
  25. return @"See if an update is needed, but don't install it";
  26. } // blurb
  27. - (NSDictionary *)requiredArguments {
  28. return [NSDictionary dictionaryWithObjectsAndKeys:
  29. @"Product ID for the product to update", @"productid",
  30. @"Current version of the product", @"version",
  31. @"Server URL", @"url",
  32. nil];
  33. } // requiredArguments
  34. - (NSDictionary *)optionalArguments {
  35. return [NSDictionary dictionaryWithObjectsAndKeys:
  36. @"Existence checker path", @"xcpath",
  37. nil];
  38. } // optionalArguments
  39. // The way to see what products will run, but not actually install them,
  40. // is to implement the -engine:shouldPrefetchProducts: delegate method,
  41. // snag the products we would otherwise prefect, and then tell the engine
  42. // to stop. So, we'll do all the usual Update Engine setup and running here,
  43. // and let our delegate method short-circuit things once we've figured out
  44. // what we need to know.
  45. - (BOOL)runWithArguments:(NSDictionary *)args {
  46. // Make a new ticket.
  47. NSString *productID = [args objectForKey:@"productid"];
  48. NSString *version = [args objectForKey:@"version"];
  49. NSString *urlString = [args objectForKey:@"url"];
  50. NSURL *serverURL = [NSURL URLWithString:urlString];
  51. // The existence checker is optional, so use a trueChecker if one isn't
  52. // supplied.
  53. NSString *xcpath = [args objectForKey:@"xcpath"];
  54. KSExistenceChecker *existenceChecker;
  55. if (xcpath == nil) {
  56. existenceChecker = [KSExistenceChecker trueChecker];
  57. } else {
  58. existenceChecker = [KSPathExistenceChecker checkerWithPath:xcpath];
  59. }
  60. KSTicket *ticket = [KSTicket ticketWithProductID:productID
  61. version:version
  62. existenceChecker:existenceChecker
  63. serverURL:serverURL];
  64. // And stick it into an in-memory ticket store.
  65. KSTicketStore *ticketStore = [[[KSMemoryTicketStore alloc] init] autorelease];
  66. [ticketStore storeTicket:ticket];
  67. // Make an engine to run things. We look for Things. Things to make us go.
  68. KSUpdateEngine *vroom = [KSUpdateEngine engineWithTicketStore:ticketStore
  69. delegate:self];
  70. // Start the update running.
  71. [vroom updateProductWithProductID:productID];
  72. // Let Update Engine do its thing.
  73. while ([vroom isUpdating]) {
  74. NSDate *spin = [NSDate dateWithTimeIntervalSinceNow:1];
  75. [[NSRunLoop currentRunLoop] runUntilDate:spin];
  76. }
  77. // All done. Report the results.
  78. if ([products_ count] == 0) {
  79. fprintf(stdout, "No products to update\n");
  80. } else {
  81. fprintf(stdout, "Products that would update:\n");
  82. NSEnumerator *productEnumerator = [products_ objectEnumerator];
  83. KSUpdateInfo *info;
  84. while ((info = [productEnumerator nextObject])) {
  85. NSString *productID = [info productID];
  86. fprintf(stdout, " %s\n", [productID UTF8String]);
  87. }
  88. }
  89. return YES;
  90. } // runWithArguments
  91. @end // ERRunUpdateCommand
  92. @implementation ERDryRunCommand (UpdateEngineDelegateMethods)
  93. // Since we only care what might be updated, make a note and then tell
  94. // Update Engine to get lost.
  95. - (NSArray *)engine:(KSUpdateEngine *)engine
  96. shouldPrefetchProducts:(NSArray *)products {
  97. products_ = [products retain];
  98. [engine stopAndReset];
  99. return nil;
  100. } // shouldPrefetchProducts
  101. @end // UpdateEngineDelegateMethods