forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigMask.cs
More file actions
93 lines (69 loc) · 2.67 KB
/
ConfigMask.cs
File metadata and controls
93 lines (69 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
namespace ScriptCs
{
using ScriptCs.Contracts;
public class ConfigMask
{
public bool? AllowPreRelease { get; set; }
public bool? Cache { get; set; }
public bool? Clean { get; set; }
public bool? Debug { get; set; }
public bool? Global { get; set; }
public string Install { get; set; }
public LogLevel? LogLevel { get; set; }
public string Modules { get; set; }
public string Output { get; set; }
public string PackageVersion { get; set; }
public bool? Repl { get; set; }
public bool? Save { get; set; }
public string ScriptName { get; set; }
public string Eval { get; set; }
public bool? Watch { get; set; }
public static ConfigMask Create(ScriptCsArgs args)
{
Guard.AgainstNullArgument("args", args);
return new ConfigMask
{
AllowPreRelease = args.AllowPreRelease ? (bool?)true : null,
Cache = args.Cache ? (bool?)true : null,
Clean = args.Clean ? (bool?)true : null,
Debug = args.Debug ? (bool?)true : null,
Global = args.Global ? (bool?)true : null,
Install = args.Install,
LogLevel = args.LogLevel,
Modules = args.Modules,
Output = args.Output,
PackageVersion = args.PackageVersion,
Repl = args.Repl ? (bool?)true : null,
Save = args.Save ? (bool?)true : null,
ScriptName = args.ScriptName,
Eval = args.Eval,
Watch = args.Watch ? (bool?)true : null,
};
}
public static ConfigMask ReadGlobalOrDefault() => Read(new FileSystem().GlobalOptsFile, true);
public static ConfigMask ReadLocalOrDefault() => Read(Constants.ConfigFilename, true);
public static ConfigMask Read(string path) => Read(path, false);
private static ConfigMask Read(string path, bool defaultIfNotExists)
{
if (defaultIfNotExists && !File.Exists(path))
{
return null;
}
var json = File.ReadAllText(path);
try
{
return JsonConvert.DeserializeObject<ConfigMask>(json);
}
catch (Exception ex)
{
var message = string.Format(
CultureInfo.InvariantCulture, "Error reading JSON config from '{0}'.", path);
throw new InvalidOperationException(message, ex);
}
}
}
}