Top Level Namespace

Defined Under Namespace

Modules: Output, RubyUmlClassVer Classes: AppMainBase, CStruct, RubyUmlClass, Search, WsServer

Constant Summary collapse

ARGV =
[]

Instance Method Summary collapse

Instance Method Details

#config_json_hash(json) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/wsserver.rb', line 6

def config_json_hash(json)
  config = {}
  json["setting_list"].each do |j|
    config[j["name"]] = j["value"]
  end
  return config
end

#create_uml_class(in_dir, out_file) ⇒ Object



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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/create_uml_class.rb', line 106

def create_uml_class(in_dir, out_file)
  out = []
  out.push "@startuml"

  puts "in_dir = #{in_dir}"
  main_composition_list = []
  main_method_list = []
  global_var = []

  Dir.glob("#{in_dir}/**/*.{rb,ru}") do |f|
    if f =~ Regexp.new(@config["exclude_path"])
      puts "skip #{f}"
      next
    end
    puts f
    buf = ""
    Tempfile.create("rufo") do |tmp_file|
      FileUtils.cp(f, tmp_file.path)
      kernel = Facter.value(:kernel)
      if kernel == "windows"
        open("|rubyw #{get_rufo_path} #{tmp_file.path}") do |f|
          if f.read =~ /error/
            puts "rufo error #{f}"
            return
          else
            buf = File.binread tmp_file.path
          end
        end
      else
        open("|rufo #{tmp_file.path}") do |f|
          if f.read =~ /error/
            puts "rufo error #{f}"
            return
          else
            buf = File.binread tmp_file.path
          end
        end
      end
    end

    # コメント削除
    buf.gsub!(/(([\/\"\'].*?[\/\"\'])|([^\/\"\'\)\s]*#.+?$))/) do |m|
      if m[0] == "#" and m[0] != "{"
        #puts "comment #{m}"
        # コメント
        ""
      else
        #puts "not comment #{m}"
        # コメント以外
        m
      end
    end
    # ヒアドキュメント削除
    buf = delete_here_doc(buf)

    out_list = []
    cstruct_list = []
    block_count = 0
    method_type = :public
    class_name = ""
    # ソースを解析
    buf.each_line do |line|
      next if line =~ /^[\r\n]*$/  # 空行は対象外

      # ブロックの開始/終了
      indent_num = line.match(/^[ ]+/).to_s.size / 2
      if block_count == indent_num
        # 変化なし
      elsif block_count > indent_num
        # ブロックの終了
        block_count = indent_num
      else
        # ブロックの開始
        block_count = indent_num
      end

      #line.gsub!(/\".+\"/, "\"delete_string\"") # 文字列を削除
      if line =~ /^\s*class\s/
        unless line =~ /<</ # 特異クラスではない
          work = line.gsub(/class\s/, "")
          class_name = work.split("<")[0].to_s.chomp.match(/[A-Z][A-Za-z0-9_:]+/).to_s
          base_name = work.split("<")[1].to_s.chomp.match(/[A-Z][A-Za-z0-9_:]+/).to_s
          class_name.gsub!(/::/, ".")
          class_name.gsub!(/;/, "")
          if out_list.size != 0 and out_list[-1].type == :class_start # classが連続している
            class_name = out_list[-1].name + "." + class_name
            out_list[-1].name = class_name
            cstruct_list[-1].name = class_name
          else
            out_list.push CStruct.new(:class_start, class_name, block_count, [], [], [], [])
            cstruct_list.push CStruct.new(:class_end, class_name, block_count, [], [], [], [])
          end
          #pp line if class_name == ""
          if base_name != ""
            #base_name.gsub!(/::/, ".")
            cstruct_list[-1].inherit_list.push base_name
          end
        end
        next unless line =~ /end\s*$/ # 1行で終了しない場合
      elsif line =~ /^\s*module\s/
        module_name = line.split(" ")[1].to_s.chomp
        module_name.gsub!(/^[:]+/, "")
        module_name.gsub!(/::/, ".")
        module_name.gsub!(/;/, "")
        out_list.push CStruct.new(:module_start, module_name, block_count, [], [], [], [])
        cstruct_list.push CStruct.new(:module_end, module_name, block_count, [], [], [], [])
        next unless line =~ /end\s*$/ # 1行で終了しない場合
      end

      if line =~ /^\s*private$/
        method_type = :private
      elsif line =~ /^\s*protected$/
        method_type = :protected
      elsif line =~ /^\s*public$/
        method_type = :public
      end

      if line =~ /^\s*def\s/
        # 関数名を取り出す
        method = line.chomp.gsub(/\s*def\s*/, "")
        unless method =~ /\(/
          # 関数名にカッコをつける
          sp = method.split(" ")
          if sp.size > 1
            method = sp[0].to_s + "(" + sp[1..-1].to_s + ")"
          else
            method = method + "()"
          end
        end
        if cstruct_list.size != 0
          method_list = cstruct_list[-1].method_list
          case method_type
          when :public
            method_list.push "+ #{method}"
          when :private
            method_list.push "- #{method}"
          when :protected
            method_list.push "# #{method}"
          end
        else
          main_method_list.push "+ #{method}"
        end
      end

      # composition_list
      line.match(/[A-Z]([a-zA-Z:]+)\.[a-z]/) do |m|
        #pp m
        c_name = m.to_s.split(".")[0]
        if cstruct_list.size != 0
          cstruct_list[-1].composition_list.push c_name
        else
          main_composition_list.push "main *-- #{c_name}"
        end
      end

      # インスタンス変数
      if line =~ /\s*@\S+/
        if cstruct_list.size != 0
          line.match(/@[a-zA-Z0-9_]+/) { |m|
            instance_var = cstruct_list[-1].var_list
            val = m.to_s.gsub(/@/, "")
            case method_type
            when :public
              instance_var.push "+ #{val}"
            when :private
              instance_var.push "- #{val}"
            when :protected
              instance_var.push "# #{val}"
            end
          }
        end
      end

      # 外部変数
      line.match(/\$[a-zA-Z0-9_]+/) { |m|
        global_var.push "+ #{m.to_s}"
      }

      # クラスの終了
      if cstruct_list.size != 0
        if block_count == cstruct_list[-1].block_count # block_countが一致
          #puts "end of #{cstruct_list[-1].name}"
          out_list.push cstruct_list[-1]
          cstruct_list.slice!(-1) # 最後の要素を削除
        end
      end
      #puts "#{block_count} #{line.chomp}"
    end
    if block_count != 0
      # エラー
      puts f
      return ""
    end

    # UMLの出力
    out = print_uml(out, out_list)
  end

  if main_method_list.size != 0 or
     main_composition_list.size != 0 or
     main_method_list.size != 0
    out.push "class main {"
    main_method_list.each do |mml|
      out.push mml
    end
    # グローバル変数の出力
    global_var.uniq.each do |gv|
      out.push gv
    end
    out.push "}"
    main_composition_list.uniq.each do |mcl|
      out.push mcl
    end
  end

  out.push "@enduml"
  return out.join("\n")
end

#delete_here_doc(buf) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/create_uml_class.rb', line 83

def delete_here_doc(buf)
  new_buf = []
  here_doc = false
  here_word = ""
  buf.each_line do |line|
    if line =~ /(<<|<<~|<<-)[A-Z]+/
      here_doc = true
      here_word = line.match(/(<<|<<~|<<-)[A-Z]+/).to_s.gsub(/[<~-]/, "")
    end
    if here_word != "" and line =~ Regexp.new("^\s*#{here_word}$")
      here_word = ""
      here_doc = false
      #pp line
    end
    if here_doc == false
      new_buf.push line
    else
      #pp line
    end
  end
  return new_buf.join("")
end

#get_rufo_pathObject



12
13
14
15
16
17
18
19
20
# File 'lib/create_uml_class.rb', line 12

def get_rufo_path
  ENV["PATH"].split(";").each do |path|
    rufo_path = "#{path}\\rufo"
    if File.exists? rufo_path
      return rufo_path
    end
  end
  return ""
end

#get_unused_portObject

空きポートを取得



67
68
69
70
71
72
# File 'lib/start.rb', line 67

def get_unused_port
  s = TCPServer.open(0)
  port = s.addr[1]
  s.close
  return port
end


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
# File 'lib/create_uml_class.rb', line 22

def print_uml(out, out_list)
  out_list.each do |o_list|
    if o_list.type == :class_start
      # nop
    elsif o_list.type == :module_start
      out.push "namespace #{o_list.name} {"
    elsif o_list.type == :class_end
      pp o_list if o_list.name == ""
      out.push "class #{o_list.name} {"
      # インスタンス変数の出力
      o_list.var_list.uniq.each do |iv|
        out.push iv
      end
      # メソッドの出力
      o_list.method_list.each do |ml|
        out.push ml
      end
      out.push "}"
      # 継承リストの出力
      o_list.inherit_list.each do |ih|
        out.push "#{o_list.name} --|> #{ih}"
      end
      # compo
      o_list.composition_list.uniq.each do |co|
        out.push "#{o_list.name} *-- #{co}"
      end
    elsif o_list.type == :module_end
      # インスタンス変数がある場合はモジュール名と同じクラスを定義
      if o_list.var_list.size != 0 or
         o_list.method_list.size != 0 or
         o_list.inherit_list.size != 0 or
         o_list.composition_list.size != 0
        pp o_list if o_list.name == ""
        out.push "class #{o_list.name} {"
        # インスタンス変数の出力
        o_list.var_list.uniq.each do |iv|
          out.push iv
        end
        # メソッドの出力
        o_list.method_list.each do |ml|
          out.push ml
        end
        out.push "}"
        # 継承リストの出力
        o_list.inherit_list.each do |ih|
          out.push "#{o_list.name} --|> #{ih}"
        end
        # compo
        o_list.composition_list.uniq.each do |co|
          out.push "#{o_list.name} *-- #{co}"
        end
      end
      out.push "}"
    else
      # error
      puts "error!"
    end
  end
  return out
end