Skip to content

Commit

Permalink
Merge pull request #95 from pjf/ks2ckanfixes
Browse files Browse the repository at this point in the history
Added download_size field.
  • Loading branch information
pjf committed Oct 20, 2014
2 parents f041d48 + 9576a00 commit d16eacb
Show file tree
Hide file tree
Showing 18 changed files with 278 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ test-results
TestResult.xml
StyleCop.Cache
ckan.exe
ks2ckan.exe
.*.swp
.fatten.out
*.resources
*.suo
*.orig
*.ckan
11 changes: 11 additions & 0 deletions BootKAN/CrewFiles.ckan
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"spec_version" : 1,
"identifier" : "CrewFiles",
"$kref" : "#/ckan/kerbalstuff",
"install" : [
{
"file" : "CrewFiles/GameData/CrewFiles",
"install_to" : "GameData"
}
]
}
28 changes: 28 additions & 0 deletions BootKAN/FerramAerospaceResearch.ckan
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"spec_version": 1,
"identifier": "FerramAerospaceResearch",
"$kref" : "#/ckan/kerbalstuff",
"license" : "GPL-3.0",
"release_status" : "stable",
"comment" : "Setting to *no* github releases so the code doesn't try to confuse NEAR with FAR",
"resources" : {
"github" : {
"url" : "https://github.com/ferram4/Ferram-Aerospace-Research",
"releases" : false
}
},
"depends" : [ { "name" : "ModuleManager" } ],
"install" : [
{
"file" : "GameData/FerramAerospaceResearch",
"install_to" : "GameData"
},
{
"file" : "Ships",
"install_to" : "Ships",
"optional" : true,
"description" : "FAR example craft"
}

]
}
6 changes: 6 additions & 0 deletions BootKAN/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The BootKAN are CKAN fragments that exist for mods that will support
the CKAN, but haven't yet released fragments in their own distributions
yet.

It allows us to boot up the CKAN, using bots to generate metadata, but
without requiring everyone make a new release of their mods.
19 changes: 19 additions & 0 deletions BootKAN/RemoteTech.ckan
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"spec_version" : 1,
"identifier" : "RemoteTech",
"$kref" : "#/ckan/kerbalstuff",
"license" : "restricted",
"release_status" : "stable",
"resources" : {
"github" : {
"url" : "https://github.com/RemoteTechnologiesGroup/RemoteTech"
}
},
"depends": [ { "name" : "ModuleManager" } ],
"install" : [
{
"file" : "GameData/RemoteTech",
"install_to" : "GameData"
}
]
}
20 changes: 20 additions & 0 deletions BootKAN/kOS.ckan
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"spec_version" : 1,
"identifier" : "kOS",
"$kref" : "#/ckan/kerbalstuff",
"license" : "GPL-3.0",
"release_status" : "stable",
"resources" : {
"manual" : "http://ksp-kos.github.io/KOS_DOC/",
"github" : {
"url" : "https://github.com/KSP-KOS/KOS",
"releases" : true
}
},
"install" : [
{
"file" : "GameData/kOS",
"install_to" : "GameData"
}
]
}
4 changes: 4 additions & 0 deletions CKAN.schema
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"type" : "string",
"format" : "uri"
},
"download_size" : {
"description" : "The size of the download in bytes",
"type" : "integer"
},
"license" : {
"description" : "Machine readable license, or array of licenses",
"$ref" : "#/definitions/licenses"
Expand Down
1 change: 1 addition & 0 deletions CKAN/CKAN/CKAN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Types\JsonSimpleStringConverter.cs" />
<Compile Include="Repo.cs" />
<Compile Include="Meta.cs" />
<Compile Include="Types\JsonSingleOrArrayConverter.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion CKAN/CKAN/Module.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Collections.Generic;
using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -24,7 +25,13 @@ public class Module

[JsonProperty("abstract")] public string @abstract;

[JsonProperty("author")] public string[] author;
[JsonProperty("author")]
[JsonConverter(typeof(JsonSingleOrArrayConverter<string>))]
public List<string> author;

[JsonProperty("download_size")]
public long download_size;

[JsonProperty("comment")] public string comment;
[JsonProperty("conflicts")] public dynamic[] conflicts;
[JsonProperty("depends")] public dynamic[] depends;
Expand Down
41 changes: 41 additions & 0 deletions CKAN/CKAN/Types/JsonSingleOrArrayConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;

namespace CKAN
{

// With thanks to
// https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n

public class JsonSingleOrArrayConverter<T> : JsonConverter
{
public override bool CanConvert(Type object_type)
{
return(object_type == typeof(List<T>));
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Array)
{
return token.ToObject<List<T>>();
}
return new List<T> { token.ToObject<T>() };
}

public override bool CanWrite
{
get { return false; }
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}

19 changes: 18 additions & 1 deletion CKAN/KerbalStuff/KSMod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using log4net;
using Newtonsoft.Json.Linq;

Expand All @@ -13,6 +14,7 @@ public class KSMod
public string license;
public string name;
public string short_description;
public string author;
public KSVersion[] versions;
public string website;

Expand All @@ -25,8 +27,12 @@ public override string ToString()
/// Takes a JObject and inflates it with KS metadata.
/// This will not overwrite fields that already exist.
/// </summary>
public void InflateMetadata(JObject metadata, KSVersion version)
public void InflateMetadata(JObject metadata, KSVersion version, string filename)
{

// Check how big our file is
long download_size = (new FileInfo (filename)).Length;

// Make sure resources exist.
if (metadata["resources"] == null)
{
Expand All @@ -42,9 +48,11 @@ public void InflateMetadata(JObject metadata, KSVersion version)
Inflate(metadata, "name", name);
Inflate(metadata, "license", license);
Inflate(metadata, "abstract", short_description);
Inflate(metadata, "author", author);
Inflate(metadata, "version", version.friendly_version.ToString());
Inflate(metadata, "download", Uri.EscapeUriString(version.download_path));
Inflate(metadata, "comment", "Generated by ks2ckan");
Inflate(metadata, "download_size", download_size);
Inflate((JObject) metadata["resources"], "homepage", website);
Inflate((JObject) metadata["resources"]["kerbalstuff"], "url", KSHome());
Inflate(metadata, "ksp_version", version.KSP_version.ToString());
Expand All @@ -68,5 +76,14 @@ internal static void Inflate(JObject metadata, string key, string value)
log.DebugFormat("Leaving {0} as {1}", key, metadata[key]);
}
}

internal static void Inflate(JObject metadata, string key, long value)
{
if (metadata[key] == null)
{
metadata[key] = value;
}
}

}
}
2 changes: 1 addition & 1 deletion CKAN/KerbalStuff/KerbalStuff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ProjectGuid>{4336F356-33DB-442A-BF74-5E89AF47A5B9}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>CKAN.KerbalStuff</RootNamespace>
<AssemblyName>KerbalStuff</AssemblyName>
<AssemblyName>ks2ckan</AssemblyName>
<StartupObject>CKAN.KerbalStuff.MainClass</StartupObject>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
Expand Down
28 changes: 25 additions & 3 deletions CKAN/KerbalStuff/MainClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static int Main(string[] args)

