Skip to content

Commit

Permalink
Custom replacing tokens, file extensions ignore case, skipping readon…
Browse files Browse the repository at this point in the history
…ly and hidden files
  • Loading branch information
topas committed Jan 28, 2012
1 parent ca3d00b commit f818123
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
4 changes: 4 additions & 0 deletions warmup/App.config
Expand Up @@ -29,6 +29,10 @@
<add ext="zip"/>
<add ext="7z"/>
</ignoredExtensions>

<textReplaces>
<add find="__COMPANY_NAME__" replace="XXX inc." />
</textReplaces>
</warmup>

<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
22 changes: 21 additions & 1 deletion warmup/TargetDir.cs
Expand Up @@ -10,13 +10,15 @@
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

namespace warmup
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using settings;

[DebuggerDisplay("{FullPath}")]
Expand Down Expand Up @@ -57,17 +59,35 @@ void ReplaceTokensInTheFiles(DirectoryInfo point, string name)
List<string> ignoredExtensions = GetIgnoredExtensions();
foreach (var info in point.GetFiles("*.*", SearchOption.AllDirectories))
{
if (ignoredExtensions.Contains(info.Extension)) continue;
if (ignoredExtensions.Contains(info.Extension, StringComparer.InvariantCultureIgnoreCase)) continue;

//skip the .git directory
if (new[] {"\\.git\\"}.Contains(info.FullName)) continue;

// skip readonly and hidden files
if (info.IsReadOnly || (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;

//process contents
string contents = File.ReadAllText(info.FullName);

// replace main token
contents = contents.Replace(_replacementToken, name);

// replace custom tokens
foreach (TextReplaceItem replaceItem in GetReplaceTokens())
{
contents = contents.Replace(replaceItem.Find, replaceItem.Replace);
}

File.WriteAllText(info.FullName, contents, Encoding.UTF8);
}
}

IEnumerable<TextReplaceItem> GetReplaceTokens()
{
return new List<TextReplaceItem>(WarmupConfiguration.settings.TextReplaceCollection.Cast<TextReplaceItem>());
}

static List<string> GetIgnoredExtensions()
{
var extension = new List<string>();
Expand Down
104 changes: 104 additions & 0 deletions warmup/settings/TextReplaceCollection.cs
@@ -0,0 +1,104 @@
// Copyright 2007-2010 The Apache Software Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace warmup.settings
{
using System.Configuration;

/// <summary>
/// Text replace config collection
/// </summary>
public class TextReplaceCollection : ConfigurationElementCollection
{
/// <summary>
/// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
/// </summary>
/// <returns>
/// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
/// </returns>
protected override ConfigurationElement CreateNewElement()
{
return new TextReplaceItem();
}

/// <summary>
/// Gets the element key for a specified configuration element when overridden in a derived class.
/// </summary>
/// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
/// </returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((TextReplaceItem)element).Find;
}

/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
BaseClear();
}

/// <summary>
/// Indexes the of.
/// </summary>
/// <param name="textReplaceItem">The text replace item.</param>
/// <returns></returns>
public int IndexOf(TextReplaceItem textReplaceItem)
{
return BaseIndexOf(textReplaceItem);
}

/// <summary>
/// Adds the specified text replace item.
/// </summary>
/// <param name="textReplaceItem">The text replace item.</param>
public void Add(TextReplaceItem textReplaceItem)
{
BaseAdd(textReplaceItem);
}
}

/// <summary>
/// Text replace item
/// </summary>
public class TextReplaceItem : ConfigurationElement
{
/// <summary>
/// Gets or sets the find.
/// </summary>
/// <value>
/// The find.
/// </value>
[ConfigurationProperty("find", IsRequired = true, IsKey = true)]
public string Find
{
get { return (string)this["find"]; }
set { this["find"] = value; }
}

/// <summary>
/// Gets or sets the replace.
/// </summary>
/// <value>
/// The replace.
/// </value>
[ConfigurationProperty("replace", IsRequired = true, IsKey = false)]
public string Replace
{
get { return (string)this["replace"]; }
set { this["replace"] = value; }
}
}
}
9 changes: 9 additions & 0 deletions warmup/settings/WarmupConfiguration.cs
Expand Up @@ -79,5 +79,14 @@ public IgnoredFileTypeCollection IgnoredFileTypeCollection
{
get { return (IgnoredFileTypeCollection) base["ignoredExtensions"]; }
}

/// <summary>
/// Gets the text replace collection.
/// </summary>
[ConfigurationProperty("textReplaces", IsDefaultCollection = false)]
public TextReplaceCollection TextReplaceCollection
{
get { return (TextReplaceCollection)base["textReplaces"]; }
}
}
}
1 change: 1 addition & 0 deletions warmup/warmup.csproj
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="settings\IgnoredFileTypeCollection.cs" />
<Compile Include="settings\TextReplaceCollection.cs" />
<Compile Include="settings\WarmupConfiguration.cs" />
<Compile Include="SourceControlType.cs" />
<Compile Include="Svn.cs" />
Expand Down

0 comments on commit f818123

Please sign in to comment.