Top Level Namespace

Defined Under Namespace

Modules: RubyMakeScript, Target Classes: Array, FileTarget, InDir, InEnv, PhonyTarget, String, Symbol, TargetDoc

Instance Method Summary collapse

Instance Method Details

#arg!(name, doc) ⇒ Object



39
40
41
# File 'lib/ruby_make_script/doc.rb', line 39

def arg!(name, doc)
    $targetdoc.add_arg(name, doc)
end

#cd(str) ⇒ Object

since ~ “cd <path>” invalid, add a function here



2
3
4
# File 'lib/ruby_make_script/utils.rb', line 2

def cd(str)
    Dir.chdir(str)
end

#cd?(str) ⇒ Boolean

since ~ “cd <path>” invalid, add a function here

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/ruby_make_script/utils.rb', line 36

def cd?(str)
    begin
        Dir.chdir(str)
    rescue => exception
        puts Pastel.new.yellow("warning> ") + "command error: cd " + str + " (suppressed)"
        return false
    end
    return true
end

#cp(*str) ⇒ Object

these were like cd function



21
22
23
# File 'lib/ruby_make_script/utils.rb', line 21

def cp(*str)
    r"cp #{str.join(' ')}"
end

#cp?(*str) ⇒ Boolean

these were like cd function

Returns:

  • (Boolean)


62
63
64
# File 'lib/ruby_make_script/utils.rb', line 62

def cp?(*str)
    r?"cp #{str.join(' ')}"
end

#descr!(str) ⇒ Object



35
36
37
# File 'lib/ruby_make_script/doc.rb', line 35

def descr!(str)
    $targetdoc.add_descr(str)
end

#dir(path) ⇒ Object



112
113
114
# File 'lib/ruby_make_script/utils.rb', line 112

def dir(path)
    InDir.new(path)
end

#dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/ruby_make_script/utils.rb', line 116

def dir?(path)
    InDir.new(path, false)
end

#DOCKER(*args) ⇒ Object



149
150
151
# File 'lib/ruby_make_script/utils.rb', line 149

def DOCKER(*args)
    r "docker", *args.map{|s| String(s)}
end

#dump_md(filename, thisfile = "make.rb") ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ruby_make_script.rb', line 191

def dump_md(filename, thisfile="make.rb")
    puts Pastel.new.bright_cyan("make> ") + "dumping markdown file #{filename}"
    File.open(filename, 'w') do |f|
        f.puts "# #{thisfile} Documentation"
        f.puts ""
        if block_given?
            yield f
            f.puts ""
        end
        f.puts "## Usage"
        f.puts ""
        f.puts "Please install ruby and some package first:"
        f.puts ""
        f.puts "```sh"
        f.puts "$ apt instal ruby"
        f.puts "$ gem instal pastel ruby_make_script"
        f.puts "```"
        f.puts ""
        f.puts "then you can run these command below."
        $targetlist.each { |t|
            if t.class == PhonyTarget
                args = t.doc.arglist.map{ |a| a[0] }.join(' ')
                f.puts "### `./#{thisfile} #{t.target} #{args}`"
                f.puts ""
                f.puts "#{t.doc.descr}"
                f.puts ""
                t.doc.arglist.each { |a|
                    f.puts "* `#{a[0]}` : #{a[1]}"
                }
                if t.doc.arglist != []
                    f.puts ""
                end
            end
        }
    end
end

#envir(expr) ⇒ Object



135
136
137
# File 'lib/ruby_make_script/utils.rb', line 135

def envir(expr)
    InEnv.new(expr)
end

#file_modified!(file) ⇒ Object

mark a file is modified



84
85
86
87
88
89
90
91
92
# File 'lib/ruby_make_script.rb', line 84

def file_modified!(file)
    if $file_target_dict[file].class == FileTarget
        $cur_file_time_dict[file] = File.mtime(file)
    elsif $file_target_dict[file].class == PhonyTarget
        $cur_file_time_dict[file] = true
    else
        raise "file type error #{file.class}"
    end
end

#file_modified?(file) ⇒ Boolean

check if a file is modified or its dependency is modified

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_make_script.rb', line 61

def file_modified?(file)
    if $file_target_dict[file].class == FileTarget
        # 文件真正被修改:文件之前不存在,或文件现在已经不存在,或时间戳修改
        real_modified = $file_time_dict[file] == nil || !File.exist?(file) || ($file_time_dict[file] != File.mtime(file))
        # 文件依赖被修改
        return real_modified || $file_target_dict[file].depend_modified?
    elsif $file_target_dict[file].class == PhonyTarget
        # 假目标被修改:依赖被修改或之前不存在
        return $file_time_dict[file] == nil || $file_target_dict[file].depend_modified?
    elsif $file_target_dict[file] == nil
        # 对无目标的文件,判断其存在,存在则直接使用即可
        if !File.exist?(file)
            raise "file not found #{file}"
        else
            $cur_file_time_dict[file] = File.mtime(file)
            return $file_time_dict[file] == nil || ($file_time_dict[file] != File.mtime(file))
        end 
    else
        raise "file type error #{$file_target_dict[file].class}"
    end
