-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from WildernessLabs/v1.1.0.0
V1.1.0.0
- Loading branch information
Showing
78 changed files
with
2,442 additions
and
1,621 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using Meadow.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Core.Unit.Tests")] | ||
|
||
namespace Meadow; | ||
|
||
internal class AppSettingsParser | ||
{ | ||
public MeadowAppSettings Parse(string settingsFile) | ||
{ | ||
var settings = new MeadowAppSettings(); | ||
|
||
var index = 0; | ||
var spacing = 2; | ||
var parent = string.Empty; | ||
var parents = new List<string>(); | ||
var lastLevel = 0; | ||
|
||
int endLine; | ||
string lastKey; | ||
do | ||
{ | ||
var level = 0; | ||
|
||
endLine = settingsFile.IndexOf('\n', index); | ||
|
||
while (settingsFile[index] == ' ') | ||
{ | ||
index += spacing; | ||
level++; | ||
} | ||
|
||
string line; | ||
|
||
if (endLine != -1) | ||
{ | ||
line = settingsFile.Substring(index, endLine - index - 1).TrimEnd(); | ||
} | ||
else | ||
{ | ||
// last line with no newline | ||
line = settingsFile.Substring(index).TrimEnd(); | ||
} | ||
|
||
if (line != string.Empty && line[0] != '#') | ||
{ | ||
|
||
if (level > lastLevel) | ||
{ | ||
//parent = parent.Length == 0 ? lastKey : $"{parent}.{lastKey}"; | ||
parent = string.Join('.', parents); | ||
} | ||
else if (level < lastLevel) | ||
{ | ||
parents.RemoveRange(level, parents.Count - level); | ||
parent = string.Join('.', parents.Take(level)); | ||
} | ||
lastLevel = level; | ||
|
||
var kvp = line | ||
.Split(':', System.StringSplitOptions.RemoveEmptyEntries); | ||
|
||
switch (kvp.Length) | ||
{ | ||
case 0: | ||
break; | ||
case 1: | ||
lastKey = kvp[0].Trim(); | ||
parents.Add(lastKey); | ||
break; | ||
default: | ||
var name = $"{parent}.{kvp[0]}"; | ||
var value = kvp[1].Trim(); | ||
ApplySetting(settings, name, value); | ||
break; | ||
} | ||
} | ||
|
||
index = endLine + 1; | ||
} while (endLine > 0 && index < settingsFile.Length); | ||
|
||
return settings; | ||
} | ||
|
||
private void ApplySetting(MeadowAppSettings settings, string settingName, string settingValue) | ||
{ | ||
switch (settingName) | ||
{ | ||
case "Logging.LogLevel.Default": | ||
if (Enum.TryParse<LogLevel>(settingValue, true, out LogLevel level)) | ||
{ | ||
settings.LoggingSettings.LogLevel.Default = level; | ||
} | ||
break; | ||
case "Logging.ShowTicks": | ||
if (bool.TryParse(settingValue, out bool st)) | ||
{ | ||
settings.LoggingSettings.ShowTicks = st; | ||
} | ||
break; | ||
|
||
case "Lifecycle.ResetOnAppFailure": | ||
if (bool.TryParse(settingValue, out bool r)) | ||
{ | ||
settings.LifecycleSettings.RestartOnAppFailure = r; | ||
} | ||
break; | ||
case "Lifecycle.AppFailureRestartDelaySeconds": | ||
if (int.TryParse(settingValue, out int rd)) | ||
{ | ||
settings.LifecycleSettings.AppFailureRestartDelaySeconds = rd; | ||
} | ||
break; | ||
|
||
case "Update.Enabled": | ||
if (bool.TryParse(settingValue, out bool ue)) | ||
{ | ||
settings.UpdateSettings.Enabled = ue; | ||
} | ||
break; | ||
case "Update.UpdateServer": | ||
settings.UpdateSettings.UpdateServer = settingValue; | ||
break; | ||
case "Update.UpdatePort": | ||
if (int.TryParse(settingValue, out int up)) | ||
{ | ||
settings.UpdateSettings.UpdatePort = up; | ||
} | ||
break; | ||
case "Update.AuthServer": | ||
settings.UpdateSettings.AuthServer = settingValue; | ||
break; | ||
case "Update.AuthPort": | ||
if (int.TryParse(settingValue, out int cp)) | ||
{ | ||
settings.UpdateSettings.AuthPort = cp; | ||
} | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.