-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for file manager plain text files
- Loading branch information
Showing
10 changed files
with
273 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
HubitatPackageManagerTools/Executors/ManifestAddFileExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
HubitatPackageManagerTools/Executors/ManifestModifyFileExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
HubitatPackageManagerTools/Executors/ManifestRemoveFileExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
HubitatPackageManagerTools/Options/ManifestAddFileOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
HubitatPackageManagerTools/Options/ManifestModifyFileOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
HubitatPackageManagerTools/Options/ManifestRemoveFileOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters