-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathUtil.cs
65 lines (59 loc) · 1.85 KB
/
Util.cs
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
using Meadow.Core.Utils;
using System;
using System.IO;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
namespace Meadow.Cli
{
static class Util
{
public static string GetUniqueID()
{
var guidBytes = new byte[9];
System.Security.Cryptography.RandomNumberGenerator.Fill(guidBytes);
var guid = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "_" + BaseEncoding.ToBaseString(guidBytes, BaseEncoding.CHAR_SETS.BASE36); //HexUtil.GetHexFromBytes(guidBytes);
return guid;
}
public static string GetSolSourcePath(Config config, SessionState sessionState)
{
if (Path.IsPathRooted(config.SourceDirectory))
{
return Path.GetFullPath(config.SourceDirectory);
}
else
{
var cwd = sessionState.Path.CurrentLocation.Path;
var dir = Path.Combine(cwd, config.SourceDirectory);
return Path.GetFullPath(dir);
}
}
public static T GetResultSafe<T>(this Task<T> task)
{
if (SynchronizationContext.Current == null)
{
return task.Result;
}
if (task.IsCompleted)
{
return task.Result;
}
var tcs = new TaskCompletionSource<T>();
task.ContinueWith(
t =>
{
var ex = t.Exception;
if (ex != null)
{
tcs.SetException(ex);
}
else
{
tcs.SetResult(t.Result);
}
},
TaskScheduler.Default);
return tcs.Task.Result;
}
}
}