Skip to content

Commit

Permalink
Added an option to convert a ST Community Installer Manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmeglio committed Apr 13, 2020
1 parent 40e8b79 commit 72c01c1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
74 changes: 74 additions & 0 deletions HubitatPackageManagerTools/Executors/ManifestConvertExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using HubitatPackageManagerTools.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HubitatPackageManagerTools.Executors
{
internal class ManifestConvertExecutor : ManifestExecutorBase
{
public int Execute(ManifestConvertOptions options)
{
using var file = File.OpenText(options.SmartThingsFile);
var stFile = (JObject)JToken.ReadFrom(new JsonTextReader(file));

var newManifestContents = new JObject
{
["packageName"] = stFile["name"],
["author"] = stFile["author"],
["minimumHEVersion"] = "0.0",
["dateReleased"] = DateTime.Now.ToString("yyyy-MM-dd"),
["apps"] = new JArray(),
["drivers"] = new JArray()
};

var stParentApp = stFile["smartApps"]["parent"];
var parentApp = JObject.FromObject(new
{
id = Guid.NewGuid(),
name = stParentApp["name"],
@namespace = stFile["namespace"],
version = stParentApp["version"],
location = $"https://raw.githubusercontent.com/{stFile["repoOwner"]}/{stFile["repoName"]}/{stFile["repoBranch"]}/{stParentApp["appUrl"]}",
required = !((bool?)stParentApp["optional"] ?? true),
oauth = (bool?)stParentApp["oAuth"] ?? false
});
(newManifestContents["apps"] as JArray).Add(parentApp);
foreach (var stApp in stFile["smartApps"]["children"])
{
var app = JObject.FromObject(new
{
id = Guid.NewGuid(),
name = stApp["name"],
@namespace = stFile["namespace"],
version = stApp["version"],
location = $"https://raw.githubusercontent.com/{stFile["repoOwner"]}/{stFile["repoName"]}/{stFile["repoBranch"]}/{stApp["appUrl"]}",
required = !((bool?)stApp["optional"] ?? true),
oauth = (bool?)stApp["oAuth"] ?? false
});
(newManifestContents["apps"] as JArray).Add(app);
}

foreach (var dth in stFile["deviceHandlers"])
{
var driver = JObject.FromObject(new
{
id = Guid.NewGuid(),
name = dth["name"],
@namespace = stFile["namespace"],
version = dth["version"],
location = $"https://raw.githubusercontent.com/{stFile["repoOwner"]}/{stFile["repoName"]}/{stFile["repoBranch"]}/{dth["appUrl"]}",
required = !((bool?)dth["optional"] ?? true)
});
(newManifestContents["drivers"] as JArray).Add(driver);
}

SaveManifest(options, newManifestContents);

return 0;
}
}
}
7 changes: 7 additions & 0 deletions HubitatPackageManagerTools/HubitatPackageManagerTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>hpm</AssemblyName>
<Version>1.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x86</PlatformTarget>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestConvertOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Text;

namespace HubitatPackageManagerTools.Options
{
[Verb("manifest-convert", HelpText = "Convert a SmartThings Community Installer manifest to a Hubitat Package Manager manifest.")]
internal class ManifestConvertOptions : ManifestOptionsBase
{
[Value(0, HelpText = "The local path to the SmartThings installer manifest JSON.", MetaName = "stInstallerFile", Required = true)]
public string SmartThingsFile { get; set; }
}
}
4 changes: 3 additions & 1 deletion HubitatPackageManagerTools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static int Main(string[] args)
ManifestModifyOptions,
ManifestRemoveAppOptions,
ManifestRemoveDriverOptions,
ManifestConvertOptions,
RepositoryAddPackageOptions,
RepositoryCreateOptions,
RepositoryModifyOptions,
Expand All @@ -50,7 +51,8 @@ static int Main(string[] args)
(ManifestRemoveAppOptions opts) => new ManifestRemoveAppExecutor().Execute(opts),
(ManifestRemoveDriverOptions opts) => new ManifestRemoveDriverExecutor().Execute(opts),
(ManifestModifyAppOptions opts) => new ManifestModifyAppExecutor().Execute(opts),
(ManifestModifyDriverOptions opts) => new ManifestModifyDriverExecutor().Execute(opts)
(ManifestModifyDriverOptions opts) => new ManifestModifyDriverExecutor().Execute(opts),
(ManifestConvertOptions opts) => new ManifestConvertExecutor().Execute(opts)
, errs =>
{
var helpText = HelpText.AutoBuild(result, h =>
Expand Down

0 comments on commit 72c01c1

Please sign in to comment.