Skip to content

Commit 213ffba

Browse files
committed
1.0.0-preview2
1 parent d31cc10 commit 213ffba

File tree

10 files changed

+399
-61
lines changed

10 files changed

+399
-61
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ build/
2828
# Visual Studio 2015 cache/options directory
2929
.vs/
3030
.vscode/
31+
.idea/
3132
# Uncomment if you have tasks that create the project's static files in wwwroot
32-
#wwwroot/
33+
wwwroot/
34+
unpkg.json
3335

3436
# MSTest test Results
3537
[Tt]est[Rr]esult*/

Add.cs

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
using System.Linq;
55
using System.Net.Http;
66
using System.Threading.Tasks;
7-
using Newtonsoft.Json.Linq;
87

98
namespace dotnet_unpkg
109
{
11-
static class UnpkgJson
12-
{
13-
14-
}
15-
16-
static class Add
10+
public static class Add
1711
{
1812
private static readonly HttpClient Client = new HttpClient
1913
{
@@ -22,15 +16,22 @@ static class Add
2216

2317
private static readonly string BaseDirectory = Path.Combine("wwwroot", "lib");
2418

25-
public static async Task<List<UnpkgJsonEntry>> Run(IEnumerable<string> args)
19+
public static async Task Run(IEnumerable<string> args)
2620
{
21+
var argList = args.ToList();
22+
if (argList[0] == "--help" || argList[0] == "-h")
23+
{
24+
Help.Add();
25+
return;
26+
}
27+
2728
if (!Directory.Exists(BaseDirectory))
2829
{
2930
Directory.CreateDirectory(BaseDirectory);
3031
}
3132

32-
var results = await Task.WhenAll(args.Select(AddPackage));
33-
return results.ToList();
33+
var results = await Task.WhenAll(argList.Select(AddPackage));
34+
await UnpkgJson.Save(results);
3435
}
3536

3637
private static async Task<UnpkgJsonEntry> AddPackage(string package)
@@ -41,56 +42,26 @@ private static async Task<UnpkgJsonEntry> AddPackage(string package)
4142
return null;
4243
}
4344

44-
await Download(package, distFile.BaseUrl, distFile.Files);
45+
await DownloadPackage(package, distFile.BaseUrl, distFile.Files);
4546
return UnpkgJsonEntry.Create(package, distFile);
4647
}
4748

48-
private static Task Download(string package, string basePath, IEnumerable<DistFile> files)
49+
private static Task DownloadPackage(string package, string basePath, IEnumerable<DistFile> files)
4950
{
5051
var tasks = new List<Task>();
5152
foreach (var file in files)
5253
{
5354
if (file.Type == "file")
5455
{
55-
tasks.Add(Download(package, basePath, file.Path));
56+
tasks.Add(Download.DistFile(package, $"{basePath}{file.Path}"));
5657
}
5758
else if (file.Files?.Count > 0)
5859
{
59-
tasks.Add(Download(package, basePath, file.Files));
60+
tasks.Add(DownloadPackage(package, basePath, file.Files));
6061
}
6162
}
6263

6364
return Task.WhenAll(tasks);
6465
}
65-
66-
private static async Task Download(string package, string basePath, string path)
67-
{
68-
using (var response = await Client.GetAsync($"{basePath}{path}"))
69-
{
70-
if (response.IsSuccessStatusCode)
71-
{
72-
// Remove /dist/ from start of path
73-
path = path.Substring(6);
74-
75-
if (Path.DirectorySeparatorChar != '/')
76-
{
77-
path = path.Replace('/', Path.DirectorySeparatorChar);
78-
}
79-
80-
var file = Path.GetFileName(path);
81-
var directory = Path.Combine(BaseDirectory, package, Path.GetDirectoryName(path));
82-
83-
if (!Directory.Exists(directory))
84-
{
85-
Directory.CreateDirectory(directory);
86-
}
87-
88-
using (var fileStream = File.Create(Path.Combine(directory, file)))
89-
{
90-
await response.Content.CopyToAsync(fileStream);
91-
}
92-
}
93-
}
94-
}
9566
}
9667
}

Dist.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Net;
34
using System.Net.Http;
45
using System.Net.Http.Headers;
@@ -8,23 +9,18 @@
89

