How do you reference a shell script packaged in your app bundle? - objective-c

Given two scripts
foo.sh
bar.sh
copied under /Contents/Resources in an .app bundle, where foo.sh
#!/bin/bash
. ./bar.sh
echo $1
Do get an error of
No such file or directory
on the line where the script tries to source bar.sh
Is there a way to relatively reference bar.sh?
Is there another way to bundle a set of bash scripts in a .app?

What you can do is:
get the full path & directory where the actual running script (your foo.sh) is stored. see https://stackoverflow.com/a/246128/3701456
call or source the second script (your bar.sh) with directory from 1. (or relative to that directory)
Simple Example:
$cat script2
#! /usr/bin/env bash
echo "Hello World, this is script2"
$cat script1
#! /usr/bin/env bash
echo "Hello World from script 1"
echo "Full script path: $BASH_SOURCE"
echo "extracted directory: $(dirname $BASH_SOURCE)"
echo "running script 2"
$(dirname $BASH_SOURCE)/script2 && echo "running script 2 successful" || echo "error running script 2"
echo "sourcing script 2"
source $(dirname $BASH_SOURCE)/script2 && echo "sourcing script 2 successful" || echo "error sourcing script 2"
Test:
$ls /tmp/test
script1 script2
$pwd
/home/michael
$/tmp/test/script1
Hello World from script 1
Full script path: /tmp/test/script1
extracted directory: /tmp/test
running script 2
Hello World, this is script2
running script 2 successful
sourcing script 2
Hello World, this is script2
sourcing script 2 successful
See link above for more in detail discussion ...

Old question but to address the last part (reflecting the current Apple guidelines): yes, you should definitely place all executables (including scripts) in the MacOS sub-folder of your bundle:
MacOS - (Required) Contains the application’s standalone executable code. Typically, this directory contains only one binary file with your application’s main entry point and statically linked code. However, you may put other standalone executables (such as command-line tools) in this directory as well.
(Source: Anatomy of a macOS Application Bundle.)
Breaking these rules will prevent you from successfully signing your app bundle for Gatekeer (and, of course, macOS Notarization).
The first part was adequately handled by the other response.

Related

Running PMD in GitLab CI Script doesn't work unless echo command is added after the script runs

