If-else and error handing in command prompt - shell

I am trying to detect if certain ruby gem is not installed, install it first and then proceed.
For instance:
gem which rails
returns path to gem's driver file (.rb) on stdout, if gem exists, if not it checks throws error on stderr.
How can I solve it?
Pseudo code:
if throws "gem which example1" ( gem install example1 )
if throws "gem which example2" ( gem install example2 )
if throws "gem which example3" ( gem install example3 )
:: install other stuff
Either cmd or powershell would work.
I need to add these in AppVeyor CI yaml configuration file. AppVeyor CI has this wonderful feature of caching the directory for DRY'ing installations to improve build performance. So I have cached the gems directory and it restores fine on running the build, but then gem install reinstalls the gem anyway!

By using powershell to run the command you can use error redirection to check if the command was successful:
$gemPath = gem which example3 2>$null
if ($gemPath)
{
# We got a path for the gem driver
}
else
{
# Failed, gem might not be installed
}
Explanation: We're telling powershell to store the result of "gem which example3" in the variable $gemPath, however if there is an error we want to redirect the error to the variable $null by using 2>$null
You can also replace 2 with the number corresponding to the stream you'd like to capture (Success, Error, Warning etc)
* All output
1 Success output
2 Errors
3 Warning messages
4 Verbose output
5 Debug messages
More on redirection here About_Redirection

Try, in .cmd script:
gem which rails 1>nul 2>&1
IF ERRORLEVEL 1 (gem install rails)
I'm assuming here you want the outputs to stdout and stderr stifled for purposes of this batch script - otherwise, drop the 1>nul 2>&1.

Related

Install a particular Ruby version using chef-run

I have been trying to install a particular (latest) version of Ruby using Chef Workstation and its included chef-run CLI.
This is the recipe I'm using for Ruby:
package 'ruby' do
version '2.5.3'
action :install
end
Which, running with the command line
chef-run -i /path-to/private_key user#host ruby.rb
Produces the not very helpful message:
[✔] Packaging cookbook... done!
[✔] Generating local policyfile... exporting... done!
[✖] Applying ruby from ruby.rb to target.
└── [✖] [127.0.0.1] Failed to converge ruby.
The converge of the remote host failed for the
following reason:
Expected process to exit with [0], but received '100'
I have tried to run it with the -V flag, or look for a log file, but I can't seem to find it. Any idea?
raise the log_level by setting it to debug in the chef-workstation configuration
$ cat ~/.chef-workstation/config.toml
[log]
level="debug"

Ruby: 'Zip is not installed' error message when running on Win CMD this: buildpack-packager --uncached

