Skip to content

Commit

Permalink
Merge pull request #64 from GeneralLibrary/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JusterZhu authored Sep 12, 2024
2 parents 6312c83 + 2b5d8cd commit c7ada43
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 138 deletions.
Binary file added imgs/bowl.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/c#/GeneralUpdate.Common/File/ComparisonResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@

namespace GeneralUpdate.Common;

/// <summary>
/// Result of a comparison between two directories.
/// </summary>
public class ComparisonResult
{
private readonly List<string> _uniqueToA = new List<string>();
private readonly List<string> _uniqueToB = new List<string>();
private readonly List<string> _differentFiles = new List<string>();

/// <summary>
/// List of files that are unique to A.
/// </summary>
public IReadOnlyList<string> UniqueToA => _uniqueToA.AsReadOnly();

/// <summary>
/// List of files that are unique to B.
/// </summary>
public IReadOnlyList<string> UniqueToB => _uniqueToB.AsReadOnly();

/// <summary>
/// List of files that are different between A and B.
/// </summary>
public IReadOnlyList<string> DifferentFiles => _differentFiles.AsReadOnly();

public void AddUniqueToA(IEnumerable<string> files) => _uniqueToA.AddRange(files);
Expand Down
17 changes: 11 additions & 6 deletions src/c#/GeneralUpdate.Common/File/GeneralFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static void RemoveBlackFile(string fileName)
_blackFiles.Remove(fileName);
}

/// <summary>
/// Compare two directories.
/// </summary>
/// <param name="dirA"></param>
/// <param name="dirB"></param>
public void CompareDirectories(string dirA, string dirB)
{
ComparisonResult = new ComparisonResult();
Expand Down Expand Up @@ -100,7 +105,7 @@ public static void CreateJson<T>(string targetPath, T obj)
File.WriteAllText(targetPath, jsonString);
}

