Skip to content

Commit

Permalink
Closes #1, closes #2, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-brenner committed Sep 29, 2017
1 parent 713398e commit 563965e
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vs
bin
obj
Debug
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions Brenner.SilentCmd.Tests/TestDelay.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
"%~dp0\..\Brenner.SilentCmd\bin\Debug\SilentCMD.exe" "%~dp0\Dummy.cmd" param /LOG:"%temp%\TestDelay.log" /DELAY:5
1 change: 1 addition & 0 deletions Brenner.SilentCmd/Brenner.SilentCmd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="Engine.cs" />
<Compile Include="LogWriter.cs" />
<Compile Include="Program.cs" />
Expand Down
67 changes: 67 additions & 0 deletions Brenner.SilentCmd/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace Brenner.SilentCmd
{
internal class Configuration
{
public bool LogAppend { get; private set; }
public string LogFilePath { get; private set; }
public string BatchFilePath { get; set; }
public string BatchFileArguments { get; private set; }
public TimeSpan Delay { get; private set; }

public void ParseArguments(IEnumerable<string> args)
{
var argumentsBuilder = new StringBuilder();
var batchFilePathWasRead = false;

foreach (string arg in args)
{
if (arg.StartsWith("/LOG:", true, CultureInfo.InvariantCulture))
{
LogAppend = false;
LogFilePath = arg.Substring(5).Trim('"');
continue;
}

if (arg.StartsWith("/LOG+:", true, CultureInfo.InvariantCulture))
{
LogAppend = true;
LogFilePath = arg.Substring(6).Trim('"');
continue;
}

if (arg.StartsWith("/DELAY:", true, CultureInfo.InvariantCulture))
{
string rawValue = arg.Substring(7).Trim('"');
Delay = TimeSpan.FromSeconds(Convert.ToDouble(rawValue));
continue;
}

if (!batchFilePathWasRead)
{
BatchFilePath = arg;
batchFilePathWasRead = true;
continue;
}

if (arg.Contains(" "))
{
argumentsBuilder.AppendFormat("\"{0}\" ", arg);
continue;
}

argumentsBuilder.AppendFormat("{0} ", arg);
}

if (argumentsBuilder.Length > 0)
{
BatchFileArguments = argumentsBuilder.ToString();
}
}

}
}
99 changes: 30 additions & 69 deletions Brenner.SilentCmd/Engine.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Brenner.SilentCmd.Properties;
using System.IO;
using System.Linq;
using System.Threading;

