Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
pjy612 committed Mar 22, 2023
1 parent 7b12b9e commit c6f217c
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 0 deletions.
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
[Bb]in/
[Oo]bj/
publish/
# mstest test results
TestResults

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates
.vs
UpgradeLog.htm
# Build results
[Dd]ebug/
[Rr]elease/
x64/
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.log
*.vspscc
*.vssscc
.builds

# Visual Studio 2015 cache/options directory
.vs/
/Digital.Library/Digital.Library.API/.vs
/eLending/.vs
/DigitalLooseleaf/LN.DL.SubMgmt/.vs

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*

# NCrunch
*.ncrunch*
.*crunch*.local.xml

# Installshield output folder
[Ee]xpress

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
#publish

# Publish Web Output
*.Publish.xml

# NuGet Packages Directory
packages/

# Windows Azure Build Output
csx
*.build.csdef

# Windows Store app package directory
AppPackages/

# TFS files and folders
$tf/

# Others
[Bb]in
[Oo]bj
sql
TestResults
[Tt]est[Rr]esult*
*.Cache
ClientBin
[Ss]tyle[Cc]op.*
~$*
*.dbmdl
Generated_Code #added for RIA/Silverlight projects

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML

#ELMAH error logs
error-*.xml
*.nupkg
*.transform
LnRedNotification/LnRedNotification/Web.config
DigitalLooseleaf/LN.DL.SubMgmt/UI/LN.DL.SubMgmt.UI/Pages/Ereader.aspx.cs
DigitalLooseleaf/LN.DL.SubMgmt/Common/LN.DL.SubMgmt.Common/LN.DL.SubMgmt.Common.csproj
eLending/LN.Database.Packaging/LN.Database.Packaging.jfm
eLending/LN.Database/LN.Database.jfm
*.jfm
/DigitalLooseleaf/LN.DL.SubMgmt/Service/LN.DL.SubMgmt.Services/Properties/PublishProfiles
Binary file added ManifestDownload.nrproj
Binary file not shown.
25 changes: 25 additions & 0 deletions ManifestDownload.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33502.453
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManifestDownload", "ManifestDownload\ManifestDownload.csproj", "{06F759AE-26DF-40A0-8C91-4732DB1737F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06F759AE-26DF-40A0-8C91-4732DB1737F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06F759AE-26DF-40A0-8C91-4732DB1737F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06F759AE-26DF-40A0-8C91-4732DB1737F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06F759AE-26DF-40A0-8C91-4732DB1737F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D7090C66-29BF-449B-A1D8-E8F0E9E55737}
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions ManifestDownload/ManifestDownload.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandDotNet" Version="7.0.2" />
<PackageReference Include="CommandDotNet.NameCasing" Version="4.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Octokit" Version="5.0.2" />
<PackageReference Include="PromptPlus" Version="3.3.3" />
<PackageReference Include="PromptPlusCommandDotNet" Version="1.0.0.333" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
188 changes: 188 additions & 0 deletions ManifestDownload/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using System.Reflection.Metadata;
using System.Text;
using CommandDotNet;
using CommandDotNet.NameCasing;
using Microsoft.Extensions.Configuration;
using Octokit;
using Octokit.Internal;
using PPlus;
using PPlus.CommandDotNet;
using PPlus.Objects;
using Blob = Octokit.Blob;

