Skip to content

Commit

Permalink
Extend ruby linting tasks to handle file inputs (#16660)
Browse files Browse the repository at this point in the history
This commit extends the gradle and rake tasks to pass through a list of files
for rubocop to lint. This allows more specificity and fine grained control for
linting when the consumer of the tasks only wishes to lint a select few files.
  • Loading branch information
donoghuc authored Nov 12, 2024
1 parent 74d87c9 commit ff8c154
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,16 @@ class JDKDetails {
}

tasks.register("lint") {
// Calls rake's 'lint' task
description = "Lint Ruby source files. Use -PrubySource=file1.rb,file2.rb to specify files"
dependsOn installDevelopmentGems
doLast {
rake(projectDir, buildDir, 'lint:report')
if (project.hasProperty("rubySource")) {
// Split the comma-separated files and pass them as separate arguments
def files = project.property("rubySource").split(",")
rake(projectDir, buildDir, "lint:report", *files)
} else {
rake(projectDir, buildDir, "lint:report")
}
}
}

Expand Down
47 changes: 31 additions & 16 deletions rakelib/lint.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

namespace "lint" do
module RuboCLI
def self.run!(*args)
Expand All @@ -25,28 +24,44 @@ namespace "lint" do
end
end

# task that runs lint report
desc "Report all Lint Cops"
task "report" do
RuboCLI.run!("--lint")
desc "Report all Lint Cops. Optional: Specify one or more files"
task :report, [:file] do |t, args|
files = [args[:file], *args.extras].compact

if files.empty?
RuboCLI.run!("--lint")
else
puts "Running lint report on specific files: #{files.join(', ')}"
RuboCLI.run!("--lint", *files)
end
end

# Tasks automatically fixes a Cop passed as a parameter (e.g. Lint/DeprecatedClassMethods)
# TODO: Add a way to autocorrect all cops, and not just the one passed as parameter
desc "Automatically fix all instances of a Cop passed as a parameter"
task "correct", [:cop] do |t, args|
# Tasks automatically fixes a Cop passed as a parameter
desc "Automatically fix all instances of a Cop passed as a parameter. Optional: Specify one or more files"
task :correct, [:cop] do |t, args|
if args[:cop].to_s.empty?
puts "No Cop has been provided, aborting..."
exit(0)
else
puts "Attempting to correct Lint issues for: #{args[:cop].to_s}"
RuboCLI.run!("--autocorrect-all", "--only", args[:cop].to_s)
files = args.extras
if files.empty?
puts "Attempting to correct Lint issues for: #{args[:cop]}"
RuboCLI.run!("--autocorrect-all", "--only", args[:cop])
else
puts "Attempting to correct Lint issues for #{args[:cop]} in files: #{files.join(', ')}"
RuboCLI.run!("--autocorrect-all", "--only", args[:cop], *files)
end
end
end

# task that automatically fixes code formatting
desc "Automatically fix Layout Cops"
task "format" do
RuboCLI.run!("--fix-layout")
desc "Automatically fix Layout Cops. Optional: Specify one or more files"
task :format, [:file] do |t, args|
files = [args[:file], *args.extras].compact
if files.empty?
RuboCLI.run!("--fix-layout")
else
puts "Running format fixes on specific files: #{files.join(', ')}"
RuboCLI.run!("--fix-layout", *files)
end
end
end
end
7 changes: 4 additions & 3 deletions rubyUtils.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,18 @@ void buildGem(File projectDir, File buildDir, String gemspec) {
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param plugin Plugin to run specs for
* @param args CLI arguments to pass to rspec
* @param args Optional arguments to pass to the rake task
*/
void rake(File projectDir, File buildDir, String task) {
void rake(File projectDir, File buildDir, String task, String... args) {
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
jruby.currentDirectory = projectDir
jruby.runScriptlet("require 'rake'; require 'time'")
def rakeArgs = args ? "'${args.join("','")}'" : ""
jruby.runScriptlet("""
rake = Rake.application
rake.init
rake.load_rakefile
rake['${task}'].invoke
rake['${task}'].invoke(${rakeArgs})
"""
)
}
Expand Down

0 comments on commit ff8c154

Please sign in to comment.