public static T GetJson<T>(string path)
public static T? GetJson<T>(string path)
{
if (File.Exists(path))
{
Expand All @@ -115,13 +120,13 @@ public static T GetJson<T>(string path)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string Serialize(object obj)
public static string Serialize(object? obj)
{
if (obj == null) return string.Empty;
var json = JsonConvert.SerializeObject(obj);
var bytes = Encoding.Default.GetBytes(json);
var base64str = Convert.ToBase64String(bytes);
return base64str;
var base64Str = Convert.ToBase64String(bytes);
return base64Str;
}

/// <summary>
Expand All @@ -130,7 +135,7 @@ public static string Serialize(object obj)
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <returns></returns>
public static T Deserialize<T>(string str)
public static T? Deserialize<T>(string str)
{
var obj = default(T);
if (string.IsNullOrEmpty(str)) return obj;
Expand All @@ -142,7 +147,7 @@ public static T Deserialize<T>(string str)

public static string GetTempDirectory(string name)
{
var path = $"generalupdate_{DateTime.Now.ToString("yyyy-MM-dd")}_{name}";
var path = $"generalupdate_{DateTime.Now:yyyy-MM-dd}_{name}";
var tempDir = Path.Combine(Path.GetTempPath(), path);
if (!Directory.Exists(tempDir))
{
Expand Down
98 changes: 22 additions & 76 deletions src/c#/GeneralUpdate.Common/Internal/Bootstrap/AbstractBootstrap.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Diagnostics;
using System.Threading.Tasks;
using GeneralUpdate.Common.Internal.Strategy;

namespace GeneralUpdate.Common.Internal.Bootstrap
Expand All @@ -10,74 +10,31 @@ public abstract class AbstractBootstrap<TBootstrap, TStrategy>
where TBootstrap : AbstractBootstrap<TBootstrap, TStrategy>
where TStrategy : IStrategy
{
#region Private Members

private readonly ConcurrentDictionary<UpdateOption, UpdateOptionValue> _options;
private volatile Func<TStrategy> _strategyFactory;
private IStrategy _strategy;

#endregion Private Members

#region Constructors

protected internal AbstractBootstrap() => this._options = new ConcurrentDictionary<UpdateOption, UpdateOptionValue>();

#endregion Constructors

#region Methods
protected internal AbstractBootstrap() => _options = new ConcurrentDictionary<UpdateOption, UpdateOptionValue>();

/// <summary>
/// Launch udpate.
/// </summary>
/// <returns></returns>
public virtual TBootstrap LaunchAsync()
{
return (TBootstrap)this;
}

#region Strategy

protected IStrategy InitStrategy()
{
return _strategy;
}

protected string GetPlatform() => _strategy.GetPlatform();

protected IStrategy ExecuteStrategy()
{
if (_strategy != null) _strategy.Execute();
return _strategy;
}

public virtual TBootstrap Validate()
{
if (this._strategyFactory == null) throw new InvalidOperationException("Strategy or strategy factory not set.");
return (TBootstrap)this;
}

public virtual TBootstrap Strategy<T>() where T : TStrategy, new() => this.StrategyFactory(() => new T());
protected abstract TBootstrap Launch();

/// <summary>
/// Launch async udpate.
/// </summary>
/// <returns></returns>
protected abstract Task<TBootstrap> LaunchAsync();

public TBootstrap StrategyFactory(Func<TStrategy> strategyFactory)
{
this._strategyFactory = strategyFactory;
return (TBootstrap)this;
}
protected abstract IStrategy InitStrategy();

#endregion Strategy
protected abstract IStrategy ExecuteStrategy();

#region Config option.
protected virtual TBootstrap Strategy<T>() where T : TStrategy, new() => this.StrategyFactory(() => new T());

/// <summary>
/// Files in the blacklist will skip the update.
/// </summary>
/// <param name="files">blacklist file name</param>
/// <returns></returns>
public virtual TBootstrap SetBlacklist(List<string> files = null, List<string> fileFormats = null)
{
return (TBootstrap)this;
}
protected abstract TBootstrap StrategyFactory(Func<TStrategy> strategyFactory);


/// <summary>
/// Setting update configuration.
/// </summary>
Expand All @@ -87,7 +44,7 @@ public virtual TBootstrap SetBlacklist(List<string> files = null, List<string> f
/// <returns></returns>
public virtual TBootstrap Option<T>(UpdateOption<T> option, T value)
{
Contract.Requires(option != null);
Debug.Assert(option != null);
if (value == null)
{
this._options.TryRemove(option, out _);
Expand All @@ -99,24 +56,13 @@ public virtual TBootstrap Option<T>(UpdateOption<T> option, T value)
return (TBootstrap)this;
}

public virtual T GetOption<T>(UpdateOption<T> option)
public virtual T? GetOption<T>(UpdateOption<T> option)
{
try
{
if (_options == null || _options.Count == 0) return default(T);
var val = _options[option];
if (val != null) return (T)val.GetValue();
return default(T);
}
catch
{
return default(T);
}
Debug.Assert(option != null);
if (_options.Count == 0) return default(T);
var val = _options[option];
if (val != null) return (T)val.GetValue();
return default(T);
}

#endregion Config option.


#endregion Methods
}
}
26 changes: 26 additions & 0 deletions src/c#/GeneralUpdate.Common/Internal/Bootstrap/UpdateOption.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text;
using System.Threading;

namespace GeneralUpdate.Common.Internal.Bootstrap
{
public abstract class UpdateOption : AbstractConstant<UpdateOption>
{
/// <summary>
/// Update the file format of the package.
/// </summary>
public static readonly UpdateOption<string> Format = ValueOf<string>("COMPRESSFORMAT");

/// <summary>
/// Compress encoding.
/// </summary>
public static readonly UpdateOption<Encoding> Encoding = ValueOf<Encoding>("COMPRESSENCODING");

/// <summary>
/// Main program name.
/// </summary>
public static readonly UpdateOption<string> MainApp = ValueOf<string>("MAINAPP");

/// <summary>
/// Timeout period (unit: second). If this parameter is not specified, the default timeout period is 30 seconds.
/// </summary>
public static readonly UpdateOption<int> DownloadTimeOut = ValueOf<int>("DOWNLOADTIMEOUT");

/// <summary>
/// Whether to enable the driver upgrade function.
/// </summary>
public static readonly UpdateOption<bool?> Drive = ValueOf<bool?>("DRIVE");

private class UpdateOptionPool : ConstantPool
{
protected override IConstant NewConstant<T>(int id, string name) => new UpdateOption<T>(id, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace GeneralUpdate.Common.Internal.Pipeline
/// </summary>
public interface IMiddleware
{
Task InvokeAsync(IContext context, IMiddleware middleware);
Task InvokeAsync(PipelineContext context, IMiddleware middleware);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ namespace GeneralUpdate.Common.Internal.Pipeline
/// <summary>
/// Pipeline builder.
/// </summary>
public sealed class PipelineBuilder(IContext context = null)
public sealed class PipelineBuilder(PipelineContext context = null)
{
private ImmutableStack<IMiddleware> _middlewareStack = ImmutableStack<IMiddleware>.Empty;

public PipelineBuilder UseMiddleware<TMiddleware>(TMiddleware middleware) where TMiddleware : IMiddleware, new()
public PipelineBuilder UseMiddleware<TMiddleware>() where TMiddleware : IMiddleware, new()
{
var middleware = new TMiddleware();
_middlewareStack = _middlewareStack.Push(middleware);
return this;
}

public PipelineBuilder UseMiddlewareIf<TMiddleware>(TMiddleware middleware, Func<bool> condition)
public PipelineBuilder UseMiddlewareIf<TMiddleware>(Func<bool> condition)
where TMiddleware : IMiddleware, new()
{
if (!condition()) return this;
var middleware = new TMiddleware();
_middlewareStack = _middlewareStack.Push(middleware);
return this;
}
Expand Down
32 changes: 32 additions & 0 deletions src/c#/GeneralUpdate.Common/Internal/Pipeline/PipelineContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Concurrent;

namespace GeneralUpdate.Common.Internal.Pipeline;

public class PipelineContext
{
private ConcurrentDictionary<string, object?> _context = new();

public TValue? Get<TValue>(string key)
{
if (_context.TryGetValue(key, out var value))
{
return value is TValue typedValue ? typedValue : default;
}
return default;
}

public void Add<TValue>(string key, TValue? value)
{
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("Key cannot be null or whitespace.", nameof(key));
}

_context[key] = value;
}

public bool Remove(string key) => _context.TryRemove(key, out _);

public bool ContainsKey(string key) => _context.ContainsKey(key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ public abstract class AbstractStrategy : IStrategy
protected const string PATCHS = "patchs";

public virtual void Execute() => throw new NotImplementedException();

public virtual Task ExecuteAsync() => throw new NotImplementedException();

public virtual bool StartApp(string appName, int appType) => throw new NotImplementedException();
public virtual void StartApp(string appName, int appType) => throw new NotImplementedException();

public virtual string GetPlatform() => throw new NotImplementedException();

Expand Down
3 changes: 1 addition & 2 deletions src/c#/GeneralUpdate.Common/Internal/Strategy/IStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IStrategy
/// <param name="appName"></param>
/// <param name="appType"></param>
/// <returns></returns>
bool StartApp(string appName, int appType);
void StartApp(string appName, int appType);

/// <summary>
/// Get the platform for the current strategy.
Expand All @@ -34,7 +34,6 @@ public interface IStrategy
/// <summary>
/// Create a strategy.
/// </summary>
/// <param name="file">Abstraction for updating package information.</param>
void Create<T>(T parameter) where T : class;
}
}
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.Common/Shared/Object/Configinfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GeneralUpdate.Common.Shared.Object
{
public class Configinfo : Entity
public class Configinfo
{
public Configinfo()
{ }
Expand Down
Loading

0 comments on commit c7ada43

Please sign in to comment.