File size: 6,159 Bytes
230b1a5
 
 
b212889
230b1a5
560aacd
27ed0b5
b212889
560aacd
b212889
6ee54ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230b1a5
27ed0b5
230b1a5
560aacd
 
 
230b1a5
 
27ed0b5
230b1a5
 
b212889
 
 
 
6ee54ae
 
 
 
 
 
 
 
3fcefb9
6ee54ae
 
 
 
 
230b1a5
 
 
b212889
230b1a5
b212889
6ee54ae
 
3fcefb9
 
230b1a5
b212889
230b1a5
b212889
 
 
 
 
 
 
 
 
230b1a5
27ed0b5
3fcefb9
230b1a5
27ed0b5
560aacd
b212889
 
6ee54ae
27ed0b5
 
3fcefb9
 
b212889
27ed0b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560aacd
27ed0b5
560aacd
 
 
 
 
27ed0b5
560aacd
b212889
6ee54ae
27ed0b5
2600a55
27ed0b5
 
 
 
 
2600a55
27ed0b5
2600a55
6ee54ae
 
 
 
 
 
 
b212889
 
 
 
 
 
560aacd
3fcefb9
6ee54ae
3fcefb9
 
 
 
6ee54ae
3fcefb9
 
 
 
 
 
6ee54ae
3fcefb9
6ee54ae
 
 
 
 
3fcefb9
6ee54ae
3fcefb9
6ee54ae
 
 
 
54c69b5
3fcefb9
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import streamlit as st
import tempfile
import git
import os
from pathlib import Path
from datetime import datetime 
from typing import List, Set, Optional
from config.settings import Settings
from core.file_scanner import FileScanner, FileInfo
from services.llm_service import LLMService
from main import DirectoryStructureScanner, MarkdownGenerator

class StreamlitFileWriter:
    def __init__(self, output_file: Path):
        self.output_file = output_file
    
    def create_markdown(self, files: List[FileInfo]) -> str:
        content = []
        for file_info in files:
            content.append(f"## {file_info.path}")
            content.append("------------")
            if file_info.content is not None:
                content.append(file_info.content)
            else:
                content.append("# Failed to read content")
            content.append("\n")
        return "\n".join(content)

# ページ設定
st.set_page_config(
   page_title="Repository Code Analysis",
   page_icon="🔍",
   layout="wide"
)

# スタイル設定
st.markdown("""
<style>
    .stApp {
        background-color: #0e1117;
        color: #ffffff;
    }
    .directory-structure {
        font-family: monospace;
        white-space: pre;
        background-color: #1e2329;
        padding: 1rem;
        border-radius: 0.5rem;
        margin: 1rem 0;
    }
    .download-section {
        background-color: #1e2329;
        padding: 1rem;
        border-radius: 0.5rem;
        margin: 1rem 0;
    }
</style>
""", unsafe_allow_html=True)

# セッション状態の初期化
if 'repo_content' not in st.session_state:
    st.session_state.repo_content = None
if 'structure_md' not in st.session_state:
    st.session_state.structure_md = None
if 'content_md' not in st.session_state:
    st.session_state.content_md = None
if 'temp_dir' not in st.session_state:
    st.session_state.temp_dir = None
if 'llm_service' not in st.session_state:
    try:
        api_key = os.getenv("ANTHROPIC_API_KEY")
        if not api_key:
            st.error("ANTHROPIC_API_KEY環境変数が設定されていません")
            st.stop()
        st.session_state.llm_service = LLMService(api_key)
    except Exception as e:
        st.error(str(e))
        st.stop()

# メインのUIレイアウト
st.title("🔍 リポジトリ解析システム")

# サイドバーの設定
with st.sidebar:
    st.subheader("📌 使い方")
    st.markdown("""
    1. GitHubリポジトリのURLを入力
    2. スキャン対象の拡張子を選択
    3. スキャンを実行
    4. ディレクトリ構造を確認
    5. 解析結果をダウンロード
    """)
    
    # スキャン対象の拡張子選択
    st.subheader("🔍 スキャン対象の選択")
    
    # プログラミング言語
    st.write("プログラミング言語:")
    prog_exts = {'.py', '.js', '.ts', '.java', '.cpp', '.hpp', '.c', '.h', '.go', '.rs'}
    selected_prog = {ext: st.checkbox(ext, value=True, key=f"prog_{ext}") 
                    for ext in prog_exts}
    
    # 設定ファイル
    st.write("設定ファイル:")
    config_exts = {'.json', '.yml', '.yaml', '.toml'}
    selected_config = {ext: st.checkbox(ext, value=True, key=f"config_{ext}") 
                      for ext in config_exts}
    
    # ドキュメント
    st.write("ドキュメント:")
    doc_exts = {'.md', '.txt'}
    selected_doc = {ext: st.checkbox(ext, value=True, key=f"doc_{ext}") 
                   for ext in doc_exts}

# URLの入力
repo_url = st.text_input(
   "GitHubリポジトリのURLを入力",
   placeholder="https://github.com/username/repository.git"
)

# スキャン実行ボタン
if st.button("スキャン開始", disabled=not repo_url):
    try:
        with st.spinner('リポジトリを解析中...'):
            # 選択された拡張子を集約
            selected_extensions = {ext for exts in [selected_prog, selected_config, selected_doc]
                                for ext, selected in exts.items() if selected}
            
            # 選択がない場合はデフォルトを使用
            if not selected_extensions:
                selected_extensions = Settings.DEFAULT_EXTENSIONS
            
            # MarkdownGeneratorを初期化
            generator = MarkdownGenerator(repo_url, selected_extensions)
            content_md, structure_md, files = generator.generate_markdowns()
            
            writer = StreamlitFileWriter(Path("dummy"))
            formatted_content = writer.create_markdown(files)
            
            st.session_state.content_md = formatted_content
            st.session_state.structure_md = structure_md
            st.session_state.repo_content = LLMService.format_code_content(files)
            
        st.success(f"スキャン完了: {len(files)}個のファイルを検出")
        
    except Exception as e:
        st.error(f"エラーが発生しました: {str(e)}")

# スキャン結果の表示とダウンロード
if st.session_state.structure_md and st.session_state.content_md:
    # ディレクトリ構造の表示
    st.subheader("📁 ディレクトリ構造")
    st.markdown(f'<div class="directory-structure">{st.session_state.structure_md}</div>', 
               unsafe_allow_html=True)
    
    # ダウンロードセクション
    st.subheader("📥 解析結果のダウンロード")
    st.markdown('<div class="download-section">', unsafe_allow_html=True)
    
    col1, col2 = st.columns(2)
    with col1:
        st.download_button(
            label="📁 ディレクトリ構造をダウンロード",
            data=st.session_state.structure_md,
            file_name=f"directory_structure_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
            mime="text/markdown"
        )
    
    with col2:
        st.download_button(
            label="📄 ファイル内容をダウンロード",
            data=st.session_state.content_md,
            file_name=f"repository_content_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
            mime="text/markdown"
        )
    
    st.markdown('</div>', unsafe_allow_html=True)