Rework how to read and execute command line to fix issues introduced in last version. Now command line is completely separate from GUI.

This commit is contained in:
rlv-dan 2020-04-29 19:04:45 +02:00
parent fa80b22ad0
commit 8156351973
4 changed files with 117 additions and 94 deletions

View File

@ -15,6 +15,15 @@ namespace Snap2HTML
public bool openInBrowser { get; set; } public bool openInBrowser { get; set; }
public bool linkFiles { get; set; } public bool linkFiles { get; set; }
public string linkRoot { get; set; } public string linkRoot { get; set; }
public SnapSettings()
{
this.skipHiddenItems = true;
this.skipSystemItems = true;
this.openInBrowser = false;
this.linkFiles = false;
this.linkRoot = "";
}
} }

View File

@ -99,9 +99,9 @@
Starts the program with the given root path already set Starts the program with the given root path already set
Full: Snap2HTMl.exe [-path:"root folder path"] [-outfile:"filename"] Full: Snap2HTMl.exe -path:"root folder path" -outfile:"filename"
[-link:"link to path"] [-title:"page title"] [-link:"link to path"] [-title:"page title"]
[-hidden] [-system] [-hidden] [-system] [-silent]
-path:"root folder path" - The root path to load. -path:"root folder path" - The root path to load.
Example: -path:"c:\temp" Example: -path:"c:\temp"
@ -113,23 +113,26 @@
-link:"link to path" - The path to link files to. -link:"link to path" - The path to link files to.
Example: -link:"c:\temp" Example: -link:"c:\temp"
-title:"page title" - Set the page title -title:"page title" - Set the page title. If omitted, title
is generated based on path.
-hidden - Include hidden items -hidden - Include hidden items
-system - Include system items -system - Include system items
-silent - Run without showing the window (only -silent - Run without showing the window (only
if both -path and -outfile are used) if both -path and -outfile are used)
Notes: When both -path and -outfile are specified, the program will
Notes: Using -path and -outfile will cause the program to automatically automatically start generating the snapshot, and quit when done.
start generating the snapshot, and quit when done!
Always surround paths and filenames with quotes ("")! Always surround paths and filenames with quotes ("")!
Do not include the [sqaure brackets] when you write your command In silent mode, in case or error the program will just quit
line. (Sqaure brackets signify optional command line parameters) without telling why.
Do not include the [square brackets] when you write your command
line. (Square brackets signify optional command line parameters)
--- Template Design --------------------------------------------------------- --- Template Design ---------------------------------------------------------
@ -229,6 +232,8 @@
v2.11 (2020-04-18) v2.11 (2020-04-18)
Fixed a threading issue that caused the program to hang on some systems Fixed a threading issue that caused the program to hang on some systems
v2.12 (2020-04-)
--- End User License Agreement ----------------------------------------------- --- End User License Agreement -----------------------------------------------

View File

