summaryrefslogtreecommitdiff
path: root/subversion/bindings/swig/ruby/svn/util.rb
blob: d409b984c276130a36c1f54677794c657303b995 (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
# ====================================================================
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.  The ASF licenses this file
#    to you under the Apache License, Version 2.0 (the
#    "License"); you may not use this file except in compliance
#    with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,
#    software distributed under the License is distributed on an
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#    KIND, either express or implied.  See the License for the
#    specific language governing permissions and limitations
#    under the License.
# ====================================================================

if /cygwin|mingw|mswin32|bccwin32/.match(RUBY_PLATFORM)
  $LOAD_PATH.each do |load_path|
    svn_ext_path = File.join(load_path, "svn", "ext")
    if File.exists?(svn_ext_path)
      svn_ext_path_win = File.expand_path(svn_ext_path)
      svn_ext_path_win = svn_ext_path.gsub(File::SEPARATOR, File::ALT_SEPARATOR)
      unless ENV["PATH"].split(";").find {|path| path == svn_ext_path_win}
        ENV["PATH"] = "#{svn_ext_path_win};#{ENV['PATH']}"
      end
    end
  end
end

require 'tempfile'

module Svn
  module Util #:nodoc:
    module_function
    def to_ruby_class_name(name)
      # Convert to CamelCase with 'X' for a leading/double/trailing underscore.
      name.to_s.split("_").collect do |x|
        if x.empty?
          "X"
        else
          x.capitalize
        end
      end.join("")
    end

    def to_ruby_const_name(name)
      name.upcase
    end

    def valid_rev?(rev)
      rev and rev >= 0
    end

    def copy?(copyfrom_path, copyfrom_rev)
      Util.valid_rev?(copyfrom_rev) && !copyfrom_path.nil?
    end

    def hash_to_prop_array(hash)
      hash.collect do |key, value|
        Svn::Core::Prop.new(key, value)
      end
    end

    def set_constants(ext_mod, target_mod=self)
      target_name = nil
      ext_mod.constants.each do |const|
        target_name = nil
        case const
        when /^SVN__/
          # ignore private constants
        when /^SVN_(?:#{target_mod.name.split("::").last.upcase}_)?/
          target_name = $POSTMATCH
        when /^SWIG_SVN_/
          target_name = $POSTMATCH
        when /^Svn_(?:#{target_mod.name.split("::").last.downcase}_)?_(.+)_t$/
          # ignore private types
        when /^Svn_(?:#{target_mod.name.split("::").last.downcase}_)?(.+)_t$/
          target_name = to_ruby_class_name($1)
        when /^Svn_(?:#{target_mod.name.split("::").last.downcase}_)?/
          target_name = to_ruby_const_name($POSTMATCH)
#         else
#           puts const
        end
        unless target_name.nil?
          target_mod.const_set(target_name, ext_mod.const_get(const))
        end
      end
    end

    def set_methods(ext_mod, target_mod=self)
      target_name = nil
      ext_mod.public_methods(false).each do |meth|
        target_name = nil
        case meth
        when /^svn_(?:#{target_mod.name.split("::").last.downcase}_)?/
          target_name = $POSTMATCH
        when /^apr_/
          target_name = meth
        end
        unless target_name.nil?
          target_mod.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
            def #{target_name}(*args, &block)
              #{ext_mod.name}.#{meth}(*args, &block)
            end
            module_function :#{target_name}
EOC
        end
      end
    end

    def filename_to_temp_file(filename)
      file = Tempfile.new("svn-ruby")
      file.binmode
      file.print(File.open(filename, "rb") {|f| f.read})
      file.close
      file.open
      file.binmode
      file
    end

    def reset_message_directory
      if /cygwin|mingw|mswin32|bccwin32/.match(RUBY_PLATFORM)
        top_directory = File.join(File.dirname(__FILE__), "..", "..")
        top_directory = File.expand_path(top_directory)
        locale_directory = File.join(top_directory, "share", "locale")
        locale_directory_win = locale_directory.tr(File::SEPARATOR,
                                                   File::ALT_SEPARATOR)
        GetText.bindtextdomain(locale_directory_win)
      end
    end

    def validate_options(options, optional_keys, required_keys=[])
      unknown_keys = options.keys - (optional_keys + required_keys)
      unless unknown_keys.empty?
        raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}")
      end
      missing_keys = []
      required_keys.each do |key|
        missing_keys << key if options[key].nil?
      end
      unless missing_keys.empty?
        raise(ArgumentError, "Missing key(s): #{missing_keys.join(", ")}")
      end
    end

    def windows?
      /cygwin|mingw|mswin32|bccwin32/.match(RUBY_PLATFORM)
    end
  end
end