summaryrefslogtreecommitdiffstats
path: root/tasks/helpers.rb
blob: df5bd50980d8bc5805dd7b9c1773c9093e325d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: UTF-8 -*-
#
require 'fileutils'
require 'find'
require 'date'
#
# Reads a file at +path+ and spits out an array of the +paragraphs+
# specified.
#
#    changes = paragraphs_of('History.txt', 0..1).join("\n\n")
#    summary, *description = paragraphs_of('README.txt', 3, 3..8)
#
def paragraphs_of( path, *paragraphs )
    title = String === paragraphs.first ? paragraphs.shift : nil
    ary = File.read(path).delete("\r").split(/\n\n+/)

    result = if title
        tmp, matching = [], false
        rgxp = %r/^=+\s*#{Regexp.escape(title)}/i
        paragraphs << (0..-1) if paragraphs.empty?

        ary.each do |val|
            if val =~ rgxp
                break if matching
                matching = true
                rgxp = %r/^=+/i
            elsif matching
                tmp << val
            end
        end
        tmp
    else ary end

    result.values_at(*paragraphs)
end

# Adds the given gem _name_ to the current project's dependency list. An
# optional gem _version_ can be given. If omitted, the newest gem version
# will be used.
#
def depend_on( name, version = nil )
    spec = Gem::Specification.find_by_name(name)
    version = spec.version.to_s if version.nil? and !spec.nil?

    PROJ.gem.dependencies << case version
        when nil; [name]
        when %r/^\d/; [name, ">= #{version}"]
        else [name, version] end
end

# Adds the given arguments to the include path if they are not already there
#
#def ensure_in_path( *args )
#    args.each do |path|
#        path = File.expand_path(path)
#        $:.unshift(path) if test(?d, path) and not $:.include?(path)
#    end
#end

# Find a rake task using the task name and remove any description text. This
# will prevent the task from being displayed in the list of available tasks.
#
def remove_desc_for_task( names )
    Array(names).each do |task_name|
        task = Rake.application.tasks.find {|t| t.name == task_name}
        next if task.nil?
        task.instance_variable_set :@comment, nil
    end
end

# Change working directories to _dir_, call the _block_ of code, and then
# change back to the original working directory (the current directory when
# this method was called).
#
#def in_directory( dir, &block )
#    curdir = pwd
#    begin
#        cd dir
#        return block.call
#    ensure
#        cd curdir
#    end
#end

# Scans the current working directory and creates a list of files that are
# candidates to be in the manifest.
#
#def manifest
#    files = []
#    exclude = PROJ.exclude.dup
#    comment = %r/^\s*#/
#
#    # process the ignore file and add the items there to the exclude list
#    if test(?f, PROJ.ignore_file)
#        ary = []
#        File.readlines(PROJ.ignore_file).each do |line|
#            next if line =~ comment
#            line.chomp!
#            line.strip!
#            next if line.nil? or line.empty?
#
#            glob = line =~ %r/\*\./ ? File.join('**', line) : line
#            Dir.glob(glob).each {|fn| ary << "^#{Regexp.escape(fn)}"}
#        end
#        exclude.concat ary
#    end
#
#    # generate a regular expression from the exclude list
#    exclude = Regexp.new(exclude.join('|'))
#
#    Find.find '.' do |path|
#        path.sub! %r/^(\.\/|\/)/o, ''
#        next unless test ?f, path
#        next if path =~ exclude
#        files << path
#    end
#    files.sort!
#end

# We need a "valid" method that determines if a string is suitable for use
# in the gem specification.
#
class Object
    def valid?
        return !(self.empty? or self == "\000") if self.respond_to?(:to_str)
        return false
    end
end

# EOF