Skip to content

Commit

Permalink
Fix parsing INFO with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vletroye committed Oct 17, 2021
1 parent a0739a6 commit 981b9c3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 61 deletions.
22 changes: 11 additions & 11 deletions Mods/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 72 additions & 49 deletions Mods/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,14 +692,22 @@ private void LoadPackageInfo(string path)
var lineText = line.Trim();
if (!string.IsNullOrEmpty(lineText))
{
var key = lineText.Substring(0, lineText.IndexOf('='));
var value = lineText.Substring(lineText.IndexOf('=') + 1);
value = value.Trim(new char[] { '"' });
value = value.Replace("<br>", "\r\n");
if (info.ContainsKey(key))
info.Remove(key);
if (key != "checksum")
info.Add(key, value);
if (lineText.StartsWith("#"))
{
//Comments will be lost as not supported by MODS
}
else if (lineText.Contains('='))
{

var key = lineText.Substring(0, lineText.IndexOf('='));
var value = lineText.Substring(lineText.IndexOf('=') + 1);
value = value.Trim(new char[] { '"' });
value = value.Replace("<br>", "\r\n");
if (info.ContainsKey(key))
info.Remove(key);
if (key != "checksum")
info.Add(key, value);
}
}
}

Expand All @@ -710,6 +718,8 @@ private void LoadPackageInfo(string path)

groupBoxPackage.Enabled = false;

//lastBuild.Text = "";

SavePackageSettings(path);
CreateRecentsMenu();

