Custom Rake copy file to current directory in Mac - ruby

Is it possible to make a rake command that copies a file from a Mac and saves it into current directory?
I've tried using cp commands but it doesn't work.
This is what I've tried:
namespace :generate do
desc "Generate empty html5 index"
task :index do
#cp Dir['~/.rake/templates/index.html'], '.'
# cp "~/.rake/templates/index.html ."
end
end

I've just found the answer, I had to use sh command to execute shell commands in rake. Reference here
require 'fileutils'
namespace :generate do
desc "Generate empty html5 index"
task :index do
sh %{ cp ~/.rake/templates/index.html . }
end
end

Related

How to make a call rake console, these settings are set by default?

How to make a call rake console, these settings are set by default?
2.3.0: 001> (ENV [ 'BATTLE_NET_REGION'] = 'eu') and (ENV [ 'BATTLE_NET_LOCALE'] = 'ru_RU') and (ENV [ 'BATTLE_NET_KEY'] = 'my_battle_net_key')
rake task:
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task default: :spec
desc 'Open an irb session preloaded with this library'
task :console do
sh 'irb -rubygems -I lib -r wowrb.rb'
end
You could add tho variables to the shell command:
task :console do
sh 'BATTLE_NET_REGION=eu BATTLE_NET_LOCALE=ru_RU BATTLE_NET_KEY=my_battle_net_key irb -rubygems -I lib -r wowrb.rb'
end
But why do you use a rake task to start a irb console? Would it be easier to start it directly or with a small shell script or alias that sets all variables?

ruby with wildcard rspec command not working on windows machines

I have made a ruby command line "frontend" that sets different environment variables according to user input, a sort of "ncurses" frontend but only in ruby, I know there is Rake and such tools, but we made it this way because we need the variables that can be set at runtime according to need, and the frontend also can send the running results via email to my team.
This works very well in linux machines but in windows its not working.
I track down the problem to the rspec command that has a wildcard in the filename, this works perfectly in linux, but if i run the commands manually in windows, it didn't do anything. If i run the rspec command with one file it works fine, but not with wildcards.
I tried a couple of google searches, tried this workarounds i found, but without success:
(this snippet is inside a ruby file I run with "ruby FILE.rb")
system("rspec #{spec\2.2\web_pc_*} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\web_pc_/*} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\web_pc_'*'} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\web_pc_"*"} -e '#{ARGV[1]}' -o log.txt")
system("rspec #{spec\2.2\'web_pc_*'} -e '#{ARGV[1]}' -o log.txt")
The solution I had found, is to make a Rakefile and run the Rake command. This way all the files execute no matter what OS i'm running them.
Rakefile:
require 'rake/testtask'
Rake::TestTask.new(:all) do |t|
t.libs << "tests"
t.test_files = FileList['tests/test_*.rb']
t.verbose = true
end
task default: :all

run some scripts or all scripts in ruby rake

How can I do the scenario below: (the purpose is to be able to run all selenium scripts I have or only run some scripts I want if I pass test files as arguments from terminal window with the below code and scenario:
if test files are passed in terminal window
then system will run below codes:
scripts = ENV[scripts].plit(',')
FileList[scripts].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
if test files are NOT passed in terminal window (it means I want to run all scripts I have in my test suite), then system will run below codes:
FileList['test*.rb'].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
If I understand correctly, can you just use an if statement?
task :default do
if ENV[scripts]
scripts = ENV[scripts].split(',')
FileList[scripts].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
else
FileList['test*.rb'].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
end
end

How to print to stdout from system command in Rake?

I have the following Rakefile
desc "Runs tests"
namespace :test do
task :api do
`mocha`
end
end
When I run the command rake test:api, I don't get the nice output of dots that I would if I just ran the command mocha.
How do I print the output of a command real-time in a rake task?
You can just put the output:
puts `mocha`
The backticks ` are calling the command mocha and return the output of the command.
You may also use %x{}:
puts %x{mocha}
Or you use system:
system('mocha')
Or you store the output for later use in a variable:
output = `mocha`
puts output

Why can't i run rake -f?

I really have no idea what im doing. I'm trying to get rake to work so I can run rake workers:start but without having to have the working directory be the same as the folder of the rake file.
for example if i'm in my app directory, the above command works fine, but if i run rake -f ~/Code/my-app/Rakefile workers:start it says "Cannot load such file -- ./database
I'm using Sinatra (rack), and ultimately my goal is to try and install god so I can create a resque worker in production
require File.dirname(__FILE__) + "/main"
require 'resque/tasks'
namespace :workers do
desc "Launch single worker for processing jobs"
task :start do
ENV['QUEUE'] ||= '*'
puts "=== Launching single worker on '#{ENV['QUEUE']}' queue(s) with PID #{Process.pid}"
Rake::Task['resque:work'].invoke
end
end
rake -f ~/Code/my-app/Rakefile rake:workers:start
^ shouldn't need this one.
Also you might want to cd to appropriate dir before running rake.
cd ~/Code/my-app && rake workers:start

Resources