diff --git a/build.gradle b/build.gradle index a1bb8682d27..2bc51657f82 100644 --- a/build.gradle +++ b/build.gradle @@ -783,10 +783,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") + } } } diff --git a/rakelib/artifacts.rake b/rakelib/artifacts.rake index 111d44282f8..311c12431af 100644 --- a/rakelib/artifacts.rake +++ b/rakelib/artifacts.rake @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +require 'shellwords' + namespace "artifact" do SNAPSHOT_BUILD = ENV["RELEASE"] != "1" VERSION_QUALIFIER = ENV["VERSION_QUALIFIER"].to_s.strip.empty? ? nil : ENV["VERSION_QUALIFIER"] @@ -127,12 +129,34 @@ namespace "artifact" do result end - # execute Kernel#system call,checking the exist status of the executed command and eventually reporting as exception + ## + # @override safe_system([env,] command... [,options]) + # execute Kernel#system call,checking the exit status of the executed command and eventually reporting as exception def safe_system(*args) - if !system(*args) - status = $? + command = args.dup # avoid mutating input for reporting + env = command.size > 1 && command.first.kind_of?(Hash) ? command.shift : {} + options = command.size > 1 && command.last.kind_of?(Hash) ? command.pop : {} + fail("unsupported options #{options}") unless options.empty? + + # Normalize command to a single string from either a multi-word string + # or an array of individual words + command = command.size > 1 ? Shellwords.join(command.map(&:to_s)) : command.first.to_s + + # prepend the environment + env.each do |k,v| + command.prepend("#{Shellwords.escape(k.to_s)}=#{Shellwords.escape(v.to_s)} ") + end + + output = `#{command} 2>&1` + status = $? + + if !status.success? + puts "Command failed: #{args.inspect}" + puts "Output: #{output}" raise "Got exit status #{status.exitstatus} attempting to execute #{args.inspect}!" end + + true end desc "Generate rpm, deb, tar and zip artifacts" diff --git a/rakelib/lint.rake b/rakelib/lint.rake index c3aa4df45f0..f8d5eea0845 100644 --- a/rakelib/lint.rake +++ b/rakelib/lint.rake @@ -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) @@ -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 \ No newline at end of file diff --git a/rubyUtils.gradle b/rubyUtils.gradle index 94d020543c2..d86ffbfcdc7 100644 --- a/rubyUtils.gradle +++ b/rubyUtils.gradle @@ -151,17 +151,25 @@ 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 + begin + rake = Rake.application + rake.init + rake.load_rakefile + rake['${task}'].invoke(${rakeArgs}) + rescue => e + puts "Rake task error: #{e.class}: #{e.message}" + puts "Backtrace: #{e.backtrace.join("\\n")}" + raise e + end + """ ) }