Call nested rake file from the root directory - ruby

How can I run rake file for a nested project from the root directory? (2 cases: from console and from the root rakefile). Assume that I cannot modify the nested rakefile and that it must have 'libs/someproject' as the working directory.
Here is my project structure:
-root
--rakefile.rb
--libs
---someproject
----rakefile.rb

well, this is my current solution:
task :build_someproject do
Dir.chdir 'libs/someproject' do
system 'rake build'
end
end

Related

Should Rspec file structure match file structure of code being tested?

I am writing a gem. I want to test some of the classes. I have them in modules.
The file structure:
my_gem
/lib
/my_gem
/practices
/texas
/medical_practice.rb
medical_practice.rb:
module MyGem
module Practices
module Texas
class MedicalPractice < Practice
end
end
end
end
In my spec directory, should I follow the same structure?
spec
/practices
/texas
/medical_practice_spec.rb
Or is it best practice to place medical_practice_spec.rb right under the /spec directory?
In my spec directory, should I follow the same structure?
Yes.
Usually in RSpec the spec structure matches the directory structure of the files under test.
Another example (when using Rails):
spec/controllers/users_controller_spec.rb would test app/controllers/users_controller.rb

Gradle: getting the root project directory path when starting with a custom build file

The structure of my Gradle project is the following:
Project
├── app
└── build.gradle
├── foo
└── bar.txt
·
·
·
└── build.gradle
Normally to get the absolute path of the foo folder I can just simply do new File('foo').getAbsolutePath() in the root build.gradle file.
But this unfortunately doesn't work if you run the gradle script from outside the project directory, for example doing something like this:
$ trunk/gradlew -b trunk/build.gradle tasks
With the previous command gradle is looking for the foo directory in the parent of the Project, because I started the script from there.
Is there a way to get the absolute path of the Project where the build.gradle is, even if you start your script from another directory? Or is there any other way to get a reference of a directory in the same folder where the script is?
I've tried also with getClass().protectionDomain.codeSource.location.path but it is returning the path to the gradle cache directory.
I got past this problem by ensuring Java userDir was set to the project directory (i.e. project.projectDir) at the top of my build.gradle file, as follows:
System.setProperty( "user.dir", project.projectDir.toString() )
println " project dir: "+ System.getProperty("user.dir");
This can be checked by executing a separate (Groovy) code file such as:
println "User Dir: ${System.getProperty( 'user.dir' )}"
You can output the Gradle project values before and after using these statements.
println "Root project: ${project.rootProject}";
println " rootDir: ${project.rootDir}"
println " projectDir: ${project.projectDir}";
println " project dir: ${System.getProperty("user.dir")}";
If you have sub-projects, projectDir is not the same as rootDir.
This hasn't fixed my actual problem but it has ensured that I'm opening the correct file (relative to the location of build.gradle.
new File('foo') by definition (look at its JavaDoc) makes a path relative to the current working directory, so depends on where you call the app from. If you want a path relative to the project folder, use project.file('foo'), or as project is the default for resolving the method just file('foo') and you get the relative path resolved against the project directory, not the working directory. So use file('foo').absolutePath and you will be fine.
In the build.gradle file just use projectDir to get the absolute path of the build.gradle file. from there you can navigate your project's files. read this for more info:
https://www.tutorialspoint.com/gradle/gradle_build_script.htm
I was using new File() and path to get the source directory into the gradle file but in Macbook with M1 Chip it's not working, let me share the code for previous and new version:
Older code:
new File("app/src/")
Updated code:
new File(project.projectDir.getAbsolutePath() + "/src/")

Specify Cheffile to ChefSpec Server Runner

I'm trying to add some unit tests for a cookbook of mine (say cookbook1) with ChefSpec::ServerRunner.
My directory structure is as follows:
mycookbooks
->cookbook1
->->recipes
->->spec
->cookbook2
-> Cheffile
I have added require 'chefspec/librarian' to resolve the dependency cookbooks but this assumes that Cheffile is in the same directory cookbook1.
Is there a way I can specify the path of the Cheffile to
ChefSpec::ServerRunner.
Call Dir.chdir before creating the runner object. The relevant code in ChefSpec is here and you can see it uses Dir.pwd to initialize Librarian.

Rake `directory` does not recursively create folders

I'm trying out Rake today to build my project, coding along with Jim Weirich's presentation. I have a task create_directories:
task :create_directories do
directory('build/subfolder')
end
Now when I execute rake create_directories, it outputs mkdir -p build and the build folder is created, but not the subfolder. Why is the subfolder not created as well?
directory:
private instance method directory(*args, &block) in Rake::DSL in rake\dsl_definition.rb
Documentation:
Declare a set of files tasks to create the given directories on demand.
Example: directory "testdata/doc"
You can use mkdir_p in FileUtils
task :create_directories do
FileUtils.mkdir_p 'build/subfolder'
end
documentation
HTH

How do I Declare a Rake::PackageTask with Prerequisites?

In Rake, I can use the following syntax to declare that task charlie requires tasks alpha and bravo to have been completed first.
task :charlie => [:alpha, :bravo]
This seems to work fine if charlie is a typical Rake task or a file task but I cannot figure out how to do this for a Rake::PackageTask. Here are the relevant parts of the rakefile so far:
require 'rake/packagetask'
file :package_jar => [:compile] do
puts("Packaging library.jar...")
# code omitted for brevity, but this bit works fine
end
Rake::PackageTask.new("library", "1.0") do |pt|
puts("Packaging library distribution artefact...")
pt.need_tar = true
pt.package_files = ["target/library.jar"]
end
task :package => :package_jar
What's happening here is that, for a clean build, it complains that it doesn't "know how to build task 'target/library.jar'". I have to run rake package_jar from the command line manually to get it to work, which is a bit of a nuisance. Is there any way I can make package depend on package_jar?
For what it's worth, I am using Rake version 0.9.2.2 with Ruby 1.8.7 on Linux.
When you run rake package (without previously running anything else to create any needed files) Rake sees that the package task needs the file target/library.jar. Since this file doesn’t yet exist Rake checks to see if it knows how to create it. It doesn’t know of any rules that will create this file, so it fails with the error you see.
Rake does have a task that it thinks will create a file named package_jar, and that task in fact creates the file target/library.jar, but it doesn’t realise this.
The fix is to tell Rake exactly what file is created in the file task. Rake will then automatically find the dependency.
Change
file :package_jar => [:compile] do
to
file 'target/library.jar' => [:compile] do
and then remove the line
task :package => :package_jar
since package_jar no longer exists and Rake will find the dependency on the file by itself.
In general in rake, if you want to add a dependency to a task, you need that task's name. So you need to figure out the name of the actual rake task that Rake::PackageTask is registering.
The easiest way to do this is by running with --trace — it lists each task's name as it is executing.
(I believe the name of a buildr package task is the filename of the package it produces, but I don't remember for certain. Use --trace to find out.)
You can add a dependency to any task by writing,
someTask.enhance [other, tasks]
where other and tasks can be either task names or task objects.
So in your case, you could write:
library = Rake::PackageTask.new(...) do
...
end
task(:package).enhance([library])

Resources