Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmeglio committed Apr 10, 2020
1 parent 4fd86f2 commit 40e8b79
Show file tree
Hide file tree
Showing 24 changed files with 285 additions and 27 deletions.
20 changes: 15 additions & 5 deletions HubitatPackageManagerTools/Executors/ExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ protected JObject DownloadJsonFile(string url)
{
try
{
using (WebClient wc = new WebClient())
{
StringReader sr = new StringReader(wc.DownloadString(url));
return (JObject)JToken.ReadFrom(new JsonTextReader(sr));
}
StringReader sr = new StringReader(DownloadFile(url));
return (JObject)JToken.ReadFrom(new JsonTextReader(sr));
}
catch
{
return null;
}
}

protected string DownloadFile(string url)
{
try
{
using WebClient wc = new WebClient();
return wc.DownloadString(url);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public int Execute(ManifestAddAppOptions options)
name = @name,
@namespace = @namespace,
location = options.Location,
required = options.Required,
oauth = options.Oauth
required = options.Required ?? false,
oauth = options.Oauth ?? false
});
SetNonNullPropertyIfSpecified(app, "version", options.Version);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public int Execute(ManifestAddDriverOptions options)
name = @name,
@namespace = @namespace,
location = options.Location,
required = options.Required
required = options.Required ?? false
});
SetNonNullPropertyIfSpecified(driver, "version", options.Version);

