-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAutoNest.csx
79 lines (69 loc) · 2.73 KB
/
AutoNest.csx
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
#! "net5.0"
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
static string loc = Path.GetFullPath("AncientMysteries\\");
Main();
public static void Main()
{
string fullname = loc + Path.DirectorySeparatorChar + "AutoNest.generated.props";
var fileInfo = new FileInfo(fullname);
fileInfo.IsReadOnly = false;
StringBuilder props = new StringBuilder(@"<?xml version=""1.0"" encoding=""utf-8""?>
<!--Generated by AutoNest.csx-->
<!--Do not modify this file manually-->
<Project>
<ItemGroup>
", 5120);
foreach (var item in Directory.GetFiles(loc, "*.cs", SearchOption.AllDirectories))
{
var fullpath = item.AsSpan();
if (IsNestable(fullpath, out var parentFullpath) && File.Exists(parentFullpath))
{
var relativeFileName = item.Substring(loc.Length);
props.AppendLine($" <Compile Update=\"{relativeFileName}\" DependentUpon=\"{Path.GetFileName(parentFullpath)}\"/>");
}
}
props.Append(@" </ItemGroup>
</Project>");
File.WriteAllText(fullname, props.ToString(), Encoding.UTF8);
fileInfo.IsReadOnly = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNestable(ReadOnlySpan<char> fullpath, out string parentFullpath)
{
Unsafe.SkipInit(out parentFullpath);
int rootPathLength = fullpath.LastIndexOf(Path.DirectorySeparatorChar) + 1;
var filename = fullpath.Slice(rootPathLength);
var filenameWithoutExt = filename.Slice(0, filename.Length - 3);
int parentFilenameExtPos = filenameWithoutExt.IndexOf('.');
bool result = parentFilenameExtPos != -1;
if (!result) return false;
parentFullpath = GetParentFullpath(fullpath, rootPathLength, parentFilenameExtPos);
return true;
}
public static string GetParentFullpath(ReadOnlySpan<char> fullpath, int rootPathLength, int parentFilenameExtPos)
{
Span<char> result = stackalloc char[rootPathLength + parentFilenameExtPos + 3];
fullpath.Slice(0, rootPathLength).CopyTo(result);
fullpath.Slice(rootPathLength, parentFilenameExtPos).CopyTo(result.Slice(rootPathLength));
".cs".AsSpan().CopyTo(result.Slice(rootPathLength + parentFilenameExtPos));
return result.ToString();
}
public static ReadOnlySpan<char> GetFullNameWithoutExtension(ReadOnlySpan<char> fullpath)
{
// array will be optimized to static by compiler
int i = fullpath.LastIndexOfAny(new char[] { '.', Path.DirectorySeparatorChar });
if (i == -1 || fullpath[i] != '.') Debugger.Launch();
return fullpath.Slice(0, i);
}
public static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> fullpath)
{
int i = fullpath.LastIndexOf(Path.DirectorySeparatorChar);
if (i != -1)
{
return fullpath.Slice(i);
}
return fullpath;
}