namespace GitDownload
{
internal class Program
{
const string repoOwner = "wxy1343";
const string repoName = "ManifestAutoUpdate";
const string tokenKey = "GitHubToken";
private static Repository repo;
private static GitHubClient github;

static Task Init()
{
PromptPlus.WaitProcess("获取远程清单仓库")
.AddProcess(new SingleProcess(async t =>
{
github = new GitHubClient(new ProductHeaderValue("SteamDepotDownload"));
string token = Config[tokenKey];
if (!string.IsNullOrWhiteSpace(token))
{
github.Credentials = new Credentials(token);
}
repo = await github.Repository.Get(repoOwner, repoName);
return "Done";
})).Run();
return Task.CompletedTask;
}

static async Task<int> Main(string[] args)
{
PromptPlus.Banner("ManifestDownload").Run(ConsoleColor.Green);
PromptPlus.WriteLine("By [cyan]pjy612");
return await new AppRunner<Program>()
.UseDefaultMiddleware()
.UseNameCasing(Case.CamelCase)
.UsePromptPlusAnsiConsole()
.UsePrompter()
.UsePromptPlusWizard()
.UseArgumentPrompter()
//.UsePromptPlusRepl(colorizeSessionInitMessage: (msg) => msg.Yellow().Underline())
.RunAsync(args);
}

[Command("app",
Description = "download latest manifests by appid",
IgnoreUnexpectedOperands = true,
UsageLines = new[]
{
"",
"app 4000",
"app 4000 990080",
})]
public async Task App(
[Positional("appid", Description = "list of appIds")]
params string[] appIds)
{
await Init();
foreach (string appId in appIds.Where(r => !string.IsNullOrWhiteSpace(r)).Select(r => r.Trim()))
{
await github.DownloadAllByAppid(repo.Id, appId);
}
}

[Command("manifest",
IgnoreUnexpectedOperands = true,
Description = "download specify manifest by manifestId",
UsageLines = new[]
{
"",
"manifest 4004_2300048559188578691",
"manifest 4004_2300048559188578691 1998962_5952380176714618960"
})]
public async Task Manifest(
[Positional("manifestId", Description = "list of manifestIds")]
params string[] manifestIds)
{
await Init();
Task[] array = manifestIds.Where(r => !string.IsNullOrWhiteSpace(r)).Select(r => r.Trim()).Select(manifestId => github.DownloadAllByManifest(repo.Id, manifestId)).ToArray();
Task.WaitAll(array);
}

private static IConfigurationRoot Config => GetSetting();

static IConfigurationRoot GetSetting()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
return builder.Build();
}
}

static class GitHubClientExtension
{
const string depotcache = "depotcache";

public static async Task DownloadAllByAppid(this GitHubClient github, long repoId, string appid)
{
try
{
string appDir = appid;
TreeResponse tree = await github.Git.Tree.Get(repoId, appid);
string basePath = Path.Combine(depotcache, appDir);
Directory.CreateDirectory(basePath);
if (tree.Tree.Any())
{
PromptPlus.WriteLine($"AppId:[cyan]{appid}[/] [yellow]获取到清单列表...");
}

foreach (TreeItem treeItem in tree.Tree)
{
if (treeItem.Type.Value == TreeType.Blob)
{
Blob blob = await github.Git.Blob.Get(repoId, treeItem.Sha);
byte[] bytes = blob.GetContent();
if (bytes.Any())
{
PromptPlus.WriteLine($"下载完毕:[cyan]{treeItem.Path}");
await File.WriteAllBytesAsync(Path.Combine(basePath, treeItem.Path), bytes);
}
}
}
}
catch (Exception e)
{
PromptPlus.WriteLine($"[red]下载出错[/] AppId:[cyan]{appid}\n[red]{e.Message}");
}
}

static byte[] GetContent(this Blob blob)
{
string blobContent = blob.Content;
byte[] bytes = Array.Empty<byte>();
switch (blob.Encoding.Value)
{
case EncodingType.Base64:
bytes = Convert.FromBase64String(blobContent);
break;
case EncodingType.Utf8:
bytes = Encoding.UTF8.GetBytes(blobContent);
break;
}

return bytes;
}

public static async Task DownloadAllByManifest(this GitHubClient github, long repoId, string manifestId)
{
string basePath = Path.Combine(depotcache);
//foreach (string manifestId in manifestIds)
{
try
{
TreeResponse tree = await github.Git.Tree.Get(repoId, $"{manifestId}");
TreeItem treeItem = tree.Tree.FirstOrDefault(r => r.Path.StartsWith(manifestId));
if (treeItem != null)
{
if (treeItem.Type.Value == TreeType.Blob)
{
Blob blob = await github.Git.Blob.Get(repoId, treeItem.Sha);
byte[] bytes = blob.GetContent();
if (bytes.Any())
{
PromptPlus.WriteLine($"下载完毕:[cyan]{treeItem.Path}");
await File.WriteAllBytesAsync(Path.Combine(basePath, treeItem.Path), bytes);
}
}
}
}
catch (Exception e)
{
PromptPlus.WriteLine($"下载出错:{manifestId} {e.Message}", ConsoleColor.Red);
}
}
}
}
}
18 changes: 18 additions & 0 deletions ManifestDownload/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>..\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
Loading

0 comments on commit c6f217c

Please sign in to comment.