Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fake build script #51

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
tmp
bin/configlet
bin/configlet.exe
.fake/
.vs/
tools/
build/
TestResult.xml
58 changes: 58 additions & 0 deletions build.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Include Fake library
#r "tools/FAKE/tools/FakeLib.dll"

open Fake
open Fake.CscHelper
open Fake.Testing.NUnit3

// Properties
let buildDir = getBuildParamOrDefault "buildDir" "./build/"
let sourceDir = "./exercises/"
let testDll = buildDir @@ "Tests.dll"
let nunitFrameworkDll = "tools/NUnit/lib/net45/nunit.framework.dll"

let sourceFiles() = !! (buildDir @@ "./**/*.cs") |> List.ofSeq

// Targets
Target "Clean" (fun _ ->
CleanDir buildDir
)

Target "CopySource" (fun _ ->
CopyDir buildDir sourceDir allFiles
)

Target "ModifySource" (fun _ ->
sourceFiles()
|> ReplaceInFiles [("[Ignore(\"Remove to run test\")]", ""); ("; Ignore", ""); (", Ignore = \"Remove to run test case\"", "")]
)

Target "Build" (fun _ ->
sourceFiles()
|> List.ofSeq
|> Csc (fun p ->
{ p with Output = testDll
References = [nunitFrameworkDll]
Target = Library })
)

Target "Test" (fun _ ->
Copy buildDir [nunitFrameworkDll]

[testDll]
|> NUnit3 (fun p ->
{ p with
ShadowCopy = false })
)

Target "Default" (fun _ -> ())

// Dependencies
"Clean"
==> "CopySource"
==> "ModifySource"
==> "Build"
==> "Test"
==> "Default"

RunTargetOrDefault "Default"
36 changes: 36 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
$toolsDirectory = Join-Path $PSScriptRoot "tools"
$nugetDirectory = Join-Path $toolsDirectory "nuget"
$nugetExe = Join-Path $nugetDirectory "nuget.exe"
$fakeExe = Join-Path $toolsDirectory "fake/tools/fake.exe"
$nunitFrameworkDll = Join-Path $toolsDirectory "nunit/lib/net45/nunit.framework.dll"
$nunitConsoleExe = Join-Path $toolsDirectory "nunit.console/tools/nunit3-console.exe"

If (!(Test-Path $nugetExe)) {
# Ensure the directory exists (which is required by DownloadFile)
New-Item $nugetDirectory -Type Directory | Out-Null

$nugetUrl = "https://dist.nuget.org/win-x86-commandline/v3.3.0/nuget.exe"
(New-Object System.Net.WebClient).DownloadFile($nugetUrl, $nugetExe)
}

If (!(Test-Path $nugetExe)) {
Throw "Could not find nuget.exe"
}

& $nugetExe install FAKE -Version 4.17.1 -ExcludeVersion -OutputDirectory $toolsDirectory
If (!(Test-Path $fakeExe)) {
Throw "Could not find fake.exe"
}

& $nugetExe install NUnit -Version 3.0.1 -ExcludeVersion -OutputDirectory $toolsDirectory
If (!(Test-Path $nunitFrameworkDll)) {
Throw "Could not find nunit.framework.dll"
}

& $nugetExe install NUnit.Console -Version 3.0.1 -ExcludeVersion -OutputDirectory $toolsDirectory
If (!(Test-Path $nunitConsoleExe)) {
Throw "Could not find nunit3-console.exe"
}

# Use FAKE to execute the build script
& $fakeExe $args
46 changes: 46 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

currentDirectory="$( cd "$( dirname "$0" )" && pwd )"
toolsDirectory=$currentDirectory/tools
nugetDirectory=$toolsDirectory/nuget
nugetExe=$nugetDirectory/nuget.exe
fakeExe=$toolsDirectory/FAKE/tools/FAKE.exe
nunitFrameworkDll=$toolsDirectory/NUnit/lib/nunit.framework.dll
nunitConsoleExe=$toolsDirectory/NUnit.Console/tools/nunit3-console.exe

if test ! -d $nugetDirectory; then
mkdir -p $nugetDirectory
fi

if test ! -f $nugetExe; then
nugetUrl="https://dist.nuget.org/win-x86-commandline/v3.3.0/nuget.exe"
wget -O $nugetExe $nugetUrl 2> /dev/null || curl -o $nugetExe --location $nugetUrl /dev/null

if test ! -f $nugetExe; then
echo "Could not find nuget.exe"
exit 1
fi

chmod 755 $nugetExe
fi

mono $nugetExe install FAKE -Version 4.17.1 -ExcludeVersion -OutputDirectory $toolsDirectory
if test ! -f $fakeExe; then
echo "Could not find fake.exe"
exit 1
fi

