tmftools.rb / last modification: 2020-01-30 14:16
#!/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,&#39;lib') ; $:.uniq!

require &#39;base/switch'
require &#39;base/logger'

class Commands

    include CommandBase

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

    def serve
        if ENV[&#39;KPSEMETHOD'] && ENV['KPSEPORT'] then
            require &#39;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(&#39;expand-braces') and not option.empty? then
            puts init_kpse.expand_braces(option)
        elsif option = @commandline.option(&#39;expand-path')   and not option.empty? then
            puts init_kpse.expand_path(option)
        elsif option = @commandline.option(&#39;expand-var')    and not option.empty? then
            if option == &#39;*' then
                init_kpse.list_expansions()
            else
                puts init_kpse.expand_var(option)
            end
        elsif option = @commandline.option(&#39;show-path')     and not option.empty? then
            puts init_kpse.show_path(option)
        elsif option = @commandline.option(&#39;var-value')     and not option.empty? then
            if option == &#39;*' 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(&#39;first')
        strict  = @commandline.option(&#39;strict')
        sort    = @commandline.option(&#39;sort')
        delete  = @commandline.option(&#39;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(&#39;expand-braces','')
commandline.registervalue(&#39;expand-path','')
commandline.registervalue(&#39;expand-var','')
commandline.registervalue(&#39;show-path','')
commandline.registervalue(&#39;var-value','')

commandline.registervalue(&#39;engine','')
commandline.registervalue(&#39;progname','')
commandline.registervalue(&#39;format','')

# additional goodies

commandline.registervalue(&#39;rootpath','')
commandline.registervalue(&#39;treepath','')
commandline.registervalue(&#39;sort','')

commandline.registerflag(&#39;diskcache')
commandline.registerflag(&#39;renewcache')
commandline.registerflag(&#39;strict')
commandline.registerflag(&#39;delete')
commandline.registerflag(&#39;force')

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

# general purpose options

commandline.registerflag(&#39;verbose')
commandline.registeraction(&#39;help')
commandline.registeraction(&#39;version')

commandline.registeraction(&#39;reload', 'reload file database')
commandline.registeraction(&#39;serve', 'act as kpse server')

commandline.expand

Commands.new(commandline,logger,banner).send(commandline.action || &#39;main')