First working build process

This commit is contained in:
Cj 2024-06-19 05:58:45 -04:00
parent 60ee4f6e72
commit 4231f902f2
4 changed files with 92 additions and 37 deletions

View File

@ -1,5 +1,4 @@
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json; using Newtonsoft.Json;
using ReCodeIt.Models; using ReCodeIt.Models;
using ReCodeIt.Utils; using ReCodeIt.Utils;
@ -179,22 +178,4 @@ public static class ProjectManager
Logger.Log($"Found {AllProjectSourceFiles.Count} source files in the project", ConsoleColor.Yellow); Logger.Log($"Found {AllProjectSourceFiles.Count} source files in the project", ConsoleColor.Yellow);
} }
private static void AnalyzeSyntaxTree(SyntaxNode root)
{
// Example: Find all method declarations
var methodDeclarations = root.DescendantNodes().OfType<MethodDeclarationSyntax>();
foreach (var method in methodDeclarations)
{
Logger.Log($"Method: {method.Identifier.Text}");
Logger.Log($"Return Type: {method.ReturnType}");
Logger.Log("Parameters:");
foreach (var parameter in method.ParameterList.Parameters)
{
Logger.Log($" {parameter.Type} {parameter.Identifier}");
}
Logger.Log(string.Empty);
}
}
} }

View File

@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using ReCodeIt.Models; using ReCodeIt.Models;
using ReCodeIt.ReMapper; using ReCodeIt.ReMapper;
using ReCodeIt.Utils; using ReCodeIt.Utils;
using System.Diagnostics;
namespace ReCodeIt.CrossCompiler; namespace ReCodeIt.CrossCompiler;
@ -19,14 +20,9 @@ public class ReCodeItCrossCompiler
public CrossCompilerProjectModel ActiveProject => ProjectManager.ActiveProject; public CrossCompilerProjectModel ActiveProject => ProjectManager.ActiveProject;
/// <summary>
/// Key: Remapped name, value: old name
/// </summary>
public Dictionary<string, string> ChangedTypes { get; set; } = [];
public void StartRemap() public void StartRemap()
{ {
ChangedTypes.Clear(); ActiveProject.ChangedTypes.Clear();
Remapper.InitializeRemap( Remapper.InitializeRemap(
ActiveProject.RemapModels, ActiveProject.RemapModels,
@ -48,7 +44,7 @@ public class ReCodeItCrossCompiler
Logger.Log("-----------------------------------------------", ConsoleColor.Yellow); Logger.Log("-----------------------------------------------", ConsoleColor.Yellow);
Logger.Log($"Cross patch remap result", ConsoleColor.Yellow); Logger.Log($"Cross patch remap result", ConsoleColor.Yellow);
Logger.Log($"Changed {ChangedTypes.Count} types", ConsoleColor.Yellow); Logger.Log($"Changed {ActiveProject.ChangedTypes.Count} types", ConsoleColor.Yellow);
Logger.Log($"Original assembly path: {ActiveProject.OriginalAssemblyPath}", ConsoleColor.Yellow); Logger.Log($"Original assembly path: {ActiveProject.OriginalAssemblyPath}", ConsoleColor.Yellow);
Logger.Log($"Original assembly hash: {ActiveProject.OriginalAssemblyHash}", ConsoleColor.Yellow); Logger.Log($"Original assembly hash: {ActiveProject.OriginalAssemblyHash}", ConsoleColor.Yellow);
Logger.Log($"Original patched assembly path: {ActiveProject.RemappedAssemblyPath}", ConsoleColor.Yellow); Logger.Log($"Original patched assembly path: {ActiveProject.RemappedAssemblyPath}", ConsoleColor.Yellow);
@ -59,6 +55,9 @@ public class ReCodeItCrossCompiler
public void StartCrossCompile() public void StartCrossCompile()
{ {
AnalyzeSourceFiles(); AnalyzeSourceFiles();
StartBuild();
MoveResult();
} }
private void AnalyzeSourceFiles() private void AnalyzeSourceFiles()
@ -67,36 +66,107 @@ public class ReCodeItCrossCompiler
{ {
AnalyzeSourcefile(file); AnalyzeSourcefile(file);
} }
var fileName = Path.GetFileName(ActiveProject.OriginalAssemblyPath);
var outPath = Path.Combine(ActiveProject.RemappedAssemblyPath, fileName);
Logger.Log($"Placing original reference back into cloned build directory", ConsoleColor.Green);
File.Copy(ActiveProject.OriginalAssemblyPath, outPath, true);
} }
private void AnalyzeSourcefile(string file) private void AnalyzeSourcefile(string file)
{ {
var source = LoadSourceFile(file); var source = File.ReadAllText(file);
var syntaxTree = CSharpSyntaxTree.ParseText(source); var syntaxTree = CSharpSyntaxTree.ParseText(source);
var root = syntaxTree.GetCompilationUnitRoot(); var root = syntaxTree.GetCompilationUnitRoot();
// Get the things we want to change
var identifiers = root.DescendantNodes() var identifiers = root.DescendantNodes()
.OfType<IdentifierNameSyntax>() .OfType<IdentifierNameSyntax>()
.Where(id => ActiveProject.ChangedTypes.ContainsKey(id.Identifier.Text)); .Where(id => ActiveProject.ChangedTypes.ContainsKey(id.Identifier.Text));
if (!identifiers.Any()) { return; } if (!identifiers.Any()) { return; }
Logger.Log($"found {identifiers.Count()} objects to change in file {Path.GetFileNameWithoutExtension(file)}"); Logger.Log($"changing {identifiers.Count()} identifiers in file {Path.GetFileName(file)}", ConsoleColor.Green);
// Replace "RigClass" with "NewRigClass" // Do Black Voodoo Magic
var newRoot = root.ReplaceNodes(identifiers, (oldNode, newNode) => var newRoot = root.ReplaceNodes(identifiers, (oldNode, newNode) =>
SyntaxFactory.IdentifierName(ActiveProject.ChangedTypes[oldNode.Identifier.Text]) SyntaxFactory.IdentifierName(ActiveProject.ChangedTypes[oldNode.Identifier.Text])
.WithLeadingTrivia(oldNode.GetLeadingTrivia()) .WithLeadingTrivia(oldNode.GetLeadingTrivia())
.WithTrailingTrivia(oldNode.GetTrailingTrivia())); .WithTrailingTrivia(oldNode.GetTrailingTrivia()));
File.WriteAllText(file, newRoot.ToFullString());
} }
/// <summary> /// <summary>
/// Loads a source file from disk /// Starts the build process for the active project.
/// </summary> /// </summary>
/// <param name="path"></param> private void StartBuild()
/// <returns></returns> {
private string LoadSourceFile(string path) var path = Path.Combine(
DataProvider.ReCodeItProjectsPath,
ActiveProject.SolutionName);
var csProjFile = Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories)
.ToList()
.FirstOrDefault();
if (csProjFile == null || csProjFile == string.Empty)
{
Logger.Log("No project files found in the solution directory or sub directories", ConsoleColor.Red);
return;
}
var dirName = Path.GetDirectoryName(csProjFile);
var solutionName = ActiveProject.SolutionName + ".sln";
var arguements = $"build {Path.Combine(path, solutionName)} " +
$"/p:Configuration=Debug " +
$"/p:Platform=\"Any CPU\"";
// clean the project first
ExecuteDotnetCommand("clean", path);
// Restore packages
ExecuteDotnetCommand("restore", path);
Logger.Log(path + ActiveProject.SolutionName + ".sln");
// Then build the project
ExecuteDotnetCommand(arguements, path);
}
private static void ExecuteDotnetCommand(string arguments, string workingDirectory)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = arguments,
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = startInfo;
process.OutputDataReceived += (sender, e) => Logger.Log(e.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
int exitCode = process.ExitCode;
Logger.Log($"dotnet {arguments} exited with code {exitCode}");
}
}
private void MoveResult()
{ {
return File.ReadAllText(path);
} }
} }

