Skip to content

Commit 950b35c

Browse files
committed
Extend ruby linting tasks to handle file inputs (#16660)
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.
1 parent 27dc298 commit 950b35c

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,16 @@ class JDKDetails {
783783
}
784784

785785
tasks.register("lint") {
786-
// Calls rake's 'lint' task
786+
description = "Lint Ruby source files. Use -PrubySource=file1.rb,file2.rb to specify files"
787787
dependsOn installDevelopmentGems
788788
doLast {
789-
rake(projectDir, buildDir, 'lint:report')
789+
if (project.hasProperty("rubySource")) {
790+
// Split the comma-separated files and pass them as separate arguments
791+
def files = project.property("rubySource").split(",")
792+
rake(projectDir, buildDir, "lint:report", *files)
793+
} else {
794+
rake(projectDir, buildDir, "lint:report")
795+
}
790796
}
791797
}
792798

rakelib/lint.rake

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
1817
namespace "lint" do
1918
module RuboCLI
2019
def self.run!(*args)
@@ -25,28 +24,44 @@ namespace "lint" do
2524
end
2625
end
2726

28-
# task that runs lint report
29-
desc "Report all Lint Cops"
30-
task "report" do
31-
RuboCLI.run!("--lint")
27+
desc "Report all Lint Cops. Optional: Specify one or more files"
28+
task :report, [:file] do |t, args|
29+
files = [args[:file], *args.extras].compact
30+
31+
if files.empty?
32+
RuboCLI.run!("--lint")
33+
else
34+
puts "Running lint report on specific files: #{files.join(', ')}"
35+
RuboCLI.run!("--lint", *files)
36+
end
3237
end
3338

34-
# Tasks automatically fixes a Cop passed as a parameter (e.g. Lint/DeprecatedClassMethods)
35-
# TODO: Add a way to autocorrect all cops, and not just the one passed as parameter
36-
desc "Automatically fix all instances of a Cop passed as a parameter"
37-
task "correct", [:cop] do |t, args|
39+
# Tasks automatically fixes a Cop passed as a parameter
40+
desc "Automatically fix all instances of a Cop passed as a parameter. Optional: Specify one or more files"
41+
task :correct, [:cop] do |t, args|
3842
if args[:cop].to_s.empty?
3943
puts "No Cop has been provided, aborting..."
4044
exit(0)
4145
else
42-
puts "Attempting to correct Lint issues for: #{args[:cop].to_s}"
43-
RuboCLI.run!("--autocorrect-all", "--only", args[:cop].to_s)
46+
files = args.extras
47+
if files.empty?
48+
puts "Attempting to correct Lint issues for: #{args[:cop]}"
49+
RuboCLI.run!("--autocorrect-all", "--only", args[:cop])
50+
else
51+
puts "Attempting to correct Lint issues for #{args[:cop]} in files: #{files.join(', ')}"
52+
RuboCLI.run!("--autocorrect-all", "--only", args[:cop], *files)
53+
end
4454
end
4555
end
4656

47-
# task that automatically fixes code formatting
48-
desc "Automatically fix Layout Cops"
49-
task "format" do
50-
RuboCLI.run!("--fix-layout")
57+
desc "Automatically fix Layout Cops. Optional: Specify one or more files"
58+
task :format, [:file] do |t, args|
59+
files = [args[:file], *args.extras].compact
60+
if files.empty?
61+
RuboCLI.run!("--fix-layout")
62+
else
63+
puts "Running format fixes on specific files: #{files.join(', ')}"
64+
RuboCLI.run!("--fix-layout", *files)
65+
end
5166
end
52-
end
67+
end

rubyUtils.gradle

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,14 @@ void buildGem(File projectDir, File buildDir, String gemspec) {
151151
* @param projectDir Gradle projectDir
152152
* @param buildDir Gradle buildDir
153153
* @param plugin Plugin to run specs for
154-
* @param args CLI arguments to pass to rspec
154+
* @param args Optional arguments to pass to the rake task
155155
*/
156-
void rake(File projectDir, File buildDir, String task) {
156+
void rake(File projectDir, File buildDir, String task, String... args) {
157157
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
158158
jruby.currentDirectory = projectDir
159159
jruby.runScriptlet("require 'rake'; require 'time'")
160+
def rakeArgs = args ? "'${args.join("','")}'" : ""
160161
jruby.runScriptlet("""
161-
<<<<<<< HEAD
162-
rake = Rake.application
163-
rake.init
164-
rake.load_rakefile
165-
rake['${task}'].invoke
166-
=======
167162
begin
168163
rake = Rake.application
169164
rake.init
@@ -174,7 +169,7 @@ void rake(File projectDir, File buildDir, String task) {
174169
puts "Backtrace: #{e.backtrace.join("\\n")}"
175170
raise e
176171
end
177-
>>>>>>> 0d931a50 (Surface failures from nested rake/shell tasks (#17310))
172+
178173
"""
179174
)
180175
}

0 commit comments

Comments
 (0)