summaryrefslogtreecommitdiff
path: root/scripts/context/ruby/base/system.rb
blob: 71c0af421f9be456c7b224de78f1e143bd76f908 (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
# module    : base/system
# copyright : PRAGMA Advanced Document Engineering
# version   : 2002-2005
# author    : Hans Hagen
#
# project   : ConTeXt / eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

require "rbconfig"

module System

    @@mswindows   = RbConfig::CONFIG['host_os'] =~ /mswin/
    @@binpaths    = ENV['PATH'].split(File::PATH_SEPARATOR)
    @@binsuffixes = if $mswindows then ['.exe','.com','.bat'] else ['','.sh','.csh'] end
    @@located     = Hash.new
    @@binnames    = Hash.new

    if @@mswindows then
        @@binnames['ghostscript'] = ['gswin32c.exe','gs.cmd','gs.bat']
        @@binnames['imagemagick'] = ['imagemagick.exe','convert.exe']
        @@binnames['inkscape']    = ['inkscape.exe']
    else
        @@binnames['ghostscript'] = ['gs']
        @@binnames['imagemagick'] = ['convert']
        @@binnames['inkscape']    = ['inkscape']
    end


    def System.null
        if @@mswindows then 'nul' else '/dev/null' end
    end

    def System.unix?
        not @@mswindows
    end
    def System.mswin?
        @@mswindows
    end

    def System.binnames(str)
        if @@binnames.key?(str) then
            @@binnames[str]
        else
            [str]
        end
    end

    def System.prependengine(str)
        if str =~ /^\S+\.(pl|rb|lua|py)/io then
            case $1
                when 'pl'  then return "perl #{str}"
                when 'rb'  then return "ruby #{str}"
                when 'lua' then return "lua #{str}"
                when 'py'  then return "python #{str}"
            end
        end
        return str
    end

    def System.locatedprogram(program)
        if @@located.key?(program) then
            return @@located[program]
        else
            System.binnames(program).each do |binname|
                if binname =~ /\..*$/io then
                    @@binpaths.each do |path|
                        if FileTest.file?(str = File.join(path,binname)) then
                            return @@located[program] = System.prependengine(str)
                        end
                    end
                end
                binname.gsub!(/\..*$/io, '')
                @@binpaths.each do |path|
                    @@binsuffixes.each do |suffix|
                        if FileTest.file?(str = File.join(path,"#{binname}#{suffix}")) then
                            return @@located[program] = System.prependengine(str)
                        end
                    end
                end
            end
        end
        return @@located[program] = "texmfstart #{program}"
    end

    def System.command(program,arguments='')
        if program =~ /^(.*?) (.*)$/ then
            program = System.locatedprogram($1) + ' ' + $2
        else
            program = System.locatedprogram(program)
        end
        program = program + ' ' + arguments if ! arguments.empty?
        program.gsub!(/\s+/io, ' ')
        #program.gsub!(/(\/\.\/)+/io, '/')
        program.gsub!(/\\/io, '/')
        return program
    end

    def System.run(program,arguments='',pipe=false,collect=false)
        if pipe then
            if collect then
                `#{System.command(program,arguments)} 2>&1`
            else
                `#{System.command(program,arguments)}`
            end
        else
            system(System.command(program,arguments))
        end
    end

    def System.pipe(program,arguments='',collect=false)
        System.run(program,arguments,true)
    end

    def System.safepath(path)
        if path.match(/ /o) then "\"#{path}\"" else path end
    end

end