source: launchers/macosx/Deployer.mm @ ba853a8c

Last change on this file since ba853a8c was 7dbf568, checked in by meeh <meeh@…>, 22 months ago

OSX Launcher: Refactor deployment code to own file, + code cleanups.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1//
2//  Deployer.m
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 19/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8#include <functional>
9#include <memory>
10#include <iostream>
11#include <algorithm>
12#include <string>
13#include <list>
14#include <sys/stat.h>
15#include <stdlib.h>
16#include <future>
17#include <vector>
18
19#import <Foundation/Foundation.h>
20#import <Foundation/NSFileManager.h>
21#include <CoreFoundation/CFPreferences.h>
22
23#import <objc/Object.h>
24#import <Cocoa/Cocoa.h>
25#import <AppKit/AppKit.h>
26#import <AppKit/NSApplication.h>
27
28#import "I2PLauncher-Swift.h"
29
30#include "AppDelegate.h"
31#include "include/fn.h"
32#import "SBridge.h"
33#include "logger_c.h"
34
35#include <string>
36
37#include "include/subprocess.hpp"
38#include "include/strutil.hpp"
39
40#include "Logger.h"
41#include "LoggerWorker.hpp"
42
43#import "Deployer.h"
44
45#include <string.h>
46
47using namespace subprocess;
48
49@implementation I2PDeployer
50
51- (I2PDeployer *) initWithMetaInfo:(ExtractMetaInfo*)mi
52{
53  self.metaInfo = mi;
54  return self;
55}
56
57- (void) extractI2PBaseDir:(void(^)(BOOL success, NSError *error))completion
58{
59#ifdef __cplusplus
60  NSBundle *launcherBundle = [NSBundle mainBundle];
61  auto homeDir = RealHomeDirectory();
62  NSLog(@"Home directory is %s", homeDir);
63 
64  std::string basePath(homeDir);
65  basePath.append("/Library/I2P");
66 
67  self.metaInfo.zipFile = [launcherBundle pathForResource:@"base" ofType:@"zip"];
68 
69  NSParameterAssert(basePath.c_str());
70  NSError *error = NULL;
71  BOOL success = YES;
72  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
73   
74   
75    try {
76      // Create directory
77      mkdir(basePath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
78     
79      auto cli = defaultFlagsForExtractorJob;
80      setenv("I2PBASE", basePath.c_str(), true);
81      //setenv("DYLD_LIBRARY_PATH",".:/usr/lib:/lib:/usr/local/lib", true);
82     
83      chdir(basePath.c_str());
84     
85      // Everything behind --exec java - would be passed as arguments to the java executable.
86      std::string execStr = "/usr/bin/unzip "; //std::string([rs.getJavaHome UTF8String]);
87      execStr += [self.metaInfo.zipFile UTF8String];
88      //for_each(cli, [&execStr](std::string str){ execStr += std::string(" ") + str; });
89     
90      NSLog(@"Trying cmd: %@", [NSString stringWithUTF8String:execStr.c_str()]);
91      sendUserNotification(APP_IDSTR, @"Please hold on while we extract I2P. You'll get a new message once done!");
92      int extractStatus = Popen(execStr.c_str(), environment{{
93        {"I2PBASE", basePath.c_str()}
94      }}).wait();
95      NSLog(@"Extraction exit code %@",[NSString stringWithUTF8String:(std::to_string(extractStatus)).c_str()]);
96      if (extractStatus == 0) {
97        NSLog(@"Extraction process done");
98      } else {
99        NSLog(@"Something went wrong");
100      }
101     
102      // All done. Assume success and error are already set.
103      dispatch_async(dispatch_get_main_queue(), ^{
104        if (completion) {
105          completion(YES, error);
106        }
107      });
108     
109     
110    } catch (OSError &err) {
111      auto errMsg = [NSString stringWithUTF8String:err.what()];
112      NSLog(@"Exception: %@", errMsg);
113      sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]);
114    }
115  });
116#endif
117}
118
119@end
120
Note: See TracBrowser for help on using the repository browser.