Skip to content

Commit

Permalink
Add KeepModifiedate + Bette Handle console
Browse files Browse the repository at this point in the history
Add KeepModifiedate + Bette Handle console
  • Loading branch information
zzzprojects committed Feb 18, 2018
1 parent 54b169e commit 22ca927
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 110 deletions.
11 changes: 8 additions & 3 deletions src/FindAndReplace.App/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ public class CommandLineOptions
[Option("useEscapeChars", HelpText = "Escape special chars.")]
public bool UseEscapeChars { get; set; }

[Option("KeepModifiedDate", HelpText = "Keep Modified Date.")]
public bool IsKeepModifiedDate { get; set; }

#endregion



#endregion

#region Specialized Option Attribute
#region Specialized Option Attribute

[HelpOption("help", HelpText = "Display this help screen.")]
[HelpOption("help", HelpText = "Display this help screen.")]
public string GetUsage()
{
var help = new HelpText("Find And Replace");
Expand Down
11 changes: 6 additions & 5 deletions src/FindAndReplace.App/FindAndReplace.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<IsWebBootstrapper>false</IsWebBootstrapper>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -26,13 +31,8 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -83,6 +83,7 @@
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="ProcessExtension.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FormData.cs" />
Expand Down
9 changes: 6 additions & 3 deletions src/FindAndReplace.App/FormData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class FormData
public string ReplaceText { get; set; }
public bool UseEscapeChars { get; set; }
public string Encoding { get; set; }
public bool IsKeepModifiedDate { get; set; }

private static readonly string _versionIndependentRegKey;
private static readonly string _versionIndependentRegKey;

static FormData()
{
Expand Down Expand Up @@ -57,7 +58,8 @@ public void SaveToRegistry()
SaveValueToRegistry("ReplaceText", ReplaceText);
SaveValueToRegistry("UseEscapeChars", UseEscapeChars.ToString());
SaveValueToRegistry("Encoding", Encoding);
}
SaveValueToRegistry("IsKeepModifiedDate", IsKeepModifiedDate.ToString());
}

public bool IsEmpty()
{
Expand All @@ -82,7 +84,8 @@ public void LoadFromRegistry()
ReplaceText = GetValueFromRegistry("ReplaceText");
UseEscapeChars = GetValueFromRegistry("UseEscapeChars") == "True";
Encoding = GetValueFromRegistry("Encoding");
}
IsKeepModifiedDate= GetValueFromRegistry("IsKeepModifiedDate") == "True";
}


private void SaveValueToRegistry(string name, string value)
Expand Down
13 changes: 13 additions & 0 deletions src/FindAndReplace.App/MainForm.Designer.cs

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

14 changes: 10 additions & 4 deletions src/FindAndReplace.App/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private void SaveToRegistry()
data.UseEscapeChars = chkUseEscapeChars.Checked;
data.Encoding = cmbEncoding.Text;
data.ExcludeDir = txtExcludeDir.Text;
data.IsKeepModifiedDate = chkKeepModifiedDate.Checked;

data.SaveToRegistry();