Expand Down Expand Up @@ -754,12 +764,19 @@ private string GetUIDir(string path)
var lineText = line.Trim();
if (!string.IsNullOrEmpty(lineText))
{
var key = lineText.Substring(0, lineText.IndexOf('='));
if (key == "dsmuidir")
if (lineText.StartsWith("#"))
{
value = lineText.Substring(lineText.IndexOf('=') + 1);
value = value.Trim(new char[] { '"' });
break;
//Comments will be lost as not supported by MODS
}
else if (lineText.Contains('='))
{
var key = lineText.Substring(0, lineText.IndexOf('='));
if (key == "dsmuidir")
{
value = lineText.Substring(lineText.IndexOf('=') + 1);
value = value.Trim(new char[] { '"' });
break;
}
}
}
}
Expand Down Expand Up @@ -1709,21 +1726,24 @@ private DialogResult EditScript(string scriptPath, string runnerPath)
{
string inputScript = null;
string inputRunner = null;
if (File.Exists(scriptPath))
if (currentScript == null && File.Exists(scriptPath))
inputScript = File.ReadAllText(scriptPath);
else
{
//When creating a new script, the script can be edited several times but is not yet saved in a file.
if (state == State.Add)
{
inputScript = currentScript;
}
inputScript = currentScript;
}

if (File.Exists(runnerPath))
if (currentRunner == null && File.Exists(runnerPath))
inputRunner = File.ReadAllText(runnerPath);
else
else if (currentRunner == null)
{
inputRunner = File.ReadAllText(Path.Combine(Helper.ResourcesDirectory, "default.runner"));
}
else
{
inputRunner = currentRunner;
}

var script = new ScriptInfo(inputScript, "Script Editor", new Uri("https://www.shellscript.sh/"), "Shell Scripting Tutorial");
var runner = new ScriptInfo(inputRunner, "Runner Editor", new Uri("https://stackoverflow.com/questions/20107147/php-reading-shell-exec-live-output"), "Reading shell_exec live output in PHP");
Expand Down Expand Up @@ -1939,26 +1959,19 @@ private bool CreateScript(KeyValuePair<string, AppsData> current, string script,

if (Directory.Exists(target))
{
var ex = Helper.DeleteDirectory(target);
if (ex != null)
{
MessageBoxEx.Show(this, string.Format("The operation cannot be completed because a fatal error occured while trying to delete {0}: {1}", target, ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
succeed = false;
}
else
{
if (string.IsNullOrEmpty(cleanedCurrent)) Directory.CreateDirectory(target);
}
if (File.Exists(targetRunner))
Helper.DeleteFile(targetRunner);
if (File.Exists(targetScript))
Helper.DeleteFile(targetScript);
}

if (succeed)
else
{
Directory.CreateDirectory(target);

// Create sh script (ANSI) to be executed by the php runner script
Helper.WriteAnsiFile(targetScript, script);
Helper.WriteAnsiFile(targetRunner, runner);
}

// Create sh script (ANSI) to be executed by the php runner script
Helper.WriteAnsiFile(targetScript, script);
Helper.WriteAnsiFile(targetRunner, runner);
}

return succeed; //TODO: handle this return value
Expand Down Expand Up @@ -1990,15 +2003,18 @@ private void SaveItemsConfig()
Properties.Settings.Default.Packages = json;
Properties.Settings.Default.Save();

var config = Path.Combine(CurrentPackageFolder, string.Format(CONFIGFILE, info["dsmuidir"]));
if (Directory.Exists(Path.GetDirectoryName(config)))
{
// Save Config as Ansi
Helper.WriteAnsiFile(config, json);
}
else
if (info.ContainsKey("dsmuidir"))
{
MessageBoxEx.Show(this, "For some reason, required resource files are missing. You will have to reconfigure your destination path", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
var config = Path.Combine(CurrentPackageFolder, string.Format(CONFIGFILE, info["dsmuidir"]));
if (Directory.Exists(Path.GetDirectoryName(config)))
{
// Save Config as Ansi
Helper.WriteAnsiFile(config, json);
}
else
{
MessageBoxEx.Show(this, "For some reason, required resource files are missing. You will have to reconfigure your destination path", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
}
}
}
Expand Down Expand Up @@ -2168,7 +2184,8 @@ private void EnableItemDetails()

// Enable the detail and package zone depending on the current state (view, new, edit or none)
enabling = (state != State.View && state != State.None);
packaging = listViewItems.Items.Count > 0 && !enabling;
//packaging = listViewItems.Items.Count > 0 && !enabling;
packaging = true; //A package can be published even without any item

EnableItemFieldDetails(!enabling && list != null, enabling);
switch (state)
Expand Down Expand Up @@ -3439,6 +3456,7 @@ private void BuildPackage(string path)
try
{
File.Move(tmpName, packName);
//lastBuild.Text = string.Format("Build on {0:dd/MM/yyyy HH:mm:ss}", DateTime.Now);
}
catch (Exception ex)
{
Expand All @@ -3456,10 +3474,12 @@ private void BuildPackage(string path)
private void PublishPackage(string PackagePath, string PackageRepo)
{
var packName = info["package"];
var archname = info["arch"];

try
{
publishFile(Path.Combine(PackagePath, packName + ".spk"), Path.Combine(PackageRepo, packName + ".spk"));
var flavour = archname.Equals("noarch", StringComparison.InvariantCultureIgnoreCase) ? "" : "[" + archname.Replace(" ", "-") + "]";
publishFile(Path.Combine(PackagePath, packName + ".spk"), Path.Combine(PackageRepo, packName + flavour + ".spk"));
MessageBoxEx.Show(this, "The package has been successfuly published.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
catch (Exception ex)
Expand Down Expand Up @@ -3556,8 +3576,11 @@ private void ShowIconOnScriptMenu(ToolStripItem menu)
{
if (file.Contains("*"))
{
var files = Directory.GetFiles(CurrentPackageFolder, file, SearchOption.TopDirectoryOnly);
if (files.Length > 0) file = files[0]; else file = null;
if (Directory.Exists(Path.Combine(CurrentPackageFolder, file)))
{
var files = Directory.GetFiles(CurrentPackageFolder, file, SearchOption.TopDirectoryOnly);
if (files.Length > 0) file = files[0]; else file = null;
}
}
else
{
Expand Down Expand Up @@ -5381,7 +5404,7 @@ private void menuDefaultRouterScript_Click(object sender, EventArgs e)

var content = File.ReadAllText(file);
var routercgi = new ScriptInfo(content, "Default Router Script", new Uri("https://github.com/vletroye/SynoPackages/wiki/MODS-Advanced-Test-CGI"), "CGI Router handling calls to php");
DialogResult result = Helper.ScriptEditor(routercgi,null, null);
DialogResult result = Helper.ScriptEditor(routercgi, null, null);
if (result == DialogResult.OK)
{
Helper.WriteAnsiFile(file, routercgi.Code);
Expand Down
8 changes: 8 additions & 0 deletions Mods/Forms/ScriptForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public ScriptForm(ScriptInfo script1, ScriptInfo script2, List<Tuple<string, str
SetHelpToolTip(this.scriptTab1.Help);
}

if (this.scriptTab1 !=null && this.scriptTab1.Code != null && this.scriptTab1.Code.Contains("\r") && this.scriptTab1.Title != "INFO Editor")
{
if (MessageBox.Show("WARNING: your script contains 'Carriage Returns' (\\r) which are not supported on Linux. Do you want to clean them up ?", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
this.scriptTab1.Code = this.scriptTab1.Code.Replace("\r", "");
}
}


this.scriptTab2 = script2;
if (this.scriptTab2 != null)
Expand Down
6 changes: 6 additions & 0 deletions Mods/Workers/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public Linker(JToken linker, string targetPath, string ui)

textboxLinkerPath.AutoCompleteCustomSource = new AutoCompleteStringCollection();

if (Directory.Exists(Path.Combine(targetPath, "bin")))
{
//structure "old style" with app, bin, etc, lib in the target director.
ui = "";
}

var dir = string.IsNullOrEmpty(ui) ? new DirectoryInfo(targetPath) : new DirectoryInfo(Path.Combine(targetPath, ui));
textboxLinkerPath.AutoCompleteCustomSource.Clear();
var excludes = new string[] { "images", "config", "dsm.cgi.conf", "router.cgi" };
Expand Down
2 changes: 1 addition & 1 deletion Mods/Workers/Privilege.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Privilege(JToken privilege, SortedDictionary<string, string> info)
private void SetPrivilege(JToken privilege)
{
origPrivilege = privilege;
Specification = privilege == null ? JsonConvert.DeserializeObject<JObject>(@"{""defaults"":{""run-as"":""system""}}") : privilege.DeepClone();
Specification = privilege == null ? JsonConvert.DeserializeObject<JObject>(@"{""defaults"":{""run-as"":""package""}}") : privilege.DeepClone();

DisplayPrivilege();
}
Expand Down

0 comments on commit 981b9c3

Please sign in to comment.