Class: Rake::Delphi::CustomDelphiTool

Inherits:
CustomExec show all
Defined in:
lib/rake/delphi/tool.rb

Direct Known Subclasses

Dcc32Tool, RCResourceCompiler

Constant Summary collapse

DelphiRegRoot =
'SOFTWARE\\Borland\\Delphi'
BDSRegRoot =
'SOFTWARE\\Borland\\BDS'
EDSRegRoot =
'SOFTWARE\\CodeGear\\BDS'
EmbarcaderoRegRoot =
'SOFTWARE\\Embarcadero\\BDS'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CustomExec

#execute, #to_system_path

Methods inherited from BasicTask

#trace?

Constructor Details

#initialize(checkExistance = false) ⇒ CustomDelphiTool

Returns a new instance of CustomDelphiTool.



23
24
25
# File 'lib/rake/delphi/tool.rb', line 23

def initialize(checkExistance = false)
    @@version, @@delphidir, @@toolpath = self.class.find(checkExistance) unless @@version
end

Class Method Details

.checkToolFailure(toolpath) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rake/delphi/tool.rb', line 157

def self.checkToolFailure(toolpath)
    if toolpath.kind_of?(TrueClass) || toolpath.kind_of?(FalseClass)
        unless toolpath
            fail 'Could not detect path for %s! Check your registry and DELPHI_VERSION environment variable.' % [toolName]
        end
        cond = ! toolpath
        toolpath = ''
    else
      cond = File.exists?(toolpath)
    end
    fail 'Could not find %s: (%s)' % [toolName, toolpath] unless cond
end

.find(failIfNotFound = false) ⇒ Object



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
# File 'lib/rake/delphi/tool.rb', line 119

def self.find(failIfNotFound = false)
    v = EnvVariables.delphi_version
    if ENV['DELPHI_DIR']
        Logger.trace(Logger::DEBUG, 'DELPHI_DIR is set: ' + ENV['DELPHI_DIR'])
        # append trailing path delimiter
        ENV['DELPHI_DIR'] = ENV['DELPHI_DIR'].gsub(/[^\/]$/, '\&/')
        tool = ENV['DELPHI_DIR'] + toolName
        checkToolFailure(tool) if failIfNotFound
        return v, ENV['DELPHI_DIR'], tool
    end
    if v.to_s.empty?
        v = []
        (4..20).each { |n| v << n.to_s }
        v.reverse!
    else
        Logger.trace(Logger::DEBUG, 'DELPHI_VERSION is set: ' + v)
        v = [v]
    end
    tool_was_found = false
    v.each do |ver|
        path = readDelphiDir(ver)
        next unless path
        tool = path + toolName
        tool_was_found = true
        Logger.trace(Logger::DEBUG, "Got tool: '#{tool}'")
        if File.exists?(tool) # found it !
            ENV['DELPHI_VERSION'] = ver
            ENV['DELPHI_DIR'] = path
            Logger.trace(Logger::DEBUG, "Set: DELPHI_VERSION=#{ver}; DELPHI_DIR='#{path}'")
            return ver, path, tool
        else
            Logger.trace(Logger::DEBUG, 'But file does not exist!')
        end
    end
    checkToolFailure(tool_was_found) if failIfNotFound
    return nil
end

.readDelphiDir(ver) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rake/delphi/tool.rb', line 96

def self.readDelphiDir(ver)
    begin
        require 'win32/registry'
        [::Win32::Registry::HKEY_LOCAL_MACHINE, \
                # for local/manual installations
                ::Win32::Registry::HKEY_CURRENT_USER].each do |regRoot|
            begin
                Logger.trace(Logger::DEBUG, "Finding Delphi dir for #{ver}")
                regRoot.open(rootForVersion(ver)) do |reg|
                    reg_typ, reg_val = reg.read('RootDir')
                    return reg_val.gsub('\\', '/')
                end
            rescue ::Win32::Registry::Error
                Logger.trace(Logger::DEBUG, "No reg key '#{regRoot}'?!")
            end
        end
        return nil
    rescue LoadError
        Logger.trace(Logger::DEBUG, 'No `win32/registry` gem?!')
        return nil
    end
end

.readUserOption(key, name, ver) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rake/delphi/tool.rb', line 45

def self.readUserOption(key, name, ver)
    begin
        require 'win32/registry'
        root = rootForVersion(ver) + '\\' + key
        key_exists = false
        begin
            Logger.trace(Logger::DEBUG, "Reading user option '#{name}' in '#{root}'")
            ::Win32::Registry::HKEY_CURRENT_USER.open(root) do |reg|
                key_exists = true
                reg_typ, reg_val = reg.read(name)
                return reg_val.gsub('\\', '/')
            end
        rescue ::Win32::Registry::Error
            Logger.trace(Logger::DEBUG, "No reg key '%s'?!" % \
              (key_exists ? "#{root}\\#{name}" : root))
            return ''
        end
    rescue LoadError
        Logger.trace(Logger::DEBUG, 'No `win32/registry` gem?!')
        return ''
    end
end

.reinitObject

used mainly in tests



17
18
19
# File 'lib/rake/delphi/tool.rb', line 17

def self.reinit
    @@version, @@delphidir, @@toolpath = nil, nil, nil
end

.rootForVersion(version) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rake/delphi/tool.rb', line 78

def self.rootForVersion(version)
    if version.to_f < 9
        regRoot = DelphiRegRoot
    else
        if version.to_f < 12
            regRoot = BDSRegRoot
        elsif version.to_f < 14
            regRoot = EDSRegRoot
        else
            regRoot = EmbarcaderoRegRoot
        end
    end
    version = version4version(version)
    regRoot = regRoot + '\\' + version
    Logger.trace(Logger::DEBUG, "Root for version #{version}: '#{regRoot}'")
    return regRoot
end

.toolNameObject



27
28
29
# File 'lib/rake/delphi/tool.rb', line 27

def self.toolName
    raise 'Abstract method "toolName". Override it'
end

.version4version(version) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rake/delphi/tool.rb', line 68

def self.version4version(version)
    if version.to_f >= 9
        version = format('%.1f', version.to_f - 6)
    end
    if !version["."]
        version << ".0"
    end
    return version
end

Instance Method Details

#toolpathObject



35
36
37
# File 'lib/rake/delphi/tool.rb', line 35

def toolpath
    @@toolpath
end

#versionObject



31
32
33
# File 'lib/rake/delphi/tool.rb', line 31

def version
    @@version
end

#versionInfoClassObject



39
40
41
42
43
# File 'lib/rake/delphi/tool.rb', line 39

def versionInfoClass
    return @@version.to_f < 11 ? BDSVersionInfo : \
        @@version.to_f < 13 ? RAD2007VersionInfo : \
        @@version.to_f < 14 ? RAD2010VersionInfo : XEVersionInfo
end