summaryrefslogtreecommitdiff
path: root/scripts/context/ruby/tmftools.rb
blob: 626ef1f4a3325ca89dd8b0820387ec0b4852cc83 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env ruby

# program   : tmftools
# copyright : PRAGMA Advanced Document Engineering
# version   : 2005
# author    : Hans Hagen
#
# project   : ConTeXt
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

# The script based alternative is not slower than the kpse one.
# Loading is a bit faster when the log file is used.

# todo: create database

# tmftools [some of the kpsewhich switches]

# tmftools --analyze
# tmftools --analyze > kpsewhat.log
# tmftools --analyze --strict > kpsewhat.log
# tmftools --analyze --delete --force "texmf-local/fonts/.*/somename"
# tmftools --serve

# the real thing

banner = ['TMFTools', 'version 1.1.0 (experimental, no help yet)', '2005/2006', 'PRAGMA ADE/POD']

$: << File.expand_path(File.dirname($0)) ; $: << File.join($:.last,'lib') ; $:.uniq!

require 'base/switch'
require 'base/logger'

class Commands

    include CommandBase

    def init_kpse
        # require 'base/kpseremote'
        # if KpseRemote::available? then
        if ENV['KPSEMETHOD'] && ENV['KPSEPORT'] then
            require 'base/kpseremote'
            k = KpseRemote.new
        else
            k = nil
        end
        if k && k.okay? then
            k.progname = @commandline.option('progname')
            k.engine   = @commandline.option('engine')
            k.format   = @commandline.option('format')
        else
            require 'base/kpsefast'
            k = KpseFast.new
            k.rootpath   = @commandline.option('rootpath')
            k.treepath   = @commandline.option('treepath')
            k.progname   = @commandline.option('progname')
            k.engine     = @commandline.option('engine')
            k.format     = @commandline.option('format')
            k.diskcache  = @commandline.option('diskcache')
            k.renewcache = @commandline.option('renewcache')
            k.load_cnf
            k.expand_variables
            k.load_lsr
        end
        return k
    end

    def serve
        if ENV['KPSEMETHOD'] && ENV['KPSEPORT'] then
            require 'base/kpseremote'
            begin
                KpseRemote::start_server
            rescue
            end
        end
    end

    def reload
        begin
            init_kpse.load
        rescue
        end
    end

    def main
        if    option = @commandline.option('expand-braces') and not option.empty? then
            puts init_kpse.expand_braces(option)
        elsif option = @commandline.option('expand-path')   and not option.empty? then
            puts init_kpse.expand_path(option)
        elsif option = @commandline.option('expand-var')    and not option.empty? then
            if option == '*' then
                init_kpse.list_expansions()
            else
                puts init_kpse.expand_var(option)
            end
        elsif option = @commandline.option('show-path')     and not option.empty? then
            puts init_kpse.show_path(option)
        elsif option = @commandline.option('var-value')     and not option.empty? then
            if option == '*' then
                init_kpse.list_variables()
            else
                puts init_kpse.expand_var(option)
            end
        elsif @commandline.arguments.size > 0 then
            kpse = init_kpse
            @commandline.arguments.each do |option|
                puts kpse.find_file(option)
            end
        else
            help
        end
    end

    def analyze
        pattern = @commandline.argument('first')
        strict  = @commandline.option('strict')
        sort    = @commandline.option('sort')
        delete  = @commandline.option('delete') and @commandline.option('force')
        init_kpse.analyze_files(pattern, strict, sort, delete)
    end

end

logger      = Logger.new(banner.shift)
commandline = CommandLine.new

# kpsewhich compatible options

commandline.registervalue('expand-braces','')
commandline.registervalue('expand-path','')
commandline.registervalue('expand-var','')
commandline.registervalue('show-path','')
commandline.registervalue('var-value','')

commandline.registervalue('engine','')
commandline.registervalue('progname','')
commandline.registervalue('format','')

# additional goodies

commandline.registervalue('rootpath','')
commandline.registervalue('treepath','')
commandline.registervalue('sort','')

commandline.registerflag('diskcache')
commandline.registerflag('renewcache')
commandline.registerflag('strict')
commandline.registerflag('delete')
commandline.registerflag('force')

commandline.registeraction('analyze', "[--strict --sort --rootpath --treepath]\n[--delete [--force]] [pattern]")

# general purpose options

commandline.registerflag('verbose')
commandline.registeraction('help')
commandline.registeraction('version')

commandline.registeraction('reload', 'reload file database')
commandline.registeraction('serve', 'act as kpse server')

commandline.expand

Commands.new(commandline,logger,banner).send(commandline.action || 'main')