diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a0b5c9e6..e0e25260 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,7 +44,7 @@ jobs: uses: ilammy/msvc-dev-cmd@v1 - name: Select the project to build shell: bash - run: echo "PROJECT=$([ "${{runner.os}}" == "Windows" ] && echo Kyoo.WindowsHost || echo Kyoo)" >> $GITHUB_ENV + run: echo "PROJECT=$([ "${{runner.os}}" == "Windows" ] && echo Kyoo.Host.WindowsTrait || echo Kyoo.Host.Console)" >> $GITHUB_ENV - name: Build the app env: INCLUDE: ${{env.INCLUDE}};C:\Program Files\FFmpeg\include diff --git a/Kyoo.Abstractions/Controllers/IApplication.cs b/Kyoo.Abstractions/Controllers/IApplication.cs index 95e01454..8e72280e 100644 --- a/Kyoo.Abstractions/Controllers/IApplication.cs +++ b/Kyoo.Abstractions/Controllers/IApplication.cs @@ -20,5 +20,12 @@ /// /// Retrieve the data directory where runtime data should be stored string GetDataDirectory(); + + /// + /// Retrieve the path of the json configuration file + /// (relative to the data directory, see ). + /// + /// The configuration file name. + string GetConfigFile(); } } \ No newline at end of file diff --git a/Kyoo.Host.Console/Kyoo.Host.Console.csproj b/Kyoo.Host.Console/Kyoo.Host.Console.csproj new file mode 100644 index 00000000..e7b396be --- /dev/null +++ b/Kyoo.Host.Console/Kyoo.Host.Console.csproj @@ -0,0 +1,16 @@ + + + Exe + net5.0 + Kyoo.Host.Console.Program + + SDG + Zoe Roux + https://github.com/AnonymusRaccoon/Kyoo + default + + + + + + diff --git a/Kyoo/Program.cs b/Kyoo.Host.Console/Program.cs similarity index 63% rename from Kyoo/Program.cs rename to Kyoo.Host.Console/Program.cs index a2c3689a..b6699fcf 100644 --- a/Kyoo/Program.cs +++ b/Kyoo.Host.Console/Program.cs @@ -1,25 +1,20 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; -namespace Kyoo +namespace Kyoo.Host.Console { /// /// Program entrypoint. /// public static class Program { - /// - /// The path of the json configuration of the application. - /// - public const string JsonConfigPath = "./settings.json"; - /// /// The string representation of the environment used in . /// #if DEBUG - public const string Environment = "Development"; + private const string Environment = "Development"; #else - public const string Environment = "Production"; + private const string Environment = "Production"; #endif /// @@ -28,7 +23,7 @@ namespace Kyoo /// Command line arguments public static Task Main(string[] args) { - Application application = new(); + Application application = new(Environment); return application.Start(args); } } diff --git a/Kyoo.WindowsHost/Kyoo.WindowsHost.csproj b/Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.csproj similarity index 52% rename from Kyoo.WindowsHost/Kyoo.WindowsHost.csproj rename to Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.csproj index 35a4a4b8..41ab83db 100644 --- a/Kyoo.WindowsHost/Kyoo.WindowsHost.csproj +++ b/Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.csproj @@ -3,8 +3,8 @@ true - - + + diff --git a/Kyoo.WindowsHost/Kyoo.WindowsHost.linux.target b/Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.linux.target similarity index 100% rename from Kyoo.WindowsHost/Kyoo.WindowsHost.linux.target rename to Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.linux.target diff --git a/Kyoo.WindowsHost/Kyoo.WindowsHost.target b/Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.target similarity index 100% rename from Kyoo.WindowsHost/Kyoo.WindowsHost.target rename to Kyoo.Host.WindowsTrait/Kyoo.Host.WindowsTrait.target diff --git a/Kyoo.WindowsHost/Program.cs b/Kyoo.Host.WindowsTrait/Program.cs similarity index 60% rename from Kyoo.WindowsHost/Program.cs rename to Kyoo.Host.WindowsTrait/Program.cs index c3ea68b4..9b4936ba 100644 --- a/Kyoo.WindowsHost/Program.cs +++ b/Kyoo.Host.WindowsTrait/Program.cs @@ -1,18 +1,27 @@ using System.Threading.Tasks; using Autofac; -namespace Kyoo.WindowsHost +namespace Kyoo.Host.WindowsTrait { public static class Program { /// - /// The main entry point for the application that overrides the default host (). + /// The string representation of the environment used in IWebHostEnvironment. + /// +#if DEBUG + private const string Environment = "Development"; +#else + private const string Environment = "Production"; +#endif + + /// + /// The main entry point for the application that overrides the default host. /// It adds a system trait for windows and since the host is build as a windows executable instead of a console /// app, the console is not showed. /// public static Task Main(string[] args) { - Application application = new(); + Application application = new(Environment); return application.Start(args, builder => { builder.RegisterType().As().SingleInstance(); diff --git a/Kyoo.WindowsHost/SystemTrait.cs b/Kyoo.Host.WindowsTrait/SystemTrait.cs similarity index 99% rename from Kyoo.WindowsHost/SystemTrait.cs rename to Kyoo.Host.WindowsTrait/SystemTrait.cs index 96525f7d..86037c1d 100644 --- a/Kyoo.WindowsHost/SystemTrait.cs +++ b/Kyoo.Host.WindowsTrait/SystemTrait.cs @@ -9,7 +9,7 @@ using Kyoo.Abstractions.Controllers; using Kyoo.Models.Options; using Microsoft.Extensions.Options; -namespace Kyoo.WindowsHost +namespace Kyoo.Host.WindowsTrait { /// /// A singleton that add an notification icon on the window's toolbar. diff --git a/Kyoo.WindowsHost/kyoo.ico b/Kyoo.Host.WindowsTrait/kyoo.ico similarity index 100% rename from Kyoo.WindowsHost/kyoo.ico rename to Kyoo.Host.WindowsTrait/kyoo.ico diff --git a/Kyoo.sln b/Kyoo.sln index 6ab7d6d9..cbb05a6c 100644 --- a/Kyoo.sln +++ b/Kyoo.sln @@ -19,7 +19,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Tests", "tests\Kyoo.Te EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.WebApp", "Kyoo.WebApp\Kyoo.WebApp.csproj", "{2374D500-1ADB-4752-85DB-8BB0DDF5A8E8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.WindowsHost", "Kyoo.WindowsHost\Kyoo.WindowsHost.csproj", "{98851001-40DD-46A6-94B3-2F8D90722076}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Host.WindowsTrait", "Kyoo.Host.WindowsTrait\Kyoo.Host.WindowsTrait.csproj", "{98851001-40DD-46A6-94B3-2F8D90722076}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Host.Console", "Kyoo.Host.Console\Kyoo.Host.Console.csproj", "{D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -75,5 +77,9 @@ Global {98851001-40DD-46A6-94B3-2F8D90722076}.Debug|Any CPU.Build.0 = Debug|Any CPU {98851001-40DD-46A6-94B3-2F8D90722076}.Release|Any CPU.ActiveCfg = Release|Any CPU {98851001-40DD-46A6-94B3-2F8D90722076}.Release|Any CPU.Build.0 = Release|Any CPU + {D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Kyoo/Application.cs b/Kyoo/Application.cs index 4cc1004d..b51192f3 100644 --- a/Kyoo/Application.cs +++ b/Kyoo/Application.cs @@ -37,6 +37,22 @@ namespace Kyoo /// private CancellationTokenSource _tokenSource; + /// + /// The environment in witch Kyoo will run (ether "Production" or "Development"). + /// + private readonly string _environment; + + + /// + /// Create a new that will use the specified environment. + /// + /// The environment to run in. + public Application(string environment) + { + _environment = environment; + } + + /// /// Start the application with the given console args. /// This is generally called from the Main entrypoint of Kyoo. @@ -100,7 +116,14 @@ namespace Kyoo { return _dataDir; } - + + + /// + public string GetConfigFile() + { + return "./settings.json"; + } + /// /// Parse the data directory from environment variables and command line arguments, create it if necessary. /// Set the current directory to said data folder and place a default configuration file if it does not already @@ -108,7 +131,7 @@ namespace Kyoo /// /// The command line arguments /// The current data directory. - private static string _SetupDataDir(string[] args) + private string _SetupDataDir(string[] args) { Dictionary registry = new(); @@ -134,9 +157,9 @@ namespace Kyoo Directory.CreateDirectory(path); Environment.CurrentDirectory = path; - if (!File.Exists(Program.JsonConfigPath)) - File.Copy(Path.Join(AppDomain.CurrentDomain.BaseDirectory, Program.JsonConfigPath), - Program.JsonConfigPath); + if (!File.Exists(GetConfigFile())) + File.Copy(Path.Join(AppDomain.CurrentDomain.BaseDirectory, GetConfigFile()), + GetConfigFile()); return path; } @@ -159,7 +182,7 @@ namespace Kyoo Log.Fatal(ex, "Unhandled exception"); } } - + /// /// Create a a web host /// @@ -172,7 +195,7 @@ namespace Kyoo return new HostBuilder() .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .UseContentRoot(AppDomain.CurrentDomain.BaseDirectory) - .UseEnvironment(Program.Environment) + .UseEnvironment(_environment) .ConfigureAppConfiguration(x => _SetupConfig(x, args)) .UseSerilog((host, builder) => _ConfigureLogging(builder, host.Configuration)) .ConfigureServices(x => x.AddRouting()) @@ -198,8 +221,8 @@ namespace Kyoo private IConfigurationBuilder _SetupConfig(IConfigurationBuilder builder, string[] args) { return builder.SetBasePath(GetDataDirectory()) - .AddJsonFile(Path.Join(AppDomain.CurrentDomain.BaseDirectory, Program.JsonConfigPath), false, true) - .AddJsonFile(Program.JsonConfigPath, false, true) + .AddJsonFile(Path.Join(AppDomain.CurrentDomain.BaseDirectory, GetConfigFile()), false, true) + .AddJsonFile(GetConfigFile(), false, true) .AddEnvironmentVariables() .AddEnvironmentVariables("KYOO_") .AddCommandLine(args); diff --git a/Kyoo/Controllers/ConfigurationManager.cs b/Kyoo/Controllers/ConfigurationManager.cs index c6476551..d43129de 100644 --- a/Kyoo/Controllers/ConfigurationManager.cs +++ b/Kyoo/Controllers/ConfigurationManager.cs @@ -21,6 +21,11 @@ namespace Kyoo.Controllers /// private readonly IConfiguration _configuration; + /// + /// The application running Kyoo, it is used to retrieve the configuration file. + /// + private readonly IApplication _application; + /// /// The strongly typed list of options /// @@ -31,9 +36,11 @@ namespace Kyoo.Controllers /// /// The configuration to use. /// The strongly typed option list. - public ConfigurationManager(IConfiguration configuration, IEnumerable references) + /// The application running Kyoo, it is used to retrieve the configuration file. + public ConfigurationManager(IConfiguration configuration, IEnumerable references, IApplication application) { _configuration = configuration; + _application = application; _references = references.ToDictionary(x => x.Path, x => x.Type, StringComparer.OrdinalIgnoreCase); } @@ -131,7 +138,7 @@ namespace Kyoo.Controllers IDictionary configDic = config; configDic[path] = value; JObject obj = JObject.FromObject(config); - await using StreamWriter writer = new(Program.JsonConfigPath); + await using StreamWriter writer = new(_application.GetConfigFile()); await writer.WriteAsync(obj.ToString()); } diff --git a/Kyoo/Kyoo.csproj b/Kyoo/Kyoo.csproj index b3b31fcf..0e37a4f9 100644 --- a/Kyoo/Kyoo.csproj +++ b/Kyoo/Kyoo.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -7,7 +7,6 @@ SDG Zoe Roux https://github.com/AnonymusRaccoon/Kyoo - Kyoo.Program default @@ -55,10 +54,7 @@ - + @@ -70,5 +66,6 @@ +