|
5 | 5 | using System.Text; |
6 | 6 | using System.Threading.Tasks; |
7 | 7 | using DiffPatch.Data; |
| 8 | +using static SuperPatch.Core.Storages.ChromiumStorage; |
8 | 9 |
|
9 | 10 | namespace SuperPatch.Core.Storages |
10 | 11 | { |
11 | 12 | public abstract class ChromiumStorage : Storage |
12 | 13 | { |
| 14 | + public class SubModulesInfo |
| 15 | + { |
| 16 | + public string Path { get; internal set; } |
| 17 | + public string Url { get; internal set; } |
| 18 | + public string RevisionIndex { get; internal set; } |
| 19 | + public string Revision { get; internal set; } |
| 20 | + } |
| 21 | + |
13 | 22 | protected HttpClient http { get; private set; } |
14 | 23 |
|
15 | 24 | protected virtual string FileSourceUrl => @"https://raw.githubusercontent.com/chromium/chromium"; |
16 | 25 |
|
17 | | - public string ChromiumCommit { get; protected set; } |
| 26 | + public string ChromiumCommit { get; protected set; } |
| 27 | + private string v8_revision; |
18 | 28 |
|
19 | 29 | private string _CacheDirectory = null; |
20 | 30 | protected string CacheDirectory { get { return _CacheDirectory; } } |
21 | 31 |
|
| 32 | + private List<SubModulesInfo> subModulesInfos = new List<SubModulesInfo>(); |
| 33 | + |
22 | 34 | public ChromiumStorage(Workspace wrk, HttpClient http) : base(wrk) |
23 | 35 | { |
24 | 36 | this.http = http; |
25 | | - } |
26 | 37 |
|
27 | | - protected virtual async Task FetchChromiumCommit() |
| 38 | + subModulesInfos.Add(new SubModulesInfo() |
| 39 | + { |
| 40 | + Path = "v8/", |
| 41 | + Url = "https://raw.githubusercontent.com/v8/v8", |
| 42 | + RevisionIndex = "v8_revision" |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + protected virtual async Task FetchChromiumCommit() |
28 | 47 | { |
29 | | - ChromiumCommit = workspace.CommitShaOrTag; |
30 | | - await Task.CompletedTask; |
| 48 | + if (string.IsNullOrEmpty(ChromiumCommit)) |
| 49 | + ChromiumCommit = workspace.CommitShaOrTag; |
| 50 | + |
| 51 | + var depsByte = await GetFileAsync("DEPS"); |
| 52 | + foreach(var submodule in subModulesInfos) |
| 53 | + submodule.Revision = GetRevision(depsByte, submodule.RevisionIndex); |
| 54 | + |
| 55 | + await Task.CompletedTask; |
31 | 56 | } |
32 | 57 |
|
33 | | - public virtual void SetCacheDirectory( string CacheDirectory ) |
| 58 | + private string GetRevision(byte[] file, string index) |
| 59 | + { |
| 60 | + string deps = Encoding.UTF8.GetString(file); |
| 61 | + var rev = $"'{index}'"; |
| 62 | + foreach (var line in deps.Split("\n")) |
| 63 | + { |
| 64 | + if (line.Contains(rev)) |
| 65 | + { |
| 66 | + string value = line.Split(":")[1] |
| 67 | + .Replace("'", "") |
| 68 | + .Replace(",", "") |
| 69 | + .Trim(); |
| 70 | + return value; |
| 71 | + } |
| 72 | + } |
| 73 | + throw new NotSupportedException(); |
| 74 | + } |
| 75 | + |
| 76 | + public virtual void SetCacheDirectory( string CacheDirectory ) |
34 | 77 | { |
35 | 78 | _CacheDirectory = CacheDirectory; |
36 | 79 | } |
37 | 80 |
|
38 | | - public override async Task<byte[]> GetFileAsync(IFileDiff file) |
39 | | - { |
40 | | - if (file.From == "/dev/null") return null; |
| 81 | + public override async Task<byte[]> GetFileAsync(IFileDiff file) |
| 82 | + { |
| 83 | + if (file.From == "/dev/null") return null; |
| 84 | + return await GetFileAsync(file.From); |
| 85 | + } |
| 86 | + |
| 87 | + private async Task<byte[]> GetFileAsync(string file) |
| 88 | + { |
41 | 89 | if (_CacheDirectory != null) |
42 | 90 | { |
43 | | - var localFile = System.IO.Path.Combine(_CacheDirectory, file.From); |
| 91 | + var localFile = System.IO.Path.Combine(_CacheDirectory, file); |
44 | 92 | if( System.IO.File.Exists(localFile)) |
45 | 93 | return await System.IO.File.ReadAllBytesAsync(localFile); |
46 | 94 | } |
47 | 95 |
|
48 | 96 | if (string.IsNullOrEmpty(ChromiumCommit)) await FetchChromiumCommit(); |
49 | | - var content = await http.GetByteArrayAsync($"{FileSourceUrl}/{ChromiumCommit}/{file.From}"); |
50 | 97 |
|
51 | | - if (_CacheDirectory != null) |
52 | | - { |
53 | | - var localFile = System.IO.Path.Combine(_CacheDirectory, file.From); |
54 | | - var directory = System.IO.Path.GetDirectoryName(localFile); |
55 | | - if (System.IO.Directory.Exists(directory) == false) |
56 | | - System.IO.Directory.CreateDirectory(directory); |
| 98 | + var url = $"{FileSourceUrl}/{ChromiumCommit}/{file}"; |
| 99 | + foreach( var submodule in subModulesInfos) |
| 100 | + if (file.StartsWith(submodule.Path)) |
| 101 | + url = $"{submodule.Url}/{submodule.Revision}/{file.Substring(submodule.Path.Length)}"; |
57 | 102 |
|
58 | | - System.IO.File.WriteAllBytes(localFile, content); |
59 | | - } |
| 103 | + try |
| 104 | + { |
| 105 | + var content = await http.GetByteArrayAsync(url); |
| 106 | + |
| 107 | + if (_CacheDirectory != null) |
| 108 | + { |
| 109 | + var localFile = System.IO.Path.Combine(_CacheDirectory, file); |
| 110 | + var directory = System.IO.Path.GetDirectoryName(localFile); |
| 111 | + if (System.IO.Directory.Exists(directory) == false) |
| 112 | + System.IO.Directory.CreateDirectory(directory); |
| 113 | + |
| 114 | + System.IO.File.WriteAllBytes(localFile, content); |
| 115 | + } |
60 | 116 |
|
61 | | - return content; |
| 117 | + return content; |
| 118 | + } |
| 119 | + catch (Exception ex) |
| 120 | + { |
| 121 | + throw new Exception($"Url {url} error: {ex}", ex); |
| 122 | + } |
62 | 123 | } |
63 | 124 | } |
64 | 125 | } |
0 commit comments