910
namespace dotnet_unpkg
1011
{
11-
public class Dist
12+
public static class Dist
1213
{
1314
private static readonly Regex EndPart = new Regex(@"\/dist\/\?meta$");
1415
private static readonly HttpClient Client = CreateClient();
1516

1617
public static async Task<DistFile> Get(string package)
1718
{
18-
var url = $"https://unpkg.com/{package}/dist/?meta";
19-
var response = await Client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
20-
while (response.StatusCode == HttpStatusCode.Redirect)
21-
{
22-
Console.WriteLine(response.Headers.Location);
23-
url = response.Headers.Location.ToString();
24-
response = await Client.GetAsync(url);
25-
}
19+
var response = await Find(package);
20+
var url = response.RequestMessage.RequestUri.AbsolutePath;
2621
if (response.IsSuccessStatusCode)
2722
{
23+
Console.WriteLine($"Found {UnpkgJson.CleanVersion(url)}");
2824
var json = await response.Content.ReadAsStringAsync();
2925
var distFile = JsonConvert.DeserializeObject<DistFile>(json);
3026
distFile.BaseUrl = EndPart.Replace(url, string.Empty);
@@ -35,6 +31,45 @@ public static async Task<DistFile> Get(string package)
3531
return null;
3632
}
3733

34+
private static async Task<HttpResponseMessage> Find(string package)
35+
{
36+
string url;
37+
if (package.Contains("/"))
38+
{
39+
var parts = package.Split('/');
40+
url = $"{parts[0]}/dist/{string.Join('/', parts.Skip(1))}/?meta";
41+
}
42+
else
43+
{
44+
url = $"{package}/dist/?meta";
45+
}
46+
47+
var response = await FollowRedirects(url);
48+
49+
if (response.IsSuccessStatusCode)
50+
{
51+
return response;
52+
}
53+
54+
response.Dispose();
55+
56+
return await FollowRedirects($"{package}/?meta");
57+
}
58+
59+
private static async Task<HttpResponseMessage> FollowRedirects(string url)
60+
{
61+
var response = await Client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
62+
63+
while (response.StatusCode == HttpStatusCode.Redirect)
64+
{
65+
url = response.Headers.Location.ToString();
66+
response.Dispose();
67+
response = await Client.GetAsync(url);
68+
}
69+
70+
return response;
71+
}
72+
3873
private static HttpClient CreateClient()
3974
{
4075
var httpMessageHandler = new HttpClientHandler

Download.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
7+
namespace dotnet_unpkg
8+
{
9+
public static class Download
10+
{
11+
private static readonly HttpClient Client = new HttpClient
12+
{
13+
BaseAddress = new Uri("https://unpkg.com")
14+
};
15+
private static readonly string BaseDirectory = Path.Combine("wwwroot", "lib");
16+
17+
public static async Task DistFile(string package, string path)
18+
{
19+
var target = TargetFile(path);
20+
using (var response = await Client.GetAsync(path))
21+
{
22+
if (response.IsSuccessStatusCode)
23+
{
24+
var file = Path.GetFileName(target);
25+
var directory = Path.Combine(BaseDirectory, package, Path.GetDirectoryName(target));
26+
27+
if (!Directory.Exists(directory))
28+
{
29+
Directory.CreateDirectory(directory);
30+
}
31+
32+
using (var fileStream = File.Create(Path.Combine(directory, file)))
33+
{
34+
await response.Content.CopyToAsync(fileStream);
35+
}
36+
37+
Console.WriteLine($"{Path.Combine(package, target)}... OK");
38+
}
39+
else
40+
{
41+
Console.WriteLine($"{Path.Combine(package, target)}... failed ({(int)response.StatusCode}");
42+
}
43+
}
44+
}
45+
46+
private static string TargetFile(string path) => path.Contains("/dist/")
47+
? string.Join(Path.DirectorySeparatorChar,
48+
path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries)
49+
.SkipWhile(s => !s.Equals("dist", StringComparison.OrdinalIgnoreCase))
50+
.Skip(1))
51+
: string.Join(Path.DirectorySeparatorChar,
52+
path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries)
53+
.Skip(1));
54+
}
55+
}

Program.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,38 @@
44

55
namespace dotnet_unpkg
66
{
7-
class Program
7+
public static class Program
88
{
99
static async Task Main(string[] args)
1010
{
1111
if (args.Length == 0)
1212
{
13-
Console.Error.WriteLine("Usage: unpkg add [package]");
13+
Help.Empty();
14+
return;
1415
}
1516

16-
if (args[0] == "add")
17+
try
1718
{
18-
await Add.Run(args.Skip(1));
19+
switch (args[0])
20+
{
21+
case "--help":
22+
case "-h":
23+
Help.Empty();
24+
return;
25+
case "add":
26+
await Add.Run(args.Skip(1));
27+
break;
28+
case "restore":
29+
await Restore.Run(args.Skip(1));
30+
break;
31+
default:
32+
Help.Empty();
33+
break;
34+
}
35+
}
36+
catch (Exception exception)
37+
{
38+
Console.WriteLine(exception);
1939
}
2040
}
2141
}

0 commit comments

Comments
 (0)