View File

@ -170,7 +170,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" /> <PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" />

View File

@ -79,6 +79,11 @@ public class ReCodeItRemapper
// We are done, write the assembly // We are done, write the assembly
WriteAssembly(); WriteAssembly();
if (CrossMapMode)
{
ProjectManager.SaveCrossCompilerProjectModel(_compiler.ActiveProject);
}
} }
/// <summary> /// <summary>
@ -185,7 +190,7 @@ public class ReCodeItRemapper
if (CrossMapMode) if (CrossMapMode)
{ {
// Store the original types for caching // Store the original types for caching
_compiler.ChangedTypes.Add(remap.NewTypeName, type.Name); _compiler.ActiveProject.ChangedTypes.Add(remap.NewTypeName, type.Name);
} }
type.Name = remap.NewTypeName; type.Name = remap.NewTypeName;
@ -264,7 +269,7 @@ public class ReCodeItRemapper
if (CrossMapMode) if (CrossMapMode)
{// Store the original types for caching {// Store the original types for caching
_compiler.ChangedTypes.Add(highestScore.ProposedNewName, highestScore.Definition.Name); _compiler.ActiveProject.ChangedTypes.Add(highestScore.ProposedNewName, highestScore.Definition.Name);
} }
// Rename type and all associated type members // Rename type and all associated type members