If a Rake File exists with the following code:
file 'assets/*/map.a' => ['map.b', 'map.c'] do
# Code goes here...
end
I want to know what the name of the directory in the glob is (instead of the file). Any suggestions?
file 'assets/*/map.a' => ['map.b', 'map.c'] do |path|
File.basename(File.dirname(path))
end
Related
I have a set of files in a folder ../SomeFolder/AndAnother/dist
the dist folder contains a bunch of files and folders that I want to move up a level in a Rake task.
So
../SomeFolder/AndAnother/dist/subFolder/a.txt becomes ../SomeFolder/AndAnother/subFolder/a.txt
I can do this on linux by
task :lift_to_parent do
sh('mv', '../SomeFolder/AndAnother/dist/*', '../SomeFolder/AndAnother')
end
but this Rake task also runs on Windows and on that OS i get Errno::EACCES: Permission denied # unlink_internal
I'm hoping that FileUtils.mv will work on both linux and windows...
but if I
task :lift_to_parent do
FileUtils.mv '../SomeFolder/AndAnother/dist', '../SomeFolder/AndAnother', :force => true
end
I get ArgumentError: same file: ../SomeFolder/AndAnother/dist and ../SomeFolder/AndAnother/dist so I'm clearly missing something to allow FileUtils.mv to copy up a level (or going about this the wrong way)
So, how do I fix my FileUtils version or otherwise use a Rake task to copy a folder structure to its parent?
I've ended up doing this
task : lift_to_parent do
copied_by_jenkins = '../SomeFolder/AndAnother/dist'
copy_pattern = "#{copied_by_jenkins}/**/*"
target_directory = '../SomeFolder/AndAnother/Public'
next unless File.exists? copied_by_jenkins
FileList[copy_pattern].each do |file|
file_path = File.dirname(file).sub! copied_by_jenkins, ''
file_name = File.basename(file)
target_directory = File.join(target_directory, file_path)
destination = File.join(target_directory, file_name)
FileUtils.mkdir_p target_directory
FileUtils.copy_file(file, destination) unless File.directory? file
end
FileUtils.remove copied_by_jenkins
end
but that seems like a lot of the typing to achieve my goal
i want to read first line of file by using following function in ruby
IO.readlines("path")[0]
But file is not in current directory, so i use path there
puts IO.readlines("Home/Documents/vikas/SHIF.doc")
but it is giving error as
a1.rb:1:in `readlines': No such file or directory # rb_sysopen - Home/Documents/vikas/SHIF.doc (Errno::ENOENT)
from a1.rb:1:in `<main>'
You can also open a file and read only the first line instead of the entire file
File.open("Home/Documents/vikas/SHIF.doc").readline
You can use File.expand_path:
puts IO.readlines(File.expand_path("Home/Documents/vikas/SHIF.doc", __FILE__))
Note however that it will create path relatively to a file directory, not to a root directory.
If you are using rails, you could use:
puts IO.readlines(Rails.root.join 'Home', 'Documents', 'vikas', 'SHIF.doc')
I'm making gem that copy files from /template directory (inside the gem) into the current directory of the console.
Here's what it looks like:
require "fileutils"
# Get the console's current directory
destination_dir = Dir.pwd
# Home directory of my gem, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0
home_dir = File.expand_path( "..", File.dirname(__FILE__) )
# Template directory, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
template_dir = File.join( home_dir, "template" )
FileUtils.copy_file( template_dir, destination_dir )
And I got this error:
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:1370:in `initialize': Permission denied -
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template (Errno::
EACCES)
I have checked that the directory does exists by running Dir[template_dir].
Any solution? Thanks
UPDATE to answer comments below
#Babai
I added this line before copy_file, but still doesn't work. Am I doing it right?
FileUtils.chmod(0777, template_dir)
#mudasobwa
Here's the result of the code
# puts "#{template_dir} \n #{destination_dir}"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
C:/Users/myname/Documents/Test
My bad. My template directory contains another folders. So I need to use cp_r instead of copy_file
FileUtils.cp_r( template_dir, destination_dir )
I want ruby to look for a file in the current folder that ends with a certain extension. The extension would be .app.zip
How would I do this?
To get the first matching file in the current directory, you can use:
file=Dir['*.app.zip'].first
Or to find all .app.zip files in certain directory, for example files/*.app.zip, you can use something like :
Dir[File.join('files', '*.app.zip')].each |file|
puts "found: #{file}"
end
Alternative to Dir:
require "find"
Find.find(folder) do |file|
puts "#{file}" if file=~/\.app\.zip/
end
How do I get the directory where the rakefile.rb is located?
I want to use this as my root directory to locate everything off.
use __FILE__ to get the file name then you can get the directory from there:
in test.rb
puts __FILE__
output:
/users/foo/test.rb
__FILE__ resolves to the full path of the file it is in.
Use this to get the dir name:
File.dirname(__FILE__)
You can get it by calling application.original_dir method. In task you can achieve application object using application method on task object.
Why not just use Dir.pwd
?
As of Ruby 2 you can use __dir__ instead of File.dirname(__FILE__) to get the directory that contains the current script.
If this is a RoR app your Rakefile.rb should be in your RAILS_ROOT directory. So in any script you can specify file location like
config.load_paths += %W( #{RAILS_ROOT}/extras )