Initial import

This commit is contained in:
mlemanczyk committed 2012-09-24 20:55:07 +02:00
1 parent ea3f8549e4
commit fa6feafba7
133 files changed
+17503 -4

No files matched your search

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DelphiProjectHandler.Operations
{
public class DelphiFileAgentError : Exception
{
public DelphiFileAgentError(string aMessage): base(aMessage)
{
}
}
public class DelphiFileAgent: IDelphiFileAgent
{
public void Execute(String aUnitFileName, IDelphiFileOperation aOperation)
{
Execute(aUnitFileName, new IDelphiFileOperation[] { aOperation });
}
public void Execute(String aUnitFileName, params IDelphiFileOperation[] aOperations)
{
FileModifierAgent vFileAgent = new FileModifierAgent(aUnitFileName);
foreach (IDelphiFileOperation vOperation in aOperations)
{
try
{
vOperation.Initialize(aUnitFileName, vFileAgent.Content);
if (!vOperation.CanProcess())
return;
vFileAgent.Content = vOperation.Execute();
}
catch (Exception e)
{
throw new DelphiFileAgentError(String.Format("Error processing file: {0}. {1}", aUnitFileName, e.Message));
}
}
if (vFileAgent.Modified)
vFileAgent.Save(aUnitFileName);
}
}
}
@@ -0,0 +1,12 @@
using System;
namespace DelphiProjectHandler.Operations
{
public abstract class DelphiOperation
{
protected virtual string ReplaceContent(string iContent, string iOldValue, string iNewValue)
{
return iContent.Replace(iOldValue, iNewValue);
}
}
}
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using DelphiProjectHandler.Operations.Projects;
namespace DelphiProjectHandler.Operations
{
/// <summary>
/// Modifies Delphi projects, e.g. by adding units, changing unit paths etc.
/// </summary>
public class DelphiProjectAgent
{
protected FileModifierAgent FileAgent
{
get { return fFileAgent; }
}
private FileModifierAgent fFileAgent;
public DelphiProjectAgent(string iProjectName)
{
fFileAgent = new FileModifierAgent(iProjectName);
}
public bool Modified
{
get { return FileAgent.Modified; }
}
public void Save(string iProjectName)
{
FileAgent.Save(iProjectName);
}
}
}
@@ -0,0 +1,52 @@
/*
* Created by SharpDevelop.
* User: SG0894652
* Date: 8/12/2009
* Time: 10:58 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using DelphiProjectHandler.Operations.Units;
namespace DelphiProjectHandler.Operations
{
/// <summary>
/// Description of DelphiUnitAgent.
/// </summary>
public class DelphiUnitAgent
{
#region (Protected Properties)
protected FileModifierAgent FileAgent
{
get { return fFileAgent; }
}
private FileModifierAgent fFileAgent;
#endregion
public DelphiUnitAgent(string aUnitFilename)
{
fFileAgent = new FileModifierAgent(aUnitFilename);
}
public void CleanupUnits(SuggestedUnitStructure aSuggestedStructure, IList<string> aIgnoredUnits)
{
DelphiUnitCleanerOperation vOperation = new DelphiUnitCleanerOperation();
FileAgent.Content = vOperation.Execute(FileAgent.Content, aSuggestedStructure, aIgnoredUnits);
}
public bool Modified
{
get { return FileAgent.Modified; }
}
public void Save(string aUnitFilename)
{
FileAgent.Save(aUnitFilename);
}
}
}
@@ -0,0 +1,67 @@
/*
* Created by SharpDevelop.
* User: SG0894652
* Date: 8/12/2009
* Time: 10:49 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
namespace DelphiProjectHandler.Operations
{
/// <summary>
/// Description of FileModifierAgent.
/// </summary>
public class FileModifierAgent
{
public FileModifierAgent(string iFileName)
{
Load(iFileName);
}
public virtual void Save(string iFileName)
{
string vTempFileName = iFileName + ".tmp";
string vBackupFileName = iFileName + ".backup";
File.SetAttributes(iFileName, FileAttributes.Normal);
File.WriteAllText(vTempFileName, Content);
File.Replace(vTempFileName, iFileName, vBackupFileName, false);
fFileName = iFileName;
fOriginalContent = Content;
}
public virtual void Load(string iFileName)
{
fFileName = iFileName;
fOriginalContent = File.ReadAllText(iFileName);
Content = OriginalContent;
}
public string FileName
{
get { return fFileName; }
}
protected string fFileName;
public string Content
{
get { return fContent; }
set { fContent = value; }
}
protected string fContent;
public string OriginalContent
{
get { return fOriginalContent; }
}
protected string fOriginalContent;
public bool Modified
{
get { return OriginalContent != Content; }
}
}
}
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations
{
public interface IDelphiFileAgent
{
void Execute(String aUnitFileName, IDelphiFileOperation aOperation);
void Execute(String aUnitFileName, params IDelphiFileOperation[] aOperations);
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations
{
public interface IDelphiFileOperation
{
void Initialize(string aFileName, string aFileContent);
bool CanProcess();
string Execute();
}
}
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations
{
public interface IOperationState
{
}
}
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations
{
public interface IUnitItemBuilder
{
UnitItem Create(string aUnitFileName);
}
}
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DelphiProjectHandler.Model;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace DelphiProjectHandler.Operations
{
public class ProjectBulkOperationLoader
{
public static ProjectBulkOperations FromStream(Stream aStream)
{
XmlSerializer vSerializer = new XmlSerializer(typeof(ProjectBulkOperations));
//if (!vSerializer.CanDeserialize(XmlReader.Create(aStream)))
// throw new InvalidDataException("Unsupported stream format");
return vSerializer.Deserialize(aStream) as ProjectBulkOperations;
}
}
}
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DelphiProjectHandler.Operations.Uses;
namespace DelphiProjectHandler.Operations.Projects
{
public abstract class AbstractDelphiProjectOperation : IDelphiFileOperation
{
private IUsesListOperation fOperation;
private IProjectOperationSettings fSettings;
private IProjectOperationState fState;
protected IUsesListOperation Operation
{
get { return fOperation; }
set { fOperation = value; }
}
protected IProjectOperationSettings Settings
{
get { return fSettings; }
set { fSettings = value; }
}
protected IProjectOperationState State
{
get { return fState; }
set { fState = value; }
}
protected virtual string ReplaceContent(string iContent, string iOldValue, string iNewValue)
{
return iContent.Replace(iOldValue, iNewValue);
}
protected IProjectOperationSettings CreateProjectSettings(ICollection<string> aRequiredUnits, ICollection<string> aUnitsToManipulate, bool aUsePaths)
{
IProjectOperationSettings vSettings = new ProjectOperationSettings();
vSettings.RequiredUnits = aRequiredUnits;
vSettings.UnitsToManipulate = aUnitsToManipulate;
vSettings.UsePaths = aUsePaths;
return vSettings;
}
protected IUnitItemBuilder CreateUnitItemBuilder(string aFileName, bool aUsePaths)
{
return new ProjectUnitItemBuilder(aFileName, aUsePaths);
}
protected abstract IUsesListOperation CreateUsesOperation(string aFileName, IProjectOperationSettings aSettings, IUnitItemBuilder aBuilder);
public AbstractDelphiProjectOperation(ICollection<string> aRequiredUnits, ICollection<string> aUnitsToManipulate, bool aUsePaths)
{
fState = new ProjectOperationState();
fSettings = CreateProjectSettings(aRequiredUnits, aUnitsToManipulate, aUsePaths);
}
public virtual void Initialize(string aFileName, string aFileContent)
{
State.FileContent = aFileContent;
State.OriginalUsesClause = UsesClauseReader.ExtractUses(aFileContent);
State.Units = UsesClauseReader.GetUnits(State.FileContent);
IUnitItemBuilder vBuilder = CreateUnitItemBuilder(aFileName, Settings.UsePaths);
fOperation = CreateUsesOperation(aFileName, Settings, vBuilder);
Operation.Initialize(State.Units);
}
public bool CanProcess()
{
return Operation.CanProcess();
}
public string Execute()
{
if (!Operation.Execute())
return State.FileContent;
return ReplaceContent(State.FileContent, State.OriginalUsesClause, State.Units.ToString());
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using DelphiProjectHandler.Operations.Uses;
namespace DelphiProjectHandler.Operations.Projects
{
public class DelphiProjectAddUnitOperation: AbstractDelphiProjectOperation
{
protected override IUsesListOperation CreateUsesOperation(string aFileName, IProjectOperationSettings aSettings, IUnitItemBuilder aBuilder)
{
IUsesListOperationSettings vUsesSettings = new UsesListOperationSettings();
vUsesSettings.RequiredUnits = aSettings.RequiredUnits;
vUsesSettings.UnitItemBuilder = aBuilder;
vUsesSettings.UnitsToManipulate = aSettings.UnitsToManipulate;
return new UsesListAddOperation(vUsesSettings);
}
public DelphiProjectAddUnitOperation(ICollection<string> aRequiredUnits, ICollection<string> aUnitsToManipulate, bool aUsePaths)
: base(aRequiredUnits, aUnitsToManipulate, aUsePaths)
{
}
}
}
@@ -0,0 +1,23 @@
using System;
using DelphiProjectHandler.Operations.Uses;
using System.Collections.Generic;
namespace DelphiProjectHandler.Operations.Projects
{
public class DelphiProjectFixFormsOperation: AbstractDelphiProjectOperation
{
protected override IUsesListOperation CreateUsesOperation(string aFileName, IProjectOperationSettings aSettings, IUnitItemBuilder aBuilder)
{
IUsesListOperationSettings vUsesSettings = new UsesListOperationSettings();
vUsesSettings.RequiredUnits = aSettings.RequiredUnits;
vUsesSettings.UnitItemBuilder = aBuilder;
vUsesSettings.UnitsToManipulate = aSettings.UnitsToManipulate;
return new UsesListFixFormOperation(vUsesSettings);
}
public DelphiProjectFixFormsOperation(ICollection<string> aRequiredUnits, ICollection<string> aUnitsToManipulate, bool aUsePaths): base(aRequiredUnits, aUnitsToManipulate, aUsePaths)
{
}
}
}
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using DelphiProjectHandler.Operations.Uses;
namespace DelphiProjectHandler.Operations.Projects
{
public class DelphiProjectFixPathsOperation: AbstractDelphiProjectOperation
{
protected override IUsesListOperation CreateUsesOperation(string aFileName, IProjectOperationSettings aSettings, IUnitItemBuilder aBuilder)
{
IUsesListOperationSettings vUsesSettings = new UsesListOperationSettings();
vUsesSettings.RequiredUnits = aSettings.RequiredUnits;
vUsesSettings.UnitItemBuilder = aBuilder;
vUsesSettings.UnitsToManipulate = aSettings.UnitsToManipulate;
return new UsesListFixPathOperation(vUsesSettings);
}
public DelphiProjectFixPathsOperation(ICollection<string> aRequiredUnits, ICollection<string> aUnitsToManipulate, bool aUsePaths): base(aRequiredUnits, aUnitsToManipulate, aUsePaths)
{
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using DelphiProjectHandler.Operations.Uses;
namespace DelphiProjectHandler.Operations.Projects
{
public class DelphiProjectRemoveUnitOperation: AbstractDelphiProjectOperation
{
public DelphiProjectRemoveUnitOperation(ICollection<string> aRequiredUnits, ICollection<string> aUnitsToManipulate, bool aUsePaths)
: base(aRequiredUnits, aUnitsToManipulate, aUsePaths)
{
}
protected override IUsesListOperation CreateUsesOperation(string aFileName, IProjectOperationSettings aSettings, IUnitItemBuilder aBuilder)
{
IUsesListOperationSettings vUsesSettings = new UsesListOperationSettings();
vUsesSettings.RequiredUnits = aSettings.RequiredUnits;
vUsesSettings.UnitItemBuilder = aBuilder;
vUsesSettings.UnitsToManipulate = aSettings.UnitsToManipulate;
return new UsesListDeleteOperation(vUsesSettings);
}
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Projects
{
public interface IProjectOperationSettings
{
ICollection<string> RequiredUnits { get; set; }
ICollection<string> UnitsToManipulate { get; set; }
bool UsePaths { get; set; }
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Projects
{
public interface IProjectOperationState : IOperationState
{
string FileContent { get; set; }
string OriginalUsesClause { get; set; }
UnitList Units { get; set; }
}
}
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Projects
{
public class ProjectOperationSettings: IProjectOperationSettings
{
private ICollection<string> fRequiredUnits;
public ICollection<string> RequiredUnits
{
get { return fRequiredUnits; }
set { fRequiredUnits = value; }
}
private ICollection<string> fUnitsToManipulate;
public ICollection<string> UnitsToManipulate
{
get { return fUnitsToManipulate; }
set { fUnitsToManipulate = value; }
}
private bool fUsePaths;
public bool UsePaths
{
get { return fUsePaths; }
set { fUsePaths = value; }
}
}
}
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Projects
{
public class ProjectOperationState : IProjectOperationState
{
private string fFileContent;
public string FileContent
{
get { return fFileContent; }
set { fFileContent = value; }
}
private string fOriginalUsesState;
public string OriginalUsesClause
{
get { return fOriginalUsesState; }
set { fOriginalUsesState = value; }
}
private UnitList fUnits;
public UnitList Units
{
get { return fUnits; }
set { fUnits = value; }
}
}
}
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DelphiProjectHandler.Dialogs;
namespace DelphiProjectHandler.Operations.Projects
{
/// <summary>
/// UnitItem builder for creating complete project file unit item. New UnitItem will have all properties set that are normally used in a project Delphi file. It will also
/// search for corresponding dfm file.
/// </summary>
class ProjectUnitItemBuilder: IUnitItemBuilder
{
/// <summary>
/// Complete fileName or path of the project file. It is used to calculate new unit item path relative to project file location.
/// </summary>
public string BasePath
{
get { return fBasePath; }
}
private string fBasePath;
/// <summary>
/// Indicates, if unit paths should be added in a project file style, when converting the unit list to uses clause.
/// </summary>
public bool UsePaths
{
get { return fUsePaths; }
}
private bool fUsePaths;
/// <summary>
/// Creates new instance of the ProjectItemBuilder.
/// </summary>
/// <param name="aBasePath">Complete fileName or path of the project file. It is used to calculate new unit item path relative to project file location.</param>
public ProjectUnitItemBuilder(string aBasePath, bool aUsePaths)
{
fBasePath = aBasePath;
fUsePaths = aUsePaths;
}
/// <summary>
/// Creates new UnitItem object and fills it will all required information that can be used in a project file. Requires valid RelativePath builder and UnitFormFinder.
/// It finds corresponding form for the unit automatically. Path is always set relatively to the BasePath.
/// </summary>
/// <param name="aUnitFileName"></param>
/// <returns></returns>
public UnitItem Create(string aUnitFileName)
{
UnitItem vResult = new UnitItem();
vResult.Name = Path.GetFileNameWithoutExtension(aUnitFileName);
vResult.UsePath = UsePaths;
if (!aUnitFileName.Equals(""))
vResult.Path = Path.GetDirectoryName(aUnitFileName);
vResult.Path = RelativePath.GetRelativeFileName(BasePath, vResult.Path);
vResult.Form = UnitFormReader.GetUnitForm(aUnitFileName);
return vResult;
}
}
}
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DelphiProjectHandler.Operations.Uses;
namespace DelphiProjectHandler.Operations.Units
{
public abstract class AbstractDelphiUnitOperation: IDelphiFileOperation
{
private IUsesListOperation fIntfOperation;
private IUsesListOperation fImplOperation;
private IDelphiUnitOperationState fState;
protected IDelphiUnitOperationState State
{
get { return fState; }
set { fState = value; }
}
private IDelphiUnitOperationSettings fSettings;
protected IDelphiUnitOperationSettings Settings
{
get { return fSettings; }
set { fSettings = value; }
}
protected IUnitItemBuilder CreateUnitItemBuilder()
{
return new DelphiUnitItemBuilder();
}
protected IDelphiUnitOperationSettings CreateSettings(ICollection<string> aRequiredUnits, ICollection<string> aInterfaceUnits, ICollection<string> aImplementationUnits)
{
IDelphiUnitOperationSettings vSettings = new DelphiUnitOperationSettings();
vSettings.RequiredUnits = aRequiredUnits;
vSettings.IntfUnitsToManipulate = aInterfaceUnits;
vSettings.ImplUnitsToManipulate = aImplementationUnits;
return vSettings;
}
protected abstract IUsesListOperation CreateUsesOperation(ICollection<string> aUnitsToManipulate, ICollection<string> aRequiredUnits, IUnitItemBuilder aBuilder);
protected virtual string ReplaceContent(string iContent, string iOldValue, string iNewValue)
{
return iContent.Replace(iOldValue, iNewValue);
}
public AbstractDelphiUnitOperation(ICollection<string> aInterfaceUnits, ICollection<string> aImplementationUnits, ICollection<string> aRequiredUnits)
{
fState = new DelphiUnitOperationState();
fSettings = CreateSettings(aRequiredUnits, aInterfaceUnits, aImplementationUnits);
}
public void Initialize(string aFileName, string aFileContent)
{
State.FileContent = aFileContent;
string vIntfUsesClause = "", vImplUsesClause = "";
UsesClauseReader.ExtractUses(aFileContent, out vIntfUsesClause, out vImplUsesClause);
State.OriginalIntfUsesClause = vIntfUsesClause;
State.OriginalImplUsesClause = vImplUsesClause;
State.IntfUnits = UsesClauseReader.GetUnits(vIntfUsesClause);
State.ImplUnits = UsesClauseReader.GetUnits(vImplUsesClause);
IUnitItemBuilder vBuilder = CreateUnitItemBuilder();
fIntfOperation = CreateUsesOperation(Settings.IntfUnitsToManipulate, Settings.RequiredUnits, vBuilder);
fImplOperation = CreateUsesOperation(Settings.ImplUnitsToManipulate, Settings.RequiredUnits, vBuilder);
fIntfOperation.Initialize(State.IntfUnits);
fImplOperation.Initialize(State.ImplUnits);
}
public bool CanProcess()
{
return (fIntfOperation.CanProcess() || fImplOperation.CanProcess());
}
public string Execute()
{
string vContent = State.FileContent;
if (fIntfOperation.Execute())
vContent = ReplaceContent(vContent, State.OriginalIntfUsesClause, State.IntfUnits.ToString());
if (fImplOperation.Execute())
vContent = ReplaceContent(vContent, State.OriginalImplUsesClause, State.ImplUnits.ToString());
return vContent;
}
}
}
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DelphiProjectHandler.Operations.Uses;
namespace DelphiProjectHandler.Operations.Units
{
public class DelphiUnitAddOperation: AbstractDelphiUnitOperation
{
protected override IUsesListOperation CreateUsesOperation(ICollection<string> aUnitsToManipulate, ICollection<string> aRequiredUnits, IUnitItemBuilder aBuilder)
{
IUsesListOperationSettings vUsesSettings = new UsesListOperationSettings();
vUsesSettings.RequiredUnits = aRequiredUnits;
vUsesSettings.UnitItemBuilder = aBuilder;
vUsesSettings.UnitsToManipulate = aUnitsToManipulate;
return new UsesListAddOperation(vUsesSettings);
}
public DelphiUnitAddOperation(ICollection<string> aInterfaceUnits, ICollection<string> aImplementationUnits, ICollection<string> aRequiredUnits)
: base(aInterfaceUnits, aImplementationUnits, aRequiredUnits)
{
}
}
}
@@ -0,0 +1,101 @@
/*
* Created by SharpDevelop.
* User: SG0894652
* Date: 8/12/2009
* Time: 11:06 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using DelphiProjectHandler.Operations;
namespace DelphiProjectHandler.Operations.Units
{
/// <summary>
/// Description of DelphiUnitCleanerOperation.
/// </summary>
public class DelphiUnitCleanerOperation: DelphiOperation
{
public DelphiUnitCleanerOperation(): base()
{
}
protected SuggestedUnitStructure RemoveIgnoredUnits(SuggestedUnitStructure iSuggestedUnitStructure, IList<string> iIgnoredUnits)
{
List<string> vList = new List<string>(iIgnoredUnits);
for (int vUnitIdx = 0; vUnitIdx < vList.Count; vUnitIdx++)
vList[vUnitIdx] = vList[vUnitIdx].ToLower();
SuggestedUnitStructure vResult = iSuggestedUnitStructure.Clone();
RemoveUnits(vResult.MoveToInterface, vList);
RemoveUnits(vResult.ToDelete, vList);
return vResult;
}
protected void RemoveUnits(UnitList iList, IList<string> iToRemove)
{
for (int vUnitIdx = iList.Count - 1; vUnitIdx >= 0; vUnitIdx--)
if (iToRemove.IndexOf(iList[vUnitIdx].Name.ToLower()) >= 0)
iList.RemoveAt(vUnitIdx);
}
protected void AddUnits(UnitList iList, UnitList iToAdd)
{
for (int vUnitIdx = iToAdd.Count - 1; vUnitIdx >= 0; vUnitIdx--)
{
UnitItem vUnit = iToAdd[vUnitIdx];
if (iList.IndexOf(vUnit.Name) < 0)
iList.Insert(0, vUnit);
}
}
protected UnitList GetUsesList(string iUsesContent)
{
if (!iUsesContent.Trim().Equals(""))
return UsesClauseReader.GetUnits(iUsesContent);
else
return new UnitList();
}
protected string CleanUpInterface(string iUnitContent, string iUsesContent, SuggestedUnitStructure iSuggestedStructure)
{
if (iUsesContent.Trim().Equals(""))
return iUnitContent;
UnitList vUnits = GetUsesList(iUsesContent);
RemoveUnits(vUnits, iSuggestedStructure.ToDelete);
RemoveUnits(vUnits, iSuggestedStructure.MoveToInterface);
return ReplaceContent(iUnitContent, iUsesContent, vUnits.ToString());
}
protected string CleanUpImplementation(string iUnitContent, string iUsesContent, SuggestedUnitStructure iSuggestedStructure)
{
UnitList vUnits = GetUsesList(iUsesContent);
RemoveUnits(vUnits, iSuggestedStructure.ToDelete);
AddUnits(vUnits, iSuggestedStructure.MoveToInterface);
if (vUnits.Count == 0 && iUsesContent.Trim().Equals(""))
return iUnitContent;
string vNewValue = vUnits.ToString();
if (iUsesContent.Trim().Equals(""))
{
iUsesContent = "implementation";
vNewValue = "implementation\r\n\r\n" + vNewValue;
}
return ReplaceContent(iUnitContent, iUsesContent, vNewValue);
}
public string Execute(string iUnitContent, SuggestedUnitStructure iSuggestedStructure, IList<string> iIgnoredUnits)
{
string vInterface, vImplementation;
SuggestedUnitStructure vSuggestedStructure = RemoveIgnoredUnits(iSuggestedStructure.Clone(), iIgnoredUnits);
UsesClauseReader.ExtractUses(iUnitContent, out vInterface, out vImplementation);
string vResult = iUnitContent;
vResult = CleanUpInterface(vResult, vInterface, vSuggestedStructure);
vResult = CleanUpImplementation(vResult, vImplementation, vSuggestedStructure);
return vResult;
}
}
}
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DelphiProjectHandler.Operations.Units
{
public class DelphiUnitItemBuilder: IUnitItemBuilder
{
public UnitItem Create(string aUnitFileName)
{
UnitItem vResult = new UnitItem();
vResult.Name = Path.GetFileNameWithoutExtension(aUnitFileName);
return vResult;
}
}
}
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Units
{
public class DelphiUnitOperationSettings: IDelphiUnitOperationSettings
{
private ICollection<string> fRequiredUnits;
public ICollection<string> RequiredUnits
{
get { return fRequiredUnits; }
set { fRequiredUnits = value; }
}
private ICollection<string> fIntfUnitsToManipulate;
public ICollection<string> IntfUnitsToManipulate
{
get { return fIntfUnitsToManipulate; }
set { fIntfUnitsToManipulate = value; }
}
private ICollection<string> fImplUnitsToManipulate;
public ICollection<string> ImplUnitsToManipulate
{
get { return fImplUnitsToManipulate; }
set { fImplUnitsToManipulate = value; }
}
}
}
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Units
{
public class DelphiUnitOperationState: IDelphiUnitOperationState
{
private string fFileContent;
public string FileContent
{
get { return fFileContent; }
set { fFileContent = value; }
}
private string fOriginalIntfUsesClause;
public string OriginalIntfUsesClause
{
get { return fOriginalIntfUsesClause; }
set { fOriginalIntfUsesClause = value; }
}
private string fOriginalImplUsesClause;
public string OriginalImplUsesClause
{
get { return fOriginalImplUsesClause; }
set { fOriginalImplUsesClause = value; }
}
private UnitList fIntfUnits;
public UnitList IntfUnits
{
get { return fIntfUnits; }
set { fIntfUnits = value; }
}
private UnitList fImplUnits;
public UnitList ImplUnits
{
get { return fImplUnits; }
set { fImplUnits = value; }
}
}
}
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DelphiProjectHandler.Operations;
namespace DelphiProjectHandler.Operations.Units
{
public abstract class DelphiUnitUsesOperation: DelphiOperation, IDelphiUnitOperation
{
protected virtual bool CanProcessUnit(UnitList aCurrentIntfUnits, UnitList aCurrentImplUnits, ICollection<string> aRequiredUnits)
{
return aCurrentIntfUnits.Concat<string>(aCurrentImplUnits).Intersect<string>(aRequiredUnits).Count() == aRequiredUnits.Count;
}
public string Execute(string aUnitContent, ICollection<string> aInterfaceUnits, ICollection<string> aImplementationUnits, ICollection<string> aRequiredUnits)
{
string vCurrentIntfUses = "";
string vCurrentImplUses = "";
UsesClauseReader.ExtractUses(aUnitContent, out vCurrentIntfUses, out vCurrentImplUses);
UnitList vCurrentIntfUnits = UsesClauseReader.GetUnits(vCurrentIntfUses);
UnitList vCurrentImplUnits = UsesClauseReader.GetUnits(vCurrentImplUses);
if (!CanProcessUnit(vCurrentIntfUnits, vCurrentImplUnits, aRequiredUnits))
return aUnitContent;
string vNewUnitContent = ProcessUsesSection(aUnitContent, vCurrentIntfUses, vCurrentIntfUnits, aInterfaceUnits, "interface");
return ProcessUsesSection(vNewUnitContent, vCurrentImplUses, vCurrentImplUnits, aImplementationUnits, "implementation");
}
protected string ProcessUsesSection(string aUnitContent, string aCurrentUsesClause, UnitList aCurrentUnitList, ICollection<string> aUnits, string aMissingUsesReplacement)
{
bool vListModified = ProcessUsesList(aCurrentUnitList, aUnits);
if (!vListModified)
return aUnitContent;
return BuildNewUsesClause(aUnitContent, aCurrentUsesClause, aCurrentUnitList, aMissingUsesReplacement);
}
protected string BuildNewUsesClause(string aUnitContent, string aCurrentUsesClause, UnitList aCurrentUnitList, string aMissingUsesReplacement)
{
string vLineStarting = "";
string vLineEnding = "";
if (aCurrentUsesClause == "")
{
aCurrentUsesClause = aMissingUsesReplacement + Environment.NewLine;
vLineStarting = aCurrentUsesClause + Environment.NewLine;
}
return ReplaceContent(aUnitContent, aCurrentUsesClause, vLineStarting + aCurrentUnitList.ToString() + vLineEnding);
}
protected abstract bool ProcessUsesList(UnitList aCurrentUnitList, ICollection<string> aUnits);
}
}
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Units
{
public interface IDelphiUnitOperation
{
string Execute(string aUnitContent, ICollection<string> aInterfaceUnits, ICollection<string> aImplementationUnits, ICollection<string> aRequiredUnits);
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Units
{
public interface IDelphiUnitOperationSettings
{
ICollection<string> RequiredUnits { get; set; }
ICollection<string> IntfUnitsToManipulate { get; set; }
ICollection<string> ImplUnitsToManipulate { get; set; }
}
}
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Units
{
public interface IDelphiUnitOperationState: IOperationState
{
string FileContent { get; set; }
string OriginalIntfUsesClause { get; set; }
string OriginalImplUsesClause { get; set; }
UnitList IntfUnits { get; set; }
UnitList ImplUnits { get; set; }
}
}
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public abstract class AbstractUsesListOperation: IUsesListOperation
{
private IUsesListOperationSettings fSettings;
protected IUsesListOperationSettings Settings
{
get { return fSettings; }
set { fSettings = value; }
}
private IUsesListOperationState fState;
protected IUsesListOperationState State
{
get { return fState; }
}
protected virtual bool CanProcessUnit(UnitItem aUnit)
{
return (aUnit.Name != "" && State.Units.Contains(aUnit.Name));
}
protected virtual IUsesListOperationState CreateState(UnitList aUnitList)
{
UsesListOperationState vState = new UsesListOperationState();
vState.Units = aUnitList;
return vState;
}
protected abstract bool ProcessUnit(UnitList aUnits, UnitItem aUnit);
public AbstractUsesListOperation(IUsesListOperationSettings aSettings)
{
Settings = aSettings;
}
public void Initialize(UnitList aUnitList)
{
fState = CreateState(aUnitList);
}
public bool CanProcess()
{
return (State.Units.Intersect(Settings.RequiredUnits).Count() == Settings.RequiredUnits.Count);
}
public bool Execute()
{
bool vListModified = false;
foreach (string vUnitFileName in Settings.UnitsToManipulate)
{
UnitItem vUnit = Settings.UnitItemBuilder.Create(vUnitFileName);
if (!CanProcessUnit(vUnit))
continue;
vListModified = ProcessUnit(State.Units, vUnit) || vListModified;
}
return vListModified;
}
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public interface IUsesListOperation
{
void Initialize(UnitList aUnitList);
bool CanProcess();
bool Execute();
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public interface IUsesListOperationSettings
{
ICollection<string> RequiredUnits { get; set; }
ICollection<string> UnitsToManipulate { get; set; }
IUnitItemBuilder UnitItemBuilder { get; set; }
}
}
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public interface IUsesListOperationState: IOperationState
{
UnitList Units { get; }
}
}
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DelphiProjectHandler.Dialogs;
namespace DelphiProjectHandler.Operations.Uses
{
public class UsesListAddOperation: AbstractUsesListOperation
{
protected override bool ProcessUnit(UnitList aUnits, UnitItem aUnit)
{
aUnits.Add(aUnit);
return true;
}
protected override bool CanProcessUnit(UnitItem aUnit)
{
return (aUnit.Name != "") && !State.Units.Contains(aUnit.Name);
}
public UsesListAddOperation(IUsesListOperationSettings aSettings)
: base(aSettings)
{
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public class UsesListDeleteOperation : AbstractUsesListOperation
{
protected override bool ProcessUnit(UnitList aUnits, UnitItem aUnit)
{
int vUnitIdx = aUnits.IndexOf(aUnit.Name);
UnitItem vToDelete = aUnits[vUnitIdx];
if (vToDelete.HasStartingConditions && !vToDelete.HasEndingConditions && vUnitIdx < aUnits.Count - 1)
aUnits[vUnitIdx + 1].StartingConditions = vToDelete.StartingConditions;
else
if (vToDelete.HasEndingConditions && !vToDelete.HasStartingConditions && vUnitIdx > 0)
aUnits[vUnitIdx - 1].EndingConditions = vToDelete.EndingConditions;
aUnits.RemoveAt(vUnitIdx);
return true;
}
public UsesListDeleteOperation(IUsesListOperationSettings aSettings)
: base(aSettings)
{
}
}
}
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public class UsesListFixFormOperation: AbstractUsesListOperation
{
protected override bool ProcessUnit(UnitList aUnits, UnitItem aUnit)
{
int vUnitIdx = aUnits.IndexOf(aUnit.Name);
if (aUnits[vUnitIdx].Form == aUnit.Form)
return false;
UnitItem vToFix = aUnits[vUnitIdx];
vToFix.Form = aUnit.Form;
return true;
}
public UsesListFixFormOperation(IUsesListOperationSettings aSettings): base(aSettings)
{
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public class UsesListFixPathOperation: AbstractUsesListOperation
{
protected override bool ProcessUnit(UnitList aUnits, UnitItem aUnit)
{
int vUnitIdx = aUnits.IndexOf(aUnit.Name);
UnitItem iToFix = aUnits[vUnitIdx];
iToFix.UsePath = aUnit.UsePath;
iToFix.Path = aUnit.Path;
return true;
}
public UsesListFixPathOperation(IUsesListOperationSettings aSettings): base(aSettings)
{
}
}
}
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public class UsesListOperationSettings: IUsesListOperationSettings
{
private ICollection<string> fRequiredUnits;
public ICollection<string> RequiredUnits
{
get { return fRequiredUnits; }
set { fRequiredUnits = value; }
}
private ICollection<string> fUnitsToManipulate;
public ICollection<string> UnitsToManipulate
{
get { return fUnitsToManipulate; }
set { fUnitsToManipulate = value; }
}
private IUnitItemBuilder fUnitItemBuilder;
public IUnitItemBuilder UnitItemBuilder
{
get { return fUnitItemBuilder; }
set { fUnitItemBuilder = value; }
}
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelphiProjectHandler.Operations.Uses
{
public class UsesListOperationState: IUsesListOperationState
{
private UnitList fUnits;
public UnitList Units
{
get { return fUnits; }
set { fUnits = value; }
}
}
}