示例#1
0
        /// <summary>
        /// Creates a new configuration file
        /// </summary>
        /// <param name="path">The path where the file is or if it no exists, it can be created later</param>
        public ConfigurationFile(string path)
        {
            // Throw exception if path is a directory
            if (path.IsDirectory())
            {
                throw new ArgumentException("Path cannot be a directory.", nameof(path));
            }

            FileInfo fi = new FileInfo(path);

            // Set variables
            Path     = path;
            Exists   = fi.Exists;
            FileName = fi.Name;
            Length   = fi.Length;

            // If the file exists, check if its a valid configuration file
            if (fi.Exists)
            {
                if (!ConfigFileFactory.IsValidConfigurationFile(path))
                {
                    throw new InvalidConfigurationException(path);
                }
            }
        }
示例#2
0
 public TemplateService(
     ILogger <TemplateService> logger,
     ConfigFileFactory configFactory
     )
 {
     _logger        = logger;
     _configFactory = configFactory;
 }
示例#3
0
        private CommandContext CreateContext(CommandLine options)
        {
            var context = new CommandContext
            {
                CancellationToken = _cts.Token,
                Console           = _console,
                WorkingDir        = _workingDir,
                Logger            = new ConsoleLogger(_console, options.IsVerbose),
                Settings          = DnvmSettings.Load(),
            };

            var envFactory        = new DotNetEnvFactory(context.Settings);
            var configFileFactory = new ConfigFileFactory();
            var configFile        = configFileFactory.FindFile(context.WorkingDir);

            if (configFile != null)
            {
                ConfigFile config;
                try
                {
                    config = configFileFactory.Create(configFile);
                }
                catch (FormatException ex)
                {
                    context.Logger.LogError($"Config file '{configFile}' has an invalid format. {ex.Message}");
                    return(null);
                }

                context.Environment = envFactory.CreateFromConfig(config);
                context.ConfigFile  = config;
            }
            else
            {
                context.Environment = envFactory.CreateDefault();
            }

            return(context);
        }
示例#4
0
 /// <summary>
 /// Writes the configuration header in the file
 /// </summary>
 public FileStream WriteHeader() => ConfigFileFactory.GetNewConfigurationFile(this);