string identifier = args[0];
int ksid = Convert.ToInt32(args[1]);
string output_path = args[2] ?? ".";

log.InfoFormat("Processing {0}", identifier);

Expand All @@ -47,13 +48,22 @@ public static int Main(string[] args)
KSVersion latest = ks.versions[0];
string filename = latest.Download(identifier);

JObject metadata = ExtractCkanInfo(filename);
JObject metadata = null;
try
{
metadata = ExtractCkanInfo(filename);
}
catch (MetadataNotFoundKraken)
{
log.WarnFormat ("Reading BootKAN metadata for {0}", filename);
metadata = BootKAN (identifier);
}

// Check if we should auto-inflate!
if ((string) metadata[expand_token] == ks_expand_path)
{
log.InfoFormat("Inflating...");
ks.InflateMetadata(metadata, latest);
ks.InflateMetadata(metadata, latest, filename);
metadata.Remove(expand_token);
}

Expand All @@ -72,7 +82,9 @@ public static int Main(string[] args)

// All done! Write it out!

File.WriteAllText(String.Format("{0}-{1}.ckan", mod.identifier, mod.version), metadata.ToString());
string final_path = Path.Combine(output_path, String.Format ("{0}-{1}.ckan", mod.identifier, mod.version));

File.WriteAllText(final_path, metadata.ToString());

return EXIT_OK;
}
Expand Down Expand Up @@ -117,6 +129,16 @@ private static JObject ExtractCkanInfo(string filename)

throw new MetadataNotFoundKraken(filename);
}

/// <summary>
/// Returns the metadata fragment found in the BootKAN/ directory for
/// this identifier, if it exists.
/// </summary>
internal static JObject BootKAN(string identifier)
{
string fragment = File.ReadAllText(Path.Combine ("BootKAN", identifier + ".ckan"));
return JObject.Parse(fragment);
}
}

internal class MetadataNotFoundKraken : Exception
Expand Down
10 changes: 10 additions & 0 deletions KSMods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"comment" : "Here be all the mods we process from KerbalStuff, in identifier => ksid pairs",
"modlist" : {
"CrewFiles" : 39,
"DogeCoinFlag" : 269,
"kOS" : 86,
"FerramAerospaceResearch" : 52,
"RemoteTech" : 134
}
}
7 changes: 7 additions & 0 deletions Spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,13 @@ and the old name of the mod is listed in the `provides` field. This
allows for mods to be renamed without updating all other mods which
depend upon it.

##### download_size

If supplied, `download_size` is the number of bytes to expect when
downloading from the `download` URL. It is recommended this this field
only be generated by automated tools (where it is encouraged),
and not filled in by hand.

#### Extensions

Any field starting with `x_` (an x, followed by an underscore) is considered
Expand Down
15 changes: 14 additions & 1 deletion bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,8 @@ say "\n\n=== Repacking ===\n\n";

chdir("$Bin/..");

# Repack ckan.exe

my @cmd = (
$REPACK,
"--out:ckan.exe",
Expand All @@ -1199,12 +1201,23 @@ my @cmd = (
"build/CmdLine/bin/$TARGET/CKAN-GUI.exe", # Yes, bundle the .exe as a .dll
);

say "@cmd";
system(@cmd);

# Repack ks2ckan

my @cmd = (
$REPACK,
"--out:ks2ckan.exe",
"--lib:build/KerbalStuff/bin/$TARGET",
"build/KerbalStuff/bin/$TARGET/ks2ckan.exe",
glob("build/KerbalStuff/bin/$TARGET/*.dll"),
);

system(@cmd);

say "\n\n=== Tidying up===\n\n";

unlink("$OUTNAME.mdb");
unlink("ks2ckan.exe.mdb");

say "Done!";
Loading

0 comments on commit d16eacb

Please sign in to comment.