@ -11,8 +11,8 @@ namespace Snap2HTML
{ {
public partial class frmMain : Form public partial class frmMain : Form
{ {
private string outFile = ""; // set when automating via command line
private bool initDone = false; private bool initDone = false;
private bool runningAutomated = false;
public frmMain() public frmMain()
{ {
@ -65,40 +65,63 @@ namespace Snap2HTML
var arguments = new Arguments(splitCommandLine); var arguments = new Arguments(splitCommandLine);
// first test for single argument (ie path only) // first test for single argument (ie path only)
if (splitCommandLine.Length == 2 && !arguments.Exists("path")) if( splitCommandLine.Length == 2 && !arguments.Exists( "path" ) )
{ {
if (System.IO.Directory.Exists(splitCommandLine[1])) if( System.IO.Directory.Exists( splitCommandLine[1] ) )
{ {
SetRootPath( splitCommandLine[1] ); SetRootPath( splitCommandLine[1] );
} }
} }
var autoRun = false; var settings = new SnapSettings();
if( arguments.Exists( "path" ) && arguments.Exists( "outfile" ) )
if (arguments.IsTrue("hidden")) chkHidden.Checked = true;
if (arguments.IsTrue("system")) chkSystem.Checked = true;
if( arguments.Exists( "path" ) )
{ {
// note: relative paths not handled this.runningAutomated = true;
string path = arguments.Single( "path" );
if( !System.IO.Directory.Exists( path ) ) path = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + path;
if( System.IO.Directory.Exists( path ) ) settings.rootFolder = arguments.Single( "path" );
{ settings.outputFile = arguments.Single( "outfile" );
SetRootPath( path );
// First validate paths
if( !System.IO.Directory.Exists( settings.rootFolder ) )
{
if( !arguments.Exists( "silent" ) )
{
MessageBox.Show( "Input path does not exist: " + settings.rootFolder, "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
Application.Exit();
}
if( !System.IO.Directory.Exists( System.IO.Path.GetDirectoryName(settings.outputFile) ) )
{
if( !arguments.Exists( "silent" ) )
{
MessageBox.Show( "Output path does not exist: " + System.IO.Path.GetDirectoryName( settings.outputFile ), "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
Application.Exit();
}
// Rest of settings
settings.skipHiddenItems = !arguments.Exists( "hidden" );
settings.skipSystemItems = !arguments.Exists( "system" );
settings.openInBrowser = false;
settings.linkFiles = false;
if( arguments.Exists( "link" ) )
{
settings.linkFiles = true;
settings.linkRoot = arguments.Single( "link" );
}
settings.title = "Snapshot of " + settings.rootFolder;
if( arguments.Exists( "title" ) )
{
settings.title = arguments.Single( "title" );
}
// if outfile is also given, start generating snapshot
if (arguments.Exists("outfile"))
{
autoRun = true;
outFile = arguments.Single("outfile");
cmdCreate.PerformClick();
}
}
} }
// keep window hidden in silent mode // keep window hidden in silent mode
if( arguments.IsTrue( "silent" ) && autoRun ) if( arguments.IsTrue( "silent" ) && this.runningAutomated )
{ {
Visible = false; Visible = false;
} }
@ -107,24 +130,17 @@ namespace Snap2HTML
Opacity = 100; Opacity = 100;
} }
// run link/title after path, since path automatically updates title if( this.runningAutomated )
if( arguments.Exists( "link" ) )
{ {
chkLinkFiles.Checked = true; StartProcessing( settings );
txtLinkRoot.Text = arguments.Single( "link" );
txtLinkRoot.Enabled = true;
} }
if( arguments.Exists( "title" ) )
{
txtTitle.Text = arguments.Single( "title" );
}
} }
private void frmMain_FormClosing( object sender, FormClosingEventArgs e ) private void frmMain_FormClosing( object sender, FormClosingEventArgs e )
{ {
if( backgroundWorker.IsBusy ) e.Cancel = true; if( backgroundWorker.IsBusy ) e.Cancel = true;
if( outFile == "" ) // don't save settings when automated through command line if( !this.runningAutomated ) // don't save settings when automated through command line
{ {
Snap2HTML.Properties.Settings.Default.WindowLeft = this.Left; Snap2HTML.Properties.Settings.Default.WindowLeft = this.Left;
Snap2HTML.Properties.Settings.Default.WindowTop = this.Top; Snap2HTML.Properties.Settings.Default.WindowTop = this.Top;
@ -152,57 +168,25 @@ namespace Snap2HTML
private void cmdCreate_Click(object sender, EventArgs e) private void cmdCreate_Click(object sender, EventArgs e)
{ {
// ensure source path format // ask for output file
txtRoot.Text = System.IO.Path.GetFullPath( txtRoot.Text ); string fileName = new System.IO.DirectoryInfo( txtRoot.Text + @"\" ).Name;
if (txtRoot.Text.EndsWith(@"\")) txtRoot.Text = txtRoot.Text.Substring(0, txtRoot.Text.Length - 1); char[] invalid = System.IO.Path.GetInvalidFileNameChars();
if( Utils.IsWildcardMatch( "?:", txtRoot.Text, false ) ) txtRoot.Text += @"\"; // add backslash to path if only letter and colon eg "c:" for (int i = 0; i < invalid.Length; i++)
// add slash or backslash to end of link (in cases where it is clearthat we we can)
if( !txtLinkRoot.Text.EndsWith( @"/" ) && txtLinkRoot.Text.ToLower().StartsWith( @"http" ) ) // web site
{ {
txtLinkRoot.Text += @"/"; fileName = fileName.Replace(invalid[i].ToString(), "");
}
if( !txtLinkRoot.Text.EndsWith( @"\" ) && Utils.IsWildcardMatch( "?:*", txtLinkRoot.Text, false ) ) // local disk
{
txtLinkRoot.Text += @"\";
} }
// get output file saveFileDialog1.DefaultExt = "html";
if( outFile == "" ) if( !fileName.ToLower().EndsWith( ".html" ) ) fileName += ".html";
{ saveFileDialog1.FileName = fileName;
string fileName = new System.IO.DirectoryInfo( txtRoot.Text + @"\" ).Name; saveFileDialog1.Filter = "HTML files (*.html)|*.html|All files (*.*)|*.*";
char[] invalid = System.IO.Path.GetInvalidFileNameChars(); saveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(txtRoot.Text);
for (int i = 0; i < invalid.Length; i++) saveFileDialog1.CheckPathExists = true;
{ if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
fileName = fileName.Replace(invalid[i].ToString(), "");
}
saveFileDialog1.DefaultExt = "html";
if( !fileName.ToLower().EndsWith( ".html" ) ) fileName += ".html";
saveFileDialog1.FileName = fileName;
saveFileDialog1.Filter = "HTML files (*.html)|*.html|All files (*.*)|*.*";
saveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(txtRoot.Text);
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
}
else // command line
{
saveFileDialog1.FileName = outFile;
}
if( !saveFileDialog1.FileName.ToLower().EndsWith( ".html" ) ) saveFileDialog1.FileName += ".html"; if( !saveFileDialog1.FileName.ToLower().EndsWith( ".html" ) ) saveFileDialog1.FileName += ".html";
// make sure output path exists
if( !System.IO.Directory.Exists( System.IO.Path.GetDirectoryName( saveFileDialog1.FileName ) ) )
{
MessageBox.Show( "The output folder does not exists...\n\n" + System.IO.Path.GetDirectoryName( saveFileDialog1.FileName ), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
return;
}
// begin generating html // begin generating html
Cursor.Current = Cursors.WaitCursor;
this.Text = "Snap2HTML (Working... Press Escape to Cancel)";
tabControl1.Enabled = false;
var settings = new SnapSettings() var settings = new SnapSettings()
{ {
rootFolder = txtRoot.Text, rootFolder = txtRoot.Text,
@ -214,8 +198,33 @@ namespace Snap2HTML
linkFiles = chkLinkFiles.Checked, linkFiles = chkLinkFiles.Checked,
linkRoot = txtLinkRoot.Text, linkRoot = txtLinkRoot.Text,
}; };
backgroundWorker.RunWorkerAsync(argument: settings); StartProcessing(settings);
}
private void StartProcessing(SnapSettings settings)
{
// ensure source path format
settings.rootFolder = System.IO.Path.GetFullPath( settings.rootFolder );
if( settings.rootFolder.EndsWith( @"\" ) ) settings.rootFolder = settings.rootFolder.Substring( 0, settings.rootFolder.Length - 1 );
if( Utils.IsWildcardMatch( "?:", settings.rootFolder, false ) ) settings.rootFolder += @"\"; // add backslash to path if only letter and colon eg "c:"
// add slash or backslash to end of link (in cases where it is clear that we we can)
if( settings.linkFiles )
{
if( !settings.linkRoot.EndsWith( @"/" ) && settings.linkRoot.ToLower().StartsWith( @"http" ) ) // web site
{
settings.linkRoot += @"/";
}
if( !settings.linkRoot.EndsWith( @"\" ) && Utils.IsWildcardMatch( "?:*", settings.linkRoot, false ) ) // local disk
{
settings.linkRoot += @"\";
}
}
Cursor.Current = Cursors.WaitCursor;
this.Text = "Snap2HTML (Working... Press Escape to Cancel)";
tabControl1.Enabled = false;
backgroundWorker.RunWorkerAsync( argument: settings );
} }
private void backgroundWorker_ProgressChanged( object sender, ProgressChangedEventArgs e ) private void backgroundWorker_ProgressChanged( object sender, ProgressChangedEventArgs e )
@ -233,7 +242,7 @@ namespace Snap2HTML
this.Text = "Snap2HTML"; this.Text = "Snap2HTML";
// Quit when finished if automated via command line // Quit when finished if automated via command line
if( outFile != "" ) if( this.runningAutomated )
{ {
Application.Exit(); Application.Exit();
} }

View File

@ -75,7 +75,7 @@ namespace Snap2HTML
sbTemplate.Replace( "[NUM FILES]", totFiles.ToString() ); sbTemplate.Replace( "[NUM FILES]", totFiles.ToString() );
sbTemplate.Replace( "[NUM DIRS]", totDirs.ToString() ); sbTemplate.Replace( "[NUM DIRS]", totDirs.ToString() );
sbTemplate.Replace( "[TOT SIZE]", totSize.ToString() ); sbTemplate.Replace( "[TOT SIZE]", totSize.ToString() );
if( chkLinkFiles.Checked ) if( settings.linkFiles )
{ {
sbTemplate.Replace( "[LINK FILES]", "true" ); sbTemplate.Replace( "[LINK FILES]", "true" );
sbTemplate.Replace( "[LINK ROOT]", settings.linkRoot.Replace( @"\", "/" ) ); sbTemplate.Replace( "[LINK ROOT]", settings.linkRoot.Replace( @"\", "/" ) );