How execute "rackup" from a remote path? - ruby

Running a rack service written in ruby, I need to execute "rackup" from an external path, without browsing into the code's directory previously.
classically:
cd /Volumes/Code/my_service; rackup
i'd like to:
rackup cd /Volumes/Code/my_service
which obv does not work because rackup doesnt not take this parameter.

You can specify the config file to use as an argument to rackup. If you don’t include one it will default to config.ru and will just look in the current directory.
In your case you should be able to run:
rackup /Volumes/Code/my_service/config.ru
You will need to make sure your app doesn’t assume that the current directory is the same as the apps directory, apart from that everything should work okay.

Related

Running an app from Terminal with a path

If I can run an app (built in Go but not really necessary) like so from its own directory:
./some_app
How can I run it from a different directory?
You should just be able to add the path to the app you are trying to run to your command. For example if the app is stored in /Users/example/apps/some_app, then you can just directly run "/Users/example/apps/some_app". You can find which directory your app is sitting in (if you are currently in that directory) with the pwd command.
Say you are in the home directory, and the app is under ~/app/some_app then you can run it with app/some_app
If you want to be able to call it with only some_app, it has to be accessible in your $PATH variable.
In go, when you do go install ./... the executable will end up in $GOPATH/bin/ which is normally part of your path if you have a correct configuration.

Location of db/config.yml on a ruby app using standalone-migrations

I'm working on an command-line application that uses the standalone_migrations gem. I have the db/config.yml file and everything works fine when I run the app from the root dir, but when I run it from other dirs (e.g. directly running a script in the /bin directory), the gem cannot find the db/config.yml.
I looked at the gem's source, specifically in the lib/configurator.rb file, but couldn't find a way to set the correct .yml path.
Any help? Thanks.
StandaloneMigrations::Configurator uses relative paths, it loads configuration files on line #23.
This is a bug in the code. A workaround could be to change the working directory to the root before you execute the script. You didn't mention what kind of script you have under /bin, but for example if it is a Bash script, you can do something like:
cd /project/directory && rake db:migrate ...

bundle exec thin start vs ./bundle/bin/thin start

I'm trying to understand how bundle exec works and what it does. I've installed the gems using bundle install like so:
bundle install --binstubs ./bundle/bin --path ./bundle/lib'
This creates a script ./bundle/bin/thin that I can use to start my Rails application using thin like so:
./bundle/bin/thin start -p 8080
However I see most articles on the internet recommend starting thin using bundle exec like this:
bundle exec thin start -p 8080
What is the difference between the two? My tests show that bundle exec doesn't call the ./bundle/bin/thin script, so how does bundle exec differ from the script?
There's no significant difference: they're two ways to accomplish the same thing, which is to run the correct version of the command for your bundle, with the environment set up to ensure that other bundled gens can be loaded by the command. The choice comes down to a matter of convenience.
The benefit of bundle exec is that you don't need to generate binstubs to use it: it just works with the existing Gemfile. This explains why you don't see it invoking the binstub you do have.
Some people don't like having to type bundle exec before every command, so the goal with binstubs is that you can add the directory to the front of your PATH and call the command normally. The drawback is that there is a potential security or usability concern if a bundled gem contains a command that shadows an important system command (e.g., ls).
If you don't put it in your PATH and always call it as bundle/bin/thin, you don't have the security concern, but it also gives you no particular benefit over using bundle exec thin.
In that specific case, there is no difference. bundle exec thin start -p 8080 will end up calling .bundle/bin/thin, but what if you you installed binstubs in a different path? bundle exec thin start will read your .bundle/config to find where your binstubs folder is. If you don't have binstubs installed and say you have 3 versions of thin installed, bundle exec will execute the one that's defined in your Gemfile.
Edit: #tadman also made a good point that I initially missed. When you use bundle exec, the gem environment from your Gemfile will be used, without it, it'll load the latest version of each gem currently installed.
The bundle exec method is the "official" way to do it. If you want to make use of the --binstubs feature, which is unusual, you can.
Remember that ./bundle/bin/thin is not the script, but a wrapper or "stub" that calls the script with the proper bundle exec environment loaded.

How can I view or inspect the $PATH for a bundle exec?

I am troubleshooting a certain gem (spree_cmd) and want to know where "bundle exec" expects the binaries to be.
...bundler/shared_helpers.rb:151:in `bin_path': can't find executable spree.
When I echo $PATH, there is a "spree" somewhere. Confirmed with which spree.
Without a clearer understanding of your issue, I can't offer anything specific.
You can figure out where it is storing your binaries by doing bundle exec which some_binary_from_gemfile it will tell you where that binary is located.
According to the docs it also looks like you can add your own bin directory by setting the $BUNDLE_BIN_PATH environment variable.
Another thing you could do would be to bundle install --binstubs which would create a folder in the root directory containing all of your binaries.
If your binary isn't being added to the path, the first place I would look is your .gemspec, (you must specify where the bin directory is with spec.bindir = 'bin'). I see in the current spree_cmd gem (0.0.4) that it looks correct (haven't tried running it). If that looks correct, then make sure your binary is in the dir you specified, make sure it is executable, make sure the shebang is correct, make sure it's named the same as the binary you're trying to invoke.

How execute a self contained rake build?

We have an application which is compiled using Rake (on windows). We have a new requirement that one of our clients needs to compile the source code in their own environment using a bat file.
So I need to find a way to execute a rake build without installing anything on the host environment (i.e. everything required to do the build needs to be in the source directory, ruby, gems, etc...) from a batch file.
Anyone have any clues how I could get started with this?
Download and install ruby to a folder inside your project (do not add it to your PATH). After go to this folder and delete any "uninstall" file. Go to the folder again with the console (cmd and then use cd path\to\ruby\folder) and run gem install ... to install everything you need. After add a .bat file to run your app. Something like:
#echo off
rubyfolder\bin\ruby.exe myscript.rb
This is a fully portable ruby installation, you can put it in any computer and it will work as well. (I use it as a portable ruby in my pendrive to let me play everywhere with ruby!)
PS.: rake is a script from bin, you can open it with:
rubyfolder\bin\ruby.exe rubyfolder\bin\rake

Resources