Expand Down
11 changes: 7 additions & 4 deletions HubitatPackageManagerTools/Executors/ManifestCreateExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ public int Execute(ManifestCreateOptions options)
{
var newManifestContents = new JObject
{
["name"] = options.Name,
["packageName"] = options.Name,
["author"] = options.Author
};
SetNonNullPropertyIfSpecified(newManifestContents, "version", options.Version);
SetNonNullPropertyIfSpecified(newManifestContents, "minimumHEVersion", options.HEVersion);
SetNonNullPropertyIfSpecified(newManifestContents, "minimumHEVersion", options.HEVersion ?? "0.0");
if (!string.IsNullOrEmpty(options.License))
{
if (DownloadFile(options.License) == null)
throw new ApplicationException($"Unable to access license file {options.License}");
}
SetNonNullPropertyIfSpecified(newManifestContents, "licenseFile", options.License);
SetNonNullPropertyIfSpecified(newManifestContents, "version", options.Version);

Expand All @@ -23,8 +28,6 @@ public int Execute(ManifestCreateOptions options)
else
newManifestContents["dateReleased"] = DateTime.Now.ToString("yyyy-MM-dd");

newManifestContents["packageId"] = Guid.NewGuid().ToString();

SaveManifest(options, newManifestContents);
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions HubitatPackageManagerTools/Executors/ManifestExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ protected string DownloadGroovyFile(string url)
{
try
{
using WebClient wc = new WebClient();
return wc.DownloadString(url).Replace("\r", "").Replace("\n", "");
return DownloadFile(url)?.Replace("\r", "").Replace("\n", "");
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public int Execute(ManifestModifyOptions options)
JObject manifestContents = OpenExistingManifest(options);

if (!string.IsNullOrEmpty(options.Name))
manifestContents["name"] = options.Name;
manifestContents["packageName"] = options.Name;
if (!string.IsNullOrEmpty(options.Author))
manifestContents["author"] = options.Author;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public int Execute(RepositoryAddPackageOptions options)
if (manifestContents == null)
throw new ApplicationException($"Manifest file {options.Manifest} either does not exist or is not valid.");
if (name == null)
name = manifestContents["packageName"].ToString();
name = manifestContents["packageName"]?.ToString();

if (name == null)
throw new ApplicationException("Unable to determine package name from the manifest.");

packages.Add(JObject.FromObject(new
{
Expand Down
25 changes: 23 additions & 2 deletions HubitatPackageManagerTools/Options/ManifestAddAppOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -10,8 +12,27 @@ internal class ManifestAddAppOptions : ManifestOptionsBase
[Option(HelpText = "The version of the driver.")]
public string Version { get; set; }
[Option(HelpText = "Whether or not the app is required.")]
public bool Required { get; set; }
public bool? Required { get; set; }
[Option(HelpText = "Whether or not the app uses OAuth.")]
public bool Oauth { get; set; }
public bool? Oauth { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Add an app", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestAddAppOptions
{
ManifestFile = "packageManifest.json",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/master/app.groovy"
});
yield return new Example("Add an app with version", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestAddAppOptions
{
ManifestFile = "packageManifest.json",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/master/app.groovy",
Version = "1.0"
});
}
}
}
}
23 changes: 22 additions & 1 deletion HubitatPackageManagerTools/Options/ManifestAddDriverOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -10,6 +12,25 @@ internal class ManifestAddDriverOptions : ManifestOptionsBase
[Option(HelpText = "The version of the driver.")]
public string Version { get; set; }
[Option(HelpText = "Whether or not the driver is required.")]
public bool Required { get; set; }
public bool? Required { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Add a driver", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestAddDriverOptions
{
ManifestFile = "packageManifest.json",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/master/driver.groovy"
});
yield return new Example("Add a driver with version", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestAddDriverOptions
{
ManifestFile = "packageManifest.json",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/master/driver.groovy",
Version = "1.0"
});
}
}
}
}
30 changes: 30 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestCreateOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -17,5 +19,33 @@ internal class ManifestCreateOptions : ManifestOptionsBase
public string License { get; set; }
[Option(HelpText = "The release date of the package in YYYY-MM-DD format. If not specified today's date is used.")]
public string DateReleased { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Create a package with version", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestCreateOptions
{
ManifestFile = "packageManifest.json",
Name = "My Package",
Author = "My Name",
Version = "1.0"
});
yield return new Example("Create a package with no version", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestCreateOptions
{
ManifestFile = "packageManifest.json",
Name = "My Package",
Author = "My Name"
});
yield return new Example("Create a package with license", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestCreateOptions
{
ManifestFile = "packageManifest.json",
Name = "My Package",
Author = "My Name",
License = "https://opensource.org/licenses/LGPL-2.0"
});
}
}
}
}
21 changes: 21 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestModifyAppOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -18,5 +20,24 @@ internal class ManifestModifyAppOptions : ManifestOptionsBase
[Option(HelpText = "Whether or not the app uses OAuth.", Group = "modify", Default = null)]
public bool? Oauth { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Release a new version by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestModifyAppOptions
{
ManifestFile = "packageManifest.json",
Version = "1.2",
Name="My App"
});
yield return new Example("Release a new version by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestModifyAppOptions
{
ManifestFile = "packageManifest.json",
Version = "1.2",
Id = "13ded13f-8ab5-42e7-9b80-31159f62ecfa"
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -14,6 +16,26 @@ internal class ManifestModifyDriverOptions : ManifestOptionsBase
[Option(HelpText = "The version of the driver.", Group = "modify")]
public string Version { get; set; }
[Option(HelpText = "Whether or not the driver is required.", Group = "modify")]
public bool Required { get; set; }
public bool? Required { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Release a new version by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestModifyDriverOptions
{
ManifestFile = "packageManifest.json",
Version = "1.2",
Name = "My App"
});
yield return new Example("Release a new version by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestModifyDriverOptions
{
ManifestFile = "packageManifest.json",
Version = "1.2",
Id = "13ded13f-8ab5-42e7-9b80-31159f62ecfa"
});
}
}
}
}
16 changes: 16 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestModifyOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -19,5 +21,19 @@ internal class ManifestModifyOptions : ManifestOptionsBase
public string ReleaseNotes { get; set; }
[Option(HelpText = "The release date of the package in YYYY-MM-DD format. If not specified today's date is used.", Group = "modify")]
public string DateReleased { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Release a new version", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestModifyOptions
{
ManifestFile = "packageManifest.json",
Version = "1.2",
ReleaseNotes = "Bug fixes and performance improvements"
});
}
}
}
}
2 changes: 1 addition & 1 deletion HubitatPackageManagerTools/Options/ManifestOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace HubitatPackageManagerTools.Options
{
internal class ManifestOptionsBase
{
[Value(0, HelpText = "The local path to the package manifest JSON.", MetaName = "manifestFile")]
[Value(0, HelpText = "The local path to the package manifest JSON.", MetaName = "manifestFile", Required = true)]
public string ManifestFile { get; set; }
}
}
20 changes: 20 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestRemoveAppOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -9,5 +11,23 @@ internal class ManifestRemoveAppOptions : ManifestOptionsBase
public string Name { get; set; }
[Option(SetName = "matcher", HelpText = "The id of the app.")]
public string Id { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Remove an app by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveDriverOptions
{
ManifestFile = "packageManifest.json",
Name = "My App"
});
yield return new Example("Remove an app by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveDriverOptions
{
ManifestFile = "packageManifest.json",
Id = "da254635-819c-4a9e-949c-2b1812d2c310"
});
}
}
}
}
20 changes: 20 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestRemoveDriverOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
Expand All @@ -9,5 +11,23 @@ internal class ManifestRemoveDriverOptions : ManifestOptionsBase
public string Name { get; set; }
[Option(SetName = "matcher", HelpText = "The id of the driver.")]
public string Id { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Remove a driver by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveDriverOptions
{
ManifestFile = "packageManifest.json",
Name="My App"
});
yield return new Example("Remove a driver by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveDriverOptions
{
ManifestFile = "packageManifest.json",
Id= "da254635-819c-4a9e-949c-2b1812d2c310"
});
}
}
}
}
Loading

0 comments on commit 40e8b79

Please sign in to comment.