I'm running cmd command buildpack-packer --uncached (or any other option of buildpack-packer). I had many error messages prior that. They were caused by bad content of manifest.yml. I corrected them. So now I receive this error message: Zip is not installed (RuntimeError)
I used gem install to install zip gem and rubyzip gem (as first did not work, so I tried a second). So now both not helping to get rid of this error message.
Here is a part of the installed gem list:
And here is the code that drops this error (found it based on the error message in file: C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/buildpack-packager-2.3.4/lib/buildpack/packager.rb):
I'm quite new in Ruby, so maybe I do some very basic mistake...
Thx in advance!!
Please don't use pictures or screenshots in your post. Use plaintext only.
I think you are misunderstanding the code:
_, _, status = Open3.capture3('which zip')
It checks if you have any zip program (executable) installed not a ruby gem (library). It actually executes which zip in your cmd shell.
For example on my system it found an oracle one:
c:\> which zip
/c/app/oracle/client11g/product/11.2.0/client/bin/zip
Then if you test it in irb:
irb(main):004:0> _, _, status = Open3.capture3('which zip')
=> ["/c/app/oracle/client11g/product/11.2.0/client/bin/zip\n", "", #<Process::Status: pid 10944 exit 0>]
You can see that the executable was found and success state is indicated by the 0. The variable status holds the return message - status => #<Process::Status: pid 10944 exit 0>
I have the which program from dev_kit:
c:\>which which
/c/prg_sdk/ruby/dev_kit/bin/which

Node.Js: Trying to access a ruby gem inside node app on heroku

I am trying to use this https://github.com/mooktakim/image_optim_bin on my Heroku application, and I'm really not sure as to how to do it. Right now I have the following code trying to run pngcrush
exec('pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB ' + file + ' ' + file, function(error, stdout, stderr) {
console.log('PNGCRUSH OUTPUT: ', error, stdout, stderr);
});
But it results in the following error on my heroku logs output:
PNGCRUSH OUTPUT: { [Error: Command failed: /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs': Could not find 'image_optim_bin' (>= 0) among 9 total gem(s) (Gem::LoadError)
This is my Gemfile
gem 'image_optim_bin', :git => 'https://github.com/mooktakim/image_optim_bin.git'
This is the first time I've tried using ruby gems inside a nodejs application so I really am unclear of how to do so. Is there a specific directory I need to target to run the gem?
Are you already using Heroku multi buildpacks to support both Node and Ruby? If so, I recommend running heroku run bash and then trying to execute pngcrush from the command line. You should be able to quickly identify the path you need.

Jeet Framework: Got an error when trying to run Jeet 2:

Got an error when trying to run Jeet 2:
In CMD:
Compiling Stylus OK!
Saving Compiled Stylus OK!
Live Reload is on and listening!
Compiling SCSS ... Error!
I'm on Windows XP
Is there something with the Ruby installation on Windows?
I followed your tutorial on https://github.com/CorySimmons/jeet#quick-start
If you can type ruby into your Command Prompt and it doesn't return an error then you have Ruby.
You need to install Compass as well: http://compass-style.org/install/
In Windows command prompt you don't use the $ in front of the gem command! You just type gem update system, instead of $ gem update system.

Errno::EEXIST File Exists error when installing 'ferret' gem from local .gem file

I am trying to install the ferret ruby gem on a RHEL zlinux (s390x architecture) machine, and am trying to install a .gem file after patching it so that it will compile.
But even trying to install the pristine fetched gem, it fails as follows:
[ me#s390x ]$ sudo gem fetch ferret
Downloaded ferret-0.11.6
[ me#s390x ]$ sudo gem install -lV ferret-0.11.6.gem
Installing gem ferret-0.11.6
Using local gem /home/rubyusr/rubygems/gems/cache/ferret-0.11.6.gem
/home/rubyusr/rubygems/gems/gems/ferret-0.11.6/bin
ERROR: While executing gem ... (Errno::EEXIST)
File exists - /home/rubyusr/rubygems/gems/gems/ferret-0.11.6/bin
None of the above-mentioned directories or files related to "ferret" existed before running this command.
Also strange is that /home/rubyusr/rubygems/gems/gems/ferret-0.11.6/bin is a directory, although maybe that is a normal complaint.
A final complicating factor is when I run the gem command I am actually running a shell script that sets the environment variables for my unusual rubygems directory (I haven't had any problems so far with this set up). Here is my gem shell script:
#!/bin/bash
export GEM_HOME=/home/rubyusr/rubygems/gems
export GEM_PREFIX=/home/rubyusr/rubygems
export RUBYLIB=$GEM_PREFIX/lib:/usr/lib/ruby:/usr/lib/ruby/site_ruby:/usr/lib/site_ruby
export GEM_PATH=$GEM_HOME
OUR_GEM_COMMAND=$GEM_PREFIX/bin/gem
$OUR_GEM_COMMAND $#
EDIT:
I forgot to add that running the gem install command normally does not seem to result in this error (but ferret fails to compile), with the error:
posh.h:515:4: error: #error POSH cannot determine target CPU
There is a bug in Debian asking for adding support for arm64:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=770922
It needs adding a few lines in the file ext/posh.h to add support for that CPU:
--- a/ext/posh.h
+++ b/ext/posh.h
## -512,6 +512,11 ##
# define POSH_CPU_STRING "PA-RISC"
#endif
+#if defined __aarch64__
+# define POSH_CPU_AARCH64 1
+# define POSH_CPU_STRING "AArch64"
+#endif
+
#if !defined POSH_CPU_STRING
# error POSH cannot determine target CPU
# define POSH_CPU_STRING "Unknown" /* this is here for Doxygen's benefit */
Adding support for s390 was about adding these lines:
#if defined __s390__
# define POSH_CPU_S390 1
# define POSH_CPU_STRING "S/390"
#endif
if you know the corresponding values for S390/X, you can add them in there.

Resources