using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using JD.Collections.Extensions;
using JD.Kernel.StaticHelpers;
using JD.LogService;
using JD.TeaBoxAddinSystem.Addins.Management;
namespace JD.TeaBoxAddinSystem.Bootstrap
{
///
/// Static class that holds many properties of the current application.
///
///
/// Note that these properties are only valid after an has been started.
///
public static class Runtime
{
private static bool _commandLineMode = true; // True by default so that it's true in unit tests.
private static ApplicationConfiguration _configuration;
[MethodImpl(MethodImplOptions.NoInlining)] // Ensures that Assembly.GetCallingAssembly() works as expected
static Runtime()
{
EntryAssembly = Assembly.GetCallingAssembly();
var entryAssembly = Assembly.GetEntryAssembly();
ApplicationName = entryAssembly != null ? entryAssembly.GetName().Name : "Unknown application";
}
///
/// Whether the runtime has been initialized.
///
public static bool Initialized { get; private set; }
///
/// Entry Assembly of the application..
///
public static Assembly EntryAssembly { get; }
///
/// Unique identifier used to identify the application.
///
public static string UniqueIdentifier
{
get { return EntryAssembly.FullName; }
}
// Useful for shared data (playlists, scripts)
// Takes deployed apps (BuildBox) into account
public static string DeploymentPath
{
get
{
string deploymentPath = null;
// First check if the working directory contains a Scripts folder and use that if it does
// Useful for forcing a specific scripts folder when launching from VS
string currentDirectory = Environment.CurrentDirectory;
// HACK: Would be nice if we didn't have to hardcode the folder name
foreach (string folderName in new[] {"Script", "Scripts"})
{
string scriptDirectory = Path.Combine(currentDirectory, folderName);
if (Directory.Exists(scriptDirectory) &&
!Directory.GetFiles(scriptDirectory, "*.cs", SearchOption.AllDirectories).IsEmpty())
{
deploymentPath = currentDirectory;
break;
}
}
if (deploymentPath == null)
{
deploymentPath = ApplicationRootPath;
}
return FileUtility.CleanPath(deploymentPath);
}
}
///
/// Arguments that were used to start the application.
///
public static Arguments Arguments { get; private set; }
///
/// Application Root Path.
///
public static string ApplicationRootPath { get; private set; } = Environment.CurrentDirectory;
///
/// Whether the application was started in command-line mode.
///
public static bool CommandLineMode
{
get { return _commandLineMode; }
set
{
if (_commandLineMode != value)
{
_commandLineMode = value;
MessageService.MessageService.IsCommandLine = _commandLineMode;
}
}
}
public static ApplicationType ApplicationType
{
get { return CommandLineMode ? ApplicationType.Default : ApplicationType.Gui; }
}
///
/// The mode into which this application was started.
///
public static RuntimeMode RuntimeMode { get; private set; } = RuntimeMode.Normal;
///
/// Whether the app is running in embedded mode.
///
public static bool IsEmbedded
{
get
{
switch (RuntimeMode)
{
case RuntimeMode.Normal:
return false;
case RuntimeMode.EmbeddedInProcess:
case RuntimeMode.SystemTests:
return true;
default:
throw new NotImplementedException();
}
}
}
///
/// Application Name.
///
public static string ApplicationName { get; private set; }
public const int SuccessErrorCode = 0;
public const int FailureErrorCode = 1;
///
/// Gets or sets the exit code of the application.
///
public static int ExitCode { get; set; } = SuccessErrorCode;
///
/// The directory used to store user-specific preferences.
///
///
/// By default, this is ApplicationData/ApplicationName in the MyDocuments folder of the current user.
///
public static string ConfigDirectory
{
get { return _configuration == null ? null : _configuration.ConfigDirectory; }
}
///
/// The directory used to store global resources.
///
///
/// By default, this is ApplicationRootPath/Data.
///
public static string DataDirectory
{
get { return _configuration == null ? null : _configuration.DataDirectory; }
}
///
/// Paths in the Addin Tree where CommandLineHandlers are.
///
public static IEnumerable CommandLineHandlerPaths
{
get { return _configuration == null ? new string[0] : _configuration.CommandLineHandlerPaths; }
}
///
/// Addin Repository of this application.
///
public static AddinRepository AddinRepository { get; private set; }
///
/// Whether this application should start its own instance.
///
public static bool ShouldStartOwnInstance
{
get
{
return (CommandLineMode || Arguments.Parameters.Count == 0 || !ApplicationInstanceManager.IsInstanceRunning(UniqueIdentifier));
}
}
internal static void Initialize(ApplicationStartup startup, ILogger logger)
{
Initialized = true;
ApplicationRootPath = !string.IsNullOrEmpty(startup.Configuration.ApplicationRootPath)
? startup.Configuration.ApplicationRootPath
: Path.GetDirectoryName(EntryAssembly.Location);
RuntimeMode = startup.RuntimeMode;
Arguments = startup.Arguments;
ApplicationName = startup.ApplicationName;
AddinRepository = new AddinRepository(ApplicationRootPath);
_configuration = startup.Configuration;
var defaultPropertiesFileName = $"{Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName)}Properties.xml";
var defaultPropertiesFilePath = Path.Combine(_configuration.ConfigDirectory, defaultPropertiesFileName);
PropertyService.PropertyService.DefaultFilePath = defaultPropertiesFilePath;
}
}
}