Skip to content

Commit

Permalink
green: implemented HTTP #load
Browse files Browse the repository at this point in the history
  • Loading branch information
adamralph committed May 28, 2016
1 parent 03e6aa7 commit 5c0bf53
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ConfigR.Roslyn.CSharp/ConfigR.Roslyn.CSharp.csproj
Expand Up @@ -51,6 +51,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="App_Packages\LibLog.4.2\LibLog.cs" />
<Compile Include="Internal\HttpSourceFileResolver.cs" />
<Compile Include="Internal\StringExtensions.cs" />
<Compile Include="ScriptOptionsExtensions.cs" />
<Compile Include="Internal\ScriptGlobals.cs" />
Expand Down
58 changes: 58 additions & 0 deletions src/ConfigR.Roslyn.CSharp/Internal/HttpSourceFileResolver.cs
@@ -0,0 +1,58 @@
// <copyright file="HttpSourceFileResolver.cs" company="ConfigR contributors">
// Copyright (c) ConfigR contributors. (configr.net@gmail.com)
// </copyright>

namespace ConfigR.Roslyn.CSharp.Internal
{
using System;
using System.Collections.Immutable;
using System.IO;
using System.Net;
using ConfigR.Roslyn.CSharp.Logging;
using Microsoft.CodeAnalysis;
using static System.FormattableString;

[CLSCompliant(false)]
public class HttpSourceFileResolver : SourceFileResolver, IEquatable<HttpSourceFileResolver>
{
private static readonly ILog log = LogProvider.GetCurrentClassLogger();

[CLSCompliant(false)]
public HttpSourceFileResolver(ImmutableArray<string> searchPaths, string baseDirectory)
: base(searchPaths, baseDirectory)
{
}

public override string ResolveReference(string path, string baseFilePath)
{
Uri uri;
if (Uri.TryCreate(path, UriKind.Absolute, out uri) && (uri.Scheme == "http" || uri.Scheme == "https"))
{
path = Path.GetTempFileName();
log.InfoFormat("Downloading '{0}' to '{1}'.", uri.ToString(), path);

using (var response = WebRequest.Create(uri).GetResponse())
using (var responseStream = response.GetResponseStream())
using (var fileStream = File.OpenWrite(path))
{
if (responseStream == null)
{
throw new InvalidOperationException(Invariant($"No response received from '{uri}'."));
}

responseStream.CopyTo(fileStream);
}

return path;
}

return base.ResolveReference(path, baseFilePath);
}

public bool Equals(HttpSourceFileResolver other) => base.Equals(other);

public override int GetHashCode() => base.GetHashCode();

public override bool Equals(object obj) => base.Equals(obj);
}
}
6 changes: 5 additions & 1 deletion src/ConfigR.Roslyn.CSharp/ScriptOptionsExtensions.cs
Expand Up @@ -6,6 +6,7 @@ namespace ConfigR
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -61,9 +62,12 @@ public static ScriptOptions ForConfigScript(this ScriptOptions options, string s
references.Add(assembly);
}

var sourceResolver =
new HttpSourceFileResolver(searchPaths.ToImmutableArray(), ScriptSourceResolver.Default.BaseDirectory);

return options
?.WithMetadataResolver(ScriptMetadataResolver.Default.WithSearchPaths(searchPaths))
.WithSourceResolver(ScriptSourceResolver.Default.WithSearchPaths(searchPaths))
.WithSourceResolver(sourceResolver)
.AddReferences(references);
}
}
Expand Down

0 comments on commit 5c0bf53

Please sign in to comment.