This is an interesting issue. I have a GitLab project, and I've created a .gitlab-ci.yml to run a PMD that will scan my code after every commit. The ci.yml file looks like this:
image: "node:latest"
stages:
- preliminary-testing
apex-code-scan:
stage: preliminary-testing
allow_failure: false
script:
- install_java
- install_pmd
artifacts:
paths:
- pmd-reports/
####################################################
# Helper Methods
####################################################
.sfdx_helpers: &sfdx_helpers |
function install_java() {
local JAVA_VERSION=11
local JAVA_INSTALLATION=openjdk-$JAVA_VERSION-jdk
echo "Installing ${JAVA_INSTALLATION}"
apt update && apt -y install $JAVA_INSTALLATION
}
function install_pmd() {
local PMD_VERSION=6.52.0
local RULESET_PATH=ruleset.xml
local OUTPUT_DIRECTORY=pmd-reports
local SOURCE_DIRECTORY=force-app
local URL=https://github.com/pmd/pmd/releases/download/pmd_releases%2F$PMD_VERSION/pmd-bin-$PMD_VERSION.zip
# Here I would download and unzip the PMD source code. But for now I have the PMD source already in my project for testing purposes
# apt update && apt -y install unzip
# wget $URL
# unzip -o pmd-bin-$PMD_VERSION.zip
# rm pmd-bin-$PMD_VERSION.zip
echo "Installed PMD!"
mkdir -p $OUTPUT_DIRECTORY
echo "Going to run PMD!"
ls
echo "Start"
pmd-bin-$PMD_VERSION/bin/run.sh pmd -d $SOURCE_DIRECTORY -R $RULESET_PATH -f xslt -P xsltFilename=pmd_report.xsl -r $OUTPUT_DIRECTORY/pmd-apex.html
echo "Done"
rm -r pmd-bin-$PMD_VERSION
echo "Remove pmd"
}
before_script:
- *sfdx_helpers
When I try to run this pipeline, it will fail after starting the PMD:
However, if I make a small change to the PMD's .sh file and add an echo command at the very end. Then the pipeline succeeds:
PMD /bin/run.sh before (doesn't work):
...
java ${HEAPSIZE} ${PMD_JAVA_OPTS} $(jre_specific_vm_options) -cp "${classpath}" "${CLASSNAME}" "$#"
PMD /bin/run.sh after (does work):
...
java ${HEAPSIZE} ${PMD_JAVA_OPTS} $(jre_specific_vm_options) -cp "${classpath}" "${CLASSNAME}" "$#"
echo "Done1" // This is the last line in the file
I don't have the slightest idea why this is the case. Does anyone know why adding this echo command at the end of the .sh file would cause the pipeline to succeed? I could keep it as is with the echo command, but I would like to understand why it is behaving this way. I don't want to be that guy that just leaves a comment saying Hey don't touch this line of code, I don't know why, but without it the whole thing fails. Thank you!
PMD exits with a specific exit code depending whether it found some violations or not, see https://pmd.github.io/latest/pmd_userdocs_cli_reference.html#exit-status
I guess, your PMD run finds some violations, and PMD exits with exit code 4 - which is not a success exit code.
In general, this is used to make the CI build fail, in case any PMD violations are present - forcing to fix the violations before you get a green build.
If that is not what you want, e.g. you only want to report the violations but not fail the build, then you need to add the following command line option:
--fail-on-violation false
Then PMD will exit with exit code 0, even when there are violations.
So it appears that the java command that the PMD runs for some reason returns a non-zero exit code (even though the script is successful). Because I was adding an echo command at the end of that bash script, the last line in the script returned a success exit code, which is why the GitLab CI pipeline succeeded when the echo command was there.
In order to work around the non-zero exit code being returned by the java PMD command, I have changed this line in my .gitlab-ci.yml file to catch the non-zero exit code and proceed.
function install_pmd() {
// ... For brevity I'm just including the line that was changed in this method
pmd-bin-$PMD_VERSION/bin/run.sh pmd -d $SOURCE_DIRECTORY -R $RULESET_PATH -f xslt -P xsltFilename=pmd_report.xsl -r $OUTPUT_DIRECTORY/pmd-apex.html || echo "PMD Returned Exit Code"
// ...
}

Adding home-brew to PATH

I just installed Home-brew and now I'm trying to insert the home-brew directory at the top of my path environment variable by typing in two commands inside my terminal. My questions are these:
What is a path environment variable?
Are the two codes provided me correct?
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
After this I am to type in brew doctor. Nothing is happening as far as I can see.
Can anyone offer me some advice or direction?
I installed brew in my new Mac M1 and ask me to put /opt/homebrew/bin in the path, so the right command for this case is:
echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
TL;DR
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
is what you want.
To answer your first question; in order to run (execute) a program (executable) the shell must know exactly where it is in your filesystem in order to run it. The PATH environment variable is a list of directories that the shell uses to search for executables. When you use a command that is not built into the shell you are using the shell will search through these directories in order and will execute the first matching executable it finds.
For example when you type: mv foo bar the shell is almost certainly actually using an executable located in the /bin directory. Thus fully the command is
/bin/mv foo bar
The PATH environment variable therefore saves you some extra typing. You can see what is in your PATH currently (as you can with all environment variables) by entering:
echo $<NAME OF VARIABLE>
So in this instance:
echo $PATH
As I mentioned earlier, ordering is important. Adding /usr/local/bin to the beginning of PATH means that the shell will search there first and so if you have an executable foo in that folder it will be used in preference to any other foo executables you may have in the folders in your path. This means that any executables you install with brew will be used in preference to the system defaults.
On to your second question. What the command you have provided is trying to do is add a line to your .bash_profile and then source it. The .bash_profile is a text file stored in your home directory that is sourced (read) every time bash (your shell) starts. The mistake in the line you've provided is that only the first letter of PATH is capitalised. To your shell Path and PATH are very different things.
To fix it you want:
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
To explain
echo "export PATH=/usr/local/bin:$PATH"
simply prints or echoes what follows to stdout, which in the above instance is the terminal. (stdout, stderr and stdin are very important concepts on UNIX systems but rather off topic) Running this command produces the result:
export PATH=/usr/local/bin:/opt/local/sbin:/opt/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
on my system because using $PATH within double quotes means bash will substitute it with its value. >> is then used to redirect stdout to the end of the ~/.bash_profile file. ~ is shorthand for your home directory. (NB be very careful as > will redirect to the file and overwrite it rather than appending.)
&& means run the next command is the previous is successful and
source ~/.bash_profile
simply carries out the actions contained in that file.
As per the latest documentation, you need to do this:
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/dhruv/.bashrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
Now you should be able to run brew from anywhere.
When you type in a program somewhere and click enter, it checks certain locations to see if that program exists there.
Linux brew uses locations different from the normal linux programs, so we are adding these locations to the ~/.profile file which sets the paths.
Run this in your terminal, and it will place the correct code in the .profile file, automatically.
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
Don't use .bash_profile because when you use something different from bash, like zsh, it may not work. .profile is the correct location.

Bash script in Mac OS X app, calling custom app from system

In my bash script i have:
program=*program_name*
condition=$(which $program 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ] ; then
echo "$program is not installed";
echo -n *mypass* |sudo -S gem install $program;
fi
First of all, it installs program every time. It shows that program is not installed, but i can use it from terminal.
...then, i need to use this program in my cocoa application, for example
program --help
Using
system([pathToFile UTF8String]);
i get:
path_to_bundle/myBashScript.sh: Permission denied // Where path is in bundle
path_to_folder/myBashScript.sh:line 30: program: command not found // Where path is from other system folder
Using NSTask i get program: command not found every time.
I don't understand why this is happening. And i would like to know how i can use this program in my cocoa app.
So, i have found the solution.
When you're trying to run the custom system program from the cocoa app, you should give the full path to the binary.
The problem is in:
program=*program_name*
*program_name* should be full path to binary, in my case it was /Library/Ruby/Gems/2.0.0/gems/program-version/bin/program
For additional information about installation paths:
https://wiki.haskell.org/Mac_OS_X_Common_Installation_Paths
http://help.rubygems.org/discussions/problems/739-how-to-run-applications-installed-by-gem

How to start a Rails server with an .sh script in a directory of choice

I've been developing with rails for a year now, and although rails is pretty well automated, I'd like to take it one step further.
Every time i start working on a project, i open the terminal, cd to a project folder, than new tab in the terminal, then start the server with "rails s" then back to the first tab to run "sublime ." so that i load the folder in my text editor, Sublime Text.
What i would like is to create a .sh script that would do all that for me, but so far i haven't been able to make it start the server.
So, how to start rails server with a .sh script in a directory of choice?
#Manolo gave me an idea, so I've come up with this:
I modified my .bashrc as the following answer illustrates:
https://superuser.com/a/198022
Basically i added
eval "$BASH_POST_RC"
At the end of my .bashrc so i could run arbitrary commands after it was executed.
Next, i made a following script:
#launch_project.sh
#/bin/bash
cd <PROJECT DIR GOES HERE>;
firefox -P "development";
sublime . &;
gnome-terminal \
--tab --title="Server" -e 'bash -c "export BASH_POST_RC=\"rails s\";exec bash"' \
--tab -e 'bash -c "export BASH_POST_RC=\"git s\"; exec bash"';
That launches my development profile on firefox, my editor with a project root, and terminal window with two tabs - one that runs a WEBrick server, and another one which runs git status for me.
I made a desktop shortcut to the script so i have my own custom IDE of sorts :)
Try this:
#!/bin/sh
cd your_project_folder
nohup rails s > /tmp/rails_server.log 2>&1 &
sublime .
you can see the ouput of your rails server at the /tmp/rails_server.log file

