Skip to content

Commit 12be43b

Browse files
committed
Merge pull request #51 from ErikSchierboom/fake-build
Add Fake build script
2 parents b5b223e + ef27504 commit 12be43b

7 files changed

Lines changed: 174 additions & 43 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
tmp
44
bin/configlet
55
bin/configlet.exe
6+
.fake/
7+
.vs/
8+
tools/
9+
build/
10+
TestResult.xml

build.fsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Include Fake library
2+
#r "tools/FAKE/tools/FakeLib.dll"
3+
4+
open Fake
5+
open Fake.CscHelper
6+
open Fake.Testing.NUnit3
7+
8+
// Properties
9+
let buildDir = getBuildParamOrDefault "buildDir" "./build/"
10+
let sourceDir = "./exercises/"
11+
let testDll = buildDir @@ "Tests.dll"
12+
let nunitFrameworkDll = "tools/NUnit/lib/net45/nunit.framework.dll"
13+
14+
let sourceFiles() = !! (buildDir @@ "./**/*.cs") |> List.ofSeq
15+
16+
// Targets
17+
Target "Clean" (fun _ ->
18+
CleanDir buildDir
19+
)
20+
21+
Target "CopySource" (fun _ ->
22+
CopyDir buildDir sourceDir allFiles
23+
)
24+
25+
Target "ModifySource" (fun _ ->
26+
sourceFiles()
27+
|> ReplaceInFiles [("[Ignore(\"Remove to run test\")]", ""); ("; Ignore", ""); (", Ignore = \"Remove to run test case\"", "")]
28+
)
29+
30+
Target "Build" (fun _ ->
31+
sourceFiles()
32+
|> List.ofSeq
33+
|> Csc (fun p ->
34+
{ p with Output = testDll
35+
References = [nunitFrameworkDll]
36+
Target = Library })
37+
)
38+
39+
Target "Test" (fun _ ->
40+
Copy buildDir [nunitFrameworkDll]
41+
42+
[testDll]
43+
|> NUnit3 (fun p ->
44+
{ p with
45+
ShadowCopy = false })
46+
)
47+
48+
Target "Default" (fun _ -> ())
49+
50+
// Dependencies
51+
"Clean"
52+
==> "CopySource"
53+
==> "ModifySource"
54+
==> "Build"
55+
==> "Test"
56+
==> "Default"
57+
58+
RunTargetOrDefault "Default"

build.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
$toolsDirectory = Join-Path $PSScriptRoot "tools"
2+
$nugetDirectory = Join-Path $toolsDirectory "nuget"
3+
$nugetExe = Join-Path $nugetDirectory "nuget.exe"
4+
$fakeExe = Join-Path $toolsDirectory "fake/tools/fake.exe"
5+
$nunitFrameworkDll = Join-Path $toolsDirectory "nunit/lib/net45/nunit.framework.dll"
6+
$nunitConsoleExe = Join-Path $toolsDirectory "nunit.console/tools/nunit3-console.exe"
7+
8+
If (!(Test-Path $nugetExe)) {
9+
# Ensure the directory exists (which is required by DownloadFile)
10+
New-Item $nugetDirectory -Type Directory | Out-Null
11+
12+
$nugetUrl = "https://dist.nuget.org/win-x86-commandline/v3.3.0/nuget.exe"
13+
(New-Object System.Net.WebClient).DownloadFile($nugetUrl, $nugetExe)
14+
}
15+
16+
If (!(Test-Path $nugetExe)) {
17+
Throw "Could not find nuget.exe"
18+
}
19+
20+
& $nugetExe install FAKE -Version 4.17.1 -ExcludeVersion -OutputDirectory $toolsDirectory
21+
If (!(Test-Path $fakeExe)) {
22+
Throw "Could not find fake.exe"
23+
}
24+
25+
& $nugetExe install NUnit -Version 3.0.1 -ExcludeVersion -OutputDirectory $toolsDirectory
26+
If (!(Test-Path $nunitFrameworkDll)) {
27+
Throw "Could not find nunit.framework.dll"
28+
}
29+
30+
& $nugetExe install NUnit.Console -Version 3.0.1 -ExcludeVersion -OutputDirectory $toolsDirectory
31+
If (!(Test-Path $nunitConsoleExe)) {
32+
Throw "Could not find nunit3-console.exe"
33+
}
34+
35+
# Use FAKE to execute the build script
36+
& $fakeExe $args

build.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
currentDirectory="$( cd "$( dirname "$0" )" && pwd )"
4+
toolsDirectory=$currentDirectory/tools
5+
nugetDirectory=$toolsDirectory/nuget
6+
nugetExe=$nugetDirectory/nuget.exe
7+
fakeExe=$toolsDirectory/FAKE/tools/FAKE.exe
8+
nunitFrameworkDll=$toolsDirectory/NUnit/lib/nunit.framework.dll
9+
nunitConsoleExe=$toolsDirectory/NUnit.Console/tools/nunit3-console.exe
10+
11+
if test ! -d $nugetDirectory; then
12+
mkdir -p $nugetDirectory
13+
fi
14+
15+
if test ! -f $nugetExe; then
16+
nugetUrl="https://dist.nuget.org/win-x86-commandline/v3.3.0/nuget.exe"
17+
wget -O $nugetExe $nugetUrl 2> /dev/null || curl -o $nugetExe --location $nugetUrl /dev/null
18+
19+
if test ! -f $nugetExe; then
20+
echo "Could not find nuget.exe"
21+
exit 1
22+
fi
23+
24+
chmod 755 $nugetExe
25+
fi
26+
27+
mono $nugetExe install FAKE -Version 4.17.1 -ExcludeVersion -OutputDirectory $toolsDirectory
28+
if test ! -f $fakeExe; then
29+
echo "Could not find fake.exe"
30+
exit 1
31+
fi
32+
33+
mono $nugetExe install NUnit -Version 2.6.4 -ExcludeVersion -OutputDirectory $toolsDirectory
34+
if test ! -f $nunitFrameworkDll; then
35+
echo "Could not find nunit.framework.dll"
36+
exit 1
37+
fi
38+
39+
mono $nugetExe install NUnit.Console -Version 3.0.1 -ExcludeVersion -OutputDirectory $toolsDirectory
40+
if test ! -f $nunitConsoleExe; then
41+
echo "Could not find nunit3-console.exe"
42+
exit 1
43+
fi
44+
45+
# Use FAKE to execute the build script
46+
mono $fakeExe build.fsx $@

