PageRenderTime 44ms CodeModel.GetById 1ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 15#import "ERDryRunCommand.h"
 16
 17#import "KSUpdateEngine.h"
 18
 19
 20@implementation ERDryRunCommand
 21
 22- (void)dealloc {
 23  [products_ release];
 24  [super dealloc];
 25}  // dealloc
 26
 27
 28- (NSString *)name {
 29  return @"dryrun";
 30}  // name
 31
 32
 33- (NSString *)blurb {
 34  return @"See if an update is needed, but don't install it";
 35}  // blurb
 36
 37
 38- (NSDictionary *)requiredArguments {
 39  return [NSDictionary dictionaryWithObjectsAndKeys:
 40                       @"Product ID for the product to update", @"productid",
 41                       @"Current version of the product", @"version",
 42                       @"Server URL", @"url",
 43                       nil];
 44
 45}  // requiredArguments
 46
 47
 48- (NSDictionary *)optionalArguments {
 49  return [NSDictionary dictionaryWithObjectsAndKeys:
 50                       @"Existence checker path", @"xcpath",
 51                       nil];
 52
 53}  // optionalArguments
 54
 55
 56// The way to see what products will run, but not actually install them,
 57// is to implement the -engine:shouldPrefetchProducts: delegate method,
 58// snag the products we would otherwise prefect, and then tell the engine
 59// to stop.  So, we'll do all the usual Update Engine setup and running here,
 60// and let our delegate method short-circuit things once we've figured out
 61// what we need to know.
 62- (BOOL)runWithArguments:(NSDictionary *)args {
 63  // Make a new ticket.
 64  NSString *productID = [args objectForKey:@"productid"];
 65  NSString *version = [args objectForKey:@"version"];
 66  NSString *urlString = [args objectForKey:@"url"];
 67  NSURL *serverURL = [NSURL URLWithString:urlString];
 68
 69  // The existence checker is optional, so use a trueChecker if one isn't
 70  // supplied.
 71  NSString *xcpath = [args objectForKey:@"xcpath"];
 72  KSExistenceChecker *existenceChecker;
 73  if (xcpath == nil) {
 74    existenceChecker = [KSExistenceChecker trueChecker];
 75  } else {
 76    existenceChecker = [KSPathExistenceChecker checkerWithPath:xcpath];
 77  }
 78
 79  KSTicket *ticket = [KSTicket ticketWithProductID:productID
 80                                           version:version
 81                                  existenceChecker:existenceChecker
 82                                         serverURL:serverURL];
 83
 84  // And stick it into an in-memory ticket store.
 85  KSTicketStore *ticketStore = [[[KSMemoryTicketStore alloc] init] autorelease];
 86  [ticketStore storeTicket:ticket];
 87
 88  // Make an engine to run things.  We look for Things.  Things to make us go.
 89  KSUpdateEngine *vroom = [KSUpdateEngine engineWithTicketStore:ticketStore
 90                                                       delegate:self];
 91
 92  // Start the update running.
 93  [vroom updateProductWithProductID:productID];
 94
 95  // Let Update Engine do its thing.
 96  while ([vroom isUpdating]) {
 97    NSDate *spin = [NSDate dateWithTimeIntervalSinceNow:1];
 98    [[NSRunLoop currentRunLoop] runUntilDate:spin];
 99  }
100
101  // All done.  Report the results.
102  if ([products_ count] == 0) {
103    fprintf(stdout, "No products to update\n");
104  } else {
105    fprintf(stdout, "Products that would update:\n");
106
107    NSEnumerator *productEnumerator = [products_ objectEnumerator];
108    KSUpdateInfo *info;
109    while ((info = [productEnumerator nextObject])) {
110      NSString *productID = [info productID];
111      fprintf(stdout, "  %s\n", [productID UTF8String]);
112    }
113  }
114
115  return YES;
116
117}  // runWithArguments
118
119@end  // ERRunUpdateCommand
120
121
122@implementation ERDryRunCommand (UpdateEngineDelegateMethods)
123
124// Since we only care what might be updated, make a note and then tell
125// Update Engine to get lost.
126- (NSArray *)engine:(KSUpdateEngine *)engine
127  shouldPrefetchProducts:(NSArray *)products {
128
129  products_ = [products retain];
130
131  [engine stopAndReset];
132  return nil;
133
134}  // shouldPrefetchProducts
135
136@end  // UpdateEngineDelegateMethods