namespace Brenner.SilentCmd
{
internal class Engine
{
private string _batchFilePath = Settings.Default.DefaultBatchFilePath;
private string _batchFileArguments = Settings.Default.DefaultBatchFileArguments;
private string _logFilePath = Settings.Default.DefaultLogFilePath;
private bool _logAppend = Settings.Default.DefaultLogAppend;
private Configuration _config = new Configuration();
private readonly LogWriter _logWriter = new LogWriter();

/// <summary>
Expand All @@ -26,28 +21,29 @@ public int Execute(string[] args)
{
try
{
ParseArguments(args);
_config.ParseArguments(args);
_logWriter.Initialize(_config.LogFilePath, _config.LogAppend);

if (string.IsNullOrEmpty(_batchFilePath))
if (string.IsNullOrEmpty(_config.BatchFilePath))
{
ShowHelp();
return 0;
}

DelayIfNecessary();
ResolveBatchFilePath();

_logWriter.Initialize(_logFilePath, _logAppend);
_logWriter.WriteLine(Resources.StartingCommand, _batchFilePath);
_logWriter.WriteLine(Resources.StartingCommand, _config.BatchFilePath);

using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo(_batchFilePath, _batchFileArguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false, // CreateNoWindow only works, if shell is not used
CreateNoWindow = true
};
process.StartInfo = new ProcessStartInfo(_config.BatchFilePath, _config.BatchFileArguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false, // CreateNoWindow only works, if shell is not used
CreateNoWindow = true
};
process.OutputDataReceived += OutputHandler;
process.ErrorDataReceived += OutputHandler;
process.Start();
Expand All @@ -63,11 +59,19 @@ public int Execute(string[] args)
}
finally
{
_logWriter.WriteLine(Resources.FinishedCommand, _batchFilePath);
_logWriter.WriteLine(Resources.FinishedCommand, _config.BatchFilePath);
_logWriter.Dispose();
}
}

private void DelayIfNecessary()
{
if (_config.Delay <= TimeSpan.FromSeconds(0)) return;

_logWriter.WriteLine("Delaying execution by {0} seconds", _config.Delay.TotalSeconds);
Thread.Sleep(_config.Delay);
}

private static void ShowHelp()
{
Assembly assembly = Assembly.GetExecutingAssembly();
Expand All @@ -78,18 +82,18 @@ private static void ShowHelp()

private void ResolveBatchFilePath()
{
if (string.IsNullOrEmpty(_batchFilePath)) return;
if (string.IsNullOrEmpty(_config.BatchFilePath)) return;

if (!string.IsNullOrEmpty(Path.GetDirectoryName(_batchFilePath))) return;
if (!string.IsNullOrEmpty(Path.GetDirectoryName(_config.BatchFilePath))) return;

if (string.IsNullOrEmpty(Path.GetExtension(_batchFilePath)))
if (string.IsNullOrEmpty(Path.GetExtension(_config.BatchFilePath)))
{
if (FindPath(_batchFilePath + ".bat")) return;
FindPath(_batchFilePath + ".cmd");
if (FindPath(_config.BatchFilePath + ".bat")) return;
FindPath(_config.BatchFilePath + ".cmd");
}
else
{
FindPath(_batchFilePath);
FindPath(_config.BatchFilePath);
}
}

Expand All @@ -108,7 +112,7 @@ private bool FindPath(string filename)

if (!string.IsNullOrEmpty(fullPath))
{
_batchFilePath = fullPath;
_config.BatchFilePath = fullPath;
return true;
}

Expand All @@ -119,48 +123,5 @@ private void OutputHandler(object sender, DataReceivedEventArgs e)
{
_logWriter.WriteLine(e.Data);
}

private void ParseArguments(IEnumerable<string> args)
{
var argumentsBuilder = new StringBuilder();
var batchFilePathWasRead = false;

foreach (string arg in args)
{
if (arg.StartsWith("/LOG:", true, CultureInfo.InvariantCulture))
{
_logAppend = false;
_logFilePath = arg.Substring(5).Trim('"');
continue;
}

if (arg.StartsWith("/LOG+:", true, CultureInfo.InvariantCulture))
{
_logAppend = true;
_logFilePath = arg.Substring(6).Trim('"');
continue;
}

if (!batchFilePathWasRead)
{
_batchFilePath = arg;
batchFilePathWasRead = true;
continue;
}

if (arg.Contains(" "))
{
argumentsBuilder.AppendFormat("\"{0}\" ", arg);
continue;
}

argumentsBuilder.AppendFormat("{0} ", arg);
}

if (argumentsBuilder.Length > 0)
{
_batchFileArguments = argumentsBuilder.ToString();
}
}
}
}
10 changes: 6 additions & 4 deletions Brenner.SilentCmd/LogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public void Initialize(string logPath, bool append = false)
{
try
{
if (!string.IsNullOrEmpty(logPath))
{
_writer = new StreamWriter(logPath, append);
}
string checkedPath = string.IsNullOrEmpty(logPath)
? @"%temp%\SilentCMD.log"
: logPath;

string fullPath = Environment.ExpandEnvironmentVariables(checkedPath);
_writer = new StreamWriter(fullPath, append);
}
catch (Exception e)
{
Expand Down
6 changes: 3 additions & 3 deletions Brenner.SilentCmd/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Stephan Brenner")]
[assembly: AssemblyProduct("SilentCMD")]
[assembly: AssemblyCopyright("Copyright © 2011-2016 Stephan Brenner")]
[assembly: AssemblyCopyright("Copyright © 2011-2017 Stephan Brenner")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
21 changes: 15 additions & 6 deletions Brenner.SilentCmd/Properties/Resources.Designer.cs

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

19 changes: 14 additions & 5 deletions Brenner.SilentCmd/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,22 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="UserManual" xml:space="preserve">
<value>Version {0}
Freeware
<value>SilentCMD [BatchFile [BatchArguments]] [Options]

SilentCMD [&lt;BatchFilePath&gt; [&lt;BatchFileArguments&gt;]] [/LOG:&lt;LogFilePath&gt;]
Options:
/LOG:file :: output status to LOG file (overwrite existing log).
/LOG+:file :: output status to LOG file (append to existing log).
/DELAY:seconds :: delay the execution of batch file by x seconds

Default settings can be specified in SilentCMD.exe.config.
For more information see "http://www.stephan-brenner.com".</value>
Examples
SilentCMD c:\DoSomething.bat
SilentCMD c:\MyBatch.cmd MyParam1 /LOG:c:\MyLog.txt
SilentCMD c:\MyBatch.cmd /LOG+:c:\MyLog.txt
SilentCMD c:\MyBatch.cmd /DELAY:3600 /LOG+:c:\MyLog.txt

Version {0}
Free software under MIT license
More information on https://www.stephan-brenner.com</value>
</data>
<data name="ProgramTitle" xml:space="preserve">
<value>SilentCMD</value>
Expand Down

0 comments on commit 563965e

Please sign in to comment.