mono $nugetExe install NUnit -Version 2.6.4 -ExcludeVersion -OutputDirectory $toolsDirectory
if test ! -f $nunitFrameworkDll; then
echo "Could not find nunit.framework.dll"
exit 1
fi

mono $nugetExe install NUnit.Console -Version 3.0.1 -ExcludeVersion -OutputDirectory $toolsDirectory
if test ! -f $nunitConsoleExe; then
echo "Could not find nunit3-console.exe"
exit 1
fi

# Use FAKE to execute the build script
mono $fakeExe build.fsx $@
4 changes: 2 additions & 2 deletions exercises/crypto-square/CryptoSquareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public void Normalized_ciphertext_is_split_by_height_of_square()
public void Normalized_ciphertext_not_exactly_divisible_by_5_spills_into_a_smaller_segment()
{
var crypto = new Crypto("Madness, and then illumination.");
Assert.That(crypto.NormalizeCiphertext(), Is.EqualTo("msemo aanin dninn dlaet ltshu i"));
Assert.That(crypto.NormalizeCiphertext(), Is.EqualTo("msemo aanin dnin ndla etlt shui"));
}

[Ignore("Remove to run test")]
[Test]
public void Normalized_ciphertext_is_split_into_segements_of_correct_size()
{
var crypto = new Crypto("If man was meant to stay on the ground god would have given us roots");
Assert.That(crypto.NormalizeCiphertext(), Is.EqualTo("imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghns seoau"));
Assert.That(crypto.NormalizeCiphertext(), Is.EqualTo("imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau"));
}

[Ignore("Remove to run test")]
Expand Down
66 changes: 26 additions & 40 deletions exercises/crypto-square/Example.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Crypto
{
public string NormalizePlaintext { get; private set; }
public Crypto(string input)
{
this.NormalizePlaintext = GetNormalizedPlaintext(input);
this.Size = this.CalculateSize();
}

public int Size { get; private set; }

public Crypto(string value)
public string NormalizePlaintext { get; private set; }

public string NormalizeCiphertext()
{
NormalizePlaintext = NormalizeText(value);
Size = GetSquareSize(NormalizePlaintext);
return string.Join(" ", Transpose(this.PlaintextSegments()));
}

private static string NormalizeText(string text)
public string Ciphertext()
{
return string.Concat(text.ToLower().Where(char.IsLetterOrDigit));
return string.Join("", Transpose(this.PlaintextSegments()));
}

private static int GetSquareSize(string text)
public IEnumerable<string> PlaintextSegments()
{
return (int)Math.Ceiling(Math.Sqrt(text.Length));
return Chunks(this.NormalizePlaintext, this.Size);
}

public string[] PlaintextSegments()
private int CalculateSize()
{
return SegmentText(NormalizePlaintext, Size);
return (int) Math.Ceiling(Math.Sqrt(this.NormalizePlaintext.Length));
}

private static string[] SegmentText(string text, int size)
private static string GetNormalizedPlaintext(string input)
{
var segments = new List<string>();
var idx = 0;
while (idx < text.Length)
{
if (idx + size < text.Length)
segments.Add(text.Substring(idx, size));
else
segments.Add(text.Substring(idx));
idx += size;
}
return segments.ToArray();
return new string(input.ToLowerInvariant().Where(char.IsLetterOrDigit).ToArray());
}

public string Ciphertext()
private static IEnumerable<string> Chunks(string str, int chunkSize)
{
var ciphertext = new StringBuilder(NormalizePlaintext.Length);

for (int i = 0; i < Size; i++)
{
foreach (var segment in PlaintextSegments())
{
if (i < segment.Length)
ciphertext.Append(segment[i]);
}
}
return ciphertext.ToString();
return Enumerable.Range(0, (int)Math.Ceiling(str.Length / (double)chunkSize))
.Select(i => str.Substring(i * chunkSize, Math.Min(str.Length - i * chunkSize, chunkSize)));
}

public string NormalizeCiphertext()
private static IEnumerable<string> Transpose(IEnumerable<string> input)
{
string cipher = Ciphertext();
int size = cipher.Length == Size * Size - Size ? Size : Size - 1;
return string.Join(" ", SegmentText(cipher, size) );
return input.SelectMany(s => s.Select((c, i) => Tuple.Create(i, c)))
.GroupBy(x => x.Item1)
.Select(g => new string(g.Select(t => t.Item2).ToArray()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int Largest_product_for_empty_span_is_1(string digits)

[Ignore("Remove to run test")]
[Test]
public void Cannot_slice_empty_string_with_nonzero_span(string digits)
public void Cannot_slice_empty_string_with_nonzero_span()
{
Assert.Throws<ArgumentException>(() => new LargestSeriesProduct("").GetSlices(1));
}
Expand Down