code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def namespace(name=nil, &block) # :doc: name = name.to_s if name.kind_of?(Symbol) name = name.to_str if name.respond_to?(:to_str) unless name.kind_of?(String) || name.nil? raise ArgumentError, "Expected a String or Symbol for a namespace name" end Rake.application.in_namespace(name, &block) end
Create a new rake namespace and use it for evaluating the given block. Returns a NameSpace object that can be used to lookup tasks defined in the namespace. Example: ns = namespace "nested" do # the "nested:run" task task :run end task_run = ns[:run] # find :run in the given namespace. Tasks can also be defined in a namespace by using a ":" in the task name: task "nested:test" do # ... end
namespace
ruby
ruby/rake
lib/rake/dsl_definition.rb
https://github.com/ruby/rake/blob/master/lib/rake/dsl_definition.rb
MIT
def rule(*args, &block) # :doc: Rake::Task.create_rule(*args, &block) end
Declare a rule for auto-tasks. Example: rule '.o' => '.c' do |t| sh 'cc', '-c', '-o', t.name, t.source end
rule
ruby
ruby/rake
lib/rake/dsl_definition.rb
https://github.com/ruby/rake/blob/master/lib/rake/dsl_definition.rb
MIT
def desc(description) # :doc: Rake.application.last_description = description end
Describes the next rake task. Duplicate descriptions are discarded. Descriptions are shown with <code>rake -T</code> (up to the first sentence) and <code>rake -D</code> (the entire description). Example: desc "Run the Unit Tests" task test: [:build] do # ... run tests end
desc
ruby
ruby/rake
lib/rake/dsl_definition.rb
https://github.com/ruby/rake/blob/master/lib/rake/dsl_definition.rb
MIT
def import(*fns) # :doc: fns.each do |fn| Rake.application.add_import(fn) end end
Import the partial Rakefiles +fn+. Imported files are loaded _after_ the current file is completely loaded. This allows the import statement to appear anywhere in the importing file, and yet allowing the imported files to depend on objects defined in the importing file. A common use of the import statement is to include files containing dependency declarations. See also the --rakelibdir command line option. Example: import ".depend", "my_rules"
import
ruby
ruby/rake
lib/rake/dsl_definition.rb
https://github.com/ruby/rake/blob/master/lib/rake/dsl_definition.rb
MIT
def initialize(*patterns) @pending_add = [] @pending = false @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup @exclude_procs = DEFAULT_IGNORE_PROCS.dup @items = [] patterns.each { |pattern| include(pattern) } yield self if block_given? end
Create a file list from the globbable patterns given. If you wish to perform multiple includes or excludes at object build time, use the "yield self" pattern. Example: file_list = FileList.new('lib/**/*.rb', 'test/test*.rb') pkg_files = FileList.new('lib/**/*') do |fl| fl.exclude(/\bCVS\b/) end
initialize
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def include(*filenames) # TODO: check for pending filenames.each do |fn| if fn.respond_to? :to_ary include(*fn.to_ary) else @pending_add << Rake.from_pathname(fn) end end @pending = true self end
Add file names defined by glob patterns to the file list. If an array is given, add each element of the array. Example: file_list.include("*.java", "*.cfg") file_list.include %w( math.c lib.h *.o )
include
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def exclude(*patterns, &block) patterns.each do |pat| if pat.respond_to? :to_ary exclude(*pat.to_ary) else @exclude_patterns << Rake.from_pathname(pat) end end @exclude_procs << block if block_given? resolve_exclude unless @pending self end
Register a list of file name patterns that should be excluded from the list. Patterns may be regular expressions, glob patterns or regular strings. In addition, a block given to exclude will remove entries that return true when given to the block. Note that glob patterns are expanded against the file system. If a file is explicitly added to a file list, but does not exist in the file system, then an glob pattern in the exclude list will not exclude the file. Examples: FileList['a.c', 'b.c'].exclude("a.c") => ['b.c'] FileList['a.c', 'b.c'].exclude(/^a/) => ['b.c'] If "a.c" is a file, then ... FileList['a.c', 'b.c'].exclude("a.*") => ['b.c'] If "a.c" is not a file, then ... FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
exclude
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def clear_exclude @exclude_patterns = [] @exclude_procs = [] self end
Clear all the exclude patterns so that we exclude nothing.
clear_exclude
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def resolve if @pending @pending = false @pending_add.each do |fn| resolve_add(fn) end @pending_add = [] resolve_exclude end self end
Resolve all the pending adds now.
resolve
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def sub(pat, rep) inject(self.class.new) { |res, fn| res << fn.sub(pat, rep) } end
Return a new FileList with the results of running +sub+ against each element of the original list. Example: FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
sub
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def gsub(pat, rep) inject(self.class.new) { |res, fn| res << fn.gsub(pat, rep) } end
Return a new FileList with the results of running +gsub+ against each element of the original list. Example: FileList['lib/test/file', 'x/y'].gsub(/\//, "\\") => ['lib\\test\\file', 'x\\y']
gsub
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def sub!(pat, rep) each_with_index { |fn, i| self[i] = fn.sub(pat, rep) } self end
Same as +sub+ except that the original file list is modified.
sub!
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def gsub!(pat, rep) each_with_index { |fn, i| self[i] = fn.gsub(pat, rep) } self end
Same as +gsub+ except that the original file list is modified.
gsub!
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def pathmap(spec=nil, &block) collect { |fn| fn.pathmap(spec, &block) } end
Apply the pathmap spec to each of the included file names, returning a new file list with the modified paths. (See String#pathmap for details.)
pathmap
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def ext(newext="") collect { |fn| fn.ext(newext) } end
Return a new FileList with <tt>String#ext</tt> method applied to each member of the array. This method is a shortcut for: array.collect { |item| item.ext(newext) } +ext+ is a user added method for the Array class.
ext
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def egrep(pattern, *options) matched = 0 each do |fn| begin File.open(fn, "r", *options) do |inf| count = 0 inf.each do |line| count += 1 if pattern.match(line) matched += 1 if block_given? yield fn, count, line else puts "#{fn}:#{count}:#{line}" end end end end rescue StandardError => ex $stderr.puts "Error while processing '#{fn}': #{ex}" end end matched end
Grep each of the files in the filelist using the given pattern. If a block is given, call the block on each matching line, passing the file name, line number, and the matching line of text. If no block is given, a standard emacs style file:linenumber:line message will be printed to standard out. Returns the number of matched items.
egrep
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def existing select { |fn| File.exist?(fn) }.uniq end
Return a new file list that only contains file names from the current file list that exist on the file system.
existing
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def existing! resolve @items = @items.select { |fn| File.exist?(fn) }.uniq self end
Modify the current file list so that it contains only file name that exist on the file system.
existing!
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def partition(&block) # :nodoc: resolve result = @items.partition(&block) [ self.class.new.import(result[0]), self.class.new.import(result[1]), ] end
FileList version of partition. Needed because the nested arrays should be FileLists in this version.
partition
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def to_s resolve self.join(" ") end
Convert a FileList to a string by joining all elements with a space.
to_s
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def excluded_from_list?(fn) return true if @exclude_patterns.any? do |pat| case pat when Regexp fn =~ pat when GLOB_PATTERN flags = File::FNM_PATHNAME # Ruby <= 1.9.3 does not support File::FNM_EXTGLOB flags |= File::FNM_EXTGLOB if defined? File::FNM_EXTGLOB File.fnmatch?(pat, fn, flags) else fn == pat end end @exclude_procs.any? { |p| p.call(fn) } end
Should the given file name be excluded from the list? NOTE: This method was formerly named "exclude?", but Rails introduced an exclude? method as an array method and setup a conflict with file list. We renamed the method to avoid confusion. If you were using "FileList#exclude?" in your user code, you will need to update.
excluded_from_list?
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def glob(pattern, *args) Dir.glob(pattern, *args).sort end
Get a sorted list of files matching the pattern. This method should be preferred to Dir[pattern] and Dir.glob(pattern) because the files returned are guaranteed to be sorted.
glob
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def each_dir_parent(dir) # :nodoc: old_length = nil while dir != "." && dir.length != old_length yield(dir) old_length = dir.length dir = File.dirname(dir) end end
Yield each file or directory component.
each_dir_parent
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def from_pathname(path) # :nodoc: path = path.to_path if path.respond_to?(:to_path) path = path.to_str if path.respond_to?(:to_str) path end
Convert Pathname and Pathname-like objects to strings; leave everything else alone
from_pathname
ruby
ruby/rake
lib/rake/file_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_list.rb
MIT
def needed? begin out_of_date?(File.mtime(name)) || @application.options.build_all rescue Errno::ENOENT true end end
Is this file task needed? Yes if it doesn't exist, or if its time stamp is out of date.
needed?
ruby
ruby/rake
lib/rake/file_task.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_task.rb
MIT
def out_of_date?(stamp) all_prerequisite_tasks.any? { |prereq| prereq_task = application[prereq, @scope] if prereq_task.instance_of?(Rake::FileTask) prereq_task.timestamp > stamp || @application.options.build_all else prereq_task.timestamp > stamp end } end
Are there any prerequisites with a later time than the given time stamp?
out_of_date?
ruby
ruby/rake
lib/rake/file_task.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_task.rb
MIT
def sh(*cmd, &block) options = (Hash === cmd.last) ? cmd.pop : {} shell_runner = block_given? ? block : create_shell_runner(cmd) set_verbose_option(options) verbose = options.delete :verbose noop = options.delete(:noop) || Rake::FileUtilsExt.nowrite_flag Rake.rake_output_message sh_show_command cmd if verbose unless noop res = (Hash === cmd.last) ? system(*cmd) : system(*cmd, options) status = $? status = Rake::PseudoStatus.new(1) if !res && status.nil? shell_runner.call(res, status) end end
Run the system command +cmd+. If multiple arguments are given the command is run directly (without the shell, same semantics as Kernel::exec and Kernel::system). It is recommended you use the multiple argument form over interpolating user input for both usability and security reasons. With the multiple argument form you can easily process files with spaces or other shell reserved characters in them. With the multiple argument form your rake tasks are not vulnerable to users providing an argument like <code>; rm # -rf /</code>. If a block is given, upon command completion the block is called with an OK flag (true on a zero exit status) and a Process::Status object. Without a block a RuntimeError is raised when the command exits non-zero. Examples: sh 'ls -ltr' sh 'ls', 'file with spaces' # check exit status after command runs sh %{grep pattern file} do |ok, res| if !ok puts "pattern not found (status = #{res.exitstatus})" end end
sh
ruby
ruby/rake
lib/rake/file_utils.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils.rb
MIT
def ruby(*args, **options, &block) if args.length > 1 sh(RUBY, *args, **options, &block) else sh("#{RUBY} #{args.first}", **options, &block) end end
Run a Ruby interpreter with the given arguments. Example: ruby %{-pe '$_.upcase!' <README}
ruby
ruby
ruby/rake
lib/rake/file_utils.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils.rb
MIT
def safe_ln(*args, **options) if LN_SUPPORTED[0] begin return options.empty? ? ln(*args) : ln(*args, **options) rescue StandardError, NotImplementedError LN_SUPPORTED[0] = false end end options.empty? ? cp(*args) : cp(*args, **options) end
Attempt to do a normal file link, but fall back to a copy if the link fails.
safe_ln
ruby
ruby/rake
lib/rake/file_utils.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils.rb
MIT
def split_all(path) head, tail = File.split(path) return [tail] if head == "." || tail == "/" return [head, tail] if head == "/" return split_all(head) + [tail] end
Split a file path into individual directory names. Example: split_all("a/b/c") => ['a', 'b', 'c']
split_all
ruby
ruby/rake
lib/rake/file_utils.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils.rb
MIT
def verbose(value=nil) oldvalue = FileUtilsExt.verbose_flag FileUtilsExt.verbose_flag = value unless value.nil? if block_given? begin yield ensure FileUtilsExt.verbose_flag = oldvalue end end FileUtilsExt.verbose_flag end
Get/set the verbose flag controlling output from the FileUtils utilities. If verbose is true, then the utility method is echoed to standard output. Examples: verbose # return the current value of the # verbose flag verbose(v) # set the verbose flag to _v_. verbose(v) { code } # Execute code with the verbose flag set # temporarily to _v_. Return to the # original value when code is done.
verbose
ruby
ruby/rake
lib/rake/file_utils_ext.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils_ext.rb
MIT
def nowrite(value=nil) oldvalue = FileUtilsExt.nowrite_flag FileUtilsExt.nowrite_flag = value unless value.nil? if block_given? begin yield ensure FileUtilsExt.nowrite_flag = oldvalue end end oldvalue end
Get/set the nowrite flag controlling output from the FileUtils utilities. If verbose is true, then the utility method is echoed to standard output. Examples: nowrite # return the current value of the # nowrite flag nowrite(v) # set the nowrite flag to _v_. nowrite(v) { code } # Execute code with the nowrite flag set # temporarily to _v_. Return to the # original value when code is done.
nowrite
ruby
ruby/rake
lib/rake/file_utils_ext.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils_ext.rb
MIT
def when_writing(msg=nil) if FileUtilsExt.nowrite_flag $stderr.puts "DRYRUN: #{msg}" if msg else yield end end
Use this function to prevent potentially destructive ruby code from running when the :nowrite flag is set. Example: when_writing("Building Project") do project.build end The following code will build the project under normal conditions. If the nowrite(true) flag is set, then the example will print: DRYRUN: Building Project instead of actually building the project.
when_writing
ruby
ruby/rake
lib/rake/file_utils_ext.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils_ext.rb
MIT
def rake_check_options(options, *optdecl) h = options.dup optdecl.each do |name| h.delete name end raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty? end
Check that the options do not contain options not listed in +optdecl+. An ArgumentError exception is thrown if non-declared options are found.
rake_check_options
ruby
ruby/rake
lib/rake/file_utils_ext.rb
https://github.com/ruby/rake/blob/master/lib/rake/file_utils_ext.rb
MIT
def member?(invocation) head == invocation || tail.member?(invocation) end
Is the invocation already in the chain?
member?
ruby
ruby/rake
lib/rake/invocation_chain.rb
https://github.com/ruby/rake/blob/master/lib/rake/invocation_chain.rb
MIT
def append(invocation) if member?(invocation) fail RuntimeError, "Circular dependency detected: #{to_s} => #{invocation}" end conj(invocation) end
Append an invocation to the chain of invocations. It is an error if the invocation already listed.
append
ruby
ruby/rake
lib/rake/invocation_chain.rb
https://github.com/ruby/rake/blob/master/lib/rake/invocation_chain.rb
MIT
def chain @rake_invocation_chain ||= nil end
Return the invocation chain (list of Rake tasks) that were in effect when this exception was detected by rake. May be null if no tasks were active.
chain
ruby
ruby/rake
lib/rake/invocation_exception_mixin.rb
https://github.com/ruby/rake/blob/master/lib/rake/invocation_exception_mixin.rb
MIT
def conj(item) self.class.cons(item, self) end
Polymorphically add a new element to the head of a list. The type of head node will be the same list type as the tail.
conj
ruby
ruby/rake
lib/rake/linked_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/linked_list.rb
MIT
def to_s items = map(&:to_s).join(", ") "LL(#{items})" end
Convert to string: LL(item, item...)
to_s
ruby
ruby/rake
lib/rake/linked_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/linked_list.rb
MIT
def inspect items = map(&:inspect).join(", ") "LL(#{items})" end
Same as +to_s+, but with inspected items.
inspect
ruby
ruby/rake
lib/rake/linked_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/linked_list.rb
MIT
def each current = self while !current.empty? yield(current.head) current = current.tail end self end
For each item in the list.
each
ruby
ruby/rake
lib/rake/linked_list.rb
https://github.com/ruby/rake/blob/master/lib/rake/linked_list.rb
MIT
def initialize(task_manager, scope_list) @task_manager = task_manager @scope = scope_list.dup end
# Create a namespace lookup object using the given task manager and the list of scopes.
initialize
ruby
ruby/rake
lib/rake/name_space.rb
https://github.com/ruby/rake/blob/master/lib/rake/name_space.rb
MIT
def initialize(name=nil, version=nil) init(name, version) yield self if block_given? define unless name.nil? end
Create a Package Task with the given name and version. Use +:noversion+ as the version to build a package without a version or to provide a fully-versioned package name.
initialize
ruby
ruby/rake
lib/rake/packagetask.rb
https://github.com/ruby/rake/blob/master/lib/rake/packagetask.rb
MIT
def init(name, version) @name = name @version = version @package_files = Rake::FileList.new @package_dir = "pkg" @need_tar = false @need_tar_gz = false @need_tar_bz2 = false @need_tar_xz = false @need_zip = false @tar_command = "tar" @zip_command = "zip" @without_parent_dir = false end
Initialization that bypasses the "yield self" and "define" step.
init
ruby
ruby/rake
lib/rake/packagetask.rb
https://github.com/ruby/rake/blob/master/lib/rake/packagetask.rb
MIT
def define fail "Version required (or :noversion)" if @version.nil? @version = nil if :noversion == @version desc "Build all the packages" task :package desc "Force a rebuild of the package files" task repackage: [:clobber_package, :package] desc "Remove package products" task :clobber_package do rm_r package_dir rescue nil end task clobber: [:clobber_package] [ [need_tar, tgz_file, "z"], [need_tar_gz, tar_gz_file, "z"], [need_tar_bz2, tar_bz2_file, "j"], [need_tar_xz, tar_xz_file, "J"] ].each do |need, file, flag| if need task package: ["#{package_dir}/#{file}"] file "#{package_dir}/#{file}" => [package_dir_path] + package_files do chdir(working_dir) { sh @tar_command, "#{flag}cvf", file, target_dir } mv "#{package_dir_path}/#{target_dir}", package_dir if without_parent_dir end end end if need_zip task package: ["#{package_dir}/#{zip_file}"] file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do chdir(working_dir) { sh @zip_command, "-r", zip_file, target_dir } mv "#{package_dir_path}/#{zip_file}", package_dir if without_parent_dir end end directory package_dir_path => @package_files do @package_files.each do |fn| f = File.join(package_dir_path, fn) fdir = File.dirname(f) mkdir_p(fdir) unless File.exist?(fdir) if File.directory?(fn) mkdir_p(f) else rm_f f safe_ln(fn, f) end end end self end
Create the tasks defined by this task library.
define
ruby
ruby/rake
lib/rake/packagetask.rb
https://github.com/ruby/rake/blob/master/lib/rake/packagetask.rb
MIT
def initialize(args, &block) @mutex = Mutex.new @result = NOT_SET @error = NOT_SET @args = args @block = block end
Create a promise to do the chore specified by the block.
initialize
ruby
ruby/rake
lib/rake/promise.rb
https://github.com/ruby/rake/blob/master/lib/rake/promise.rb
MIT
def value unless complete? stat :sleeping_on, item_id: object_id @mutex.synchronize do stat :has_lock_on, item_id: object_id chore stat :releasing_lock_on, item_id: object_id end end error? ? raise(@error) : @result end
Return the value of this promise. If the promised chore is not yet complete, then do the work synchronously. We will wait.
value
ruby
ruby/rake
lib/rake/promise.rb
https://github.com/ruby/rake/blob/master/lib/rake/promise.rb
MIT
def work stat :attempting_lock_on, item_id: object_id if @mutex.try_lock stat :has_lock_on, item_id: object_id chore stat :releasing_lock_on, item_id: object_id @mutex.unlock else stat :bailed_on, item_id: object_id end end
If no one else is working this promise, go ahead and do the chore.
work
ruby
ruby/rake
lib/rake/promise.rb
https://github.com/ruby/rake/blob/master/lib/rake/promise.rb
MIT
def complete? result? || error? end
Are we done with the promise
complete?
ruby
ruby/rake
lib/rake/promise.rb
https://github.com/ruby/rake/blob/master/lib/rake/promise.rb
MIT
def discard @args = nil @block = nil end
free up these items for the GC
discard
ruby
ruby/rake
lib/rake/promise.rb
https://github.com/ruby/rake/blob/master/lib/rake/promise.rb
MIT
def stat(*args) @recorder.call(*args) if @recorder end
Record execution statistics if there is a recorder
stat
ruby
ruby/rake
lib/rake/promise.rb
https://github.com/ruby/rake/blob/master/lib/rake/promise.rb
MIT
def add_rakelib(*files) application.options.rakelib ||= [] application.options.rakelib.concat(files) end
Add files to the rakelib list
add_rakelib
ruby
ruby/rake
lib/rake/rake_module.rb
https://github.com/ruby/rake/blob/master/lib/rake/rake_module.rb
MIT
def with_application(block_application = Rake::Application.new) orig_application = Rake.application Rake.application = block_application yield block_application block_application ensure Rake.application = orig_application end
Make +block_application+ the default rake application inside a block so you can load rakefiles into a different application. This is useful when you want to run rake tasks inside a library without running rake in a sub-shell. Example: Dir.chdir 'other/directory' other_rake = Rake.with_application do |rake| rake.load_rakefile end puts other_rake.tasks
with_application
ruby
ruby/rake
lib/rake/rake_module.rb
https://github.com/ruby/rake/blob/master/lib/rake/rake_module.rb
MIT
def trim(n) result = self while n > 0 && !result.empty? result = result.tail n -= 1 end result end
Trim +n+ innermost scope levels from the scope. In no case will this trim beyond the toplevel scope.
trim
ruby
ruby/rake
lib/rake/scope.rb
https://github.com/ruby/rake/blob/master/lib/rake/scope.rb
MIT
def all_prerequisite_tasks seen = {} collect_prerequisites(seen) seen.values end
List of all unique prerequisite tasks including prerequisite tasks' prerequisites. Includes self when cyclic dependencies are found.
all_prerequisite_tasks
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def initialize(task_name, app) @name = task_name.to_s @prerequisites = [] @actions = [] @already_invoked = false @comments = [] @lock = Monitor.new @application = app @scope = app.current_scope @arg_names = nil @locations = [] @invocation_exception = nil @order_only_prerequisites = [] end
Create a task named +task_name+ with no actions or prerequisites. Use +enhance+ to add actions and prerequisites.
initialize
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def enhance(deps=nil, &block) @prerequisites |= deps if deps @actions << block if block_given? self end
Enhance a task with prerequisites or actions. Returns self.
enhance
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def name_with_args # :nodoc: if arg_description "#{name}#{arg_description}" else name end end
Name of task with argument list description.
name_with_args
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def arg_names @arg_names || [] end
Name of arguments for this task.
arg_names
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def reenable @already_invoked = false @invocation_exception = nil end
Reenable the task, allowing its tasks to be executed if the task is invoked again.
reenable
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def clear clear_prerequisites clear_actions clear_comments clear_args self end
Clear the existing prerequisites, actions, comments, and arguments of a rake task.
clear
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def clear_prerequisites prerequisites.clear self end
Clear the existing prerequisites of a rake task.
clear_prerequisites
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def clear_actions actions.clear self end
Clear the existing actions on a rake task.
clear_actions
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def clear_comments @comments = [] self end
Clear the existing comments on a rake task.
clear_comments
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def clear_args @arg_names = nil self end
Clear the existing arguments on a rake task.
clear_args
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def invoke(*args) task_args = TaskArguments.new(arg_names, args) invoke_with_call_chain(task_args, InvocationChain::EMPTY) end
Invoke the task if it is needed. Prerequisites are invoked first.
invoke
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def invoke_with_call_chain(task_args, invocation_chain) new_chain = Rake::InvocationChain.append(self, invocation_chain) @lock.synchronize do begin if application.options.trace application.trace "** Invoke #{name} #{format_trace_flags}" end if @already_invoked if @invocation_exception if application.options.trace application.trace "** Previous invocation of #{name} failed #{format_trace_flags}" end raise @invocation_exception else return end end @already_invoked = true invoke_prerequisites(task_args, new_chain) execute(task_args) if needed? rescue Exception => ex add_chain_to(ex, new_chain) @invocation_exception = ex raise ex end end end
Same as invoke, but explicitly pass a call chain to detect circular dependencies. If multiple tasks depend on this one in parallel, they will all fail if the first execution of this task fails.
invoke_with_call_chain
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def invoke_prerequisites(task_args, invocation_chain) # :nodoc: if application.options.always_multitask invoke_prerequisites_concurrently(task_args, invocation_chain) else prerequisite_tasks.each { |p| prereq_args = task_args.new_scope(p.arg_names) p.invoke_with_call_chain(prereq_args, invocation_chain) } end end
Invoke all the prerequisites of a task.
invoke_prerequisites
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def invoke_prerequisites_concurrently(task_args, invocation_chain)# :nodoc: futures = prerequisite_tasks.map do |p| prereq_args = task_args.new_scope(p.arg_names) application.thread_pool.future(p) do |r| r.invoke_with_call_chain(prereq_args, invocation_chain) end end # Iterate in reverse to improve performance related to thread waiting and switching futures.reverse_each(&:value) end
Invoke all the prerequisites of a task in parallel.
invoke_prerequisites_concurrently
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def format_trace_flags flags = [] flags << "first_time" unless @already_invoked flags << "not_needed" unless needed? flags.empty? ? "" : "(" + flags.join(", ") + ")" end
Format the trace flags for display.
format_trace_flags
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def execute(args=nil) args ||= EMPTY_TASK_ARGS if application.options.dryrun application.trace "** Execute (dry run) #{name}" return end application.trace "** Execute #{name}" if application.options.trace application.enhance_with_matching_rule(name) if @actions.empty? if opts = Hash.try_convert(args) and !opts.empty? @actions.each { |act| act.call(self, args, **opts) } else @actions.each { |act| act.call(self, args) } end end
Execute the actions associated with this task.
execute
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def add_description(description) return unless description comment = description.strip add_comment(comment) if comment && !comment.empty? end
Add a description to the task. The description can consist of an option argument list (enclosed brackets) and an optional comment.
add_description
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def comment transform_comments(" / ") { |c| first_sentence(c) } end
First line (or sentence) of all comments. Multiple comments are separated by a "/".
comment
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def transform_comments(separator, &block) if @comments.empty? nil else block ||= lambda { |c| c } @comments.map(&block).join(separator) end end
Transform the list of comments as specified by the block and join with the separator.
transform_comments
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def first_sentence(string) string.split(/(?<=\w)(\.|!)[ \t]|(\.$|!)|\n/).first end
Get the first sentence in a string. The sentence is terminated by the first period, exclamation mark, or the end of the line. Decimal points do not count as periods.
first_sentence
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def set_arg_names(args) @arg_names = args.map(&:to_sym) end
Set the names of the arguments for this task. +args+ should be an array of symbols, one for each argument name.
set_arg_names
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def investigation result = "------------------------------\n".dup result << "Investigating #{name}\n" result << "class: #{self.class}\n" result << "task needed: #{needed?}\n" result << "timestamp: #{timestamp}\n" result << "pre-requisites: \n" prereqs = prerequisite_tasks prereqs.sort! { |a, b| a.timestamp <=> b.timestamp } prereqs.each do |p| result << "--#{p.name} (#{p.timestamp})\n" end latest_prereq = prerequisite_tasks.map(&:timestamp).max result << "latest-prerequisite time: #{latest_prereq}\n" result << "................................\n\n" return result end
Return a string describing the internal state of a task. Useful for debugging.
investigation
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def task_defined?(task_name) Rake.application.lookup(task_name) != nil end
TRUE if the task name is already defined.
task_defined?
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def define_task(*args, &block) Rake.application.define_task(self, *args, &block) end
Define a task given +args+ and an option block. If a rule with the given name already exists, the prerequisites and actions are added to the existing task. Returns the defined task.
define_task
ruby
ruby/rake
lib/rake/task.rb
https://github.com/ruby/rake/blob/master/lib/rake/task.rb
MIT
def initialize(names, values, parent=nil) @names = names @parent = parent @hash = {} @values = values names.each_with_index { |name, i| next if values[i].nil? || values[i] == "" @hash[name.to_sym] = values[i] } end
Create a TaskArgument object with a list of argument +names+ and a set of associated +values+. +parent+ is the parent argument object.
initialize
ruby
ruby/rake
lib/rake/task_arguments.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_arguments.rb
MIT
def extras @values[@names.length..-1] || [] end
Retrieve the list of values not associated with named arguments
extras
ruby
ruby/rake
lib/rake/task_arguments.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_arguments.rb
MIT
def new_scope(names) values = names.map { |n| self[n] } self.class.new(names, values + extras, self) end
Create a new argument scope using the prerequisite argument names.
new_scope
ruby
ruby/rake
lib/rake/task_arguments.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_arguments.rb
MIT
def with_defaults(defaults) @hash = defaults.merge(@hash) end
Specify a hash of default values for task arguments. Use the defaults only if there is no specific value for the given argument.
with_defaults
ruby
ruby/rake
lib/rake/task_arguments.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_arguments.rb
MIT
def values_at(*keys) keys.map { |k| lookup(k) } end
Extracts the argument values at +keys+
values_at
ruby
ruby/rake
lib/rake/task_arguments.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_arguments.rb
MIT
def intern(task_class, task_name) @tasks[task_name.to_s] ||= task_class.new(task_name, self) end
Lookup a task. Return an existing task if found, otherwise create a task of the current type.
intern
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def resolve_args(args) if args.last.is_a?(Hash) deps = args.pop resolve_args_with_dependencies(args, deps) else resolve_args_without_dependencies(args) end end
Resolve the arguments for a task/rule. Returns a tuple of [task_name, arg_name_list, prerequisites, order_only_prerequisites].
resolve_args
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def resolve_args_without_dependencies(args) task_name = args.shift if args.size == 1 && args.first.respond_to?(:to_ary) arg_names = args.first.to_ary else arg_names = args end [task_name, arg_names, [], nil] end
Resolve task arguments for a task or rule when there are no dependencies declared. The patterns recognized by this argument resolving function are: task :t task :t, [:a]
resolve_args_without_dependencies
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def resolve_args_with_dependencies(args, hash) # :nodoc: fail "Task Argument Error" if hash.size != 1 && (hash.size != 2 || !hash.key?(:order_only)) order_only = hash.delete(:order_only) key, value = hash.map { |k, v| [k, v] }.first if args.empty? task_name = key arg_names = [] deps = value || [] else task_name = args.shift arg_names = key || args.shift|| [] deps = value || [] end deps = [deps] unless deps.respond_to?(:to_ary) [task_name, arg_names, deps, order_only] end
Resolve task arguments for a task or rule when there are dependencies declared. The patterns recognized by this argument resolving function are: task :t, order_only: [:e] task :t => [:d] task :t => [:d], order_only: [:e] task :t, [a] => [:d] task :t, [a] => [:d], order_only: [:e]
resolve_args_with_dependencies
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def enhance_with_matching_rule(task_name, level=0) fail Rake::RuleRecursionOverflowError, "Rule Recursion Too Deep" if level >= 16 @rules.each do |pattern, args, extensions, order_only, block| if pattern && pattern.match(task_name) task = attempt_rule(task_name, pattern, args, extensions, block, level) task | order_only unless order_only.nil? return task if task end end nil rescue Rake::RuleRecursionOverflowError => ex ex.add_target(task_name) fail ex end
If a rule can be found that matches the task name, enhance the task with the prerequisites and actions from the rule. Set the source attribute of the task appropriately for the rule. Return the enhanced task or nil of no rule was found.
enhance_with_matching_rule
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def tasks @tasks.values.sort_by { |t| t.name } end
List of all defined tasks in this application.
tasks
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def tasks_in_scope(scope) prefix = scope.path tasks.select { |t| /^#{prefix}:/ =~ t.name } end
List of all the tasks defined in the given scope (and its sub-scopes).
tasks_in_scope
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def clear @tasks.clear @rules.clear end
Clear all tasks in this application.
clear
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def lookup(task_name, initial_scope=nil) initial_scope ||= @scope task_name = task_name.to_s if task_name =~ /^rake:/ scopes = Scope.make task_name = task_name.sub(/^rake:/, "") elsif task_name =~ /^(\^+)/ scopes = initial_scope.trim($1.size) task_name = task_name.sub(/^(\^+)/, "") else scopes = initial_scope end lookup_in_scope(task_name, scopes) end
Lookup a task, using scope and the scope hints in the task name. This method performs straight lookups without trying to synthesize file tasks or rules. Special scope names (e.g. '^') are recognized. If no scope argument is supplied, use the current scope. Return nil if the task cannot be found.
lookup
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def in_namespace(name) name ||= generate_name @scope = Scope.new(name, @scope) ns = NameSpace.new(self, @scope) yield(ns) ns ensure @scope = @scope.tail end
Evaluate the block in a nested namespace named +name+. Create an anonymous namespace if +name+ is nil.
in_namespace
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def add_location(task) loc = find_location task.locations << loc if loc task end
Add a location to the locations field of the given task.
add_location
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def find_location locations = caller i = 0 while locations[i] return locations[i + 1] if locations[i] =~ /rake\/dsl_definition.rb/ i += 1 end nil end
Find the location that called into the dsl layer.
find_location
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def attempt_rule(task_name, task_pattern, args, extensions, block, level) sources = make_sources(task_name, task_pattern, extensions) prereqs = sources.map { |source| trace_rule level, "Attempting Rule #{task_name} => #{source}" if File.exist?(source) || Rake::Task.task_defined?(source) trace_rule level, "(#{task_name} => #{source} ... EXIST)" source elsif parent = enhance_with_matching_rule(source, level + 1) trace_rule level, "(#{task_name} => #{source} ... ENHANCE)" parent.name else trace_rule level, "(#{task_name} => #{source} ... FAIL)" return nil end } task = FileTask.define_task(task_name, { args => prereqs }, &block) task.sources = prereqs task end
Attempt to create a rule given the list of prerequisites.
attempt_rule
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def make_sources(task_name, task_pattern, extensions) result = extensions.map { |ext| case ext when /%/ task_name.pathmap(ext) when %r{/} ext when /^\./ source = task_name.sub(task_pattern, ext) source == ext ? task_name.ext(ext) : source when String, Symbol ext.to_s when Proc, Method if ext.arity == 1 ext.call(task_name) else ext.call end else fail "Don't know how to handle rule dependent: #{ext.inspect}" end } result.flatten end
Make a list of sources from the list of file name extensions / translation procs.
make_sources
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def get_description desc = @last_description @last_description = nil desc end
Return the current description, clearing it in the process.
get_description
ruby
ruby/rake
lib/rake/task_manager.rb
https://github.com/ruby/rake/blob/master/lib/rake/task_manager.rb
MIT
def define desc @description task @name => Array(deps) do FileUtilsExt.verbose(@verbose) do puts "Use TESTOPTS=\"--verbose\" to pass --verbose" \ ", etc. to runners." if ARGV.include? "--verbose" args = "#{ruby_opts_string} #{run_code} " + "#{file_list_string} #{option_list}" ruby args do |ok, status| if !ok && status.respond_to?(:signaled?) && status.signaled? raise SignalException.new(status.termsig) elsif !ok status = "Command failed with status (#{status.exitstatus})" details = ": [ruby #{args}]" message = if Rake.application.options.trace or @verbose status + details else status end fail message end end end end self end
Create the tasks defined by this task lib.
define
ruby
ruby/rake
lib/rake/testtask.rb
https://github.com/ruby/rake/blob/master/lib/rake/testtask.rb
MIT