// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.IO; namespace Progmasoft.Tazel.Dsl.Configuration; /// A TAZEL_HOME Tazel cannot use. public sealed class ConfigurationHomeException : Exception { /// What is wrong with it. public ConfigurationHomeException(string message) : base(message) { } /// Nothing to say. public ConfigurationHomeException() { } /// What is wrong, and why. public ConfigurationHomeException(string message, Exception inner) : base(message, inner) { } } /// /// The directory of Tazel's own files for this user: TAZEL_HOME when it is set, else $HOME/.Tazel on /// Linux, macOS and the BSDs and %APPDATA%\Tazel on Windows, whatever XDG says. A TAZEL_HOME that is set /// but empty or relative is an error, never a reason to use another directory. Nothing here creates it. /// public static class TazelHome { /// The name of the variable that sets it. public static string Variable { get; } = "TAZEL_HOME"; /// The directory, resolved from the environment as it is now. public static string Directory() { if (System.Environment.GetEnvironmentVariable(Variable) is { } explicitHome) { if (explicitHome.Length == 0 || !Path.IsPathFullyQualified(explicitHome)) { throw new ConfigurationHomeException( $"{Variable} is '{explicitHome}', which is not an absolute path; set it to one, or unset it for the default"); } return Path.GetFullPath(explicitHome); } if (OperatingSystem.IsWindows()) { var appData = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); return appData.Length > 0 ? Path.Combine(appData, "Tazel") : throw new ConfigurationHomeException("%APPDATA% is not set; set TAZEL_HOME"); } var home = System.Environment.GetEnvironmentVariable("HOME"); return home is { Length: > 0 } && Path.IsPathFullyQualified(home) ? Path.Combine(home, ".Tazel") : throw new ConfigurationHomeException("HOME is not an absolute path; set TAZEL_HOME"); } }