2024-06-19 21:11:42 -04:00
using CliFx ;
using CliFx.Attributes ;
using CliFx.Infrastructure ;
using ReCodeIt.CrossCompiler ;
2024-06-19 21:34:46 -04:00
using ReCodeIt.Utils ;
2024-06-19 21:11:42 -04:00
using ReCodeItLib.Utils ;
namespace ReCodeIt.Commands ;
2024-06-22 12:12:18 -04:00
[Command("Build", Description = "(Compile Time Reflection) Build your project and get a dll output for the original assembly.")]
2024-06-22 12:39:10 -04:00
public class Build : ICommand
2024-06-19 21:11:42 -04:00
{
private ReCodeItCrossCompiler CrossCompiler { get ; set ; }
2024-06-22 12:12:18 -04:00
[CommandParameter(0, IsRequired = false, Description = "the location of your project file (ReCodeItProj.json). You don't need to provide a path if the last project you built is the one you want to target, or you are running this command from inside a directory where a project file exists.")]
2024-06-19 21:11:42 -04:00
public string ProjectJsonPath { get ; init ; }
2024-06-20 19:49:06 -04:00
public async ValueTask ExecuteAsync ( IConsole console )
2024-06-20 20:38:58 -04:00
{
var isLocal = await UseLocalProject ( console ) ;
if ( isLocal ) { return ; }
var isRemote = await UseRemoteProject ( console ) ;
if ( isRemote ) { return ; }
await UseLastLoadedProject ( console ) ;
2024-06-26 14:45:54 -04:00
// Wait for log termination
Logger . Terminate ( ) ;
while ( Logger . IsRunning ( ) ) { }
2024-06-20 20:38:58 -04:00
}
private async Task < bool > UseLocalProject ( IConsole console )
{
var jsonPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "ReCodeItProj.json" ) ;
if ( File . Exists ( jsonPath ) )
{
Logger . Log ( "Found a project file in the current directory, loading it" , ConsoleColor . Yellow ) ;
CrossCompiler = new ( ) ;
DataProvider . LoadAppSettings ( ) ;
DataProvider . IsCli = true ;
ProjectManager . LoadProject ( jsonPath ) ;
await CrossCompiler . StartCrossCompile ( ) ;
return true ;
}
return false ;
}
private async Task < bool > UseRemoteProject ( IConsole console )
2024-06-19 21:11:42 -04:00
{
if ( ProjectJsonPath is not null & & ProjectJsonPath ! = string . Empty )
{
2024-06-20 20:38:58 -04:00
if ( ! File . Exists ( ProjectJsonPath ) )
{
console . Output . WriteLine ( "The project file you provided does not exist" ) ;
return false ;
}
2024-06-19 21:11:42 -04:00
CrossCompiler = new ( ) ;
2024-06-20 20:38:58 -04:00
DataProvider . LoadAppSettings ( ) ;
DataProvider . IsCli = true ;
2024-06-19 21:11:42 -04:00
ProjectManager . LoadProject ( ProjectJsonPath ) ;
2024-06-20 20:38:58 -04:00
await CrossCompiler . StartCrossCompile ( ) ;
2024-06-19 21:11:42 -04:00
2024-06-20 20:38:58 -04:00
return true ;
2024-06-19 21:11:42 -04:00
}
2024-06-20 20:38:58 -04:00
return false ;
}
2024-06-19 21:11:42 -04:00
2024-06-20 20:38:58 -04:00
private async Task < bool > UseLastLoadedProject ( IConsole console )
{
2024-06-19 21:11:42 -04:00
if ( RegistryHelper . GetRegistryValue < string > ( "LastLoadedProject" ) ! = null )
{
2024-06-20 20:38:58 -04:00
string currentDirectory = Directory . GetCurrentDirectory ( ) ;
console . Output . WriteLine ( $"Project: {RegistryHelper.GetRegistryValue<string>(" LastLoadedProject ")}" ) ;
console . Output . WriteLine ( $"Working Dir: {currentDirectory}" ) ;
2024-06-19 21:11:42 -04:00
CrossCompiler = new ( ) ;
2024-06-19 21:34:46 -04:00
DataProvider . LoadAppSettings ( ) ;
2024-06-20 13:58:39 -04:00
DataProvider . IsCli = true ;
2024-06-19 21:34:46 -04:00
2024-06-19 21:11:42 -04:00
ProjectManager . LoadProject ( RegistryHelper . GetRegistryValue < string > ( "LastLoadedProject" ) , true ) ;
2024-06-19 22:33:08 -04:00
2024-06-20 20:38:58 -04:00
if ( ! Validate ( console ) ) { return false ; }
2024-06-19 22:33:08 -04:00
2024-06-20 19:49:06 -04:00
await CrossCompiler . StartCrossCompile ( ) ;
2024-06-19 21:11:42 -04:00
2024-06-19 21:34:46 -04:00
DataProvider . SaveAppSettings ( ) ;
2024-06-20 20:38:58 -04:00
return true ;
2024-06-19 21:11:42 -04:00
}
2024-06-20 20:38:58 -04:00
return false ;
2024-06-19 21:11:42 -04:00
}
2024-06-19 22:33:08 -04:00
private bool Validate ( IConsole console )
{
if ( ProjectManager . ActiveProject = = null )
{
console . Output . WriteLine ( "No project loaded, go create or load a project in the gui first" ) ;
return false ;
}
if ( ProjectManager . ActiveProject . RemapModels . Count = = 0 )
{
console . Output . WriteLine ( "No Remaps present, go to the gui and create some first" ) ;
return false ;
}
if ( ProjectManager . ActiveProject . ChangedTypes . Count = = 0 )
{
console . Output . WriteLine ( "There are no changed types, have you created and used a remapped reference? \nRun the command `buildRef` to generate a project reference" ) ;
return false ;
}
return true ;
}
2024-06-19 21:11:42 -04:00
}