end

#GIT(*args) ⇒ Object



145
146
147
# File 'lib/ruby_make_script/utils.rb', line 145

def GIT(*args)
    r "git", *args.map{|s| String(s)}
end

#makeObject

Usage:

“‘ make do

<define target here>

end “‘



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ruby_make_script.rb', line 154

def make
    $targetlist = []
    
    yield

    raise "at least a target" if $targetlist.length < 1
    
    if File.exist?('./.make_script.yaml')
        $file_time_dict = YAML.load(File.read('./.make_script.yaml'))
        $cur_file_time_dict = $file_time_dict.clone()
    end
    puts Pastel.new.bright_cyan("make> ") + "start"
    
    begin
        if ARGV.length == 0
            $targetlist[0].resolve_all
        else
            p ARGV[0]
            resolve(ARGV[0], true)
        end

    rescue StandardError => e
        puts Pastel.new.red.bold("make failed> ") + e.message
        if e.message != "make command failed"
            puts e.backtrace
        end
    else
        puts Pastel.new.bright_cyan("make> ") + "completed"
    end
    if !File.exist?('./.make_script.yaml')
        File.open('.gitignore', 'a') do |f|
            f << "\n.make_script.yaml\n"
        end
    end
    File.open('./.make_script.yaml', 'w') { |f| f.write(YAML.dump($cur_file_time_dict)) }
end

#mkdir(*str) ⇒ Object

these were like cd function



12
13
14
# File 'lib/ruby_make_script/utils.rb', line 12

def mkdir(*str)
    r"mkdir #{str.join(' ')}"
end

#mkdir?(*str) ⇒ Boolean

these were like cd function

Returns:

  • (Boolean)


52
53
54
# File 'lib/ruby_make_script/utils.rb', line 52

def mkdir?(*str)
    r?"mkdir #{str.join(' ')}"
end

#mv(*str) ⇒ Object

these were like cd function



16
17
18
# File 'lib/ruby_make_script/utils.rb', line 16

def mv(*str)
    r"mv #{str.join(' ')}"
end

#mv?(*str) ⇒ Boolean

these were like cd function

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_make_script/utils.rb', line 57

def mv?(*str)
    r?"mv #{str.join(' ')}"
end

#PACK(cmd, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_make_script/package.rb', line 16

def PACK(cmd, *args)
    cmd = String(cmd)
    $system_to_pack.each do |k,v|
        if `uname -a`[k]
            if $pack[v][cmd]
                cmd = $pack[v][cmd]
            end
            r v, cmd, *args
            break
        end
    end
end

#r(cmd, *str) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/ruby_make_script/utils.rb', line 25

def r(cmd, *str)
    cmd = String(cmd)
    str =  cmd + " " + str.join(" ")
    puts Pastel.new.green("running> ") + str
    if !system(str) 
        puts Pastel.new.red.bold("error> ") + "command error: " + str
        raise "make command failed"
    end
end

#r?(cmd, *str) ⇒ Boolean

no error

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_make_script/utils.rb', line 67

def r?(cmd, *str)
    cmd = String(cmd)
    str =  cmd + " " + str.join(" ")
    puts Pastel.new.green("running> ") + str
    flag = system(str) 
    if !flag
        puts Pastel.new.yellow("warning> ") + "command error: " + str + " (suppressed)"
    end
    flag
end

#resolve(file, force_exec = false) ⇒ Object

check a file (recursively) and run the commands of the target.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_make_script.rb', line 41

def resolve(file, force_exec=false)
    if force_exec || file_modified?(file)
        t = $file_target_dict[file]
        # when t == nil, its a file not used for target
        if t != nil 
            t.depend_each { |f|
                resolve(f)
            }
            t.run()
            # if File.exist?(file) 
            #     puts "#{file} modified #{$file_time_dict[file]} != #{File.mtime(file)}"
            # else
            #     puts "#{file} modified not exist?"
            # end
            file_modified!(file)
        end
    end 
end

#rm(*str) ⇒ Object

these were like cd function



7
8
9
# File 'lib/ruby_make_script/utils.rb', line 7

def rm(*str)
    r"rm #{str.join(' ')}"
end

#rm?(*str) ⇒ Boolean

these were like cd function

Returns:

  • (Boolean)


47
48
49
# File 'lib/ruby_make_script/utils.rb', line 47

def rm?(*str)
    r?"rm #{str.join(' ')}"
end

#runfile(file, *args) ⇒ Object



78
79
80
81
# File 'lib/ruby_make_script/utils.rb', line 78

def runfile(file, *args)
    path = File.expand_path(file)
    r path, *args
end

#runfile?(file, *args) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/ruby_make_script/utils.rb', line 83

def runfile?(file, *args)
    path = File.expand_path(file)
    r? path, *args
end

#use(*operation) ⇒ Object



139
140
141
142
143
# File 'lib/ruby_make_script/utils.rb', line 139

def use(*operation)
    operation.each{ |o| o.enter}
    yield
    operation.each{ |o| o.exit}
end