LoginSignup
0
2

More than 5 years have passed since last update.

C#コードのPlantUmlのクラス関係図をpythonで自動作成してみた

Posted at

C#コードのPlantUmlのクラス関係図をpythonで自動作成してみた

Qiitaは初めての投稿で色々とまだまだですが、投稿することに意味がある気がしてきたので始めてみる。
(恥ずかしながら、Qiitaをキータと読むことも、1ヶ月前に知りました笑)

※コードをたまに記載していますが、まだまだ修行中なので何かあればアドバイスお願いします。

きっかけ

アルバイト先でコードの整理を頼まれました。
なんやかんやあってコードの保守が難しくなった模様。
そのためのステップとして、まずはPlantUmlを使ってコードの流れを掴みたい!
とのこと

しかしコードの量が多すぎてまだまだ未熟な私にはとても手に負えない分量。

とあらば機械に任せてしまうのが得策では?と考えて自動化スクリプト?を作ってみました。

コードの仕組み

1.ファイルを走査する

for curDir, dirs, files in os.walk(main_folder_path):
    for file_ in files:
        if file_.endswith('cs'):
            file_path = curDir + "/" + file_

2.宣言されたクラス名とnewされたクラス名をそれぞれ取得する

ー>汚すぎるのでまだ見せれません笑
綺麗に書けたらまた投稿します

3.宣言されたクラス名とnewされたクラス名をPlantUmlにまとめる

class PlantUml_Factory():
    def __init__(self):
        self.PlantUml_code = ""
        self.PlantUml_code += "@startuml salt\n"

    def register_file__class(self,classname_lst,new_class_lst,file_):
        file_ = file_.replace(".","")
        file_ = file_.replace(" ","")

        add_code = ""
        add_code += "\n"
        add_code += "file {}".format(file_) + "{\n"
        for class_inherit in classname_lst:
            for class_ in class_inherit:
                add_code += "   class {} ".format(class_) + "" + "\n"

            if len(class_inherit) != 1:
                add_code += "\n"
                add_code += "   "
                add_code += "<-".join(class_inherit)
                add_code += "\n"

        add_code += "}\n"
        self.PlantUml_code += add_code

    def Construct(self):
        self.PlantUml_code += "@enduml"
        return self.PlantUml_code

ここの部分はまだまだ綺麗になりそうですねー...
アドバイスあればお願いします!

4.あとは出力のみ
あとは各々の方法でPlantUmlのコードを作成するのみ!

これからの野望

1.newされたクラスも関係図に組み込みたい!
2.関数とか変数とかもどうにか表示してみたい!
3.全体図だけでなく、1部分だけの図も作りたい!

0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2