exercises/crypto-square/CryptoSquareTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ public void Normalized_ciphertext_is_split_by_height_of_square()
103103
public void Normalized_ciphertext_not_exactly_divisible_by_5_spills_into_a_smaller_segment()
104104
{
105105
var crypto = new Crypto("Madness, and then illumination.");
106-
Assert.That(crypto.NormalizeCiphertext(), Is.EqualTo("msemo aanin dninn dlaet ltshu i"));
106+
Assert.That(crypto.NormalizeCiphertext(), Is.EqualTo("msemo aanin dnin ndla etlt shui"));
107107
}
108108

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

117117
[Ignore("Remove to run test")]

exercises/crypto-square/Example.cs

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,54 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54

65
public class Crypto
76
{
8-
public string NormalizePlaintext { get; private set; }
7+
public Crypto(string input)
8+
{
9+
this.NormalizePlaintext = GetNormalizedPlaintext(input);
10+
this.Size = this.CalculateSize();
11+
}
12+
913
public int Size { get; private set; }
1014

11-
public Crypto(string value)
15+
public string NormalizePlaintext { get; private set; }
16+
17+
public string NormalizeCiphertext()
1218
{
13-
NormalizePlaintext = NormalizeText(value);
14-
Size = GetSquareSize(NormalizePlaintext);
19+
return string.Join(" ", Transpose(this.PlaintextSegments()));
1520
}
1621

17-
private static string NormalizeText(string text)
22+
public string Ciphertext()
1823
{
19-
return string.Concat(text.ToLower().Where(char.IsLetterOrDigit));
24+
return string.Join("", Transpose(this.PlaintextSegments()));
2025
}
2126

22-
private static int GetSquareSize(string text)
27+
public IEnumerable<string> PlaintextSegments()
2328
{
24-
return (int)Math.Ceiling(Math.Sqrt(text.Length));
29+
return Chunks(this.NormalizePlaintext, this.Size);
2530
}
2631

27-
public string[] PlaintextSegments()
32+
private int CalculateSize()
2833
{
29-
return SegmentText(NormalizePlaintext, Size);
34+
return (int) Math.Ceiling(Math.Sqrt(this.NormalizePlaintext.Length));
3035
}
3136

32-
private static string[] SegmentText(string text, int size)
37+
private static string GetNormalizedPlaintext(string input)
3338
{
34-
var segments = new List<string>();
35-
var idx = 0;
36-
while (idx < text.Length)
37-
{
38-
if (idx + size < text.Length)
39-
segments.Add(text.Substring(idx, size));
40-
else
41-
segments.Add(text.Substring(idx));
42-
idx += size;
43-
}
44-
return segments.ToArray();
39+
return new string(input.ToLowerInvariant().Where(char.IsLetterOrDigit).ToArray());
4540
}
4641

47-
public string Ciphertext()
42+
private static IEnumerable<string> Chunks(string str, int chunkSize)
4843
{
49-
var ciphertext = new StringBuilder(NormalizePlaintext.Length);
50-
51-
for (int i = 0; i < Size; i++)
52-
{
53-
foreach (var segment in PlaintextSegments())
54-
{
55-
if (i < segment.Length)
56-
ciphertext.Append(segment[i]);
57-
}
58-
}
59-
return ciphertext.ToString();
44+
return Enumerable.Range(0, (int)Math.Ceiling(str.Length / (double)chunkSize))
45+
.Select(i => str.Substring(i * chunkSize, Math.Min(str.Length - i * chunkSize, chunkSize)));
6046
}
61-
62-
public string NormalizeCiphertext()
47+
48+
private static IEnumerable<string> Transpose(IEnumerable<string> input)
6349
{
64-
string cipher = Ciphertext();
65-
int size = cipher.Length == Size * Size - Size ? Size : Size - 1;
66-
return string.Join(" ", SegmentText(cipher, size) );
50+
return input.SelectMany(s => s.Select((c, i) => Tuple.Create(i, c)))
51+
.GroupBy(x => x.Item1)
52+
.Select(g => new string(g.Select(t => t.Item2).ToArray()));
6753
}
6854
}

exercises/largest-series-product/LargestSeriesProductTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public int Largest_product_for_empty_span_is_1(string digits)
8989

9090
[Ignore("Remove to run test")]
9191
[Test]
92-
public void Cannot_slice_empty_string_with_nonzero_span(string digits)
92+
public void Cannot_slice_empty_string_with_nonzero_span()
9393
{
9494
Assert.Throws<ArgumentException>(() => new LargestSeriesProduct("").GetSlices(1));
9595
}

0 commit comments

Comments
 (0)