Skip to content

Commit

Permalink
Add a build task to validate LINQPad samples (octokit#1551)
Browse files Browse the repository at this point in the history
* add a build task to run LINQPad samples

* make modifications to the validation of LINQPad samples

* rename the CAKE task to its original name ValidateLINQPadSamples
* rename the LINQPad samples file so they get executed in ordinal order (1, 2, ..., 10 and not 1, 10, 2, ...)

* remove openssl linking in TravisCI when on macOS
  • Loading branch information
mderriey authored and ryangribble committed Jan 28, 2017
1 parent 13d5dab commit 3369a4a
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ Backup/
!.vscode/launch.json

# CAKE
tools/
tools/*
!tools/LINQPad
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ matrix:
osx_image: xcode8
dotnet: 1.0.0-preview2-003121

before_install:
- if test "$TRAVIS_OS_NAME" == "osx"; then ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/; ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/; fi

script:
- dotnet --info
- ./build.sh
8 changes: 4 additions & 4 deletions build/Tasks/LinkSources.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Cake.Common.Build;
using System.Linq;
using Cake.Common;
using Cake.Common.Build;
using Cake.Common.Diagnostics;
using Cake.Core;
using Cake.Core.IO;
using Cake.Frosting;
using System.Linq;

[Dependency(typeof(Build))]
public class LinkSources : FrostingTask<BuildContext>
Expand Down Expand Up @@ -43,6 +43,6 @@ public override void Run(BuildContext context)

public override bool ShouldRun(BuildContext context)
{
return !context.Environment.Platform.IsUnix();
return context.IsRunningOnWindows();
}
}
1 change: 1 addition & 0 deletions build/Tasks/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

[Dependency(typeof(UnitTests))]
[Dependency(typeof(LinkSources))]
[Dependency(typeof(ValidateLINQPadSamples))]
public class Package : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
Expand Down
114 changes: 114 additions & 0 deletions build/Tasks/ValidateLINQPadSamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Cake.Common;
using Cake.Common.Diagnostics;
using Cake.Core;
using Cake.Core.IO;
using Cake.Frosting;

[Dependency(typeof(Build))]
public class ValidateLINQPadSamples : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var assembliesDirectoryPath = context.Environment.WorkingDirectory
.Combine("Octokit.Reactive")
.Combine("bin")
.Combine(context.Configuration)
.Combine("net45")
.MakeAbsolute(context.Environment)
.FullPath;

var linqpadSamples = context.FileSystem
.GetDirectory("samples/linqpad-samples")
.GetFiles("*.linq", SearchScope.Current)
.Select(x => x.Path)
.ToArray();

var linqpadExe = context.Environment.WorkingDirectory
.Combine("tools")
.Combine("LINQPad")
.CombineWithFilePath("lprun.exe")
.MakeAbsolute(context.Environment);

foreach (var linqpadSample in linqpadSamples)
{
var sampleName = linqpadSample.GetFilename();
var rewrittenSample = RewriteLinqpadScriptToUseLocalAssemblies(assembliesDirectoryPath, linqpadSample.FullPath);

context.Information("Executing sample {0}...", sampleName);
var exitCode = context.StartProcess(
linqpadExe,
$"-compileonly -lang=Program {rewrittenSample}");

if (exitCode != 0)
{
throw new CakeException($"Execution of sample {sampleName} failed");
}
}

context.Information("All samples executed successfully");
}

public override bool ShouldRun(BuildContext context)
{
return context.IsRunningOnWindows();
}

private static string RewriteLinqpadScriptToUseLocalAssemblies(string directory, string filePath)
{
var text = File.ReadAllText(filePath);

var openTag = "<Query Kind=\"Program\">";
var openTagIndex = text.IndexOf(openTag);
var closeTag = "</Query>";
var closeTagIndex = text.IndexOf(closeTag);

if (openTagIndex == -1 || closeTagIndex == -1)
{
throw new InvalidOperationException();
}

var endOfMetadata = closeTagIndex + closeTag.Length;

// write to temp file on disk
var tempFilePath = System.IO.Path.GetTempFileName();

using (var stream = File.OpenWrite(tempFilePath))
using (var writer = new StreamWriter(stream))
{
// reference all known assemblies
writer.WriteLine("ref {0}\\System.Reactive.Core.dll;", directory);
writer.WriteLine("ref {0}\\System.Reactive.Interfaces.dll;", directory);
writer.WriteLine("ref {0}\\System.Reactive.Linq.dll;", directory);
writer.WriteLine("ref {0}\\Octokit.dll;", directory);
writer.WriteLine("ref {0}\\Octokit.Reactive.dll;", directory);
writer.WriteLine("ref C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5\\System.Net.Http.dll;");
writer.WriteLine();

var xmlText = text.Substring(openTagIndex, endOfMetadata);
var rest = text.Substring(endOfMetadata);

var doc = XDocument.Parse(xmlText);

// add namespaces specified in xml
var namespaces = doc.Descendants()
.Where(x => x.Name == "Namespace")
.Select(x => x.Value.ToString());

foreach (var @namespace in namespaces)
{
writer.WriteLine("using {0};", @namespace);
}

writer.WriteLine();
writer.WriteLine(rest);

writer.Flush();
}

return tempFilePath;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3369a4a

Please sign in to comment.