Repository navigation
Expand file tree
/
Copy pathPyProjectManager.tsx
More file actions
415 lines (376 loc) · 17.4 KB
/
Copy pathPyProjectManager.tsx
File metadata and controls
415 lines (376 loc) · 17.4 KB
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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { useState, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { TauriAPI, type PyProjectToml, type ProjectMetadata } from '../lib/tauri'
import { Icon } from './Icon'
interface PyProjectManagerProps {
projectPath: string
onConsoleOutput?: (output: string) => void
onConsoleError?: (error: string) => void
}
export function PyProjectManager({ projectPath, onConsoleOutput, onConsoleError }: PyProjectManagerProps) {
const { t } = useTranslation()
const [config, setConfig] = useState<PyProjectToml | null>(null)
const [loading, setLoading] = useState(false)
const [saving, setSaving] = useState(false)
const [exists, setExists] = useState(false)
const [newDependency, setNewDependency] = useState('')
const [newDevDependency, setNewDevDependency] = useState('')
useEffect(() => {
loadConfig()
}, [projectPath])
const loadConfig = async () => {
if (!projectPath) return
setLoading(true)
try {
const pyprojectExists = await TauriAPI.checkPyProjectExists(projectPath)
setExists(pyprojectExists)
if (pyprojectExists) {
const pyprojectConfig = await TauriAPI.readPyProjectToml(projectPath)
setConfig(pyprojectConfig)
onConsoleOutput?.(t('pyProjectManager.loadedSuccess'))
} else {
// Create default config
const projectName = projectPath.split(/[\\/]/).pop() || 'pyra-project'
const defaultConfig: PyProjectToml = {
project: {
name: projectName,
version: '0.1.0',
description: t('pyProjectManager.defaultDescription'),
authors: ['Your Name <your.email@example.com>'],
requires_python: '>=3.8',
},
dependencies: [],
dev_dependencies: [],
build_system: {
requires: ['setuptools', 'wheel'],
build_backend: 'setuptools.build_meta'
}
}
setConfig(defaultConfig)
onConsoleOutput?.(t('pyProjectManager.createdDefault'))
}
} catch (error) {
onConsoleError?.(t('pyProjectManager.loadError', { error: String(error) }))
} finally {
setLoading(false)
}
}
const saveConfig = async () => {
if (!config || !projectPath) return
setSaving(true)
try {
await TauriAPI.writePyProjectToml(projectPath, config)
setExists(true)
onConsoleOutput?.(t('pyProjectManager.savedSuccess'))
} catch (error) {
onConsoleError?.(t('pyProjectManager.saveError', { error: String(error) }))
} finally {
setSaving(false)
}
}
const updateProjectMetadata = (field: keyof ProjectMetadata, value: any) => {
if (!config) return
setConfig({
...config,
project: { ...config.project, [field]: value }
})
}
const addDependency = () => {
if (!config || !newDependency.trim()) return
const updatedDeps = [...config.dependencies, newDependency.trim()]
setConfig({ ...config, dependencies: updatedDeps })
setNewDependency('')
}
const removeDependency = (index: number) => {
if (!config) return
const updatedDeps = config.dependencies.filter((_, i) => i !== index)
setConfig({ ...config, dependencies: updatedDeps })
}
const addDevDependency = () => {
if (!config || !newDevDependency.trim()) return
const updatedDeps = [...config.dev_dependencies, newDevDependency.trim()]
setConfig({ ...config, dev_dependencies: updatedDeps })
setNewDevDependency('')
}
const removeDevDependency = (index: number) => {
if (!config) return
const updatedDeps = config.dev_dependencies.filter((_, i) => i !== index)
setConfig({ ...config, dev_dependencies: updatedDeps })
}
const updateAuthors = (authorsString: string) => {
const authors = authorsString.split(',').map(a => a.trim()).filter(a => a)
updateProjectMetadata('authors', authors)
}
if (loading) {
return (
<div className="p-4 text-center text-gray-400">
{t('pyProjectManager.loading')}
</div>
)
}
if (!config) {
return (
<div className="p-4 text-center text-gray-500">
<div className="text-4xl mb-4"><Icon name="file-alt" size={48} /></div>
<div className="text-lg mb-2">{t('pyProjectManager.loadFailed')}</div>
<button
onClick={loadConfig}
className="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded text-sm"
>
{t('pyProjectManager.retry')}
</button>
</div>
)
}
return (
<div className="h-full flex flex-col bg-gray-900">
{/* Header */}
<div className="px-4 py-3 bg-gray-800 border-b border-gray-700">
<div className="flex items-center justify-between">
<div className="text-sm font-semibold text-gray-300 flex items-center gap-2">
<Icon name="file-alt" size={14} /> {t('pyProjectManager.title')}
{exists && <span className="text-green-400 text-xs"><Icon name="check" size={10} /></span>}
{!exists && <span className="text-yellow-400 text-xs"><Icon name="exclamation-triangle" size={10} /> {t('pyProjectManager.notSaved')}</span>}
</div>
<button
onClick={saveConfig}
disabled={saving}
className={`px-3 py-1 text-sm rounded transition-colors ${
saving
? 'bg-yellow-600 text-white cursor-not-allowed'
: 'bg-green-600 hover:bg-green-700 text-white'
}`}
>
{saving ? <><Icon name="clock" size={12} /> {t('pyProjectManager.saving')}</> : <><Icon name="save" size={12} /> {t('pyProjectManager.save')}</>}
</button>
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-4 space-y-6">
{/* Project Metadata */}
<div className="bg-gray-800 rounded-lg p-4">
<h3 className="text-lg font-semibold text-gray-200 mb-4"><Icon name="clipboard-list" size={16} /> {t('pyProjectManager.projectInfo')}</h3>
<div className="space-y-4">
{/* First row - Project Name and Version */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">{t('pyProjectManager.projectName')}</label>
<input
type="text"
value={config.project.name}
onChange={(e) => updateProjectMetadata('name', e.target.value)}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">{t('pyProjectManager.version')}</label>
<input
type="text"
value={config.project.version}
onChange={(e) => updateProjectMetadata('version', e.target.value)}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
/>
</div>
</div>
{/* Second row - Description (full width) */}
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">{t('pyProjectManager.description')}</label>
<textarea
value={config.project.description || ''}
onChange={(e) => updateProjectMetadata('description', e.target.value)}
rows={2}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
placeholder={t('pyProjectManager.descriptionPlaceholder')}
/>
</div>
{/* Third row - Authors and Python Version */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-300 mb-2 h-8 flex items-end">{t('pyProjectManager.authors')}</label>
<input
type="text"
value={config.project.authors.join(', ')}
onChange={(e) => updateAuthors(e.target.value)}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
placeholder={t('pyProjectManager.authorsPlaceholder')}
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-2 h-8 flex items-end">{t('pyProjectManager.pythonVersion')}</label>
<input
type="text"
value={config.project.requires_python || ''}
onChange={(e) => updateProjectMetadata('requires_python', e.target.value)}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
placeholder={t('pyProjectManager.pythonVersionPlaceholder')}
/>
</div>
</div>
{/* Fourth row - License and README */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-300 mb-2 h-8 flex items-end">{t('pyProjectManager.license')}</label>
<input
type="text"
value={config.project.license || ''}
onChange={(e) => updateProjectMetadata('license', e.target.value)}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
placeholder={t('pyProjectManager.licensePlaceholder')}
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-2 h-8 flex items-end">{t('pyProjectManager.readme')}</label>
<input
type="text"
value={config.project.readme || ''}
onChange={(e) => updateProjectMetadata('readme', e.target.value)}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
placeholder={t('pyProjectManager.readmePlaceholder')}
/>
</div>
</div>
</div>
</div>
{/* Dependencies */}
<div className="bg-gray-800 rounded-lg p-4">
<h3 className="text-lg font-semibold text-gray-200 mb-4"><Icon name="box" size={16} /> {t('pyProjectManager.dependencies')}</h3>
<div className="mb-4">
<div className="flex gap-2">
<input
type="text"
value={newDependency}
onChange={(e) => setNewDependency(e.target.value)}
placeholder={t('pyProjectManager.dependenciesPlaceholder')}
className="flex-1 min-w-0 px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
onKeyPress={(e) => e.key === 'Enter' && addDependency()}
/>
<button
onClick={addDependency}
disabled={!newDependency.trim()}
className="flex-shrink-0 px-2 py-2 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 text-white text-xs rounded"
>
+
</button>
</div>
</div>
<div className="space-y-2">
{config.dependencies.map((dep, index) => (
<div key={index} className="flex items-center justify-between p-2 bg-gray-700 rounded">
<span className="text-gray-300 font-mono text-sm truncate mr-2">{dep}</span>
<button
onClick={() => removeDependency(index)}
className="flex-shrink-0 px-2 py-1 bg-red-600 hover:bg-red-700 text-white text-xs rounded"
>
<Icon name="times" size={12} />
</button>
</div>
))}
{config.dependencies.length === 0 && (
<div className="text-gray-500 text-sm py-4 text-center">{t('pyProjectManager.noDependencies')}</div>
)}
</div>
</div>
{/* Dev Dependencies */}
<div className="bg-gray-800 rounded-lg p-4">
<h3 className="text-lg font-semibold text-gray-200 mb-4"><Icon name="tools" size={16} /> {t('pyProjectManager.devDependencies')}</h3>
<div className="mb-4">
<div className="flex gap-2">
<input
type="text"
value={newDevDependency}
onChange={(e) => setNewDevDependency(e.target.value)}
placeholder={t('pyProjectManager.devDependenciesPlaceholder')}
className="flex-1 min-w-0 px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
onKeyPress={(e) => e.key === 'Enter' && addDevDependency()}
/>
<button
onClick={addDevDependency}
disabled={!newDevDependency.trim()}
className="flex-shrink-0 px-2 py-2 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600 text-white text-xs rounded"
>
+
</button>
</div>
</div>
<div className="space-y-2">
{config.dev_dependencies.map((dep, index) => (
<div key={index} className="flex items-center justify-between p-2 bg-gray-700 rounded">
<span className="text-gray-300 font-mono text-sm truncate mr-2">{dep}</span>
<button
onClick={() => removeDevDependency(index)}
className="flex-shrink-0 px-2 py-1 bg-red-600 hover:bg-red-700 text-white text-xs rounded"
>
<Icon name="times" size={12} />
</button>
</div>
))}
{config.dev_dependencies.length === 0 && (
<div className="text-gray-500 text-sm py-4 text-center">{t('pyProjectManager.noDevDependencies')}</div>
)}
</div>
</div>
{/* Build System */}
{config.build_system && (
<div className="bg-gray-800 rounded-lg p-4">
<h3 className="text-lg font-semibold text-gray-200 mb-4"><Icon name="cog" size={16} /> {t('pyProjectManager.buildSystem')}</h3>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">{t('pyProjectManager.buildBackend')}</label>
<input
type="text"
value={config.build_system.build_backend}
onChange={(e) => setConfig({
...config,
build_system: { ...config.build_system!, build_backend: e.target.value }
})}
className="w-full px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">{t('pyProjectManager.buildRequirements')}</label>
<div className="space-y-2">
{config.build_system.requires.map((req, index) => (
<div key={index} className="flex items-center gap-2">
<input
type="text"
value={req}
onChange={(e) => {
const newRequires = [...config.build_system!.requires]
newRequires[index] = e.target.value
setConfig({
...config,
build_system: { ...config.build_system!, requires: newRequires }
})
}}
className="flex-1 px-3 py-2 bg-gray-700 text-gray-300 text-sm rounded border border-gray-600 focus:outline-none focus:border-blue-500"
/>
<button
onClick={() => {
const newRequires = config.build_system!.requires.filter((_, i) => i !== index)
setConfig({
...config,
build_system: { ...config.build_system!, requires: newRequires }
})
}}
className="px-2 py-1 bg-red-600 hover:bg-red-700 text-white text-xs rounded"
>
<Icon name="times" size={12} />
</button>
</div>
))}
</div>
</div>
</div>
</div>
)}
</div>
{/* Footer */}
<div className="px-4 py-2 bg-gray-800 border-t border-gray-700 text-xs text-gray-400">
<div className="flex items-center justify-between">
<span><Icon name="lightbulb" size={12} /> {t('pyProjectManager.saveWarning')}</span>
<span>{t('pyProjectManager.formatInfo')}</span>
</div>
</div>
</div>
)
}