summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/ruby/base/merge.rb
blob: a66b97e916bdbbcf0e9636b2cad3ec6fb338b4bf (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
131
132
133
134
135
136
137
138
139
# module    : base/merge
# copyright : PRAGMA Advanced Document Engineering
# version   : 2006
# author    : Hans Hagen
#
# project   : ConTeXt / eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

# --selfmerg ewill create stand alone script (--selfcleanup does the opposite)

# this module will package all the used modules in the file itself
# so that we can relocate the file at wish, usage:
#
# merge:
#
# unless SelfMerge::ok? && SelfMerge::merge then
#     puts("merging should happen on the path were the base inserts reside")
# end
#
# cleanup:
#
# unless SelfMerge::cleanup then
#     puts("merging should happen on the path were the base inserts reside")
# end

module SelfMerge

    @@kpsemergestart = "\# kpse_merge_start"
    @@kpsemergestop  = "\# kpse_merge_stop"
    @@kpsemergefile  = "\# kpse_merge_file: "
    @@kpsemergedone  = "\# kpse_merge_done: "

    @@filename = File.basename($0)
    @@ownpath  = File.expand_path(File.dirname($0))
    @@modroot  = '(base|graphics|rslb|www)' # needed in regex in order not to mess up SelfMerge
    @@modules  = $".collect do |file| File.expand_path(file) end

    @@modules.delete_if do |file|
        file !~ /^#{@@ownpath}\/#{@@modroot}.*$/i
    end

    def SelfMerge::ok?
        begin
            @@modules.each do |file|
                return false unless FileTest.file?(file)
            end
        rescue
            return false
        else
            return true
        end
    end

    def SelfMerge::merge
        begin
            if SelfMerge::ok? && rbfile = IO.read(@@filename) then
                begin
                    inserts = "#{@@kpsemergestart}\n\n"
                    @@modules.each do |file|
                        inserts << "#{@@kpsemergefile}'#{file}'\n\n"
                        inserts << IO.read(file).gsub(/^#.*?\n$/,'')
                        inserts << "\n\n"
                    end
                    inserts << "#{@@kpsemergestop}\n\n"
                    # no gsub! else we end up in SelfMerge
                    rbfile.sub!(/#{@@kpsemergestart}\s*#{@@kpsemergestop}/moi) do
                        inserts
                    end
                    rbfile.gsub!(/^(.*)(require [\"\'].*?#{@@modroot}.*)$/) do
                        pre, post = $1, $2
                        if pre =~ /#{@@kpsemergedone}/ then
                            "#{pre}#{post}"
                        else
                            "#{pre}#{@@kpsemergedone}#{post}"
                        end
                    end
                rescue
                    return false
                else
                    begin
                        File.open(@@filename,'w') do |f|
                            f << rbfile
                        end
                    rescue
                        return false
                    end
                end
            end
        rescue
            return false
        else
            return true
        end
    end

    def SelfMerge::cleanup
        begin
            if rbfile = IO.read(@@filename) then
                begin
                    rbfile.sub!(/#{@@kpsemergestart}(.*)#{@@kpsemergestop}\s*/moi) do
                        "#{@@kpsemergestart}\n\n#{@@kpsemergestop}\n\n"
                    end
                    rbfile.gsub!(/^(.*#{@@kpsemergedone}.*)$/) do
                        str = $1
                        if str =~ /require [\"\']/ then
                            str.gsub(/#{@@kpsemergedone}/, '')
                        else
                            str
                        end
                    end
                rescue
                    return false
                else
                    begin
                        File.open(@@filename,'w') do |f|
                            f << rbfile
                        end
                    rescue
                        return false
                    end
                end
            end
        rescue
            return false
        else
            return true
        end
    end

    def SelfMerge::replace
        if SelfMerge::ok? then
            SelfMerge::cleanup
            SelfMerge::merge
        end
    end

end