Using NSTask to execute ionic build commands - launch path not accessible
Asked Answered
C

2

8

I'm working with Ionic which has a command line interface. I would like to create a small Mac app that helps to execute certain commands.

In the terminal I cd Users/me/Desktop/Repos/ionic-project/myApp After changing I would e.g. ionic run

NSTask *task = [[NSTask alloc]init];

task.launchPath = @"/bin/bash";
task.currentDirectoryPath = @"cd Users/me/Desktop/Repos/ionic-tryouts/myApp";
task.arguments = @[@"ionic run"];

[task launch];

But this gives me 'working directory doesn't exist.' I've already read quite some threads here on SO. Whats my mistake?


EDIT:

Thanks Christik for the detailed answer. I now have this code:

NSTask *task = [[NSTask alloc]init];
task.launchPath = @"/usr/local/bin/ionic";
task.currentDirectoryPath = @"/Users/me/ionic-tryouts/myApp";
task.arguments = @[@"run"];

[task launch];

Now I get the following error: env: node: No such file or directory. I guess this comes from a problem, that node.js isn't found (ionic is build on top of node). I found this issue - a missing symlink with the right name might be the cause. But setting the symlink for node didn't help. Any ideas?


EDIT2: I gave Christik the correct answer, even though I couldn't finally get it to work. I'm still investigation. Maybe it's the node installation thats wrong.


EDIT3: I saw some posts that mentioned that it might be better, if node was re-installed by homebrew since homebrew installs it without sudo. Short comment: didn't help

Ceballos answered 21/5, 2015 at 13:34 Comment(0)
M
3

If you want to use bash to launch ionic, the equivalent command that you'll need to execute is /bin/bash -c ionic run, thus you'll need to change the following:

  1. remove the leading cd from currentDirectoryPath (that's probably a typo from a copy+paste from terminal) and add a leading / to have an absolute path
  2. change argumentsto @[@"-c",@"ionic",@"run"], as each argument to NSTask should represent an item of the array.

If you have problems with /usr/bin/bash due to some other tools not being found, you can try using /bin/sh.

Update As Aditya Vaidyam pointed out here you might also need to setup the environment variables (especially the PATH one), to simulate the same conditions as for the terminal. If you want to find out which environment variables are set, just run the envcommand.

Magdeburg answered 21/5, 2015 at 15:21 Comment(7)
Thanks for the answer, but as described above I'm still having trouble. I'll set a bounty ;-)Ceballos
Thanks for paying attention :-) But even with only @[@"run"] as argument it will give env: node: No such file or directory. I guess is has something to di with the ionic installationCeballos
Did you ever find a solution to this? I have the same trouble running a Gulp with NSTask.Purvey
@Magdeburg i appreciate your responce; I am sure The argument passing; see if i run specific command on terminal e.g.. 'class-dump -H -o <dir_path> <mac-o_path>' it works and makes header files to the <dir_path>; but with NSTask i am doing the same with above code suggested. and it is giving error "/bin/sh: class-dump: No such file or directory".Ghazi
how to do that? @MagdeburgGhazi
Let us continue this discussion in chat.Ghazi
@Magdeburg well I came to the solution by passing the command like this -> /usr/local/bin/class-dump -H -o %@ %@",<path_to_directory>, <path_to_binary>]Ghazi
C
3

On top of what Cristik suggested, you need to add to the environment variables (i.e. PATH), the location of node.js, unless it's already in /usr/bin/ or so.

Clapperclaw answered 27/5, 2015 at 3:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.