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 linkFiles { 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
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"]
[-hidden] [-system]
[-hidden] [-system] [-silent]
-path:"root folder path" - The root path to load.
Example: -path:"c:\temp"
@ -113,7 +113,8 @@
-link:"link to path" - The path to link files to.
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
@ -122,14 +123,16 @@
-silent - Run without showing the window (only
if both -path and -outfile are used)
Notes: Using -path and -outfile will cause the program to automatically
start generating the snapshot, and quit when done!
Notes: When both -path and -outfile are specified, the program will
automatically start generating the snapshot, and quit when done.
Always surround paths and filenames with quotes ("")!
Do not include the [sqaure brackets] when you write your command
line. (Sqaure brackets signify optional command line parameters)
In silent mode, in case or error the program will just quit
without telling why.
Do not include the [square brackets] when you write your command
line. (Square brackets signify optional command line parameters)
--- Template Design ---------------------------------------------------------
@ -229,6 +232,8 @@
v2.11 (2020-04-18)
Fixed a threading issue that caused the program to hang on some systems
v2.12 (2020-04-)
--- End User License Agreement -----------------------------------------------

View File

@ -11,8 +11,8 @@ namespace Snap2HTML
{
public partial class frmMain : Form
{
private string outFile = ""; // set when automating via command line
private bool initDone = false;
private bool runningAutomated = false;
public frmMain()
{
@ -65,40 +65,63 @@ namespace Snap2HTML
var arguments = new Arguments(splitCommandLine);
// 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] );
}
}
var autoRun = false;
if (arguments.IsTrue("hidden")) chkHidden.Checked = true;
if (arguments.IsTrue("system")) chkSystem.Checked = true;
if( arguments.Exists( "path" ) )
var settings = new SnapSettings();
if( arguments.Exists( "path" ) && arguments.Exists( "outfile" ) )
{
// note: relative paths not handled
string path = arguments.Single( "path" );
if( !System.IO.Directory.Exists( path ) ) path = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + path;
this.runningAutomated = true;
if( System.IO.Directory.Exists( path ) )
{
SetRootPath( path );
settings.rootFolder = arguments.Single( "path" );
settings.outputFile = arguments.Single( "outfile" );
// if outfile is also given, start generating snapshot
if (arguments.Exists("outfile"))
// First validate paths
if( !System.IO.Directory.Exists( settings.rootFolder ) )
{
autoRun = true;
outFile = arguments.Single("outfile");
cmdCreate.PerformClick();
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" );
}
}
// keep window hidden in silent mode
if( arguments.IsTrue( "silent" ) && autoRun )
if( arguments.IsTrue( "silent" ) && this.runningAutomated )
{
Visible = false;
}
@ -107,16 +130,9 @@ namespace Snap2HTML
Opacity = 100;
}
// run link/title after path, since path automatically updates title
if( arguments.Exists( "link" ) )
if( this.runningAutomated )
{
chkLinkFiles.Checked = true;
txtLinkRoot.Text = arguments.Single( "link" );
txtLinkRoot.Enabled = true;
}
if( arguments.Exists( "title" ) )
{
txtTitle.Text = arguments.Single( "title" );
StartProcessing( settings );
}
}
@ -124,7 +140,7 @@ namespace Snap2HTML
{
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.WindowTop = this.Top;
@ -152,24 +168,7 @@ namespace Snap2HTML
private void cmdCreate_Click(object sender, EventArgs e)
{
// ensure source path format
txtRoot.Text = System.IO.Path.GetFullPath( txtRoot.Text );
if (txtRoot.Text.EndsWith(@"\")) txtRoot.Text = txtRoot.Text.Substring(0, txtRoot.Text.Length - 1);
if( Utils.IsWildcardMatch( "?:", txtRoot.Text, false ) ) txtRoot.Text += @"\"; // add backslash to path if only letter and colon eg "c:"
// 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 += @"/";
}
if( !txtLinkRoot.Text.EndsWith( @"\" ) && Utils.IsWildcardMatch( "?:*", txtLinkRoot.Text, false ) ) // local disk
{
txtLinkRoot.Text += @"\";
}
// get output file
if( outFile == "" )
{
// ask for output file
string fileName = new System.IO.DirectoryInfo( txtRoot.Text + @"\" ).Name;
char[] invalid = System.IO.Path.GetInvalidFileNameChars();
for (int i = 0; i < invalid.Length; i++)
@ -182,27 +181,12 @@ namespace Snap2HTML
saveFileDialog1.FileName = fileName;
saveFileDialog1.Filter = "HTML files (*.html)|*.html|All files (*.*)|*.*";
saveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(txtRoot.Text);
saveFileDialog1.CheckPathExists = true;
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
}
else // command line
{
saveFileDialog1.FileName = outFile;
}
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
Cursor.Current = Cursors.WaitCursor;
this.Text = "Snap2HTML (Working... Press Escape to Cancel)";
tabControl1.Enabled = false;
var settings = new SnapSettings()
{
rootFolder = txtRoot.Text,
@ -214,8 +198,33 @@ namespace Snap2HTML
linkFiles = chkLinkFiles.Checked,
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 )
@ -233,7 +242,7 @@ namespace Snap2HTML
this.Text = "Snap2HTML";
// Quit when finished if automated via command line
if( outFile != "" )
if( this.runningAutomated )
{
Application.Exit();
}

View File

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