Skip to content

Commit

Permalink
Added support for file manager plain text files
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmeglio committed Jul 28, 2020
1 parent 7404f0d commit 1ea095e
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 42 deletions.
10 changes: 10 additions & 0 deletions HubitatPackageManagerTools/Executors/ExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ protected string DownloadFile(string url)
}
}

protected bool IsFilePlaintext(string contents)
{
for (var i = 0; i < contents.Length; i++)
{
if ((int)contents[i] < 32 && contents[i] != '\t' && contents[i] != '\n' && contents[i] != '\r')
return false;
}
return true;
}

protected void SetNullableProperty(JObject json, string property, string value)
{
if (value.IsSpecified())
Expand Down
40 changes: 40 additions & 0 deletions HubitatPackageManagerTools/Executors/ManifestAddFileExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using HubitatPackageManagerTools.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;

namespace HubitatPackageManagerTools.Executors
{
internal class ManifestAddFileExecutor : ManifestExecutorBase
{
public int Execute(ManifestAddFileOptions options, Settings settings)
{
JObject manifestContents = OpenExistingManifest(options);

JArray files = EnsureArrayExists(manifestContents, "files");
var fileContents = DownloadFile(options.Location);


if (fileContents != null)
{
if (!IsFilePlaintext(fileContents))
throw new ApplicationException($"The file manager file {options.Location} is not a plaintext file.");
}
else
throw new ApplicationException($"The file manager file {options.Location} either was not found or is not valid.");


var app = JObject.FromObject(new
{
id = Guid.NewGuid().ToString(),
name = options.Name,
location = options.Location
});

files.Add(app);

SaveManifest(options, manifestContents);
return 0;
}
}
}
44 changes: 44 additions & 0 deletions HubitatPackageManagerTools/Executors/ManifestModifyFileExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using HubitatPackageManagerTools.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;

namespace HubitatPackageManagerTools.Executors
{
internal class ManifestModifyFileExecutor : ManifestExecutorBase
{
public int Execute(ManifestModifyFileOptions options, Settings settings)
{
JObject manifestContents = OpenExistingManifest(options);

JArray files = manifestContents["files"] as JArray;
if (files == null)
throw new ApplicationException("Package is missing a files element.");

JObject file = null;
if (!string.IsNullOrEmpty(options.Name))
file = files.FirstOrDefault(p => p["name"]?.ToString() == options.Name) as JObject;
else
file = files.FirstOrDefault(p => p["id"]?.ToString() == options.Id) as JObject;

if (file != null)
{

var fileContents = DownloadFile(options.Location);

if (fileContents != null)
{
if (!IsFilePlaintext(fileContents))
throw new ApplicationException($"The file manager file {options.Location} is not a plaintext file.");
}
else
throw new ApplicationException($"The file manager file {options.Location} either was not found or is not valid.");
}
else
throw new ApplicationException($"The file was not found in the manifest.");

SaveManifest(options, manifestContents);
return 0;
}
}
}
31 changes: 31 additions & 0 deletions HubitatPackageManagerTools/Executors/ManifestRemoveFileExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using HubitatPackageManagerTools.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;

namespace HubitatPackageManagerTools.Executors
{
internal class ManifestRemoveFileExecutor : ManifestExecutorBase
{
public int Execute(ManifestRemoveFileOptions options, Settings settings)
{
JObject manifestContents = OpenExistingManifest(options);

JArray files = manifestContents["files"] as JArray;
if (files == null)
throw new ApplicationException("Package is missing a files element.");

JToken file = null;
if (!string.IsNullOrEmpty(options.Name))
file = files.FirstOrDefault(p => p["name"]?.ToString() == options.Name);
else
file = files.FirstOrDefault(p => p["id"]?.ToString() == options.Id);

if (file != null)
files.Remove(file);

SaveManifest(options, manifestContents);
return 0;
}
}
}
6 changes: 3 additions & 3 deletions HubitatPackageManagerTools/HubitatPackageManagerTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64</RuntimeIdentifiers>
<AssemblyName>hpm</AssemblyName>
<Version>1.5.0</Version>
<AssemblyVersion>1.5.0</AssemblyVersion>
<FileVersion>1.5.0</FileVersion>
<Version>1.6.0</Version>
<AssemblyVersion>1.6.0</AssemblyVersion>
<FileVersion>1.6.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
31 changes: 31 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestAddFileOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
[Verb("manifest-add-file", HelpText = "Add a file manager file to a manifest.")]
internal class ManifestAddFileOptions : ManifestOptionsBase
{
[Option(HelpText = "The URL of the file.", Required = true)]
public string Location { get; set; }


[Option(HelpText="The name of the file to be installed in file manager.")]
public string Name { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Add a file", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestAddFileOptions
{
ManifestFile = "packageManifest.json",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/file.js",
Name = "myfile.js"
});
}
}
}
}
38 changes: 38 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestModifyFileOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
[Verb("manifest-modify-file", HelpText = "Modify a file manager file in a manifest.")]
internal class ManifestModifyFileOptions : ManifestOptionsBase
{
[Option(SetName = "matcher", HelpText = "The name of the file.")]
public string Name { get; set; }
[Option(SetName = "matcher", HelpText = "The id of the file.")]
public string Id { get; set; }
[Option(HelpText = "The URL of the file.", Required = true)]
public string Location { 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 ManifestModifyFileOptions
{
ManifestFile = "packageManifest.json",
Name="myscript.js",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/file.js"
});
yield return new Example("Release a new version by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestModifyFileOptions
{
ManifestFile = "packageManifest.json",
Id = "13ded13f-8ab5-42e7-9b80-31159f62ecfa",
Location = "https://raw.githubusercontent.com/someuser/hubitat-app/file.js"
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Remove an app by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveDriverOptions
yield return new Example("Remove an app by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveAppOptions
{
ManifestFile = "packageManifest.json",
Name = "My App"
});
yield return new Example("Remove an app by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveDriverOptions
yield return new Example("Remove an app by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveAppOptions
{
ManifestFile = "packageManifest.json",
Id = "da254635-819c-4a9e-949c-2b1812d2c310"
Expand Down
33 changes: 33 additions & 0 deletions HubitatPackageManagerTools/Options/ManifestRemoveFileOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;

namespace HubitatPackageManagerTools.Options
{
[Verb("manifest-remove-file", HelpText = "Remove a file manager file in a manifest.")]
internal class ManifestRemoveFileOptions : ManifestOptionsBase
{
[Option(SetName = "matcher", HelpText = "The name of the file.")]
public string Name { get; set; }
[Option(SetName = "matcher", HelpText = "The id of the file.")]
public string Id { get; set; }

[Usage(ApplicationAlias = "hpm")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Remove a file by name", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveFileOptions
{
ManifestFile = "packageManifest.json",
Name = "myscript.js"
});
yield return new Example("Remove a file by id", new[] { UnParserSettings.WithUseEqualTokenOnly() }, new ManifestRemoveFileOptions
{
ManifestFile = "packageManifest.json",
Id = "da254635-819c-4a9e-949c-2b1812d2c310"
});
}
}
}
}
78 changes: 41 additions & 37 deletions HubitatPackageManagerTools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,48 @@ static int Main(string[] args)
with.IgnoreUnknownArguments = false;
with.HelpWriter = null;
});
var result = parser.ParseArguments<
ManifestAddAppOptions,
ManifestAddDriverOptions,
ManifestCreateOptions,
ManifestModifyAppOptions,
ManifestModifyDriverOptions,
ManifestModifyOptions,
ManifestRemoveAppOptions,
ManifestRemoveDriverOptions,
ManifestConvertOptions,
RepositoryAddPackageOptions,
RepositoryCreateOptions,
RepositoryModifyOptions,
RepositoryModifyPackageOptions,
RepositoryRemovePackageOptions
>(args);
var result = parser.ParseArguments(args,
typeof(ManifestAddAppOptions),
typeof(ManifestAddDriverOptions),
typeof(ManifestAddFileOptions),
typeof(ManifestCreateOptions),
typeof(ManifestModifyAppOptions),
typeof(ManifestModifyDriverOptions),
typeof(ManifestModifyFileOptions),
typeof(ManifestModifyOptions),
typeof(ManifestRemoveAppOptions),
typeof(ManifestRemoveDriverOptions),
typeof(ManifestRemoveFileOptions),
typeof(ManifestConvertOptions),
typeof(RepositoryAddPackageOptions),
typeof(RepositoryCreateOptions),
typeof(RepositoryModifyOptions),
typeof(RepositoryModifyPackageOptions),
typeof(RepositoryRemovePackageOptions));
try
{
WebClient wc = new WebClient();
var settingsFileContents = wc.DownloadString(settingsJson);
Settings settings = new Settings(settingsFileContents);

return result
.MapResult(
(RepositoryCreateOptions opts) => new RepositoryCreateExecutor().Execute(opts, settings),
(RepositoryModifyOptions opts) => new RepositoryModifyExecutor().Execute(opts, settings),
(RepositoryAddPackageOptions opts) => new RepositoryAddPackageExecutor().Execute(opts, settings),
(RepositoryRemovePackageOptions opts) => new RepositoryRemovePackageExecutor().Execute(opts, settings),
(RepositoryModifyPackageOptions opts) => new RepositoryModifyPackageExecutor().Execute(opts, settings),
(ManifestCreateOptions opts) => new ManifestCreateExecutor().Execute(opts, settings),
(ManifestModifyOptions opts) => new ManifestModifyExecutor().Execute(opts, settings),
(ManifestAddAppOptions opts) => new ManifestAddAppExecutor().Execute(opts, settings),
(ManifestAddDriverOptions opts) => new ManifestAddDriverExecutor().Execute(opts, settings),
(ManifestRemoveAppOptions opts) => new ManifestRemoveAppExecutor().Execute(opts, settings),
(ManifestRemoveDriverOptions opts) => new ManifestRemoveDriverExecutor().Execute(opts, settings),
(ManifestModifyAppOptions opts) => new ManifestModifyAppExecutor().Execute(opts, settings),
(ManifestModifyDriverOptions opts) => new ManifestModifyDriverExecutor().Execute(opts, settings),
(ManifestConvertOptions opts) => new ManifestConvertExecutor().Execute(opts, settings)
, errs =>
bool failed = false;
result.WithParsed((RepositoryCreateOptions opts) => new RepositoryCreateExecutor().Execute(opts, settings))
.WithParsed((RepositoryCreateOptions opts) => new RepositoryCreateExecutor().Execute(opts, settings))
.WithParsed((RepositoryModifyOptions opts) => new RepositoryModifyExecutor().Execute(opts, settings))
.WithParsed((RepositoryAddPackageOptions opts) => new RepositoryAddPackageExecutor().Execute(opts, settings))
.WithParsed((RepositoryRemovePackageOptions opts) => new RepositoryRemovePackageExecutor().Execute(opts, settings))
.WithParsed((RepositoryModifyPackageOptions opts) => new RepositoryModifyPackageExecutor().Execute(opts, settings))
.WithParsed((ManifestCreateOptions opts) => new ManifestCreateExecutor().Execute(opts, settings))
.WithParsed((ManifestModifyOptions opts) => new ManifestModifyExecutor().Execute(opts, settings))
.WithParsed((ManifestAddAppOptions opts) => new ManifestAddAppExecutor().Execute(opts, settings))
.WithParsed((ManifestAddDriverOptions opts) => new ManifestAddDriverExecutor().Execute(opts, settings))
.WithParsed((ManifestAddFileOptions opts) => new ManifestAddFileExecutor().Execute(opts, settings))
.WithParsed((ManifestRemoveAppOptions opts) => new ManifestRemoveAppExecutor().Execute(opts, settings))
.WithParsed((ManifestRemoveDriverOptions opts) => new ManifestRemoveDriverExecutor().Execute(opts, settings))
.WithParsed((ManifestRemoveFileOptions opts) => new ManifestRemoveFileExecutor().Execute(opts, settings))
.WithParsed((ManifestModifyAppOptions opts) => new ManifestModifyAppExecutor().Execute(opts, settings))
.WithParsed((ManifestModifyDriverOptions opts) => new ManifestModifyDriverExecutor().Execute(opts, settings))
.WithParsed((ManifestModifyFileOptions opts) => new ManifestModifyFileExecutor().Execute(opts, settings))
.WithNotParsed(errs =>
{
var helpText = HelpText.AutoBuild(result, h =>
{
Expand All @@ -71,9 +74,10 @@ static int Main(string[] args)
},
e => e);
Console.Error.WriteLine(helpText);
return -1;
}
);
failed = true;
});
return failed ? -1 : 0;

}
catch (Exception e)
{
Expand Down

0 comments on commit 1ea095e

Please sign in to comment.