How do I call a local shell script from a web server?

I am running Ubuntu 11 and I would like to setup a simple webserver that responds to an http request by calling a local script with the GET or POST parameters. This script (already written) does some stuff and creates a file. This file should be made available at a URL, and the webserver should then make an http request to another server telling it to download the created file.
How would I go about setting this up? I'm not a total beginner with linux, but I wouldn't say I know it well either.
What webserver should I use? How do I give permission for the script to access local resources to create the file in question? I'm not too concerned with security or anything, this is for a personal experiment (I have control over all the computers involved). I've used apache before, but I've never set it up.
Any help would be appreciated..
This tutorial looks good, but it's a bit brief.
I have apache installed. If you don't: sudo apt-get install apache2.
cd /usr/lib/cgi-bin
# Make a file and let everyone execute it
sudo touch test.sh && chmod a+x test.sh
Then put the some code in the file. For example:
#!/bin/bash
# get today's date
OUTPUT="$(date)"
# You must add following two lines before
# outputting data to the web browser from shell
# script
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Demo</title></head><body>"
echo "Today is $OUTPUT <br>"
echo "Current directory is $(pwd) <br>"
echo "Shell Script name is $0"
echo "</body></html>"
And finally open your browser and type http://localhost/cgi-bin/test.sh
If all goes well (as it did for me) you should see...
Today is Sun Dec 4 ...
Current directory is /usr/lib/cgi-bin Shell
Shell Script name is /usr/lib/cgi-bin/test.sh