-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.linq
111 lines (99 loc) · 3.17 KB
/
func.linq
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.IO.Compression.FileSystem.dll</Reference>
<Reference><RuntimeDirectory>\System.IO.Compression.dll</Reference>
<NuGetReference Version="0.10.3">Mono.Cecil</NuGetReference>
<Namespace>Mono.Cecil</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.IO.Compression</Namespace>
</Query>
readonly string rootDir = Path.GetDirectoryName(Util.CurrentQueryPath);
void Main()
{
var d = GetAssemblyNameMappings(Path.Combine(rootDir, @"CloudPad\bin\Debug\net461"));
var version = "1.0.19";
var azureFunctionsCoreTools = GetAzureFunctionsCoreTools(version).TrimEnd('\\') + "\\";
var funcRoot = Path.Combine(rootDir, "func");
var funcDir = Path.Combine(funcRoot, version + "-net461");
foreach (var path in Directory.EnumerateFiles(azureFunctionsCoreTools, "*.*", SearchOption.AllDirectories))
{
var rel = path.Substring(azureFunctionsCoreTools.Length);
var dll = LoadAssemblyDefinition(path);
if (dll != null)
{
var redirected = false;
foreach (var r in dll.MainModule.AssemblyReferences)
{
if (d.TryGetValue(r.Name, out var redirect))
{
if (redirect.Version < r.Version)
{
r.Version = redirect.Version;
redirected = true;
$"Assembly '{dll.FullName}' reference to '{r.Name}' redirected to version '{redirect.Version}'".Dump();
}
}
}
if (redirected)
{
dll.Write(Path.Combine(funcDir, dll.MainModule.Assembly.Name.Name + Path.GetExtension(path)));
continue;
}
}
var dst = Path.Combine(funcDir, rel);
Directory.CreateDirectory(Path.GetDirectoryName(dst));
File.Copy(path, dst, true);
}
ZipFile.CreateFromDirectory(funcDir, Path.Combine(funcRoot, version + "-net461" + ".zip"));
}
// Define other methods and classes here
private string GetAzureFunctionsCoreTools(string version)
{
var funcRoot = Path.Combine(rootDir, "func");
var funcDir = Path.Combine(funcRoot, version + "-net471");
var funcFileName = Path.Combine(funcDir, "func.exe");
if (!File.Exists(funcFileName))
{
Directory.CreateDirectory(funcDir);
var azureFunctionsCliZip = funcDir + ".zip";
var req = WebRequest.Create($"https://functionscdn.azureedge.net/public/{version}/Azure.Functions.Cli.zip");
using (var res = req.GetResponse())
{
using (var zip = File.Create(azureFunctionsCliZip))
{
res.GetResponseStream().CopyTo(zip);
}
}
ZipFile.ExtractToDirectory(azureFunctionsCliZip, funcDir);
File.Delete(azureFunctionsCliZip);
}
return funcDir;
}
private Dictionary<string, AssemblyNameDefinition> GetAssemblyNameMappings(string dir)
{
var d = new Dictionary<string, AssemblyNameDefinition>();
foreach (var path in Directory.EnumerateFiles(dir, "*.*"))
{
var dll = LoadAssemblyDefinition(path);
if (dll != null)
{
d[dll.MainModule.Assembly.Name.Name] = dll.MainModule.Assembly.Name;
}
}
return d;
}
private AssemblyDefinition LoadAssemblyDefinition(string path)
{
var ext = Path.GetExtension(path);
if (".dll".Equals(ext, StringComparison.OrdinalIgnoreCase) || ".exe".Equals(ext, StringComparison.OrdinalIgnoreCase))
{
try
{
return AssemblyDefinition.ReadAssembly(path);
}
catch
{
// nom nom nom...
}
}
return null;
}