272 lines
7.5 KiB
Markdown
Executable File
272 lines
7.5 KiB
Markdown
Executable File
# Running MarketAlly Refactoring on Your Real Code
|
|
|
|
## Quick Start (Safe Mode)
|
|
|
|
### 1. Clone/Download the Test Project
|
|
```bash
|
|
# Create a new directory for testing
|
|
mkdir refactoring-test
|
|
cd refactoring-test
|
|
|
|
# Copy the plugin files and test console
|
|
# (Copy all the C# files I provided into this directory)
|
|
```
|
|
|
|
### 2. Minimal Project Setup
|
|
Create these files in your test directory:
|
|
|
|
**Program.cs** (simplified version):
|
|
```csharp
|
|
using MarketAlly.AIPlugin;
|
|
using MarketAlly.AIPlugin.Refactoring.Plugins;
|
|
|
|
// Simple test runner
|
|
var registry = new AIPluginRegistry();
|
|
registry.RegisterPlugin(new CodeAnalysisPlugin());
|
|
registry.RegisterPlugin(new CodeRefactoringPlugin());
|
|
|
|
Console.WriteLine("Enter path to your C# file:");
|
|
var filePath = Console.ReadLine();
|
|
|
|
if (File.Exists(filePath))
|
|
{
|
|
// SAFE: Only analyze, don't modify
|
|
var result = await registry.CallFunctionAsync("CodeAnalysis", new Dictionary<string, object>
|
|
{
|
|
["path"] = filePath,
|
|
["analysisDepth"] = "detailed"
|
|
});
|
|
|
|
Console.WriteLine($"Analysis Result: {result.Message}");
|
|
Console.WriteLine($"Success: {result.Success}");
|
|
}
|
|
```
|
|
|
|
**MarketAlly.AIPlugin.Refactoring.csproj**:
|
|
```xml
|
|
<Project Sdk="Microsoft.NET.Sdk">
|
|
<PropertyGroup>
|
|
<OutputType>Exe</OutputType>
|
|
<TargetFramework>net8.0</TargetFramework>
|
|
<ImplicitUsings>enable</ImplicitUsings>
|
|
<Nullable>enable</Nullable>
|
|
</PropertyGroup>
|
|
|
|
<ItemGroup>
|
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
|
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
|
|
</ItemGroup>
|
|
</Project>
|
|
```
|
|
|
|
### 3. Test on YOUR Code (Analysis Only)
|
|
```bash
|
|
dotnet run
|
|
# Enter: C:\YourProject\SomeClass.cs
|
|
```
|
|
|
|
## For Production Use (Modifications Needed)
|
|
|
|
### 1. Update File Exclusion Patterns
|
|
|
|
**Current (demo-safe):**
|
|
```csharp
|
|
private bool ShouldExcludeFile(string filePath)
|
|
{
|
|
var fileName = Path.GetFileName(filePath);
|
|
var excludePatterns = new[] { ".Designer.cs", ".generated.cs", "AssemblyInfo.cs" };
|
|
return excludePatterns.Any(pattern => fileName.EndsWith(pattern));
|
|
}
|
|
```
|
|
|
|
**Your Production Code:**
|
|
```csharp
|
|
private bool ShouldExcludeFile(string filePath)
|
|
{
|
|
var fileName = Path.GetFileName(filePath);
|
|
var relativePath = Path.GetRelativePath(_rootDirectory, filePath);
|
|
|
|
// ADD YOUR SPECIFIC EXCLUSIONS
|
|
var excludePatterns = new[]
|
|
{
|
|
".Designer.cs",
|
|
".generated.cs",
|
|
"AssemblyInfo.cs",
|
|
"Reference.cs", // If you have reference files
|
|
"GlobalAssemblyInfo.cs", // Global assembly files
|
|
".g.cs", // Generated UI files
|
|
".xaml.cs" // XAML code-behind (be careful!)
|
|
};
|
|
|
|
var excludeDirectories = new[]
|
|
{
|
|
"bin", "obj", ".git", ".vs",
|
|
"packages", "node_modules",
|
|
"Migrations", // EF Migrations
|
|
"wwwroot", // Web assets
|
|
"ClientApp" // If you have Angular/React
|
|
};
|
|
|
|
// Check file patterns
|
|
if (excludePatterns.Any(pattern => fileName.Contains(pattern, StringComparison.OrdinalIgnoreCase)))
|
|
return true;
|
|
|
|
// Check directory patterns
|
|
if (excludeDirectories.Any(dir => relativePath.Contains(dir, StringComparison.OrdinalIgnoreCase)))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
```
|
|
|
|
### 2. Add Your Project-Specific Configuration
|
|
|
|
**Create refactor-config.json** in your project root:
|
|
```json
|
|
{
|
|
"RootDirectory": "C:\\YourProject",
|
|
"ExcludedFiles": [
|
|
"*.Designer.cs",
|
|
"*.generated.cs",
|
|
"Reference.cs",
|
|
"*Migration*.cs"
|
|
],
|
|
"ExcludedDirectories": [
|
|
"bin", "obj", ".git", ".vs",
|
|
"Migrations", "wwwroot", "ClientApp"
|
|
],
|
|
"MaxMethodLength": 30,
|
|
"MaxClassSize": 400,
|
|
"MinComplexityForExtraction": 8,
|
|
"StopOnFirstError": false,
|
|
"CreateBackups": true
|
|
}
|
|
```
|
|
|
|
### 3. Safe Testing Workflow
|
|
|
|
**Step 1: Analysis Only**
|
|
```bash
|
|
# Test analysis on a single file first
|
|
dotnet run test-analysis --file "C:\YourProject\Models\User.cs"
|
|
```
|
|
|
|
**Step 2: Preview Refactoring**
|
|
```bash
|
|
# See what would be changed (NO modifications)
|
|
dotnet run test-refactor --file "C:\YourProject\Services\UserService.cs" --operations "extract-methods"
|
|
```
|
|
|
|
**Step 3: Small Test with Backup**
|
|
```bash
|
|
# Apply to ONE non-critical file first
|
|
dotnet run test-refactor --file "C:\YourProject\Utils\Helper.cs" --operations "extract-methods" --apply
|
|
```
|
|
|
|
**Step 4: Batch Testing**
|
|
```bash
|
|
# Test on a small subdirectory
|
|
dotnet run test-batch --directory "C:\YourProject\Models" --operations "codeanalysis,documentation"
|
|
```
|
|
|
|
## Real-World Integration Options
|
|
|
|
### Option 1: Standalone Tool (Safest)
|
|
```csharp
|
|
// Create a separate console app just for refactoring
|
|
// Keep it isolated from your main project
|
|
// Run it against copies of your code first
|
|
```
|
|
|
|
### Option 2: MSBuild Integration
|
|
```xml
|
|
<!-- Add to your .csproj file -->
|
|
<Target Name="RefactorCode" BeforeTargets="Build">
|
|
<Exec Command="RefactoringTool.exe --analyze --path $(MSBuildProjectDirectory)" />
|
|
</Target>
|
|
```
|
|
|
|
### Option 3: CI/CD Integration
|
|
```yaml
|
|
# Azure DevOps / GitHub Actions
|
|
- name: Code Quality Analysis
|
|
run: |
|
|
dotnet run --project RefactoringTool -- --analyze --path ./src
|
|
# Only analyze in CI, don't modify
|
|
```
|
|
|
|
## Safety Checklist ✅
|
|
|
|
Before running on your production code:
|
|
|
|
- [ ] **Backup your entire project** (Git commit or copy)
|
|
- [ ] **Test on non-critical files first**
|
|
- [ ] **Use preview mode before applying changes**
|
|
- [ ] **Review the generated backup files**
|
|
- [ ] **Test that your project still compiles after refactoring**
|
|
- [ ] **Run your unit tests after refactoring**
|
|
- [ ] **Check that the refactored code still behaves correctly**
|
|
|
|
## Common Project-Specific Adjustments
|
|
|
|
### For Web Projects (ASP.NET):
|
|
```csharp
|
|
// Exclude web-specific files
|
|
excludePatterns.Add("*.cshtml.cs"); // Razor pages
|
|
excludePatterns.Add("Program.cs"); // Don't auto-refactor startup
|
|
excludePatterns.Add("Startup.cs"); // Don't auto-refactor startup
|
|
```
|
|
|
|
### For Entity Framework Projects:
|
|
```csharp
|
|
// Exclude EF migrations and generated models
|
|
excludePatterns.Add("*Migration*.cs");
|
|
excludePatterns.Add("*DbContext.cs"); // Be careful with DbContext
|
|
excludeDirectories.Add("Migrations");
|
|
```
|
|
|
|
### For WPF/WinForms Projects:
|
|
```csharp
|
|
// Exclude designer files and auto-generated code
|
|
excludePatterns.Add("*.Designer.cs");
|
|
excludePatterns.Add("*.xaml.cs"); // Consider carefully
|
|
excludePatterns.Add("AssemblyInfo.cs");
|
|
```
|
|
|
|
## Recommended First Test
|
|
|
|
1. **Pick ONE simple utility class** from your project
|
|
2. **Copy it to a test directory**
|
|
3. **Run analysis only:**
|
|
```bash
|
|
dotnet run test-analysis --file TestClass.cs
|
|
```
|
|
4. **If analysis works, try preview refactoring:**
|
|
```bash
|
|
dotnet run test-refactor --file TestClass.cs --operations "extract-methods"
|
|
```
|
|
5. **If preview looks good, apply to the copy:**
|
|
```bash
|
|
dotnet run test-refactor --file TestClass.cs --operations "extract-methods" --apply
|
|
```
|
|
6. **Verify the refactored code compiles and makes sense**
|
|
|
|
## What You'll Get
|
|
|
|
✅ **Immediate Benefits:**
|
|
- Code complexity analysis
|
|
- Documentation suggestions
|
|
- Naming convention analysis
|
|
- Code smell detection
|
|
|
|
✅ **With Minimal Setup:**
|
|
- Method extraction suggestions
|
|
- Class splitting recommendations
|
|
- Duplicate code identification
|
|
|
|
✅ **With Project Integration:**
|
|
- Automated refactoring in your build process
|
|
- Continuous code quality improvement
|
|
- Team-wide coding standards enforcement
|
|
|
|
The key is to **start small and safe** - analyze first, then gradually introduce automated refactoring as you gain confidence in the tool with your specific codebase! |