2

I'm trying to run a perl regex one liner on a bunch of files in a directory (via recursive find) and having a bit of trouble getting NSTask to do what the one line does on the command line.

The perl one liner in the terminal works, and a simplified NSTask running the command as a string through shell works too.

Perl command (works)

perl -p -i -e 's/DEBUG_INFORMATION_FORMAT = \"dwarf-with-dsym\";/DEBUG_INFORMATION_FORMAT = \"dwarf\";/g' `find . -name *.pbxproj`

Simple NSTask (works)

Using Inket's answer from another question:

NSString *command = @"perl -p -i -e 's/DEBUG_INFORMATION_FORMAT = \"dwarf-with-dsym\";/DEBUG_INFORMATION_FORMAT = \"dwarf\";/g' `find /Users/nflacco/Projects/XXX/XXX.xcodeproj -name *.pbxproj`";

NSTask* task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];
[task setArguments:@[@"-c", command]];
[task launch];

Xcode terminal app (doesn't work)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSString *workspacePath = @"/Users/nflacco/Projects/XXX/XXX.xcodeproj";

        NSString *old = @"DEBUG_INFORMATION_FORMAT = \"dwarf-with-dsym\";";
        NSString *new = @"DEBUG_INFORMATION_FORMAT = \"dwarf\";";

        NSTask *task = [[NSTask alloc] init];
        [task setLaunchPath:@"/usr/bin/perl"];
        NSString *regex = [NSString stringWithFormat:@"'s/%@/%@/g'", old, new];
        NSString *pathArg = [NSString stringWithFormat:@"`find %@ -name *.pbxproj`", workspacePath];
        [task setArguments:@[ @"-p", @"-i", @"-e", regex, pathArg]];
        [task launch];

    }
    return 0;
}

Xcode console:

Can't open `find /Users/nflacco/Projects/XXX/XXX.xcodeproj -name *.pbxproj`: No such file or directory.
Program ended with exit code: 0
Community
  • 1
  • 1
nflacco
  • 4,972
  • 8
  • 45
  • 78

1 Answers1

2

You have to call /bin/sh as per your first code example. This is because of the use of ` which is a shell expression to execute a subshell and echo its output.

Therefore:

NSString *workspacePath = @"/Users/nflacco/Projects/XXX/XXX.xcodeproj";

NSString *old = @"DEBUG_INFORMATION_FORMAT = \"dwarf-with-dsym\";";
NSString *new = @"DEBUG_INFORMATION_FORMAT = \"dwarf\";";

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
NSString *regex = [NSString stringWithFormat:@"'s/%@/%@/g'", old, new];
NSString *pathArg = [NSString stringWithFormat:@"`find %@ -name *.pbxproj`", workspacePath];
[task setArguments:@[ @"/usr/bin/perl", @"-p", @"-i", @"-e", regex, pathArg]];
[task launch];

Better still is to do all that work inside your app...

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • I'm writing a plugin to toggle various Xcode project settings (changing the settings can lead to a 60% improvement on build time). Otherwise I'd just check a script into the project repo. – nflacco Jul 16 '14 at 07:13