I am working on a Command Line Tool in Xcode. The challenge that I am facing is the following: If a file does not exist where I expect it to be, e.g. /Path/To/The.File
, how can I place a copy of The.File
at /Path/To/The.File
if it has been added to the project's resources?
With a MacOS application project, I believe I've used something similar to the following with success to get the file path:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"The" ofType:@"File"];
With the file path, I would then be able to execute code to copy the file from the file path to the expected location /Path/To/The.File
.
Is it possible to accomplish this with a Command Line Tool in Xcode?
I was able to "embed" the resource The.File
with the use of Makefile.
After building the project in Xcode, I placed the resource The.File
into a temporary folder along with the compiled project TheCompiledProject
. Here are the contents of the Makefile:
UPDATE=bundledExecutable
BIN1=TheCompiledProject
BIN2=The.File
all: $(UPDATE)
$(UPDATE): ./$(BIN1) ./$(BIN2)
@echo "UP [ $@ ]"
echo '#!/bin/bash' > $@
echo 'FOLDER=`mktemp -d -t upgrade`' >> $@
echo 'cd $$FOLDER; uudecode -c << '"'EOF' | tar -x" >> $@
uuencode $(BIN1) $(BIN1) >> $@
uuencode $(BIN2) $(BIN2) >> $@
echo 'EOF' >> $@
echo 'chmod a+x $$FOLDER/$(BIN1)' >> $@
echo 'chmod a+x $$FOLDER/$(BIN2)' >> $@
echo '$$FOLDER/$(BIN1)' >> $@
echo 'rm -rf $$FOLDER' >> $@
chmod a+x $@
If you use the pathForResource
method, in TheCompiledProject
, the filePath for The.File
is returned as expected.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"The" ofType:@"File"];