Skip to content

Instantly share code, notes, and snippets.

@stdray
Created October 28, 2019 14:14
Show Gist options
  • Save stdray/a83f0f067648624414610385537ef161 to your computer and use it in GitHub Desktop.
Save stdray/a83f0f067648624414610385537ef161 to your computer and use it in GitHub Desktop.
async Task Main()
{
var dir = @"C:\Work\work\nsrj\trunk\lib";
var http = new HttpClient { BaseAddress = new Uri("https://search.maven.org") };
var deps = new StringBuilder();
foreach (var jar in Directory.EnumerateFiles(dir, @"*.jar", SearchOption.AllDirectories))
{
jar.Dump();
var hash = Sha1(File.ReadAllBytes(jar));
var url = $"https://search.maven.org/solrsearch/select?q=1:%20{hash}&start=0&rows=20";
var data = await http.GetStringAsync(url);
var json = JObject.Parse(data);
var docs = (JArray)json["response"]["docs"];
var pref = docs.Count > 1 ? "//" : string.Empty;
foreach(var doc in docs)
{
var meta = new JarManifest
{
Group = (string)doc["g"],
Name = (string)doc["a"],
Version = (string)doc["v"],
};
meta.ToString().Dump();
deps.Append(pref).AppendLine(meta.ToString());
}
if(docs.Count < 1 )
{
"packages not found".Dump();
deps.Append(pref).Append(new JarManifest().ToString()).Append("// ").AppendLine(jar);
}
Environment.NewLine.Dump();
}
deps.ToString().Dump();
}
public static string Sha1(byte[] inputBytes)
{
byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(inputBytes);
return BitConverter.ToString(hash)
.Replace("-", string.Empty)
.ToLower();
}
class JarManifest
{
public string Group { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public override string ToString() => $"compile group: '{Group}', name: '{Name}', version: '{Version}'";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment