Skip to content

Commit

Permalink
add support for submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
uazo committed Aug 3, 2023
1 parent 4498819 commit e83504a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 21 deletions.
1 change: 1 addition & 0 deletions SuperPatch.Core/Storages/Bromite/BromiteRemoteStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override async Task FetchChromiumCommit()
(await http.GetStringAsync($"{PatchSourceUrl}/{workspace.CommitShaOrTag}/build/RELEASE"))
.Replace("\n", "")
.Replace("\r", "");
await base.FetchChromiumCommit();
}

public override async Task<byte[]> GetPatchAsync(string filename)
Expand Down
101 changes: 81 additions & 20 deletions SuperPatch.Core/Storages/ChromiumStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,121 @@
using System.Text;
using System.Threading.Tasks;
using DiffPatch.Data;
using static SuperPatch.Core.Storages.ChromiumStorage;

namespace SuperPatch.Core.Storages
{
public abstract class ChromiumStorage : Storage
{
public class SubModulesInfo
{
public string Path { get; internal set; }
public string Url { get; internal set; }
public string RevisionIndex { get; internal set; }
public string Revision { get; internal set; }
}

protected HttpClient http { get; private set; }

protected virtual string FileSourceUrl => @"https://raw.githubusercontent.com/chromium/chromium";

public string ChromiumCommit { get; protected set; }
public string ChromiumCommit { get; protected set; }
private string v8_revision;

private string _CacheDirectory = null;
protected string CacheDirectory { get { return _CacheDirectory; } }

private List<SubModulesInfo> subModulesInfos = new List<SubModulesInfo>();

public ChromiumStorage(Workspace wrk, HttpClient http) : base(wrk)
{
this.http = http;
}

protected virtual async Task FetchChromiumCommit()
subModulesInfos.Add(new SubModulesInfo()
{
Path = "v8/",
Url = "https://raw.githubusercontent.com/v8/v8",
RevisionIndex = "v8_revision"
});
}

protected virtual async Task FetchChromiumCommit()
{
ChromiumCommit = workspace.CommitShaOrTag;
await Task.CompletedTask;
if (string.IsNullOrEmpty(ChromiumCommit))
ChromiumCommit = workspace.CommitShaOrTag;

var depsByte = await GetFileAsync("DEPS");
foreach(var submodule in subModulesInfos)
submodule.Revision = GetRevision(depsByte, submodule.RevisionIndex);

await Task.CompletedTask;
}

public virtual void SetCacheDirectory( string CacheDirectory )
private string GetRevision(byte[] file, string index)
{
string deps = Encoding.UTF8.GetString(file);
var rev = $"'{index}'";
foreach (var line in deps.Split("\n"))
{
if (line.Contains(rev))
{
string value = line.Split(":")[1]
.Replace("'", "")
.Replace(",", "")
.Trim();
return value;
}
}
throw new NotSupportedException();
}

public virtual void SetCacheDirectory( string CacheDirectory )
{
_CacheDirectory = CacheDirectory;
}

public override async Task<byte[]> GetFileAsync(IFileDiff file)
{
if (file.From == "/dev/null") return null;
public override async Task<byte[]> GetFileAsync(IFileDiff file)
{
if (file.From == "/dev/null") return null;
return await GetFileAsync(file.From);
}

private async Task<byte[]> GetFileAsync(string file)
{
if (_CacheDirectory != null)
{
var localFile = System.IO.Path.Combine(_CacheDirectory, file.From);
var localFile = System.IO.Path.Combine(_CacheDirectory, file);
if( System.IO.File.Exists(localFile))
return await System.IO.File.ReadAllBytesAsync(localFile);
}

if (string.IsNullOrEmpty(ChromiumCommit)) await FetchChromiumCommit();
var content = await http.GetByteArrayAsync($"{FileSourceUrl}/{ChromiumCommit}/{file.From}");

if (_CacheDirectory != null)
{
var localFile = System.IO.Path.Combine(_CacheDirectory, file.From);
var directory = System.IO.Path.GetDirectoryName(localFile);
if (System.IO.Directory.Exists(directory) == false)
System.IO.Directory.CreateDirectory(directory);
var url = $"{FileSourceUrl}/{ChromiumCommit}/{file}";
foreach( var submodule in subModulesInfos)
if (file.StartsWith(submodule.Path))
url = $"{submodule.Url}/{submodule.Revision}/{file.Substring(submodule.Path.Length)}";

System.IO.File.WriteAllBytes(localFile, content);
}
try
{
var content = await http.GetByteArrayAsync(url);

if (_CacheDirectory != null)
{
var localFile = System.IO.Path.Combine(_CacheDirectory, file);
var directory = System.IO.Path.GetDirectoryName(localFile);
if (System.IO.Directory.Exists(directory) == false)
System.IO.Directory.CreateDirectory(directory);

System.IO.File.WriteAllBytes(localFile, content);
}

return content;
return content;
}
catch (Exception ex)
{
throw new Exception($"Url {url} error: {ex}", ex);
}
}
}
}
3 changes: 2 additions & 1 deletion SuperPatch.Core/Storages/Kiwi/KiwiRemoteStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ protected override async Task FetchChromiumCommit()
.Select(x => x.Substring(x.IndexOf("=") + 1))
.ToList();
ChromiumCommit = string.Join(".", build);
}
await base.FetchChromiumCommit();
}

public override async Task<byte[]> GetPatchAsync(string filename)
{
Expand Down

0 comments on commit e83504a

Please sign in to comment.