Expand Down Expand Up @@ -504,7 +505,8 @@ private void ShowReplaceResult(Replacer.ReplaceResultItem replaceResultItem, Sta
{
string fileContent = string.Empty;

using (var sr = new StreamReader(replaceResultItem.FilePath, replaceResultItem.FileEncoding))

using (var sr = new StreamReader(replaceResultItem.FilePath, replaceResultItem.FileEncoding))
{
fileContent = sr.ReadToEnd();
}
Expand All @@ -528,7 +530,7 @@ private void ShowReplaceResult(Replacer.ReplaceResultItem replaceResultItem, Sta

lblStatus.Text = "Processing " + stats.Files.Processed + " of " + stats.Files.Total + " files. Last file: " +
replaceResultItem.FileRelativePath;
;

ShowStats(stats, true);
}
Expand Down Expand Up @@ -896,8 +898,10 @@ private void InitWithRegistryData()
chkShowEncoding.Checked = data.ShowEncoding;
txtReplace.Text = data.ReplaceText;
chkUseEscapeChars.Checked = data.UseEscapeChars;
chkKeepModifiedDate.Checked = data.IsKeepModifiedDate;


if (!string.IsNullOrEmpty(data.Encoding))
if (!string.IsNullOrEmpty(data.Encoding))
cmbEncoding.SelectedIndex = cmbEncoding.Items.IndexOf(data.Encoding);
}

Expand Down Expand Up @@ -1025,8 +1029,10 @@ private Replacer GetReplacer()
replacer.IncludeFilesWithoutMatches = chkIncludeFilesWithoutMatches.Checked;
replacer.ReplaceText = CleanRichBoxText(txtReplace.Text);
replacer.UseEscapeChars = chkUseEscapeChars.Checked;
replacer.IsKeepModifiedDate = chkKeepModifiedDate.Checked;

if (cmbEncoding.SelectedIndex > 0)

if (cmbEncoding.SelectedIndex > 0)
replacer.AlwaysUseEncoding = Utils.GetEncodingByName(cmbEncoding.Text);

return replacer;
Expand Down
12 changes: 0 additions & 12 deletions src/FindAndReplace.App/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -849,16 +849,4 @@
<metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>147, 17</value>
</metadata>
<metadata name="toolTip_btnSwap.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>316, 17</value>
</metadata>
<metadata name="toolTip_chkIncludeFilesWithoutMatches.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>459, 17</value>
</metadata>
<metadata name="toolTip_chkSkipBinaryFileDetection.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 56</value>
</metadata>
<metadata name="toolTip_chkShowEncoding.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>259, 56</value>
</metadata>
</root>
43 changes: 43 additions & 0 deletions src/FindAndReplace.App/ProcessExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FindAndReplace.App
{
public static class ProcessExtensions
{
//https://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way
private static string FindIndexedProcessName(int pid)
{
var processName = Process.GetProcessById(pid).ProcessName;
var processesByName = Process.GetProcessesByName(processName);
string processIndexdName = null;

for (var index = 0; index < processesByName.Length; index++)
{
processIndexdName = index == 0 ? processName : processName + "#" + index;
var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
if ((int)processId.NextValue() == pid)
{
return processIndexdName;
}
}

return processIndexdName;
}

private static Process FindPidFromIndexedProcessName(string indexedProcessName)
{
var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
return Process.GetProcessById((int)parentId.NextValue());
}

public static Process Parent(this Process process)
{
return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
}
}
}
36 changes: 14 additions & 22 deletions src/FindAndReplace.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,21 @@ private static int Main(string[] args)

if (args.Length != 0 && args.Contains("--cl"))
{
//Get a pointer to the forground window. The idea here is that
//IF the user is starting our application from an existing console
//shell, that shell will be the uppermost window. We'll get it
//and attach to it
IntPtr ptr = GetForegroundWindow();
//Get a parent procss for see where send the output.
Process process = Process.GetProcessById(Process.GetCurrentProcess().Id).Parent();

int u;
if (process.ProcessName == "cmd") //Is the uppermost window a cmd process?
{
AttachConsole(process.Id);
}
else
{
//no console AND we're in console mode ... create a new console.

GetWindowThreadProcessId(ptr, out u);
AllocConsole();
}

Process process = Process.GetProcessById(u);

if (process.ProcessName == "cmd") //Is the uppermost window a cmd process?
{
AttachConsole(process.Id);
}
else
{
//no console AND we're in console mode ... create a new console.

AllocConsole();
}

var clRunner = new CommandLineRunner();
var clRunner = new CommandLineRunner();
int dosErrorLevel = clRunner.Run(args);

FreeConsole();
Expand Down Expand Up @@ -234,8 +225,9 @@ public int Run(string[] args)
replacer.IsSilent = _options.Silent;

replacer.FileProcessed += OnReplacerFileProcessed;
replacer.IsKeepModifiedDate = _options.IsKeepModifiedDate;

var replaceResult = replacer.Replace();
var replaceResult = replacer.Replace();

if (_options.SetErrorLevelIfAnyFileErrors)
if (replaceResult.Stats.Files.FailedToRead > 0 || replaceResult.Stats.Files.FailedToWrite > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/FindAndReplace.App/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,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.7.2")]
[assembly: AssemblyFileVersion("1.7.2")]
[assembly: AssemblyVersion("1.7.3")]
[assembly: AssemblyFileVersion("1.7.3")]
1 change: 0 additions & 1 deletion src/FindAndReplace.Tests/DetectEncodingAccuracyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace FindAndReplace.Tests
{
[TestFixture]
[Ignore]
public class DetectEncodingAccuracyTest : TestBase
{

Expand Down
Loading

0 comments on commit 22ca927

Please sign in to comment.