class Object

Public Instance Methods

DOCKER(*args) click to toggle source
# File lib/ruby_make_script/utils.rb, line 149
def DOCKER(*args)
    r "docker", *args.map{|s| String(s)}
end
GIT(*args) click to toggle source
# File lib/ruby_make_script/utils.rb, line 145
def GIT(*args)
    r "git", *args.map{|s| String(s)}
end
PACK(cmd, *args) click to toggle source
# 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
arg!(name, doc) click to toggle source
# File lib/ruby_make_script/doc.rb, line 39
def arg!(name, doc)
    $targetdoc.add_arg(name, doc)
end
cd(str) click to toggle source

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

# File lib/ruby_make_script/utils.rb, line 2
def cd(str)
    Dir.chdir(str)
end
cd?(str) click to toggle source

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

# 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) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 21
def cp(*str)
    r"cp #{str.join(' ')}"
end
cp?(*str) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 62
def cp?(*str)
    r?"cp #{str.join(' ')}"
end
descr!(str) click to toggle source
# File lib/ruby_make_script/doc.rb, line 35
def descr!(str)
    $targetdoc.add_descr(str)
end
dir(path) click to toggle source
# File lib/ruby_make_script/utils.rb, line 112
def dir(path)
    InDir.new(path)
end
dir?(path) click to toggle source
# File lib/ruby_make_script/utils.rb, line 116
def dir?(path)
    InDir.new(path, false)
end
dump_md(filename, thisfile="make.rb") { |f| ... } click to toggle source
# 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) click to toggle source
# File lib/ruby_make_script/utils.rb, line 135
def envir(expr)
    InEnv.new(expr)
end
file_modified!(file) click to toggle source

mark a file is modified

# 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) click to toggle source

check if a file is modified or its dependency is modified

# 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
make() { || ... } click to toggle source

Usage:

“` make do

<define target here>

end “`

# 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) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 12
def mkdir(*str)
    r"mkdir #{str.join(' ')}"
end
mkdir?(*str) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 52
def mkdir?(*str)
    r?"mkdir #{str.join(' ')}"
end
mv(*str) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 16
def mv(*str)
    r"mv #{str.join(' ')}"
end
mv?(*str) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 57
def mv?(*str)
    r?"mv #{str.join(' ')}"
end
r(cmd, *str) click to toggle source
# 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) click to toggle source

no error

# 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) click to toggle source

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

# 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) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 7
def rm(*str)
    r"rm #{str.join(' ')}"
end
rm?(*str) click to toggle source

these were like cd function

# File lib/ruby_make_script/utils.rb, line 47
def rm?(*str)
    r?"rm #{str.join(' ')}"
end
runfile(file, *args) click to toggle source
# 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) click to toggle source
# File lib/ruby_make_script/utils.rb, line 83
def runfile?(file, *args)
    path = File.expand_path(file)
    r? path, *args
end
use(*operation) { || ... } click to toggle source
# File lib/ruby_make_script/utils.rb, line 139
def use(*operation)
    operation.each{ |o| o.enter}
    yield
    operation.each{ |o| o.exit}
end